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