f4c20c98650cc6dbff6818fae9bb76a295988599
[silc.git] / apps / irssi / src / silc / core / silc-core.c
1 /*
2
3   silc-core.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 #include "module.h"
22 #include "chat-protocols.h"
23 #include "args.h"
24
25 #include "chatnets.h"
26 #include "servers-setup.h"
27 #include "channels-setup.h"
28 #include "silc-servers.h"
29 #include "silc-channels.h"
30 #include "silc-queries.h"
31 #include "silc-nicklist.h"
32 #include "silc-chatnets.h"
33 #include "silc-cmdqueue.h"
34 #include "silc-commands.h"
35
36 #include "signals.h"
37 #include "levels.h"
38 #include "settings.h"
39 #include "commands.h"
40 #include "fe-common/core/printtext.h"
41 #include "fe-common/core/fe-channels.h"
42 #include "fe-common/core/keyboard.h"
43 #include "fe-common/silc/module-formats.h"
44
45 #ifdef SILC_PLUGIN
46 static int init_failed = 0;
47 #else
48 static char *opt_pkcs = NULL;
49 static int opt_bits = 0;
50 #endif
51
52 static int running = 0;
53
54 /* SILC Client */
55 SilcClient silc_client = NULL;
56 extern SilcClientOperations ops;
57
58 /* Our keypair */
59 SilcPublicKey irssi_pubkey = NULL;
60 SilcPrivateKey irssi_privkey = NULL;
61
62 char *opt_hostname = NULL;
63
64 /* Default hash function */
65 SilcHash sha1hash = NULL;
66
67 void silc_expandos_init(void);
68 void silc_expandos_deinit(void);
69
70 void silc_lag_init(void);
71 void silc_lag_deinit(void);
72
73 #ifdef SILC_PLUGIN
74 void silc_core_deinit(void);
75 #endif
76
77 static gboolean my_silc_scheduler(gpointer data)
78 {
79   SILC_LOG_DEBUG(("Timeout"));
80   silc_client_run_one(silc_client);
81   return FALSE;
82 }
83
84 static gboolean my_silc_scheduler_fd(GIOChannel *source,
85                                      GIOCondition condition,
86                                      gpointer data)
87 {
88   SILC_LOG_DEBUG(("I/O event, %d", SILC_PTR_TO_32(data)));
89   silc_client_run_one(silc_client);
90   return TRUE;
91 }
92
93 static void scheduler_notify_cb(SilcSchedule schedule,
94                                 SilcBool added, SilcTask task,
95                                 SilcBool fd_task, SilcUInt32 fd,
96                                 SilcTaskEvent event,
97                                 long seconds, long useconds,
98                                 void *context)
99 {
100   if (added) {
101     if (fd_task) {
102       /* Add fd */
103       GIOChannel *ch;
104       guint e = 0;
105
106       SILC_LOG_DEBUG(("Add fd %d, events %d", fd, event));
107       g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
108
109       if (event & SILC_TASK_READ)
110         e |= (G_IO_IN | G_IO_PRI | G_IO_HUP | G_IO_ERR);
111       if (event & SILC_TASK_WRITE)
112         e |= (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL);
113
114       if (e) {
115         ch = g_io_channel_unix_new(fd);
116         g_io_add_watch(ch, e, my_silc_scheduler_fd, SILC_32_TO_PTR(fd));
117         g_io_channel_unref(ch);
118       }
119     } else {
120       /* Add timeout */
121       guint t;
122
123       t = (seconds * 1000) + (useconds / 1000);
124       SILC_LOG_DEBUG(("interval %d msec", t));
125       g_timeout_add(t, my_silc_scheduler, NULL);
126     }
127   } else {
128     if (fd_task) {
129       /* Remove fd */
130       g_source_remove_by_user_data(SILC_32_TO_PTR(fd));
131     }
132   }
133 }
134
135 static CHATNET_REC *create_chatnet(void)
136 {
137   return g_malloc0(sizeof(CHATNET_REC));
138 }
139
140 static SERVER_SETUP_REC *create_server_setup(void)
141 {
142   return g_malloc0(sizeof(SERVER_SETUP_REC));
143 }
144
145 static CHANNEL_SETUP_REC *create_channel_setup(void)
146 {
147   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
148 }
149
150 static SERVER_CONNECT_REC *create_server_connect(void)
151 {
152   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
153 }
154
155 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
156 {
157
158 }
159
160 /* Checks user information and saves them to the config file it they
161    do not exist there already. */
162
163 static void silc_init_userinfo(void)
164 {
165   const char *set, *nick, *user_name, *str;
166   char *tmp;
167
168   /* check if nick/username/realname wasn't read from setup.. */
169   set = settings_get_str("real_name");
170   if (set == NULL || *set == '\0') {
171     str = g_getenv("SILCNAME");
172     if (!str)
173       str = g_getenv("IRCNAME");
174     settings_set_str("real_name",
175                      str != NULL ? str : silc_get_real_name());
176   }
177
178   /* Check that real name is UTF-8 encoded */
179   set = settings_get_str("real_name");
180   if (!silc_utf8_valid(set, strlen(set))) {
181     int len = silc_utf8_encoded_len(set, strlen(set), SILC_STRING_LOCALE);
182     tmp = silc_calloc(len, sizeof(*tmp));
183     if (tmp) {
184       silc_utf8_encode(set, strlen(set), SILC_STRING_LOCALE, tmp, len);
185       settings_set_str("real_name", tmp);
186       silc_free(tmp);
187     }
188   }
189
190   /* username */
191   user_name = settings_get_str("user_name");
192   if (user_name == NULL || *user_name == '\0') {
193     str = g_getenv("SILCUSER");
194     if (!str)
195       str = g_getenv("IRCUSER");
196     settings_set_str("user_name",
197                      str != NULL ? str : silc_get_username());
198
199     user_name = settings_get_str("user_name");
200   }
201
202   /* nick */
203   nick = settings_get_str("nick");
204   if (nick == NULL || *nick == '\0') {
205     str = g_getenv("SILCNICK");
206     if (!str)
207       str = g_getenv("IRCNICK");
208     settings_set_str("nick", str != NULL ? str : user_name);
209
210     nick = settings_get_str("nick");
211   }
212
213   /* alternate nick */
214   set = settings_get_str("alternate_nick");
215   if (set == NULL || *set == '\0') {
216     tmp = g_strconcat(nick, "_", NULL);
217     settings_set_str("alternate_nick", tmp);
218     g_free(tmp);
219   }
220
221   /* host name */
222   set = settings_get_str("hostname");
223   if (set == NULL || *set == '\0') {
224     str = g_getenv("SILCHOST");
225     if (!str)
226       str = g_getenv("IRCHOST");
227     if (str != NULL)
228       settings_set_str("hostname", str);
229   }
230 }
231
232 #if defined(SILC_DEBUG) || defined(SILC_PLUGIN)
233 static bool i_debug;
234 #endif
235
236 #ifdef SILC_DEBUG
237 static SilcBool silc_irssi_debug_print(char *file, char *function, int line,
238                                        char *message, void *context)
239 {
240   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP,
241             "DEBUG: %s:%d: %s", function, line, message);
242   return TRUE;
243 }
244 #endif
245
246 static void silc_sig_setup_changed(void)
247 {
248 #ifdef SILC_DEBUG
249   bool debug = settings_get_bool("debug");
250   if (debug) {
251     const char *debug_string = settings_get_str("debug_string");
252     i_debug = TRUE;
253     silc_log_debug(TRUE);
254     if (strlen(debug_string))
255       silc_log_set_debug_string(debug_string);
256     silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
257     return;
258   }
259   if (i_debug)
260     silc_log_debug(FALSE);
261 #endif
262 }
263
264 /* Log callbacks */
265
266 static SilcBool silc_log_misc(SilcLogType type, char *message, void *context)
267 {
268   printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s: %s",
269             (type == SILC_LOG_INFO ? "[Info]" :
270              type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
271   return TRUE;
272 }
273
274 static SilcBool silc_log_stderr(SilcLogType type, char *message, void *context)
275 {
276   fprintf(stderr, "%s: %s\n",
277           (type == SILC_LOG_INFO ? "[Info]" :
278            type == SILC_LOG_WARNING ? "[Warning]" : "[Error]"), message);
279   return TRUE;
280 }
281
282 static void silc_register_cipher(SilcClient client, const char *cipher)
283 {
284   int i;
285
286   if (cipher) {
287     for (i = 0; silc_default_ciphers[i].name; i++)
288       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
289         silc_cipher_register(&(silc_default_ciphers[i]));
290         break;
291       }
292
293     if (!silc_cipher_is_supported(cipher)) {
294       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
295 #ifdef SILC_PLUGIN
296       init_failed = -1;
297       return;
298 #else
299       exit(1);
300 #endif
301     }
302   }
303
304   /* Register other defaults */
305   silc_cipher_register_default();
306 }
307
308 static void silc_register_hash(SilcClient client, const char *hash)
309 {
310   int i;
311
312   if (hash) {
313     for (i = 0; silc_default_hash[i].name; i++)
314       if (!strcmp(silc_default_hash[i].name, hash)) {
315         silc_hash_register(&(silc_default_hash[i]));
316         break;
317       }
318
319     if (!silc_hash_is_supported(hash)) {
320       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
321 #ifdef SILC_PLUGIN
322       init_failed = -1;
323       return;
324 #else
325       exit(1);
326 #endif
327     }
328   }
329
330   /* Register other defaults */
331   silc_hash_register_default();
332 }
333
334 static void silc_register_hmac(SilcClient client, const char *hmac)
335 {
336   int i;
337
338   if (hmac) {
339     for (i = 0; silc_default_hmacs[i].name; i++)
340       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
341         silc_hmac_register(&(silc_default_hmacs[i]));
342         break;
343       }
344
345     if (!silc_hmac_is_supported(hmac)) {
346       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
347 #ifdef SILC_PLUGIN
348       init_failed = -1;
349       return;
350 #else
351       exit(1);
352 #endif
353     }
354   }
355
356   /* Register other defaults */
357   silc_hmac_register_default();
358 }
359
360 /* Finalize init. Init finish signal calls this. */
361
362 #ifdef SILC_PLUGIN
363 void silc_opt_callback(const char *data, SERVER_REC *server,
364                         WI_ITEM_REC *item)
365 {
366   unsigned char **argv=NULL, *tmp;
367   SilcUInt32 *argv_lens=NULL, *argv_types=NULL, argc=0;
368   int i;
369   unsigned char privkey[128], pubkey[128];
370
371   memset(privkey, 0, sizeof(privkey));
372   memset(pubkey, 0, sizeof(pubkey));
373   snprintf(pubkey, sizeof(pubkey) - 1, "%s/%s", get_irssi_dir(),
374            SILC_CLIENT_PUBLIC_KEY_NAME);
375   snprintf(privkey, sizeof(privkey) - 1, "%s/%s", get_irssi_dir(),
376            SILC_CLIENT_PRIVATE_KEY_NAME);
377
378   tmp = g_strconcat("SILC", " ", data, NULL);
379   silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 6);
380   g_free(tmp);
381
382   if (argc < 2)
383     goto err;
384
385   if ((argc == 2) && (strcasecmp(argv[1], "list-ciphers") == 0)) {
386     silc_client_list_ciphers();
387     goto out;
388   }
389
390   if ((argc == 2) && (strcasecmp(argv[1], "list-hash-funcs") == 0)) {
391     silc_client_list_hash_funcs();
392     goto out;
393   }
394
395   if ((argc == 2) && (strcasecmp(argv[1], "list-hmacs") == 0)) {
396     silc_client_list_hmacs();
397     goto out;
398   }
399
400   if ((argc == 2) && (strcasecmp(argv[1], "list-pkcs") == 0)) {
401     silc_client_list_pkcs();
402     goto out;
403   }
404
405   if ((argc < 5) && (strcasecmp(argv[1], "debug") == 0)) {
406     if (argc == 2) {
407       printformat_module("fe-common/silc", NULL, NULL,
408                          MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
409                          (i_debug == TRUE ? "enabled" : "disabled"));
410       goto out;
411     }
412 #ifndef SILC_DEBUG
413     printformat_module("fe-common/silc", NULL, NULL,
414                        MSGLEVEL_CRAP, SILCTXT_CONFIG_NODEBUG);
415 #else
416     if (strcasecmp(argv[2], "on") == 0) {
417       settings_set_bool("debug", TRUE);
418       if (argc == 4)
419          settings_set_str("debug_string", argv[3]);
420     } else if ((argc == 3) && (strcasecmp(argv[2], "off") == 0)) {
421       settings_set_bool("debug", FALSE);
422     } else
423       goto err;
424     silc_sig_setup_changed();
425     printformat_module("fe-common/silc", NULL, NULL,
426                        MSGLEVEL_CRAP, SILCTXT_CONFIG_DEBUG,
427                        (settings_get_bool("debug") == TRUE ?
428                         "enabled" : "disabled"));
429     goto out;
430 #endif
431   }
432
433   if (strcasecmp(argv[1], "create-key-pair") == 0) {
434     /* Create new key pair and exit */
435     char *endptr, *pkcs=NULL;
436     long int val;
437     int bits=0;
438     CREATE_KEY_REC *rec;
439
440     if ((argc == 3) || (argc == 5))
441       goto err;
442
443     for (i=2; i<argc-1; i+=2)
444       if (strcasecmp(argv[i], "-pkcs") == 0) {
445         if (pkcs == NULL)
446           pkcs = argv[i+1];
447         else
448           goto err;
449       } else if (strcasecmp(argv[i], "-bits") == 0) {
450         if (bits == 0) {
451           val = strtol(argv[i+1], &endptr, 10);
452           if ((*endptr != '\0') || (val <= 0) || (val >= INT_MAX))
453             goto err;
454           bits = val;
455         } else
456           goto err;
457       } else
458         goto err;
459
460     rec = g_new0(CREATE_KEY_REC, 1);
461     rec->pkcs = (pkcs == NULL ? NULL : g_strdup(pkcs));
462     rec->bits = bits;
463
464     keyboard_entry_redirect((SIGNAL_FUNC) create_key_passphrase,
465                             format_get_text("fe-common/silc", NULL, NULL,
466                                             NULL, SILCTXT_CONFIG_PASS_ASK2),
467                             ENTRY_REDIRECT_FLAG_HIDDEN, rec);
468     printformat_module("fe-common/silc", NULL, NULL,
469                        MSGLEVEL_CRAP, SILCTXT_CONFIG_NEXTTIME);
470     goto out;
471   }
472
473   if ((argc < 4) && (strcasecmp(argv[1], "passphrase-change") == 0)) {
474     /* Change the passphrase of the private key file */
475     CREATE_KEY_REC *rec;
476
477     rec = g_new0(CREATE_KEY_REC, 1);
478     rec->file = g_strdup((argc == 3 ? argv[2] : privkey));
479
480     keyboard_entry_redirect((SIGNAL_FUNC) change_private_key_passphrase,
481                             format_get_text("fe-common/silc", NULL, NULL,
482                                             NULL, SILCTXT_CONFIG_PASS_ASK1),
483                             ENTRY_REDIRECT_FLAG_HIDDEN, rec);
484     goto out;
485   }
486
487 err:
488   printformat_module("fe-common/silc", NULL, NULL,
489                      MSGLEVEL_CRAP, SILCTXT_CONFIG_UNKNOWN,
490                      data);
491
492 out:
493   for (i=0; i<argc; i++)
494     silc_free(argv[i]);
495
496   silc_free(argv);
497   silc_free(argv_lens);
498   silc_free(argv_types);
499 }
500 #endif /* SILC_PLUGIN */
501
502 /* Called to indicate the client library has stopped. */
503 static void
504 silc_stopped(SilcClient client, void *context)
505 {
506   SILC_LOG_DEBUG(("Client library has stopped"));
507   *(int*)context = -1;
508 }
509
510 /* Called to indicate the client library is running. */
511
512 static void
513 silc_running(SilcClient client, void *context)
514 {
515   running = 1;
516   SILC_LOG_DEBUG(("Client library is running"));
517 }
518
519 static void sig_init_finished(void)
520 {
521   /* Check ~/.silc directory and public and private keys */
522   if (!silc_client_check_silc_dir()) {
523 #ifdef SILC_PLUGIN
524     init_failed = -1;
525 #else
526     sleep(1);
527     exit(1);
528 #endif
529     return;
530   }
531
532   /* Load public and private key */
533   if (!silc_client_load_keys(silc_client)) {
534 #ifdef SILC_PLUGIN
535     init_failed = -1;
536 #else
537     sleep(1);
538     exit(1);
539 #endif
540     return;
541   }
542
543   /* Initialize the SILC client */
544   opt_hostname = (char *)settings_get_str("hostname");
545   if (!opt_hostname || *opt_hostname == '\0')
546     opt_hostname = silc_net_localhost();
547   if (!silc_client_init(silc_client, settings_get_str("user_name"),
548                         opt_hostname, settings_get_str("real_name"),
549                         silc_running, NULL)) {
550 #ifdef SILC_PLUGIN
551     init_failed = -1;
552 #else
553     sleep(1);
554     exit(1);
555 #endif
556     return;
557   }
558
559   silc_schedule_set_notify(silc_client->schedule, scheduler_notify_cb, NULL);
560
561   silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
562   silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
563   silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
564   silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
565
566   silc_hash_alloc("sha1", &sha1hash);
567
568   /* Run SILC scheduler */
569   my_silc_scheduler(NULL);
570 }
571
572 #ifndef SILC_PLUGIN
573 static gboolean list_ciphers(const gchar *option_name, const gchar *value,
574                              gpointer data, GError **error)
575 {
576   silc_cipher_register_default();
577   silc_client_list_ciphers();
578   exit(0);
579   return true;
580 }
581
582 static gboolean list_hash_funcs(const gchar *option_name, const gchar *value,
583                                 gpointer data, GError **error)
584 {
585   silc_hash_register_default();
586   silc_client_list_hash_funcs();
587   exit(0);
588   return true;
589 }
590
591 static gboolean list_hmacs(const gchar *option_name, const gchar *value,
592                            gpointer data, GError **error)
593 {
594   silc_hmac_register_default();
595   silc_client_list_hmacs();
596   exit(0);
597   return true;
598 }
599
600 static gboolean list_pkcs(const gchar *option_name, const gchar *value,
601                           gpointer data, GError **error)
602 {
603   silc_pkcs_register_default();
604   silc_client_list_pkcs();
605   exit(0);
606   return true;
607 }
608
609 #ifdef SILC_DEBUG
610 static gboolean enable_debug(const gchar *option_name, const gchar *value,
611                              gpointer data, GError **error)
612 {
613   /* Set debug string setting but don't enable it.  User must be
614      /set enable on to get the debugs. */
615   settings_set_str("debug_string", (char *)value);
616
617   /* Default debugging when -d was given in command line */
618   silc_log_debug(TRUE);
619   silc_log_debug_hexdump(TRUE);
620   silc_log_set_debug_string((char *)value);
621   return true;
622 }
623 #else
624 static gboolean enable_debug(const gchar *option_name, const gchar *value,
625                              gpointer data, GError **error)
626 {
627   fprintf(stdout,
628           "Run-time debugging is not enabled. To enable it recompile\n"
629           "the client with --enable-debug configuration option.\n");
630   sleep(1);
631   return true;
632 }
633 #endif /* SILC_DEBUG */
634
635 static gboolean create_keypair(const gchar *option_name, const gchar *value,
636                                gpointer data, GError **error)
637 {
638   silc_cipher_register_default();
639   silc_pkcs_register_default();
640   silc_hash_register_default();
641   silc_hmac_register_default();
642   silc_create_key_pair(opt_pkcs, opt_bits, NULL, NULL, NULL, NULL,
643                        NULL, NULL, TRUE);
644   exit(0);
645   return true;
646 }
647
648 static gboolean change_passphrase(const gchar *option_name, const gchar *value,
649                                   gpointer data, GError **error)
650 {
651   silc_cipher_register_default();
652   silc_pkcs_register_default();
653   silc_hash_register_default();
654   silc_hmac_register_default();
655   silc_change_private_key_passphrase((char *)value, NULL, NULL);
656   exit(0);
657   return true;
658 }
659
660 static gboolean show_key(const gchar *option_name, const gchar *value,
661                                   gpointer data, GError **error)
662 {
663   silc_cipher_register_default();
664   silc_pkcs_register_default();
665   silc_hash_register_default();
666   silc_hmac_register_default();
667   silc_show_public_key_file((char *)value);
668   exit(0);
669   return true;
670 }
671 #endif /* !SILC_PLUGIN */
672
673 /* Init SILC. Called from src/fe-text/silc.c */
674
675 void silc_core_init(void)
676 {
677 #ifndef SILC_PLUGIN
678   static GOptionEntry silc_options[] = {
679     { "list-ciphers", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
680       list_ciphers, "List supported ciphers", NULL },
681     { "list-hash-funcs", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
682       list_hash_funcs, "List supported hash functions", NULL },
683     { "list-hmacs", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, list_hmacs,
684       "List supported HMACs", NULL },
685     { "list-pkcs", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, list_pkcs,
686       "List supported PKCSs", NULL },
687     { "debug", 'd', 0, G_OPTION_ARG_CALLBACK, enable_debug,
688       "Enable debugging", NULL },
689     { "create-key-pair", 'C', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
690       create_keypair, "Create new public key pair", NULL },
691     { "pkcs", 0, 0, G_OPTION_ARG_STRING, &opt_pkcs,
692       "Set the PKCS of the public key pair (-C)", "PKCS" },
693     { "bits", 0, 0, G_OPTION_ARG_INT, &opt_bits,
694       "Set the length of the public key pair (-C)", "VALUE" },
695     { "passphrase-change", 'P', 0, G_OPTION_ARG_CALLBACK, change_passphrase,
696       "Change the passphrase of private key file", "FILE" },
697     { "show-key", 'S', 0, G_OPTION_ARG_CALLBACK, show_key, 
698       "Show the contents of the public key", "FILE" },
699     { NULL }
700   };
701 #endif
702
703   CHAT_PROTOCOL_REC *rec;
704   SilcClientParams params;
705   const char *def_cipher, *def_hash, *def_hmac;
706
707 #ifndef SILC_PLUGIN
708   args_register(silc_options);
709 #endif
710
711   /* Settings */
712 #ifndef SILC_PLUGIN
713   settings_add_bool("server", "skip_motd", FALSE);
714   settings_add_str("server", "alternate_nick", NULL);
715 #endif
716   settings_add_bool("server", "use_auto_addr", FALSE);
717   settings_add_str("server", "auto_bind_ip", "");
718   settings_add_str("server", "auto_public_ip", "");
719   settings_add_int("server", "auto_bind_port", 0);
720   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
721   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
722   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
723   settings_add_int("server", "key_exchange_timeout_secs", 120);
724   settings_add_int("server", "key_exchange_rekey_secs", 3600);
725   settings_add_bool("server", "key_exchange_rekey_pfs", FALSE);
726   settings_add_int("server", "heartbeat", 300);
727   settings_add_bool("server", "ignore_message_signatures", FALSE);
728   settings_add_str("server", "session_filename", "session.$chatnet");
729   settings_add_bool("server", "sign_channel_messages", FALSE);
730   settings_add_bool("server", "sign_private_messages", FALSE);
731   settings_add_str("silc", "nickname_format", "%n#%a");
732
733   /* Requested Attributes settings */
734   settings_add_bool("silc", "attr_allow", TRUE);
735   settings_add_str("silc", "attr_vcard", "");
736   settings_add_str("silc", "attr_services", "");
737   settings_add_str("silc", "attr_status_mood", "NORMAL");
738   settings_add_str("silc", "attr_status_text", "");
739   settings_add_str("silc", "attr_status_message", NULL);
740   settings_add_str("silc", "attr_preferred_language", "");
741   settings_add_str("silc", "attr_preferred_contact", "CHAT");
742   settings_add_bool("silc", "attr_timezone", TRUE);
743   settings_add_str("silc", "attr_geolocation", "");
744   settings_add_str("silc", "attr_device_info", NULL);
745   settings_add_str("silc", "attr_public_keys", "");
746
747 #ifdef SILC_DEBUG
748   settings_add_bool("debug", "debug", FALSE);
749   settings_add_str("debug", "debug_string", "");
750 #endif
751
752   signal_add("setup changed", (SIGNAL_FUNC) silc_sig_setup_changed);
753 #ifndef SILC_PLUGIN
754   signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
755 #endif /* !SILC_PLUGIN */
756
757 #if defined (SILC_PLUGIN) && defined (SILC_DEBUG)
758   if (settings_get_bool("debug") == TRUE)
759     silc_sig_setup_changed();
760 #endif
761
762   silc_init_userinfo();
763
764   silc_log_set_callback(SILC_LOG_INFO, silc_log_stderr, NULL);
765   silc_log_set_callback(SILC_LOG_WARNING, silc_log_stderr, NULL);
766   silc_log_set_callback(SILC_LOG_ERROR, silc_log_stderr, NULL);
767   silc_log_set_callback(SILC_LOG_FATAL, silc_log_stderr, NULL);
768
769   /* Initialize client parameters */
770   memset(&params, 0, sizeof(params));
771   strcat(params.nickname_format, settings_get_str("nickname_format"));
772   params.full_channel_names = TRUE;
773
774   /* Allocate SILC client */
775   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
776
777   /* Get the ciphers and stuff from config file */
778   def_cipher = settings_get_str("crypto_default_cipher");
779   def_hash = settings_get_str("crypto_default_hash");
780   def_hmac = settings_get_str("crypto_default_hmac");
781   silc_register_cipher(silc_client, def_cipher);
782 #ifdef SILC_PLUGIN
783   if (init_failed)
784     return;
785 #endif
786   silc_register_hash(silc_client, def_hash);
787 #ifdef SILC_PLUGIN
788   if (init_failed)
789     return;
790 #endif
791   silc_register_hmac(silc_client, def_hmac);
792 #ifdef SILC_PLUGIN
793   if (init_failed)
794     return;
795 #endif
796   silc_pkcs_register_default();
797
798 #ifdef SILC_PLUGIN
799   command_bind("silc", MODULE_NAME, (SIGNAL_FUNC) silc_opt_callback);
800 #endif
801
802   /* Register SILC to the irssi */
803   rec = g_new0(CHAT_PROTOCOL_REC, 1);
804   rec->name = "SILC";
805   rec->fullname = "Secure Internet Live Conferencing";
806   rec->chatnet = "silcnet";
807   rec->create_chatnet = create_chatnet;
808   rec->create_server_setup = create_server_setup;
809   rec->create_channel_setup = create_channel_setup;
810   rec->create_server_connect = create_server_connect;
811   rec->destroy_server_connect = destroy_server_connect;
812   rec->server_init_connect = silc_server_init_connect;
813   rec->server_connect = silc_server_connect;
814   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *,
815                                            const char *, int))
816     silc_channel_create;
817   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
818     silc_query_create;
819
820   chat_protocol_register(rec);
821   g_free(rec);
822
823   silc_queue_init();
824   silc_server_init();
825   silc_channels_init();
826   silc_queries_init();
827   silc_expandos_init();
828   silc_lag_init();
829   silc_chatnets_init();
830
831 #ifdef SILC_PLUGIN
832   sig_init_finished();
833   if (init_failed) {
834     silc_core_deinit();
835     return;
836   }
837 #endif
838
839   module_register("silc", "core");
840 }
841
842 /* Deinit SILC. Called from src/fe-text/silc.c */
843
844 void silc_core_deinit(void)
845 {
846   if (running) {
847     volatile int stopped = 0;
848     silc_client_stop(silc_client, silc_stopped, (void *)&stopped);
849     while (!stopped)
850       silc_client_run_one(silc_client);
851   }
852
853   signal_remove("setup changed", (SIGNAL_FUNC) silc_sig_setup_changed);
854 #ifdef SILC_PLUGIN
855   command_unbind("silc", (SIGNAL_FUNC) silc_opt_callback);
856 #else
857   signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
858 #endif
859
860   signal_emit("chat protocol deinit", 1, chat_protocol_find("SILC"));
861
862   silc_hash_free(sha1hash);
863
864   silc_queue_deinit();
865   silc_server_deinit();
866   silc_channels_deinit();
867   silc_queries_deinit();
868   silc_expandos_deinit();
869   silc_lag_deinit();
870   silc_chatnets_deinit();
871
872   chat_protocol_unregister("SILC");
873
874   if (irssi_pubkey)
875     silc_pkcs_public_key_free(irssi_pubkey);
876   if (irssi_privkey)
877     silc_pkcs_private_key_free(irssi_privkey);
878   silc_client_free(silc_client);
879 }