Now inbound_cap_ls() can enable extensions when a bouncer uses a namespace for the...
[rofl0r-ixchat.git] / src / common / cfgfiles.c
blobbbd40f1b2e29c31e614f4d150474f6e4c98ea6e7
1 /* X-Chat
2 * Copyright (C) 1998 Peter Zelezny.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include "xchat.h"
28 #include "cfgfiles.h"
29 #include "util.h"
30 #include "fe.h"
31 #include "text.h"
32 #include "xchatc.h"
34 #define XCHAT_DIR ".xchat2"
35 #define DEF_FONT "Monospace 9"
37 void
38 list_addentry (GSList ** list, char *cmd, char *name)
40 struct popup *pop;
41 int cmd_len = 1, name_len;
43 /* remove <2.8.0 stuff */
44 if (!strcmp (cmd, "away") && !strcmp (name, "BACK"))
45 return;
47 if (cmd)
48 cmd_len = strlen (cmd) + 1;
49 name_len = strlen (name) + 1;
51 pop = malloc (sizeof (struct popup) + cmd_len + name_len);
52 pop->name = (char *) pop + sizeof (struct popup);
53 pop->cmd = pop->name + name_len;
55 memcpy (pop->name, name, name_len);
56 if (cmd)
57 memcpy (pop->cmd, cmd, cmd_len);
58 else
59 pop->cmd[0] = 0;
61 *list = g_slist_append (*list, pop);
64 /* read it in from a buffer to our linked list */
66 static void
67 list_load_from_data (GSList ** list, char *ibuf, int size)
69 char cmd[384];
70 char name[128];
71 char *buf;
72 int pnt = 0;
74 cmd[0] = 0;
75 name[0] = 0;
77 while (buf_get_line (ibuf, &buf, &pnt, size))
79 if (*buf != '#')
81 if (!strncasecmp (buf, "NAME ", 5))
83 safe_strcpy (name, buf + 5, sizeof (name));
85 else if (!strncasecmp (buf, "CMD ", 4))
87 safe_strcpy (cmd, buf + 4, sizeof (cmd));
88 if (*name)
90 list_addentry (list, cmd, name);
91 cmd[0] = 0;
92 name[0] = 0;
99 void
100 list_loadconf (char *file, GSList ** list, char *defaultconf)
102 char filebuf[256];
103 char *ibuf;
104 int fh;
105 struct stat st;
107 snprintf (filebuf, sizeof (filebuf), "%s/%s", get_xdir_fs (), file);
108 fh = open (filebuf, O_RDONLY | OFLAGS);
109 if (fh == -1)
111 if (defaultconf)
112 list_load_from_data (list, defaultconf, strlen (defaultconf));
113 return;
115 if (fstat (fh, &st) != 0)
117 perror ("fstat");
118 abort ();
121 ibuf = malloc (st.st_size);
122 read (fh, ibuf, st.st_size);
123 close (fh);
125 list_load_from_data (list, ibuf, st.st_size);
127 free (ibuf);
130 void
131 list_free (GSList ** list)
133 void *data;
134 while (*list)
136 data = (void *) (*list)->data;
137 free (data);
138 *list = g_slist_remove (*list, data);
143 list_delentry (GSList ** list, char *name)
145 struct popup *pop;
146 GSList *alist = *list;
148 while (alist)
150 pop = (struct popup *) alist->data;
151 if (!strcasecmp (name, pop->name))
153 *list = g_slist_remove (*list, pop);
154 free (pop);
155 return 1;
157 alist = alist->next;
159 return 0;
162 char *
163 cfg_get_str (char *cfg, char *var, char *dest, int dest_len)
165 while (1)
167 if (!strncasecmp (var, cfg, strlen (var)))
169 char *value, t;
170 cfg += strlen (var);
171 while (*cfg == ' ')
172 cfg++;
173 if (*cfg == '=')
174 cfg++;
175 while (*cfg == ' ')
176 cfg++;
177 /*while (*cfg == ' ' || *cfg == '=')
178 cfg++; */
179 value = cfg;
180 while (*cfg != 0 && *cfg != '\n')
181 cfg++;
182 t = *cfg;
183 *cfg = 0;
184 safe_strcpy (dest, value, dest_len);
185 *cfg = t;
186 return cfg;
188 while (*cfg != 0 && *cfg != '\n')
189 cfg++;
190 if (*cfg == 0)
191 return 0;
192 cfg++;
193 if (*cfg == 0)
194 return 0;
198 static int
199 cfg_put_str (int fh, char *var, char *value)
201 char buf[512];
202 int len;
204 snprintf (buf, sizeof buf, "%s = %s\n", var, value);
205 len = strlen (buf);
206 return (write (fh, buf, len) == len);
210 cfg_put_color (int fh, int r, int g, int b, char *var)
212 char buf[400];
213 int len;
215 snprintf (buf, sizeof buf, "%s = %04x %04x %04x\n", var, r, g, b);
216 len = strlen (buf);
217 return (write (fh, buf, len) == len);
221 cfg_put_int (int fh, int value, char *var)
223 char buf[400];
224 int len;
226 if (value == -1)
227 value = 1;
229 snprintf (buf, sizeof buf, "%s = %d\n", var, value);
230 len = strlen (buf);
231 return (write (fh, buf, len) == len);
235 cfg_get_color (char *cfg, char *var, int *r, int *g, int *b)
237 char str[128];
239 if (!cfg_get_str (cfg, var, str, sizeof (str)))
240 return 0;
242 sscanf (str, "%04x %04x %04x", r, g, b);
243 return 1;
247 cfg_get_int_with_result (char *cfg, char *var, int *result)
249 char str[128];
251 if (!cfg_get_str (cfg, var, str, sizeof (str)))
253 *result = 0;
254 return 0;
257 *result = 1;
258 return atoi (str);
262 cfg_get_int (char *cfg, char *var)
264 char str[128];
266 if (!cfg_get_str (cfg, var, str, sizeof (str)))
267 return 0;
269 return atoi (str);
272 char *xdir_fs = NULL; /* file system encoding */
273 char *xdir_utf = NULL; /* utf-8 encoding */
275 char *
276 get_xdir_fs (void)
278 if (!xdir_fs)
279 xdir_fs = g_strdup_printf ("%s/" XCHAT_DIR, g_get_home_dir ());
281 return xdir_fs;
284 char *
285 get_xdir_utf8 (void)
287 if (!xdir_utf) /* never free this, keep it for program life time */
288 xdir_utf = xchat_filename_to_utf8 (get_xdir_fs (), -1, 0, 0, 0);
290 return xdir_utf;
293 static void
294 check_prefs_dir (void)
296 char *dir = get_xdir_fs ();
297 if (access (dir, F_OK) != 0)
299 if (mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
300 fe_message (_("Cannot create ~/.xchat2"), FE_MSG_ERROR);
304 static char *
305 default_file (void)
307 static char *dfile = 0;
309 if (!dfile)
311 dfile = malloc (strlen (get_xdir_fs ()) + 12);
312 sprintf (dfile, "%s/xchat.conf", get_xdir_fs ());
314 return dfile;
317 /* Keep these sorted!! */
319 const struct prefs vars[] = {
320 {"auto_save", P_OFFINT (autosave), TYPE_BOOL},
321 {"auto_save_url", P_OFFINT (autosave_url), TYPE_BOOL},
323 {"away_auto_unmark", P_OFFINT (auto_unmark_away), TYPE_BOOL},
324 {"away_reason", P_OFFSET (awayreason), TYPE_STR},
325 {"away_show_message", P_OFFINT (show_away_message), TYPE_BOOL},
326 {"away_show_once", P_OFFINT (show_away_once), TYPE_BOOL},
327 {"away_size_max", P_OFFINT (away_size_max), TYPE_INT},
328 {"away_timeout", P_OFFINT (away_timeout), TYPE_INT},
329 {"away_track", P_OFFINT (away_track), TYPE_BOOL},
331 {"completion_amount", P_OFFINT (completion_amount), TYPE_INT},
332 {"completion_auto", P_OFFINT (nickcompletion), TYPE_BOOL},
333 {"completion_sort", P_OFFINT (completion_sort), TYPE_INT},
334 {"completion_suffix", P_OFFSET (nick_suffix), TYPE_STR},
336 {"dcc_auto_chat", P_OFFINT (autodccchat), TYPE_INT},
337 {"dcc_auto_resume", P_OFFINT (autoresume), TYPE_BOOL},
338 {"dcc_auto_send", P_OFFINT (autodccsend), TYPE_INT},
339 {"dcc_blocksize", P_OFFINT (dcc_blocksize), TYPE_INT},
340 {"dcc_completed_dir", P_OFFSET (dcc_completed_dir), TYPE_STR},
341 {"dcc_dir", P_OFFSET (dccdir), TYPE_STR},
342 {"dcc_fast_send", P_OFFINT (fastdccsend), TYPE_BOOL},
343 {"dcc_global_max_get_cps", P_OFFINT (dcc_global_max_get_cps), TYPE_INT},
344 {"dcc_global_max_send_cps", P_OFFINT (dcc_global_max_send_cps), TYPE_INT},
345 {"dcc_ip", P_OFFSET (dcc_ip_str), TYPE_STR},
346 {"dcc_ip_from_server", P_OFFINT (ip_from_server), TYPE_BOOL},
347 {"dcc_max_get_cps", P_OFFINT (dcc_max_get_cps), TYPE_INT},
348 {"dcc_max_send_cps", P_OFFINT (dcc_max_send_cps), TYPE_INT},
349 {"dcc_permissions", P_OFFINT (dccpermissions), TYPE_INT},
350 {"dcc_port_first", P_OFFINT (first_dcc_send_port), TYPE_INT},
351 {"dcc_port_last", P_OFFINT (last_dcc_send_port), TYPE_INT},
352 {"dcc_remove", P_OFFINT (dcc_remove), TYPE_BOOL},
353 {"dcc_save_nick", P_OFFINT (dccwithnick), TYPE_BOOL},
354 {"dcc_send_fillspaces", P_OFFINT (dcc_send_fillspaces), TYPE_BOOL},
355 {"dcc_stall_timeout", P_OFFINT (dccstalltimeout), TYPE_INT},
356 {"dcc_timeout", P_OFFINT (dcctimeout), TYPE_INT},
358 {"dnsprogram", P_OFFSET (dnsprogram), TYPE_STR},
360 {"flood_ctcp_num", P_OFFINT (ctcp_number_limit), TYPE_INT},
361 {"flood_ctcp_time", P_OFFINT (ctcp_time_limit), TYPE_INT},
362 {"flood_msg_num", P_OFFINT (msg_number_limit), TYPE_INT},
363 {"flood_msg_time", P_OFFINT (msg_time_limit), TYPE_INT},
365 {"gui_auto_open_chat", P_OFFINT (autoopendccchatwindow), TYPE_BOOL},
366 {"gui_auto_open_dialog", P_OFFINT (autodialog), TYPE_BOOL},
367 {"gui_auto_open_recv", P_OFFINT (autoopendccrecvwindow), TYPE_BOOL},
368 {"gui_auto_open_send", P_OFFINT (autoopendccsendwindow), TYPE_BOOL},
369 {"gui_dialog_height", P_OFFINT (dialog_height), TYPE_INT},
370 {"gui_dialog_left", P_OFFINT (dialog_left), TYPE_INT},
371 {"gui_dialog_top", P_OFFINT (dialog_top), TYPE_INT},
372 {"gui_dialog_width", P_OFFINT (dialog_width), TYPE_INT},
373 {"gui_hide_menu", P_OFFINT (hidemenu), TYPE_BOOL},
374 {"gui_input_spell", P_OFFINT (gui_input_spell), TYPE_BOOL},
375 {"gui_input_style", P_OFFINT (style_inputbox), TYPE_BOOL},
376 {"gui_join_dialog", P_OFFINT (gui_join_dialog), TYPE_BOOL},
377 {"gui_lagometer", P_OFFINT (lagometer), TYPE_INT},
378 {"gui_mode_buttons", P_OFFINT (chanmodebuttons), TYPE_BOOL},
379 {"gui_pane_left_size", P_OFFINT (gui_pane_left_size), TYPE_INT},
380 {"gui_pane_right_size", P_OFFINT (gui_pane_right_size), TYPE_INT},
381 {"gui_quit_dialog", P_OFFINT (gui_quit_dialog), TYPE_BOOL},
382 {"gui_slist_fav", P_OFFINT (slist_fav), TYPE_INT},
383 {"gui_slist_select", P_OFFINT (slist_select), TYPE_INT},
384 {"gui_slist_skip", P_OFFINT (slist_skip), TYPE_BOOL},
385 {"gui_throttlemeter", P_OFFINT (throttlemeter), TYPE_INT},
386 {"gui_topicbar", P_OFFINT (topicbar), TYPE_BOOL},
387 {"gui_tray", P_OFFINT (gui_tray), TYPE_BOOL},
388 {"gui_tray_flags", P_OFFINT (gui_tray_flags), TYPE_INT},
389 {"gui_tweaks", P_OFFINT (gui_tweaks), TYPE_INT},
390 {"gui_ulist_buttons", P_OFFINT (userlistbuttons), TYPE_BOOL},
391 {"gui_ulist_doubleclick", P_OFFSET (doubleclickuser), TYPE_STR},
392 {"gui_ulist_hide", P_OFFINT (hideuserlist), TYPE_BOOL},
393 {"gui_ulist_left", P_OFFINT (_gui_ulist_left), TYPE_BOOL}, /* obsolete */
394 {"gui_ulist_pos", P_OFFINT (gui_ulist_pos), TYPE_INT},
395 {"gui_ulist_resizable", P_OFFINT (paned_userlist), TYPE_BOOL},
396 {"gui_ulist_show_hosts", P_OFFINT(showhostname_in_userlist), TYPE_BOOL},
397 {"gui_ulist_sort", P_OFFINT (userlist_sort), TYPE_INT},
398 {"gui_ulist_style", P_OFFINT (style_namelistgad), TYPE_BOOL},
399 {"gui_url_mod", P_OFFINT (gui_url_mod), TYPE_INT},
400 {"gui_usermenu", P_OFFINT (gui_usermenu), TYPE_BOOL},
401 {"gui_win_height", P_OFFINT (mainwindow_height), TYPE_INT},
402 {"gui_win_left", P_OFFINT (mainwindow_left), TYPE_INT},
403 {"gui_win_save", P_OFFINT (mainwindow_save), TYPE_BOOL},
404 {"gui_win_state", P_OFFINT (gui_win_state), TYPE_INT},
405 {"gui_win_top", P_OFFINT (mainwindow_top), TYPE_INT},
406 {"gui_win_width", P_OFFINT (mainwindow_width), TYPE_INT},
408 {"input_balloon_chans", P_OFFINT (input_balloon_chans), TYPE_BOOL},
409 {"input_balloon_hilight", P_OFFINT (input_balloon_hilight), TYPE_BOOL},
410 {"input_balloon_priv", P_OFFINT (input_balloon_priv), TYPE_BOOL},
411 {"input_balloon_time", P_OFFINT (input_balloon_time), TYPE_INT},
412 {"input_beep_chans", P_OFFINT (input_beep_chans), TYPE_BOOL},
413 {"input_beep_hilight", P_OFFINT (input_beep_hilight), TYPE_BOOL},
414 {"input_beep_msg", P_OFFINT (input_beep_priv), TYPE_BOOL},
415 {"input_command_char", P_OFFSET (cmdchar), TYPE_STR},
416 {"input_filter_beep", P_OFFINT (filterbeep), TYPE_BOOL},
417 {"input_flash_chans", P_OFFINT (input_flash_chans), TYPE_BOOL},
418 {"input_flash_hilight", P_OFFINT (input_flash_hilight), TYPE_BOOL},
419 {"input_flash_priv", P_OFFINT (input_flash_priv), TYPE_BOOL},
420 {"input_perc_ascii", P_OFFINT (perc_ascii), TYPE_BOOL},
421 {"input_perc_color", P_OFFINT (perc_color), TYPE_BOOL},
422 {"input_tray_chans", P_OFFINT (input_tray_chans), TYPE_BOOL},
423 {"input_tray_hilight", P_OFFINT (input_tray_hilight), TYPE_BOOL},
424 {"input_tray_priv", P_OFFINT (input_tray_priv), TYPE_BOOL},
426 {"irc_auto_rejoin", P_OFFINT (autorejoin), TYPE_BOOL},
427 {"irc_ban_type", P_OFFINT (bantype), TYPE_INT},
428 {"irc_conf_mode", P_OFFINT (confmode), TYPE_BOOL},
429 {"irc_extra_hilight", P_OFFSET (irc_extra_hilight), TYPE_STR},
430 {"irc_hide_version", P_OFFINT (hidever), TYPE_BOOL},
431 {"irc_id_ntext", P_OFFSET (irc_id_ntext), TYPE_STR},
432 {"irc_id_ytext", P_OFFSET (irc_id_ytext), TYPE_STR},
433 {"irc_invisible", P_OFFINT (invisible), TYPE_BOOL},
434 {"irc_join_delay", P_OFFINT (irc_join_delay), TYPE_INT},
435 {"irc_logging", P_OFFINT (logging), TYPE_BOOL},
436 {"irc_logmask", P_OFFSET (logmask), TYPE_STR},
437 {"irc_nick1", P_OFFSET (nick1), TYPE_STR},
438 {"irc_nick2", P_OFFSET (nick2), TYPE_STR},
439 {"irc_nick3", P_OFFSET (nick3), TYPE_STR},
440 {"irc_nick_hilight", P_OFFSET (irc_nick_hilight), TYPE_STR},
441 {"irc_no_hilight", P_OFFSET (irc_no_hilight), TYPE_STR},
442 {"irc_part_reason", P_OFFSET (partreason), TYPE_STR},
443 {"irc_quit_reason", P_OFFSET (quitreason), TYPE_STR},
444 {"irc_raw_modes", P_OFFINT (raw_modes), TYPE_BOOL},
445 {"irc_real_name", P_OFFSET (realname), TYPE_STR},
446 {"irc_servernotice", P_OFFINT (servernotice), TYPE_BOOL},
447 {"irc_skip_motd", P_OFFINT (skipmotd), TYPE_BOOL},
448 {"irc_user_name", P_OFFSET (username), TYPE_STR},
449 {"irc_wallops", P_OFFINT (wallops), TYPE_BOOL},
450 {"irc_who_join", P_OFFINT (userhost), TYPE_BOOL},
451 {"irc_whois_front", P_OFFINT (irc_whois_front), TYPE_BOOL},
453 {"net_auto_reconnect", P_OFFINT (autoreconnect), TYPE_BOOL},
454 {"net_bind_host", P_OFFSET (hostname), TYPE_STR},
455 {"net_ping_timeout", P_OFFINT (pingtimeout), TYPE_INT},
456 {"net_proxy_auth", P_OFFINT (proxy_auth), TYPE_BOOL},
457 {"net_proxy_host", P_OFFSET (proxy_host), TYPE_STR},
458 {"net_proxy_pass", P_OFFSET (proxy_pass), TYPE_STR},
459 {"net_proxy_port", P_OFFINT (proxy_port), TYPE_INT},
460 {"net_proxy_type", P_OFFINT (proxy_type), TYPE_INT},
461 {"net_proxy_use", P_OFFINT (proxy_use), TYPE_INT},
462 {"net_proxy_user", P_OFFSET (proxy_user), TYPE_STR},
464 {"net_reconnect_delay", P_OFFINT (recon_delay), TYPE_INT},
465 {"net_throttle", P_OFFINT (throttle), TYPE_BOOL},
467 {"notify_timeout", P_OFFINT (notify_timeout), TYPE_INT},
468 {"notify_whois_online", P_OFFINT (whois_on_notifyonline), TYPE_BOOL},
470 {"perl_warnings", P_OFFINT (perlwarnings), TYPE_BOOL},
472 {"sound_command", P_OFFSET (soundcmd), TYPE_STR},
473 {"sound_dir", P_OFFSET (sounddir), TYPE_STR},
474 {"stamp_log", P_OFFINT (timestamp_logs), TYPE_BOOL},
475 {"stamp_log_format", P_OFFSET (timestamp_log_format), TYPE_STR},
476 {"stamp_text", P_OFFINT (timestamp), TYPE_BOOL},
477 {"stamp_text_format", P_OFFSET (stamp_format), TYPE_STR},
479 {"tab_chans", P_OFFINT (tabchannels), TYPE_BOOL},
480 {"tab_dialogs", P_OFFINT (privmsgtab), TYPE_BOOL},
481 {"tab_layout", P_OFFINT (tab_layout), TYPE_INT},
482 {"tab_new_to_front", P_OFFINT (newtabstofront), TYPE_INT},
483 {"tab_notices", P_OFFINT (notices_tabs), TYPE_BOOL},
484 {"tab_pos", P_OFFINT (tab_pos), TYPE_INT},
485 {"tab_position", P_OFFINT (_tabs_position), TYPE_INT}, /* obsolete */
486 {"tab_server", P_OFFINT (use_server_tab), TYPE_BOOL},
487 {"tab_small", P_OFFINT (tab_small), TYPE_INT},
488 {"tab_sort", P_OFFINT (tab_sort), TYPE_BOOL},
489 {"tab_trunc", P_OFFINT (truncchans), TYPE_INT},
490 {"tab_utils", P_OFFINT (windows_as_tabs), TYPE_BOOL},
492 {"text_background", P_OFFSET (background), TYPE_STR},
493 {"text_color_nicks", P_OFFINT (colorednicks), TYPE_BOOL},
494 {"text_font", P_OFFSET (font_normal), TYPE_STR},
495 {"text_indent", P_OFFINT (indent_nicks), TYPE_BOOL},
496 {"text_max_indent", P_OFFINT (max_auto_indent), TYPE_INT},
497 {"text_max_lines", P_OFFINT (max_lines), TYPE_INT},
498 {"text_replay", P_OFFINT (text_replay), TYPE_BOOL},
499 {"text_show_marker", P_OFFINT (show_marker), TYPE_BOOL},
500 {"text_show_sep", P_OFFINT (show_separator), TYPE_BOOL},
501 {"text_stripcolor", P_OFFINT (stripcolor), TYPE_BOOL},
502 {"text_thin_sep", P_OFFINT (thin_separator), TYPE_BOOL},
503 {"text_tint_blue", P_OFFINT (tint_blue), TYPE_INT},
504 {"text_tint_green", P_OFFINT (tint_green), TYPE_INT},
505 {"text_tint_red", P_OFFINT (tint_red), TYPE_INT},
506 {"text_transparent", P_OFFINT (transparent), TYPE_BOOL},
507 {"text_wordwrap", P_OFFINT (wordwrap), TYPE_BOOL},
509 {"url_grabber", P_OFFINT (url_grabber), TYPE_BOOL},
510 {"url_grabber_limit", P_OFFINT (url_grabber_limit), TYPE_INT},
511 {0, 0, 0},
514 static char *
515 convert_with_fallback (const char *str, const char *fallback)
517 char *utf;
519 utf = g_locale_to_utf8 (str, -1, 0, 0, 0);
520 if (!utf)
522 /* this can happen if CHARSET envvar is set wrong */
523 /* maybe it's already utf8 (breakage!) */
524 if (!g_utf8_validate (str, -1, NULL))
525 utf = g_strdup (fallback);
526 else
527 utf = g_strdup (str);
530 return utf;
533 void
534 load_config (void)
536 struct stat st;
537 char *cfg, *sp;
538 const char *username, *realname;
539 int res, val, i, fh;
541 check_prefs_dir ();
542 username = g_get_user_name ();
543 if (!username)
544 username = "root";
546 realname = g_get_real_name ();
547 if ((realname && realname[0] == 0) || !realname)
548 realname = username;
550 username = convert_with_fallback (username, "username");
551 realname = convert_with_fallback (realname, "realname");
553 memset (&prefs, 0, sizeof (struct xchatprefs));
555 /* put in default values, anything left out is automatically zero */
556 prefs.local_ip = 0xffffffff;
557 prefs.irc_join_delay = 3;
558 prefs.show_marker = 1;
559 prefs.newtabstofront = 0;
560 prefs.completion_amount = 5;
561 prefs.away_timeout = 60;
562 prefs.away_size_max = 300;
563 prefs.away_track = 1;
564 prefs.timestamp_logs = 1;
565 prefs.truncchans = 20;
566 prefs.autoresume = 1;
567 prefs.show_away_once = 1;
568 prefs.indent_nicks = 1;
569 prefs.thin_separator = 1;
570 prefs._tabs_position = 2; /* 2 = left */
571 prefs.fastdccsend = 1;
572 prefs.wordwrap = 1;
573 prefs.autosave = 1;
574 prefs.autodialog = 1;
575 prefs.gui_input_spell = 1;
576 prefs.autoreconnect = 1;
577 prefs.recon_delay = 10;
578 prefs.text_replay = 1;
579 prefs.tabchannels = 1;
580 prefs.tab_layout = 2; /* 0=Tabs 1=Reserved 2=Tree */
581 prefs.tab_sort = 1;
582 prefs.paned_userlist = 1;
583 prefs.newtabstofront = 2;
584 prefs.use_server_tab = 1;
585 prefs.privmsgtab = 1;
586 /*prefs.style_inputbox = 1;*/
587 prefs.dccpermissions = 0600;
588 prefs.max_lines = 500;
589 prefs.mainwindow_width = 640;
590 prefs.mainwindow_height = 400;
591 prefs.dialog_width = 500;
592 prefs.dialog_height = 256;
593 prefs.gui_join_dialog = 1;
594 prefs.gui_quit_dialog = 1;
595 prefs.dcctimeout = 180;
596 prefs.dccstalltimeout = 60;
597 prefs.notify_timeout = 15;
598 prefs.tint_red =
599 prefs.tint_green =
600 prefs.tint_blue = 195;
601 prefs.auto_indent = 1;
602 prefs.max_auto_indent = 256;
603 prefs.show_separator = 1;
604 prefs.dcc_blocksize = 1024;
605 prefs.throttle = 1;
606 /*FIXME*/ prefs.msg_time_limit = 30;
607 prefs.msg_number_limit = 5;
608 prefs.ctcp_time_limit = 30;
609 prefs.ctcp_number_limit = 5;
610 prefs.topicbar = 1;
611 prefs.lagometer = 1;
612 prefs.throttlemeter = 1;
613 prefs.autoopendccrecvwindow = 1;
614 prefs.autoopendccsendwindow = 1;
615 prefs.autoopendccchatwindow = 1;
616 prefs.userhost = 1;
617 prefs.gui_url_mod = 4; /* ctrl */
618 prefs.gui_tray = 1;
619 prefs.gui_pane_left_size = 100;
620 prefs.gui_pane_right_size = 100;
621 prefs.mainwindow_save = 1;
622 prefs.bantype = 2;
623 prefs.input_balloon_time = 20;
624 prefs.input_flash_priv = prefs.input_flash_hilight = 1;
625 prefs.input_tray_priv = prefs.input_tray_hilight = 1;
626 prefs.autodccsend = 2; /* browse mode */
627 prefs.url_grabber = 1;
628 prefs.url_grabber_limit = 0; /* 0 means unlimited for backcompat */
629 strcpy (prefs.stamp_format, "[%H:%M] ");
630 strcpy (prefs.timestamp_log_format, "%b %d %H:%M:%S ");
631 strcpy (prefs.logmask, "%n-%c.log");
632 strcpy (prefs.nick_suffix, ",");
633 strcpy (prefs.cmdchar, "/");
634 strcpy (prefs.nick1, username);
635 strcpy (prefs.nick2, username);
636 strcat (prefs.nick2, "_");
637 strcpy (prefs.nick3, username);
638 strcat (prefs.nick3, "__");
639 strcpy (prefs.realname, realname);
640 strcpy (prefs.username, username);
641 snprintf (prefs.sounddir, sizeof (prefs.sounddir), "%s/sounds", get_xdir_utf8 ());
642 snprintf (prefs.dccdir, sizeof (prefs.dccdir), "%s/downloads", get_xdir_utf8 ());
643 strcpy (prefs.doubleclickuser, "QUERY %s");
644 strcpy (prefs.awayreason, _("I'm busy"));
645 strcpy (prefs.quitreason, _("Leaving"));
646 strcpy (prefs.partreason, prefs.quitreason);
647 strcpy (prefs.font_normal, DEF_FONT);
648 strcpy (prefs.dnsprogram, "host");
649 strcpy (prefs.irc_no_hilight, "NickServ,ChanServ");
651 g_free ((char *)username);
652 g_free ((char *)realname);
654 fh = open (default_file (), OFLAGS | O_RDONLY);
655 if (fh != -1)
657 fstat (fh, &st);
658 cfg = malloc (st.st_size + 1);
659 cfg[0] = '\0';
660 i = read (fh, cfg, st.st_size);
661 if (i >= 0)
662 cfg[i] = '\0'; /* make sure cfg is NULL terminated */
663 close (fh);
664 i = 0;
667 switch (vars[i].type)
669 case TYPE_STR:
670 cfg_get_str (cfg, vars[i].name, (char *) &prefs + vars[i].offset,
671 vars[i].len);
672 break;
673 case TYPE_BOOL:
674 case TYPE_INT:
675 val = cfg_get_int_with_result (cfg, vars[i].name, &res);
676 if (res)
677 *((int *) &prefs + vars[i].offset) = val;
678 break;
680 i++;
682 while (vars[i].name);
684 free (cfg);
686 } else
688 /* OS/2 uses UID 0 all the time */
689 if (getuid () == 0)
690 fe_message (_("* Running IRC as root is stupid! You should\n"
691 " create a User Account and use that to login.\n"), FE_MSG_WARN|FE_MSG_WAIT);
693 mkdir_utf8 (prefs.dccdir);
694 mkdir_utf8 (prefs.dcc_completed_dir);
696 if (prefs.mainwindow_height < 138)
697 prefs.mainwindow_height = 138;
698 if (prefs.mainwindow_width < 106)
699 prefs.mainwindow_width = 106;
701 sp = strchr (prefs.username, ' ');
702 if (sp)
703 sp[0] = 0; /* spaces in username would break the login */
705 /* try to make sense of old ulist/tree position settings */
706 if (prefs.gui_ulist_pos == 0)
708 prefs.gui_ulist_pos = 3; /* top right */
709 if (prefs._gui_ulist_left)
710 prefs.gui_ulist_pos = 2; /* bottom left */
712 switch (prefs._tabs_position)
714 case 0:
715 prefs.tab_pos = 6; /* bottom */
716 break;
717 case 1:
718 prefs.tab_pos = 5; /* top */
719 break;
720 case 2:
721 prefs.tab_pos = 1; /* left */
722 break;
723 case 3:
724 prefs.tab_pos = 4; /* right */
725 break;
726 case 4:
727 prefs.tab_pos = 1; /* (hidden)left */
728 break;
729 case 5:
730 if (prefs._gui_ulist_left)
732 prefs.tab_pos = 1; /* above ulist left */
733 prefs.gui_ulist_pos = 2;
735 else
737 prefs.tab_pos = 3; /* above ulist right */
738 prefs.gui_ulist_pos = 4;
740 break;
746 save_config (void)
748 int fh, i;
749 char *new_config, *config;
751 check_prefs_dir ();
753 config = default_file ();
754 new_config = malloc (strlen (config) + 5);
755 strcpy (new_config, config);
756 strcat (new_config, ".new");
758 fh = open (new_config, OFLAGS | O_TRUNC | O_WRONLY | O_CREAT, 0600);
759 if (fh == -1)
761 free (new_config);
762 return 0;
765 if (!cfg_put_str (fh, "version", PACKAGE_VERSION))
767 free (new_config);
768 return 0;
771 i = 0;
774 switch (vars[i].type)
776 case TYPE_STR:
777 if (!cfg_put_str (fh, vars[i].name, (char *) &prefs + vars[i].offset))
779 free (new_config);
780 return 0;
782 break;
783 case TYPE_INT:
784 case TYPE_BOOL:
785 if (!cfg_put_int (fh, *((int *) &prefs + vars[i].offset), vars[i].name))
787 free (new_config);
788 return 0;
791 i++;
793 while (vars[i].name);
795 if (close (fh) == -1)
797 free (new_config);
798 return 0;
801 if (rename (new_config, config) == -1)
803 free (new_config);
804 return 0;
806 free (new_config);
808 return 1;
811 static void
812 set_showval (session *sess, const struct prefs *var, char *tbuf)
814 int len, dots, j;
816 len = strlen (var->name);
817 memcpy (tbuf, var->name, len);
818 dots = 29 - len;
819 if (dots < 0)
820 dots = 0;
821 tbuf[len++] = '\003';
822 tbuf[len++] = '2';
823 for (j=0;j<dots;j++)
824 tbuf[j + len] = '.';
825 len += j;
826 switch (var->type)
828 case TYPE_STR:
829 sprintf (tbuf + len, "\0033:\017 %s\n",
830 (char *) &prefs + var->offset);
831 break;
832 case TYPE_INT:
833 sprintf (tbuf + len, "\0033:\017 %d\n",
834 *((int *) &prefs + var->offset));
835 break;
836 case TYPE_BOOL:
837 if (*((int *) &prefs + var->offset))
838 sprintf (tbuf + len, "\0033:\017 %s\n", "ON");
839 else
840 sprintf (tbuf + len, "\0033:\017 %s\n", "OFF");
841 break;
843 PrintText (sess, tbuf);
846 static void
847 set_list (session * sess, char *tbuf)
849 int i;
851 i = 0;
854 set_showval (sess, &vars[i], tbuf);
855 i++;
857 while (vars[i].name);
861 cfg_get_bool (char *var)
863 int i = 0;
867 if (!strcasecmp (var, vars[i].name))
869 return *((int *) &prefs + vars[i].offset);
871 i++;
873 while (vars[i].name);
875 return -1;
879 cmd_set (struct session *sess, char *tbuf, char *word[], char *word_eol[])
881 int wild = FALSE;
882 int or = FALSE;
883 int off = FALSE;
884 int quiet = FALSE;
885 int erase = FALSE;
886 int i = 0, finds = 0, found;
887 int idx = 2;
888 char *var, *val;
890 if (strcasecmp (word[2], "-e") == 0)
892 idx++;
893 erase = TRUE;
896 /* turn a bit OFF */
897 if (strcasecmp (word[idx], "-off") == 0)
899 idx++;
900 off = TRUE;
903 /* turn a bit ON */
904 if (strcasecmp (word[idx], "-or") == 0 || strcasecmp (word[idx], "-on") == 0)
906 idx++;
907 or = TRUE;
910 if (strcasecmp (word[idx], "-quiet") == 0)
912 idx++;
913 quiet = TRUE;
916 var = word[idx];
917 val = word_eol[idx+1];
919 if (!*var)
921 set_list (sess, tbuf);
922 return TRUE;
925 if ((strchr (var, '*') || strchr (var, '?')) && !*val)
926 wild = TRUE;
928 if (*val == '=')
929 val++;
933 if (wild)
934 found = !match (var, vars[i].name);
935 else
936 found = strcasecmp (var, vars[i].name);
938 if (found == 0)
940 finds++;
941 switch (vars[i].type)
943 case TYPE_STR:
944 if (erase || *val)
946 strncpy ((char *) &prefs + vars[i].offset, val, vars[i].len);
947 ((char *) &prefs)[vars[i].offset + vars[i].len - 1] = 0;
948 if (!quiet)
949 PrintTextf (sess, "%s set to: %s\n", var, (char *) &prefs + vars[i].offset);
950 } else
952 set_showval (sess, &vars[i], tbuf);
954 break;
955 case TYPE_INT:
956 case TYPE_BOOL:
957 if (*val)
959 if (vars[i].type == TYPE_BOOL)
961 if (atoi (val))
962 *((int *) &prefs + vars[i].offset) = 1;
963 else
964 *((int *) &prefs + vars[i].offset) = 0;
965 if (!strcasecmp (val, "YES") || !strcasecmp (val, "ON"))
966 *((int *) &prefs + vars[i].offset) = 1;
967 if (!strcasecmp (val, "NO") || !strcasecmp (val, "OFF"))
968 *((int *) &prefs + vars[i].offset) = 0;
969 } else
971 if (or)
972 *((int *) &prefs + vars[i].offset) |= atoi (val);
973 else if (off)
974 *((int *) &prefs + vars[i].offset) &= ~(atoi (val));
975 else
976 *((int *) &prefs + vars[i].offset) = atoi (val);
978 if (!quiet)
979 PrintTextf (sess, "%s set to: %d\n", var,
980 *((int *) &prefs + vars[i].offset));
981 } else
983 set_showval (sess, &vars[i], tbuf);
985 break;
988 i++;
990 while (vars[i].name);
992 if (!finds && !quiet)
993 PrintText (sess, "No such variable.\n");
995 return TRUE;
999 xchat_open_file (char *file, int flags, int mode, int xof_flags)
1001 char buf[1024];
1003 if (xof_flags & XOF_FULLPATH)
1005 if (xof_flags & XOF_DOMODE)
1006 return open (file, flags | OFLAGS, mode);
1007 else
1008 return open (file, flags | OFLAGS);
1011 snprintf (buf, sizeof (buf), "%s/%s", get_xdir_fs (), file);
1012 if (xof_flags & XOF_DOMODE)
1013 return open (buf, flags | OFLAGS, mode);
1014 else
1015 return open (buf, flags | OFLAGS);
1018 FILE *
1019 xchat_fopen_file (const char *file, const char *mode, int xof_flags)
1021 char buf[1024];
1023 if (xof_flags & XOF_FULLPATH)
1024 return fopen (file, mode);
1026 snprintf (buf, sizeof (buf), "%s/%s", get_xdir_fs (), file);
1027 return fopen (buf, mode);