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