source: variable.c @ 5f784ec

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