source: variable.c @ 3b9ca71

Last change on this file since 3b9ca71 was 3b9ca71, checked in by David Benjamin <davidben@mit.edu>, 9 years ago
Allow perl variables to participate in argumentless set/unset
  • Property mode set to 100644
File size: 41.5 KB
Line 
1#include "owl.h"
2#include <stdio.h>
3#include "gmarshal_funcs.h"
4
5/* TODO(davidben): When we can require 2.30 and up, remove this. */
6#ifndef G_VALUE_INIT
7#define G_VALUE_INIT { 0, { { 0 } } }
8#endif
9
10typedef const char *(*get_string_t)(const owl_variable *);
11typedef int (*get_int_t)(const owl_variable *);
12typedef gboolean (*get_bool_t)(const owl_variable *);
13
14typedef int (*set_string_t)(owl_variable *, const char *);
15typedef int (*set_int_t)(owl_variable *, int);
16typedef int (*set_bool_t)(owl_variable *, gboolean);
17
18typedef int (*validate_string_t)(owl_variable *, const char *);
19typedef int (*validate_int_t)(owl_variable *, int);
20typedef int (*validate_bool_t)(owl_variable *, gboolean);
21
22#define OWLVAR_BOOL(name,default,summary,description) \
23        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \
24            NULL, NULL, NULL, NULL, NULL }
25
26#define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \
27        { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \
28            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
29
30#define OWLVAR_INT(name,default,summary,description) \
31        { name, OWL_VARIABLE_INT, NULL, default, "<int>", summary, description, NULL, \
32            NULL, NULL, NULL, NULL, NULL }
33
34#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
35        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
36            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
37
38#define OWLVAR_PATH(name,default,summary,description) \
39        { name, OWL_VARIABLE_STRING, default, 0, "<path>", summary, description,  NULL, \
40            NULL, NULL, NULL, NULL, NULL }
41
42#define OWLVAR_STRING(name,default,summary,description) \
43        { name, OWL_VARIABLE_STRING, default, 0, "<string>", summary, description, NULL, \
44            NULL, NULL, NULL, NULL, NULL }
45
46#define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \
47        { name, OWL_VARIABLE_STRING, default, 0, validset, summary, description, NULL, \
48            G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL }
49
50/* enums are really integers, but where validset is a comma-separated
51 * list of strings which can be specified.  The tokens, starting at 0,
52 * correspond to the values that may be specified. */
53#define OWLVAR_ENUM(name,default,summary,description,validset) \
54        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
55            G_CALLBACK(owl_variable_enum_validate),                     \
56            NULL, G_CALLBACK(owl_variable_enum_set_fromstring),         \
57            NULL, G_CALLBACK(owl_variable_enum_get_tostring),           \
58            }
59
60#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
61        { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \
62            G_CALLBACK(validate),                                       \
63            G_CALLBACK(set), G_CALLBACK(owl_variable_enum_set_fromstring), \
64            G_CALLBACK(get), G_CALLBACK(owl_variable_enum_get_tostring), \
65            }
66
67int owl_variable_add_defaults(owl_vardict *vd)
68{
69  owl_variable_init_params variables_to_init[] = {
70
71  OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off",
72                 "ring the terminal bell when personal messages are received",
73                 "Can be set to 'on', 'off', or the name of a filter which\n"
74                 "messages need to match in order to ring the bell"),
75
76  OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1,
77               "enable / disable the terminal bell", "" ),
78
79  OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG,
80                    "whether debugging is enabled",
81                    "If set to 'on', debugging messages are logged to the\n"
82                    "file specified by the debugfile variable.\n",
83                    NULL, owl_variable_debug_set, NULL),
84
85  OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1,
86               "send a login message when BarnOwl starts", "" ),
87
88  OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1,
89               "send a logout message when BarnOwl exits", "" ),
90
91  OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0,
92               "display received pings", "" ),
93
94  OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1,
95               "send pings", "" ),
96
97  OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0,
98               "disable printing information in the separator bar", "" ),
99
100  OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1,
101               "strip kerberos instance for reply", ""),
102
103  OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1,
104               "strip leading and trailing newlines", ""),
105
106  OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1,
107               "display outgoing messages", "" ),
108
109  OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1,
110               "load logins from .anyone on startup", "" ),
111
112  OWLVAR_BOOL( "logging" /* %OwlVarStub */, 0,
113               "turn personal logging on or off", 
114               "If this is set to on, personal messages are\n"
115               "logged in the directory specified\n"
116               "by the 'logpath' variable.  The filename in that\n"
117               "directory is derived from the sender of the message.\n" ),
118
119  OWLVAR_BOOL( "classlogging" /* %OwlVarStub */, 0,
120               "turn class logging on or off",
121               "If this is set to on, class messages are\n"
122               "logged in the directory specified\n"
123               "by the 'classlogpath' variable.\n" 
124               "The filename in that directory is derived from\n"
125               "the name of the class to which the message was sent.\n" ),
126
127  OWLVAR_ENUM( "loggingdirection" /* %OwlVarStub */, OWL_LOGGING_DIRECTION_BOTH,
128               "specifies which kind of messages should be logged",
129               "Can be one of 'both', 'in', or 'out'.  If 'in' is\n"
130               "selected, only incoming messages are logged, if 'out'\n"
131               "is selected only outgoing messages are logged.  If 'both'\n"
132               "is selected both incoming and outgoing messages are\n"
133               "logged.",
134               "both,in,out"),
135
136  OWLVAR_BOOL_FULL( "colorztext" /* %OwlVarStub */, 1,
137                    "allow @color() in zephyrs to change color",
138                    NULL, NULL, owl_variable_colorztext_set, NULL),
139
140  OWLVAR_BOOL( "fancylines" /* %OwlVarStub */, 1,
141               "Use 'nice' line drawing on the terminal.",
142               "If turned off, dashes, pipes and pluses will be used\n"
143               "to draw lines on the screen.  Useful when the terminal\n"
144               "is causing problems" ),
145
146  OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1,
147               "Do automatic zcrypt processing",
148               "" ),
149
150  OWLVAR_BOOL_FULL( "pseudologins" /* %OwlVarStub */, 0,
151                    "Enable zephyr pseudo logins",
152                    "When this is enabled, BarnOwl will periodically check the zephyr\n"
153                    "location of users in your .anyone file.  If a user is present\n"
154                    "but sent no login message, or a user is not present that sent no\n"
155                    "logout message, a pseudo login or logout message will be created\n",
156                    NULL, owl_variable_pseudologins_set, NULL),
157
158  OWLVAR_BOOL( "ignorelogins" /* %OwlVarStub */, 0,
159               "Enable printing of login notifications",
160               "When this is enabled, BarnOwl will print login and logout notifications\n"
161               "for AIM, zephyr, or other protocols.  If disabled BarnOwl will not print\n"
162               "login or logout notifications.\n"),
163
164  OWLVAR_STRING( "logfilter" /* %OwlVarStub */, "",
165                 "name of a filter controlling which messages to log",
166
167                 "If non empty, any messages matching the given filter will be logged.\n"
168                 "This is a completely separate mechanism from the other logging\n"
169                 "variables like logging, classlogging, loglogins, loggingdirection,\n"
170                 "etc.  If you want this variable to control all logging, make sure\n"
171                 "all other logging variables are in their default state.\n"),
172
173  OWLVAR_BOOL( "loglogins" /* %OwlVarStub */, 0,
174               "Enable logging of login notifications",
175               "When this is enabled, BarnOwl will log login and logout notifications\n"
176               "for AIM, zephyr, or other protocols.  If disabled BarnOwl will not print\n"
177               "login or logout notifications.\n"),
178
179  OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1,
180                    "don't send zephyrs on C-d",
181                    "If set to 'on', C-d won't send a zephyr from the edit\n"
182                    "window.  If set to 'off', C-d will always send a zephyr\n"
183                    "being composed in the edit window.  If set to 'middle',\n"
184                    "C-d will only ever send a zephyr if the cursor is at\n"
185                    "the end of the message being composed.\n\n"
186                    "Note that this works by changing the C-d keybinding\n"
187                    "in the editmulti keymap.\n",
188                    "off,middle,on",
189                    NULL, owl_variable_disable_ctrl_d_set, NULL),
190
191  OWLVAR_PATH( "logpath" /* %OwlVarStub */, "~/zlog/people",
192               "path for logging personal zephyrs", 
193               "Specifies a directory which must exist.\n"
194               "Files will be created in the directory for each sender.\n"),
195
196  OWLVAR_PATH( "classlogpath" /* %OwlVarStub:classlogpath */, "~/zlog/class",
197               "path for logging class zephyrs",
198               "Specifies a directory which must exist.\n"
199               "Files will be created in the directory for each class.\n"),
200
201  OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE,
202               "path for logging debug messages when debugging is enabled",
203               "This file will be logged to if 'debug' is set to 'on'.\n"
204               "BarnOwl will append a dot and the current process's pid to the filename."),
205 
206  OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL,
207               "name of a program to run that will generate zsigs",
208               "This program should produce a zsig on stdout when run.\n"
209               "Note that it is important that this program not block.\n\n"
210               "See the documentation for 'zsig' for more information about\n"
211               "how the outgoing zsig is chosen."
212               ),
213
214  OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL,
215               "name of a program to run when new messages are present",
216               "The named program will be run when BarnOwl receives new\n"
217               "messages.  It will not be run again until the first\n"
218               "instance exits"),
219
220  OWLVAR_STRING( "zsender" /* %OwlVarStub */, "",
221             "zephyr sender name",
222         "Allows you to customize the outgoing username in\n"
223         "zephyrs.  If this is unset, it will use your Kerberos\n"
224         "principal. Note that customizing the sender name will\n"
225         "cause your zephyrs to be sent unauthenticated."),
226
227  OWLVAR_STRING( "zsigfunc" /* %OwlVarStub */, "BarnOwl::default_zephyr_signature()",
228                 "zsig perl function",
229                 "Called every time you start a zephyrgram without an\n"
230                 "explicit zsig.  The default setting implements the policy\n"
231                 "described in the documentation for the 'zsig' variable.\n"
232                 "See also BarnOwl::random_zephyr_signature().\n"),
233
234  OWLVAR_STRING( "zsig" /* %OwlVarStub */, "",
235                 "zephyr signature",
236                 "The zsig to get on outgoing messages. If this variable is\n"
237                 "unset, 'zsigproc' will be run to generate a zsig. If that is\n"
238                 "also unset, the 'zwrite-signature' zephyr variable will be\n"
239                 "used instead.\n"),
240
241  OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "",
242                 "string to append to the end of the sepbar",
243                 "The sepbar is the bar separating the top and bottom\n"
244                 "of the BarnOwl screen.  Any string specified here will\n"
245                 "be displayed on the right of the sepbar\n"),
246
247  OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0,
248               "turn zaway on or off", "" ),
249
250  OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 
251                 OWL_DEFAULT_ZAWAYMSG,
252                 "zaway msg for responding to zephyrs when away", "" ),
253
254  OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 
255                 OWL_DEFAULT_ZAWAYMSG,
256                 "default zaway message", "" ),
257
258  OWLVAR_BOOL_FULL( "aaway" /* %OwlVarStub */, 0,
259                    "Set AIM away status",
260                    "",
261                    NULL, owl_variable_aaway_set, NULL),
262
263  OWLVAR_STRING( "aaway_msg" /* %OwlVarStub */, 
264                 OWL_DEFAULT_AAWAYMSG,
265                 "AIM away msg for responding when away", "" ),
266
267  OWLVAR_STRING( "aaway_msg_default" /* %OwlVarStub */, 
268                 OWL_DEFAULT_AAWAYMSG,
269                 "default AIM away message", "" ),
270
271  OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all",
272                 "home view to switch to after 'X' and 'V'", 
273                 "SEE ALSO: view, filter\n" ),
274
275  OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none",
276                 "filter on which to trigger alert actions",
277                 "" ),
278
279  OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop",
280                 "BarnOwl command to execute for alert actions",
281                 "" ),
282
283  OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "<string>", "tty name for zephyr location", "",
284                      NULL, owl_variable_tty_set, NULL),
285
286  OWLVAR_STRING( "default_style" /* %OwlVarStub */, "default",
287                 "name of the default formatting style",
288                 "This sets the default message formatting style.\n"
289                 "Styles may be created with the 'style' command.\n"
290                 "Some built-in styles include:\n"
291                 "   default  - the default BarnOwl formatting\n"
292                 "   oneline  - one line per-message\n"
293                 "   perl     - legacy perl interface\n"
294                 "\nSEE ALSO: style, show styles, view -s <style>\n"
295                 ),
296
297
298  OWLVAR_INT(    "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70,
299                 "maximum number of columns for M-q (edit:fill-paragraph) to fill text to",
300                 "This specifies the maximum number of columns for M-q to fill text\n"
301                 "to.  If set to 0, M-q will wrap to the width of the window, and\n"
302                 "values less than 0 disable M-q entirely.\n"),
303
304  OWLVAR_INT(    "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70,
305                 "maximum number of columns for line-wrapping",
306                 "This specifies the maximum number of columns for\n"
307                 "auto-line-wrapping.  If set to 0, text will be wrapped at the\n"
308                 "window width. Values less than 0 disable automatic wrapping.\n"
309                 "\n"
310                 "As a courtesy to recipients, it is recommended that outgoing\n"
311                 "Zephyr messages be no wider than 70 columns.\n"),
312
313  OWLVAR_INT( "aim_ignorelogin_timer" /* %OwlVarStub */, 15,
314              "number of seconds after AIM login to ignore login messages",
315              "This specifies the number of seconds to wait after an\n"
316              "AIM login before allowing the receipt of AIM login notifications.\n"
317              "By default this is set to 15.  If you would like to view login\n"
318              "notifications of buddies as soon as you login, set it to 0 instead."),
319
320             
321  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
322                   OWL_TYPWIN_SIZE,
323                  "number of lines in the typing window", 
324                   "This specifies the height of the window at the\n"
325                   "bottom of the screen where commands are entered\n"
326                   "and where messages are composed.\n",
327                   "int > 0",
328                   owl_variable_int_validate_gt0,
329                   owl_variable_typewinsize_set,
330                   NULL /* use default for get */
331                   ),
332
333  OWLVAR_INT( "typewindelta" /* %OwlVarStub */, 0,
334                  "number of lines to add to the typing window when in use",
335                   "On small screens you may want the typing window to\n"
336                   "auto-hide when not entering a command or message.\n"
337                   "This variable is the number of lines to add to the\n"
338           "typing window when it is in use; you can then set\n"
339           "typewinsize to 1.\n\n"
340           "This works a lot better with a non-default scrollmode;\n"
341           "try :set scrollmode pagedcenter.\n"),
342
343  OWLVAR_ENUM( "scrollmode" /* %OwlVarStub */, OWL_SCROLLMODE_NORMAL,
344               "how to scroll up and down",
345               "This controls how the screen is scrolled as the\n"
346               "cursor moves between messages being displayed.\n"
347               "The following modes are supported:\n\n"
348               "   normal      - This is the BarnOwl default.  Scrolling happens\n"
349               "                 when it needs to, and an attempt is made to\n"
350               "                 keep the current message roughly near\n"
351               "                 the middle of the screen.\n"
352               "   top         - The current message will always be the\n"
353               "                 the top message displayed.\n"
354               "   neartop     - The current message will be one down\n"
355               "                 from the top message displayed,\n"
356               "                 where possible.\n"
357               "   center      - An attempt is made to keep the current\n"
358               "                 message near the center of the screen.\n"
359               "   paged       - The top message displayed only changes\n"
360               "                 when user moves the cursor to the top\n"
361               "                 or bottom of the screen.  When it moves,\n"
362               "                 the screen will be paged up or down and\n"
363               "                 the cursor will be near the top or\n"
364               "                 the bottom.\n"
365               "   pagedcenter - The top message displayed only changes\n"
366               "                 when user moves the cursor to the top\n"
367               "                 or bottom of the screen.  When it moves,\n"
368               "                 the screen will be paged up or down and\n"
369               "                 the cursor will be near the center.\n",
370               "normal,top,neartop,center,paged,pagedcenter" ),
371
372  OWLVAR_BOOL( "narrow-related" /* %OwlVarStub:narrow_related */, 1,
373               "Make smartnarrow use broader filters",
374               "Causes smartfilter to narrow to messages \"related\" to \n"
375               "the current message, as well as ones to the same place.\n\n"
376               "for Zephyr, this controls whether to narrow to e.g. class-help or\n"
377               "class-help.d alone, or to related-class-help, which includes\n"
378               "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" ),
379
380  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
381               "enable automatic following of the last zephyr",
382               "If the cursor is at the last message, it will\n"
383               "continue to follow the last message if this is set.\n"
384               "Note that this is currently risky as you might accidentally\n"
385               "delete a message right as it came in.\n" ),
386
387  OWLVAR_STRING_FULL( "default_exposure" /* %OwlVarStub */, "",
388                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
389                      "controls the persistent value for exposure",
390                      "The default exposure level corresponds to the Zephyr exposure value\n"
391                      "in ~/.zephyr.vars.  Defaults to realm-visible if there is no value in\n"
392                      "~/.zephyr.vars.\n"
393                      "See the description of exposure for the values this can be.",
394                      NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get ),
395
396  OWLVAR_STRING_FULL( "exposure" /* %OwlVarStub */, "",
397                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
398                      "controls who can zlocate you",
399                      "The exposure level, defaulting to the value of default_exposure,\n"
400                      "can be one of the following (from least exposure to widest exposure,\n"
401                      "as listed in zctl(1)):\n"
402                      "\n"
403                      "   none            - This completely disables Zephyr for the user. \n"
404                      "                     The user is not registered with Zephyr.  No user\n"
405                      "                     location information is retained by Zephyr.  No\n"
406                      "                     login or logout announcements will be sent.  No\n"
407                      "                     subscriptions will be entered for the user, and\n"
408                      "                     no notices will be displayed by zwgc(1).\n"
409                      "   opstaff         - The user is registered with Zephyr.  No login or\n"
410                      "                     logout announcements will be sent, and location\n"
411                      "                     information will only be visible to Operations\n"
412                      "                     staff.  Default subscriptions and any additional\n"
413                      "                     personal subscriptions will be entered for the\n"
414                      "                     user.\n"
415                      "   realm-visible   - The user is registered with Zephyr.  User\n"
416                      "                     location information is retained by Zephyr and\n"
417                      "                     made available only to users within the user’s\n"
418                      "                     Kerberos realm.  No login or logout\n"
419                      "                     announcements will be sent.  This is the system\n"
420                      "                     default.  Default subscriptions and any\n"
421                      "                     additional personal subscriptions will be\n"
422                      "                     entered for the user.\n"
423                      "   realm-announced - The user is registered with Zephyr.  User\n"
424                      "                     location information is retained by Zephyr and\n"
425                      "                     made available only to users authenticated\n"
426                      "                     within the user’s Kerberos realm.  Login and\n"
427                      "                     logout announcements will be sent, but only to\n"
428                      "                     users within the user’s Kerberos realm who have\n"
429                      "                     explicitly requested such via subscriptions. \n"
430                      "                     Default subscriptions and any additional\n"
431                      "                     personal subscriptions will be entered for the\n"
432                      "                     user.\n"
433                      "   net-visible     - The user is registered with Zephyr.  User\n"
434                      "                     location information is retained by Zephyr and\n"
435                      "                     made available to any authenticated user who\n"
436                      "                     requests such.  Login and logout announcements\n"
437                      "                     will be sent only to users within the user’s\n"
438                      "                     Kerberos realm who have explicitly requested\n"
439                      "                     such via subscriptions.  Default subscriptions\n"
440                      "                     and any additional personal subscriptions will\n"
441                      "                     be entered for the user.\n"
442                      "   net-announced   - The user is registered with Zephyr.  User\n"
443                      "                     location information is retained by Zephyr and\n"
444                      "                     made available to any authenticated user who\n"
445                      "                     requests such.  Login and logout announcements\n"
446                      "                     will be sent to any user has requested such. \n"
447                      "                     Default subscriptions and any additional\n"
448                      "                     personal subscriptions will be entered for the\n"
449                      "                     user.\n",
450                      NULL, owl_variable_exposure_set, NULL /* use default for get */ ),
451
452  /* This MUST be last... */
453  { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL,
454    NULL, NULL, NULL, NULL, NULL }
455
456  };
457
458  return owl_variable_dict_add_from_list(vd, variables_to_init);
459}
460
461/**************************************************************************/
462/*********************** SPECIFIC TO VARIABLES ****************************/
463/**************************************************************************/
464
465
466/* commonly useful */
467
468int owl_variable_int_validate_gt0(const owl_variable *v, int newval)
469{
470  return !(newval < 1);
471}
472
473int owl_variable_int_validate_positive(const owl_variable *v, int newval)
474{
475  return !(newval < 0);
476}
477
478/* typewinsize */
479int owl_variable_typewinsize_set(owl_variable *v, int newval)
480{
481  int rv;
482  rv = owl_variable_int_set_default(v, newval);
483  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
484  return(rv);
485}
486
487/* debug (cache value in g->debug) */
488int owl_variable_debug_set(owl_variable *v, int newval)
489{
490  if (newval == 1 || newval == 0) {
491    g.debug = newval;
492  }
493  return owl_variable_bool_set_default(v, newval);
494}
495
496/* When 'aaway' is changed, need to notify the AIM server */
497int owl_variable_aaway_set(owl_variable *v, gboolean newval)
498{
499  if (newval == 1) {
500    owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g));
501  } else if (newval == 0) {
502    owl_aim_set_awaymsg("");
503  }
504  return owl_variable_bool_set_default(v, newval);
505}
506
507int owl_variable_colorztext_set(owl_variable *v, const void *newval)
508{
509  int ret = owl_variable_bool_set_default(v, newval);
510  /* flush the format cache so that we see the update, but only if we're done initializing BarnOwl */
511  if (owl_global_get_msglist(&g) != NULL)
512    owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
513  if (owl_global_get_mainwin(&g) != NULL) {
514    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
515    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
516  }
517  return ret;
518}
519
520int owl_variable_pseudologins_set(owl_variable *v, int newval)
521{
522  static guint timer = 0;
523  if (newval == 1) {
524    owl_function_zephyr_buddy_check(0);
525    if (timer == 0) {
526      timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL);
527    }
528  } else {
529    if (timer != 0) {
530      g_source_remove(timer);
531      timer = 0;
532    }
533  }
534  return owl_variable_bool_set_default(v, newval);
535}
536
537/* note that changing the value of this will clobber
538 * any user setting of this */
539int owl_variable_disable_ctrl_d_set(owl_variable *v, int newval)
540{
541  if (!owl_context_is_startup(owl_global_get_context(&g))) {
542    if (newval == 2) {
543      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
544    } else if (newval == 1) {
545      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
546    } else {
547      owl_function_command_norv("bindkey editmulti C-d command edit:done");
548    }
549  } 
550  return owl_variable_int_set_default(v, newval);
551}
552
553int owl_variable_tty_set(owl_variable *v, const char *newval)
554{
555  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
556  return owl_variable_string_set_default(v, newval);
557}
558
559int owl_variable_default_exposure_set(owl_variable *v, const char *newval)
560{
561  return owl_zephyr_set_default_exposure(newval);
562}
563
564const char *owl_variable_default_exposure_get(const owl_variable *v)
565{
566  return owl_zephyr_get_default_exposure();
567}
568
569int owl_variable_exposure_set(owl_variable *v, const char *newval)
570{
571  int ret = owl_zephyr_set_exposure(newval);
572  if (ret != 0)
573    return ret;
574  return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval));
575}
576
577/**************************************************************************/
578/****************************** GENERAL ***********************************/
579/**************************************************************************/
580
581int owl_variable_dict_setup(owl_vardict *vd) {
582  owl_dict_create(vd);
583  return owl_variable_add_defaults(vd);
584}
585
586CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v,
587                                               GCallback fn,
588                                               GClosureMarshal marshal) {
589  GClosure *closure = g_cclosure_new_swap(fn, v, NULL);
590  g_closure_set_marshal(closure,marshal);
591  g_closure_ref(closure);
592  g_closure_sink(closure);
593  return closure;
594}
595
596#define OWL_VARIABLE_SETUP_FUNC(variable, initializer, func_name, default_func, marshal_func, temp) do { \
597  if(initializer->func_name) { \
598    temp = initializer->func_name; \
599  } else { \
600    temp = default_func; \
601  } \
602  variable->func_name = owl_variable_make_closure(variable, G_CALLBACK(temp), \
603                                                  marshal_func);        \
604  } while(0)
605
606#define DEFAULT_CB(a, b) (a ? a : G_CALLBACK(b))
607
608#define SET_CBS(type) \
609      newvar->get_fn = DEFAULT_CB(init_params->get_fn, owl_variable_##type##_get_default); \
610      newvar->set_fn = DEFAULT_CB(init_params->set_fn, owl_variable_##type##_set_default); \
611      newvar->validate_fn = \
612        DEFAULT_CB(init_params->validate_fn, owl_variable_##type##_validate_default);
613
614int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable_init_params *variables_to_init)
615{
616  owl_variable *newvar = NULL;
617  owl_variable_init_params *init_params = NULL;
618  for (init_params = variables_to_init; init_params->name; init_params++) {
619    newvar = g_new0(owl_variable, 1);
620    newvar->type = init_params->type;
621    newvar->takes_on_off = (newvar->type == OWL_VARIABLE_BOOL);
622    /* strdup all the strings so we can delete them consistently. */
623    newvar->name = g_strdup(init_params->name);
624    newvar->summary = g_strdup(init_params->summary);
625    newvar->description = g_strdup(init_params->description);
626    newvar->validsettings = g_strdup(init_params->validsettings);
627    GCallback fn = NULL;
628    switch (init_params->type) {
629    case OWL_VARIABLE_STRING:
630      SET_CBS(string);
631      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
632                              G_CALLBACK(owl_variable_string_set_fromstring_default),
633                              g_cclosure_user_marshal_INT__STRING, fn);
634      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
635                              G_CALLBACK(owl_variable_string_get_tostring_default),
636                              g_cclosure_user_marshal_STRING__VOID, fn);
637
638      g_value_init(&newvar->val, G_TYPE_STRING);
639      owl_variable_set_string(newvar, init_params->pval_default);
640      break;
641    case OWL_VARIABLE_BOOL:
642      SET_CBS(bool);
643      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
644                              G_CALLBACK(owl_variable_bool_set_fromstring_default),
645                              g_cclosure_user_marshal_INT__STRING, fn);
646      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
647                              G_CALLBACK(owl_variable_bool_get_tostring_default),
648                              g_cclosure_user_marshal_STRING__VOID, fn);
649
650      g_value_init(&newvar->val, G_TYPE_BOOLEAN);
651      owl_variable_set_bool(newvar, !!(init_params->ival_default));
652      break;
653    case OWL_VARIABLE_INT:
654      SET_CBS(int);
655      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn,
656                              G_CALLBACK(owl_variable_int_set_fromstring_default),
657                              g_cclosure_user_marshal_INT__STRING, fn);
658      OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn,
659                              G_CALLBACK(owl_variable_int_get_tostring_default),
660                              g_cclosure_user_marshal_STRING__VOID, fn);
661
662      g_value_init(&newvar->val, G_TYPE_INT);
663      owl_variable_set_int(newvar, init_params->ival_default);
664      break;
665    default:
666      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
667      return(-2);
668    }
669
670    /* record the initial value as a string */
671    newvar->default_str = owl_variable_get_tostring(newvar);
672
673    owl_dict_insert_element(vd, newvar->name, newvar, NULL);
674  }
675  return 0;
676}
677
678void owl_variable_dict_add_variable(owl_vardict * vardict,
679                                    owl_variable * var) {
680  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
681}
682
683void owl_variable_dict_newvar_other(owl_vardict *vd, const char *name, const char *summary, const char *description, const char *validsettings, bool takes_on_off, GClosure *get_tostring_fn, GClosure *set_fromstring_fn)
684{
685  owl_variable *var = g_new0(owl_variable, 1);
686  var->name = g_strdup(name);
687  var->summary = g_strdup(summary);
688  var->description = g_strdup(description);
689  var->validsettings = g_strdup(validsettings);
690  var->takes_on_off = takes_on_off;
691
692  var->get_tostring_fn = g_closure_ref(get_tostring_fn);
693  g_closure_sink(get_tostring_fn);
694
695  var->set_fromstring_fn = g_closure_ref(set_fromstring_fn);
696  g_closure_sink(set_fromstring_fn);
697
698  var->default_str = owl_variable_get_tostring(var);
699
700  /* Note: this'll overwrite any existing variable of that name, even a C one,
701     but it's consistent with previous behavior and commands. */
702  owl_variable_dict_add_variable(vd, var);
703}
704
705void owl_variable_dict_cleanup(owl_vardict *d)
706{
707  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
708}
709
710CALLER_OWN GPtrArray *owl_variable_dict_get_names(const owl_vardict *d) {
711  return owl_dict_get_keys(d);
712}
713
714void owl_variable_cleanup(owl_variable *v)
715{
716  g_free(v->name);
717  g_free(v->summary);
718  g_free(v->description);
719  g_free(v->default_str);
720  g_free(v->validsettings);
721  g_value_unset(&(v->val));
722  g_closure_unref(v->get_tostring_fn);
723  g_closure_unref(v->set_fromstring_fn);
724}
725
726void owl_variable_delete(owl_variable *v)
727{
728  owl_variable_cleanup(v);
729  g_free(v);
730}
731
732
733const char *owl_variable_get_name(const owl_variable *v)
734{
735  return v->name;
736}
737
738const char *owl_variable_get_description(const owl_variable *v) {
739  return v->description;
740}
741
742const char *owl_variable_get_summary(const owl_variable *v) {
743  return v->summary;
744}
745
746const char *owl_variable_get_validsettings(const owl_variable *v) {
747  return v->validsettings;
748}
749
750int owl_variable_get_type(const owl_variable *v)
751{
752  return v->type;
753}
754
755/* returns 0 on success, prints a status msg if msg is true */
756int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
757  char *tostring;
758  GValue values[] = {G_VALUE_INIT, G_VALUE_INIT};
759  GValue *value_box = &values[1];
760  GValue return_box = G_VALUE_INIT;
761  int set_successfully = -1;
762  if (!v->set_fromstring_fn) {
763    if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v));
764    return -1;   
765  }
766  g_value_init(&values[0], G_TYPE_POINTER);
767  g_value_set_pointer(&values[0], NULL);
768  g_value_init(value_box, G_TYPE_STRING);
769  g_value_set_static_string(value_box, value);
770  g_value_init(&return_box, G_TYPE_INT);
771  g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL);
772  set_successfully = g_value_get_int(&return_box);
773  if (0 != set_successfully) {
774    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
775                                owl_variable_get_validsettings(v));
776  } else if (msg) {
777    tostring = owl_variable_get_tostring(v);
778    if (tostring) {
779      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
780    } else {
781      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
782    }
783    g_free(tostring);
784  }
785  g_value_unset(value_box);
786  return set_successfully;
787}
788 
789int owl_variable_set_string(owl_variable *v, const char *newval)
790{
791  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1);
792
793  set_string_t cb = (set_string_t) v->set_fn;
794  return cb(v, newval);
795}
796
797int owl_variable_set_int(owl_variable *v, int newval)
798{
799  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1);
800
801  set_int_t cb = (set_int_t) v->set_fn;
802  return cb(v, newval);
803}
804
805int owl_variable_set_bool(owl_variable *v, gboolean newval) {
806  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1);
807
808  set_bool_t cb = (set_bool_t) v->set_fn;
809  return cb(v, newval);
810}
811
812int owl_variable_set_bool_on(owl_variable *v)
813{
814  if (v->type != OWL_VARIABLE_BOOL) return -1;
815  return owl_variable_set_bool(v, true);
816}
817
818int owl_variable_set_bool_off(owl_variable *v)
819{
820  if (v->type != OWL_VARIABLE_BOOL) return -1;
821  return owl_variable_set_bool(v, false);
822}
823
824CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
825{
826  GValue instance = G_VALUE_INIT;
827  GValue tostring_box = G_VALUE_INIT;
828  char *ret = NULL;
829
830  g_value_init(&instance, G_TYPE_POINTER);
831  g_value_set_pointer(&instance, NULL);
832  g_value_init(&tostring_box, G_TYPE_STRING);
833  g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL);
834
835  ret = g_value_dup_string(&tostring_box);
836  g_value_unset(&tostring_box);
837
838  return ret;
839}
840
841const char *owl_variable_get_default_tostring(const owl_variable *v)
842{
843  return v->default_str;
844}
845
846owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
847{
848  return owl_dict_find_element(d, name);
849}
850
851const char *owl_variable_get_string(const owl_variable *v)
852{
853  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL);
854
855  get_string_t cb = (get_string_t) v->get_fn;
856  return cb(v);
857}
858
859int owl_variable_get_int(const owl_variable *v)
860{
861  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0);
862
863  get_int_t cb = (get_int_t) v->get_fn;
864  return cb(v);
865}
866
867gboolean owl_variable_get_bool(const owl_variable *v)
868{
869  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE);
870
871  get_bool_t cb = (get_bool_t) v->get_fn;
872  return cb(v);
873}
874
875void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
876{
877  const char *default_str = owl_variable_get_default_tostring(v);
878  char *default_buf;
879
880  if (default_str)
881    default_buf = g_strdup_printf("'%s'", default_str);
882  else
883    default_buf = g_strdup("<null>");
884  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
885                            owl_variable_get_name(v),
886                            owl_variable_get_summary(v), default_buf);
887  g_free(default_buf);
888}
889
890void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
891  char *tostring;
892  const char *default_str;
893
894  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
895  owl_fmtext_append_normal(fm, OWL_TABSTR);
896  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
897  owl_fmtext_append_normal(fm, " - ");
898  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
899  owl_fmtext_append_normal(fm, "\n\n");
900
901  owl_fmtext_append_normal(fm, "Current:        ");
902  tostring = owl_variable_get_tostring(v);
903  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
904  g_free(tostring);
905  owl_fmtext_append_normal(fm, "\n\n");
906
907  default_str = owl_variable_get_default_tostring(v);
908  owl_fmtext_append_normal(fm, "Default:        ");
909  owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>"));
910  owl_fmtext_append_normal(fm, "\n\n");
911
912  owl_fmtext_append_normal(fm, "Valid Settings: ");
913  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
914  owl_fmtext_append_normal(fm, "\n\n");
915
916  if (v->description && *v->description) {
917    owl_fmtext_append_normal(fm, "Description:\n");
918    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
919    owl_fmtext_append_normal(fm, "\n\n");
920  }
921}
922
923
924
925
926/**************************************************************************/
927/*********************** GENERAL TYPE-SPECIFIC ****************************/
928/**************************************************************************/
929
930/* default common functions */
931
932const char *owl_variable_string_get_default(const owl_variable *v) {
933  return g_value_get_string(&(v->val));
934}
935
936int owl_variable_int_get_default(const owl_variable *v) {
937  return g_value_get_int(&(v->val));
938}
939
940gboolean owl_variable_bool_get_default(const owl_variable *v) {
941  return g_value_get_boolean(&(v->val));
942}
943
944/* default functions for booleans */
945
946int owl_variable_bool_validate_default(const owl_variable *v, gboolean newval) {
947  return (newval == 1) || (newval == 0);
948}
949
950int owl_variable_bool_set_default(owl_variable *v, bool newval) {
951  if (!((validate_bool_t)v->validate_fn)(v, newval))
952    return -1;
953
954  g_value_set_boolean(&(v->val), newval);
955  return(0);
956}
957
958int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
959  gboolean i;
960  if (!strcmp(newval, "on")) {
961    i = TRUE;
962  } else if (!strcmp(newval, "off")) {
963    i = FALSE;
964  } else {
965    return(-1);
966  }
967
968  return owl_variable_set_bool(v, i);
969}
970
971CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy)
972{
973  bool val = owl_variable_get_bool(v);
974  if (val == 0) {
975    return g_strdup("off");
976  } else if (val == 1) {
977    return g_strdup("on");
978  } else {
979    return g_strdup("<invalid>");
980  }
981}
982
983/* default functions for integers */
984
985int owl_variable_int_validate_default(const owl_variable *v, int newval)
986{
987  return (1);
988}
989
990int owl_variable_int_set_default(owl_variable *v, int newval) {
991  if (!((validate_int_t)v->validate_fn)(v, newval))
992    return -1;
993
994  g_value_set_int(&(v->val), newval);
995  return(0);
996}
997
998int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
999  int i;
1000  char *ep;
1001  i = strtol(newval, &ep, 10);
1002  if (*ep || ep==newval) return(-1);
1003  return owl_variable_set_int(v, i);
1004}
1005
1006CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy)
1007{
1008  return g_strdup_printf("%d", owl_variable_get_int(v));
1009}
1010
1011/* default functions for enums (a variant of integers) */
1012
1013int owl_variable_enum_validate(const owl_variable *v, int newval) {
1014  char **enums;
1015  int nenums, val;
1016  enums = g_strsplit_set(v->validsettings, ",", 0);
1017  nenums = g_strv_length(enums);
1018  g_strfreev(enums);
1019  val = newval;
1020  if (val < 0 || val >= nenums) {
1021    return(0);
1022  }
1023  return(1);
1024}
1025
1026int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval, void *dummy) {
1027  char **enums;
1028  int i, val=-1;
1029  if (newval == NULL) return(-1);
1030  enums = g_strsplit_set(v->validsettings, ",", 0);
1031  for (i = 0; enums[i] != NULL; i++) {
1032    if (0==strcmp(newval, enums[i])) {
1033      val = i;
1034    }
1035  }
1036  g_strfreev(enums);
1037  if (val == -1) return(-1);
1038  return owl_variable_set_int(v, val);
1039}
1040
1041CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy)
1042{
1043  char **enums;
1044  int nenums, i;
1045  char *tostring;
1046
1047  enums = g_strsplit_set(v->validsettings, ",", 0);
1048  nenums = g_strv_length(enums);
1049  i = owl_variable_get_int(v);
1050  if (i<0 || i>=nenums) {
1051    g_strfreev(enums);
1052    return g_strdup_printf("<invalid:%d>", i);
1053  }
1054  tostring = g_strdup(enums[i]);
1055  g_strfreev(enums);
1056  return tostring;
1057}
1058
1059/* default functions for stringeans */
1060
1061int owl_variable_string_validate_default(const owl_variable *v, const char *newval) {
1062  if (newval == NULL) return(0);
1063  else return (1);
1064}
1065
1066int owl_variable_string_set_default(owl_variable *v, const char *newval) {
1067  if (!((validate_string_t)v->validate_fn)(v, newval))
1068    return -1;
1069
1070  g_value_set_string(&(v->val), newval);
1071  return(0);
1072}
1073
1074int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 
1075{
1076  return owl_variable_set_string(v, newval);
1077}
1078
1079CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy)
1080{
1081  return g_strdup(owl_variable_get_string(v));
1082}
1083
Note: See TracBrowser for help on using the repository browser.