source: variable.c @ a0c06dc

release-1.10release-1.9
Last change on this file since a0c06dc was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 41.8 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  owl_variable *var, *cur;
571  for (var = variables_to_init; var->name != NULL; var++) {
572    cur = g_new(owl_variable, 1);
573    *cur = *var;
574    /* strdup all the strings so we can delete them consistently. */
575    cur->name = g_strdup(var->name);
576    cur->summary = g_strdup(var->summary);
577    cur->description = g_strdup(var->description);
578    switch (cur->type) {
579    case OWL_VARIABLE_OTHER:
580      cur->set_fn(cur, cur->pval_default);
581      break;
582    case OWL_VARIABLE_STRING:
583      if (!cur->validate_fn) 
584        cur->validate_fn = owl_variable_string_validate_default;
585      if (!cur->set_fn) 
586        cur->set_fn = owl_variable_string_set_default;
587      if (!cur->set_fromstring_fn) 
588        cur->set_fromstring_fn = owl_variable_string_set_fromstring_default;
589      if (!cur->get_fn) 
590        cur->get_fn = owl_variable_get_default;
591      if (!cur->get_tostring_fn) 
592        cur->get_tostring_fn = owl_variable_string_get_tostring_default;     
593      if (!cur->delete_fn)
594        cur->delete_fn = owl_variable_delete_default;
595      cur->pval_default = g_strdup(var->pval_default);
596      cur->set_fn(cur, cur->pval_default);
597      break;
598    case OWL_VARIABLE_BOOL:
599      if (!cur->validate_fn) 
600        cur->validate_fn = owl_variable_bool_validate_default;
601      if (!cur->set_fn) 
602        cur->set_fn = owl_variable_bool_set_default;
603      if (!cur->set_fromstring_fn) 
604        cur->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
605      if (!cur->get_fn) 
606        cur->get_fn = owl_variable_get_default;
607      if (!cur->get_tostring_fn) 
608        cur->get_tostring_fn = owl_variable_bool_get_tostring_default;     
609      if (!cur->delete_fn)
610        cur->delete_fn = owl_variable_delete_default;
611      cur->val = g_new(int, 1);
612      cur->set_fn(cur, &cur->ival_default);
613      break;
614    case OWL_VARIABLE_INT:
615      if (!cur->validate_fn) 
616        cur->validate_fn = owl_variable_int_validate_default;
617      if (!cur->set_fn) 
618        cur->set_fn = owl_variable_int_set_default;
619      if (!cur->set_fromstring_fn) 
620        cur->set_fromstring_fn = owl_variable_int_set_fromstring_default;
621      if (!cur->get_fn) 
622        cur->get_fn = owl_variable_get_default;
623      if (!cur->get_tostring_fn) 
624        cur->get_tostring_fn = owl_variable_int_get_tostring_default;     
625      if (!cur->delete_fn)
626        cur->delete_fn = owl_variable_delete_default;
627      cur->val = g_new(int, 1);
628      cur->set_fn(cur, &cur->ival_default);
629      break;
630    default:
631      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
632      return(-2);
633    }
634    owl_dict_insert_element(vd, cur->name, cur, NULL);
635  }
636  return 0;
637}
638
639void owl_variable_dict_add_variable(owl_vardict * vardict,
640                                    owl_variable * var) {
641  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
642}
643
644CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description)
645{
646  owl_variable * var = g_new0(owl_variable, 1);
647  var->name = g_strdup(name);
648  var->summary = g_strdup(summary);
649  var->description = g_strdup(description);
650  return var;
651}
652
653void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
654  g_free(var->summary);
655  var->summary = g_strdup(summary);
656  g_free(var->description);
657  var->description = g_strdup(desc);
658}
659
660void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
661{
662  owl_variable *old = owl_variable_get_var(vd, name);
663  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
664    owl_variable_update(old, summ, desc);
665    g_free(old->pval_default);
666    old->pval_default = g_strdup(initval);
667  } else {
668    owl_variable * var = owl_variable_newvar(name, summ, desc);
669    var->type = OWL_VARIABLE_STRING;
670    var->validsettings = "<string>";
671    var->pval_default = g_strdup(initval);
672    var->set_fn = owl_variable_string_set_default;
673    var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
674    var->get_fn = owl_variable_get_default;
675    var->get_tostring_fn = owl_variable_string_get_tostring_default;
676    var->delete_fn = owl_variable_delete_default;
677    var->set_fn(var, initval);
678    owl_variable_dict_add_variable(vd, var);
679  }
680}
681
682void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
683{
684  owl_variable *old = owl_variable_get_var(vd, name);
685  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
686    owl_variable_update(old, summ, desc);
687    old->ival_default = initval;
688  } else {
689    owl_variable * var = owl_variable_newvar(name, summ, desc);
690    var->type = OWL_VARIABLE_INT;
691    var->validsettings = "<int>";
692    var->ival_default = initval;
693    var->validate_fn = owl_variable_int_validate_default;
694    var->set_fn = owl_variable_int_set_default;
695    var->set_fromstring_fn = owl_variable_int_set_fromstring_default;
696    var->get_fn = owl_variable_get_default;
697    var->get_tostring_fn = owl_variable_int_get_tostring_default;
698    var->delete_fn = owl_variable_delete_default;
699    var->val = g_new(int, 1);
700    var->set_fn(var, &initval);
701    owl_variable_dict_add_variable(vd, var);
702  }
703}
704
705void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
706{
707  owl_variable *old = owl_variable_get_var(vd, name);
708  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
709    owl_variable_update(old, summ, desc);
710    old->ival_default = initval;
711  } else {
712    owl_variable * var = owl_variable_newvar(name, summ, desc);
713    var->type = OWL_VARIABLE_BOOL;
714    var->validsettings = "on,off";
715    var->ival_default = initval;
716    var->validate_fn = owl_variable_bool_validate_default;
717    var->set_fn = owl_variable_bool_set_default;
718    var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
719    var->get_fn = owl_variable_get_default;
720    var->get_tostring_fn = owl_variable_bool_get_tostring_default;
721    var->delete_fn = owl_variable_delete_default;
722    var->val = g_new(int, 1);
723    var->set_fn(var, &initval);
724    owl_variable_dict_add_variable(vd, var);
725  }
726}
727
728void owl_variable_dict_cleanup(owl_vardict *d)
729{
730  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
731}
732
733CALLER_OWN GPtrArray *owl_variable_dict_get_names(const owl_vardict *d) {
734  return owl_dict_get_keys(d);
735}
736
737void owl_variable_cleanup(owl_variable *v)
738{
739  if (v->delete_fn) v->delete_fn(v);
740  g_free(v->name);
741  g_free(v->summary);
742  g_free(v->description);
743  if (v->type == OWL_VARIABLE_STRING)
744    g_free(v->pval_default);
745}
746
747void owl_variable_delete(owl_variable *v)
748{
749  owl_variable_cleanup(v);
750  g_free(v);
751}
752
753
754const char *owl_variable_get_name(const owl_variable *v)
755{
756  return v->name;
757}
758
759const char *owl_variable_get_description(const owl_variable *v) {
760  return v->description;
761}
762
763const char *owl_variable_get_summary(const owl_variable *v) {
764  return v->summary;
765}
766
767const char *owl_variable_get_validsettings(const owl_variable *v) {
768  return v->validsettings;
769}
770
771int owl_variable_get_type(const owl_variable *v)
772{
773  return v->type;
774}
775
776/* functions for getting and setting variable values */
777
778/* returns 0 on success, prints a status msg if msg is true */
779int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
780  char *tostring;
781  if (!v->set_fromstring_fn) {
782    if (msg) owl_function_error("Variable %s is read-only", owl_variable_get_name(v));
783    return -1;   
784  }
785  if (0 != v->set_fromstring_fn(v, value)) {
786    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
787                                owl_variable_get_validsettings(v));
788    return -1;
789  }
790  if (msg) {
791    tostring = v->get_tostring_fn(v, v->get_fn(v));
792    if (tostring)
793      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
794    else
795      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
796    g_free(tostring);
797  }   
798  return 0;
799}
800 
801int owl_variable_set_string(owl_variable *v, const char *newval)
802{
803  if (v->type != OWL_VARIABLE_STRING) return -1;
804  return v->set_fn(v, newval);
805}
806 
807int owl_variable_set_int(owl_variable *v, int newval)
808{
809  if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1;
810  return v->set_fn(v, &newval);
811}
812 
813int owl_variable_set_bool_on(owl_variable *v)
814{
815  if (v->type != OWL_VARIABLE_BOOL) return -1;
816  return owl_variable_set_int(v, true);
817}
818
819int owl_variable_set_bool_off(owl_variable *v)
820{
821  if (v->type != OWL_VARIABLE_BOOL) return -1;
822  return owl_variable_set_int(v, false);
823}
824
825CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
826{
827  return v->get_tostring_fn(v, v->get_fn(v));
828}
829
830CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v)
831{
832  if (v->type == OWL_VARIABLE_INT || v->type == OWL_VARIABLE_BOOL) {
833    return v->get_tostring_fn(v, &(v->ival_default));
834  } else {
835    return v->get_tostring_fn(v, v->pval_default);
836  }
837}
838
839owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
840{
841  return owl_dict_find_element(d, name);
842}
843
844/* returns a reference */
845const void *owl_variable_get(const owl_variable *v)
846{
847  return v->get_fn(v);
848}
849
850const char *owl_variable_get_string(const owl_variable *v)
851{
852  if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) {
853    owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v));
854    return NULL;
855  }
856  return owl_variable_get(v);
857}
858
859/* returns a reference */
860const void *owl_variable_get_other(const owl_variable *v)
861{
862  if (owl_variable_get_type(v) != OWL_VARIABLE_OTHER) {
863    owl_function_error("Variable '%s' is not type other.", owl_variable_get_name(v));
864    return NULL;
865  }
866  return owl_variable_get(v);
867}
868
869int owl_variable_get_int(const owl_variable *v)
870{
871  if (owl_variable_get_type(v) != OWL_VARIABLE_INT) {
872    owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v));
873    return -1;
874  }
875  const int *pi = owl_variable_get(v);
876  if (!pi) return -1;
877  return *pi;
878}
879
880int owl_variable_get_bool(const owl_variable *v)
881{
882  if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) {
883    owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v));
884    return -1;
885  }
886  const int *pi = owl_variable_get(v);
887  if (!pi) return -1;
888  return *pi;
889}
890
891void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
892{
893  char *tostring = owl_variable_get_default_tostring(v);
894  char *default_buf;
895
896  if (tostring)
897    default_buf = g_strdup_printf("'%s'", tostring);
898  else
899    default_buf = g_strdup("<null>");
900  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
901                            owl_variable_get_name(v),
902                            owl_variable_get_summary(v), default_buf);
903  g_free(default_buf);
904  g_free(tostring);
905}
906
907void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
908  char *tostring;
909
910  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
911  owl_fmtext_append_normal(fm, OWL_TABSTR);
912  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
913  owl_fmtext_append_normal(fm, " - ");
914  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
915  owl_fmtext_append_normal(fm, "\n\n");
916
917  owl_fmtext_append_normal(fm, "Current:        ");
918  tostring = owl_variable_get_tostring(v);
919  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
920  g_free(tostring);
921  owl_fmtext_append_normal(fm, "\n\n");
922
923
924  tostring = owl_variable_get_default_tostring(v);
925  owl_fmtext_append_normal(fm, "Default:        ");
926  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
927  owl_fmtext_append_normal(fm, "\n\n");
928
929  owl_fmtext_append_normal(fm, "Valid Settings: ");
930  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
931  owl_fmtext_append_normal(fm, "\n\n");
932
933  if (v->description && *v->description) {
934    owl_fmtext_append_normal(fm, "Description:\n");
935    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
936    owl_fmtext_append_normal(fm, "\n\n");
937  }
938  g_free(tostring);
939}
940
941
942
943
944/**************************************************************************/
945/*********************** GENERAL TYPE-SPECIFIC ****************************/
946/**************************************************************************/
947
948/* default common functions */
949
950const void *owl_variable_get_default(const owl_variable *v) {
951  return v->val;
952}
953
954void owl_variable_delete_default(owl_variable *v)
955{
956  g_free(v->val);
957}
958
959/* default functions for booleans */
960
961int owl_variable_bool_validate_default(const owl_variable *v, const void *newval) {
962  if (newval == NULL) return(0);
963  else if (*(const int*)newval==1 || *(const int*)newval==0) return(1);
964  else return (0);
965}
966
967int owl_variable_bool_set_default(owl_variable *v, const void *newval) {
968  if (v->validate_fn) {
969    if (!v->validate_fn(v, newval)) return(-1);
970  }
971  *(int*)v->val = *(const int*)newval;
972  return(0);
973}
974
975int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval) {
976  int i;
977  if (!strcmp(newval, "on")) i=1;
978  else if (!strcmp(newval, "off")) i=0;
979  else return(-1);
980  return (v->set_fn(v, &i));
981}
982
983CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, const void *val)
984{
985  if (val == NULL) {
986    return NULL;
987  } else if (*(const int*)val == 0) {
988    return g_strdup("off");
989  } else if (*(const int*)val == 1) {
990    return g_strdup("on");
991  } else {
992    return g_strdup("<invalid>");
993  }
994}
995
996/* default functions for integers */
997
998int owl_variable_int_validate_default(const owl_variable *v, const void *newval) {
999  if (newval == NULL) return(0);
1000  else return (1);
1001}
1002
1003int owl_variable_int_set_default(owl_variable *v, const void *newval) {
1004  if (v->validate_fn) {
1005    if (!v->validate_fn(v, newval)) return(-1);
1006  }
1007  *(int*)v->val = *(const int*)newval;
1008  return(0);
1009}
1010
1011int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval) {
1012  int i;
1013  char *ep;
1014  i = strtol(newval, &ep, 10);
1015  if (*ep || ep==newval) return(-1);
1016  return (v->set_fn(v, &i));
1017}
1018
1019CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, const void *val)
1020{
1021  if (val == NULL) {
1022    return NULL;
1023  } else {
1024    return g_strdup_printf("%d", *(const int*)val);
1025  } 
1026}
1027
1028/* default functions for enums (a variant of integers) */
1029
1030int owl_variable_enum_validate(const owl_variable *v, const void *newval) { 
1031  char **enums;
1032  int nenums, val;
1033  if (newval == NULL) return(0);
1034  enums = g_strsplit_set(v->validsettings, ",", 0);
1035  nenums = g_strv_length(enums);
1036  g_strfreev(enums);
1037  val = *(const int*)newval;
1038  if (val < 0 || val >= nenums) {
1039    return(0);
1040  }
1041  return(1);
1042}
1043
1044int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval) {
1045  char **enums;
1046  int i, val=-1;
1047  if (newval == NULL) return(-1);
1048  enums = g_strsplit_set(v->validsettings, ",", 0);
1049  for (i = 0; enums[i] != NULL; i++) {
1050    if (0==strcmp(newval, enums[i])) {
1051      val = i;
1052    }
1053  }
1054  g_strfreev(enums);
1055  if (val == -1) return(-1);
1056  return (v->set_fn(v, &val));
1057}
1058
1059CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, const void *val)
1060{
1061  char **enums;
1062  int nenums, i;
1063  char *tostring;
1064
1065  if (val == NULL) {
1066    return NULL;
1067  }
1068  enums = g_strsplit_set(v->validsettings, ",", 0);
1069  nenums = g_strv_length(enums);
1070  i = *(const int*)val;
1071  if (i<0 || i>=nenums) {
1072    g_strfreev(enums);
1073    return g_strdup_printf("<invalid:%d>", i);
1074  }
1075  tostring = g_strdup(enums[i]);
1076  g_strfreev(enums);
1077  return tostring;
1078}
1079
1080/* default functions for stringeans */
1081
1082int owl_variable_string_validate_default(const struct _owl_variable *v, const void *newval) {
1083  if (newval == NULL) return(0);
1084  else return (1);
1085}
1086
1087int owl_variable_string_set_default(owl_variable *v, const void *newval) {
1088  if (v->validate_fn) {
1089    if (!v->validate_fn(v, newval)) return(-1);
1090  }
1091  g_free(v->val);
1092  v->val = g_strdup(newval);
1093  return(0);
1094}
1095
1096int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval) {
1097  return (v->set_fn(v, newval));
1098}
1099
1100CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, const void *val)
1101{
1102  return g_strdup((const char*)val);
1103}
1104
Note: See TracBrowser for help on using the repository browser.