Merges from Irssi CVS.
[silc.git] / apps / irssi / src / silc / core / silc-core.c
1 /*
2
3   silc-core.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2001 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 "silcversion.h"
33
34 #include "signals.h"
35 #include "levels.h"
36 #include "settings.h"
37 #include "fe-common/core/printtext.h"
38 #include "fe-common/core/fe-channels.h"
39 #include "fe-common/core/keyboard.h"
40 #include "fe-common/silc/module-formats.h"
41
42 /* Command line option variables */
43 static bool opt_create_keypair = FALSE;
44 static char *opt_pkcs = NULL;
45 static int opt_bits = 0;
46
47 static int idletag;
48
49 SilcClient silc_client = NULL;
50 extern SilcClientOperations ops;
51 extern bool silc_debug;
52 extern bool silc_debug_hexdump;
53
54 void silc_expandos_init(void);
55 void silc_expandos_deinit(void);
56
57 static int my_silc_scheduler(void)
58 {
59   silc_client_run_one(silc_client);
60   return 1;
61 }
62
63 static CHATNET_REC *create_chatnet(void)
64 {
65   return g_malloc0(sizeof(CHATNET_REC));
66 }
67
68 static SERVER_SETUP_REC *create_server_setup(void)
69 {
70   return g_malloc0(sizeof(SERVER_SETUP_REC));
71 }
72
73 static CHANNEL_SETUP_REC *create_channel_setup(void)
74 {
75   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
76 }
77
78 static SERVER_CONNECT_REC *create_server_connect(void)
79 {
80   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
81 }
82
83 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
84 {
85
86 }
87
88 /* Checks user information and saves them to the config file it they
89    do not exist there already. */
90
91 static void silc_init_userinfo(void)
92 {
93   const char *set, *nick, *user_name;
94   char *str;   
95         
96   /* check if nick/username/realname wasn't read from setup.. */
97   set = settings_get_str("real_name");
98   if (set == NULL || *set == '\0') {
99     str = g_getenv("SILCNAME");
100     if (!str)
101       str = g_getenv("IRCNAME");
102     settings_set_str("real_name",
103                      str != NULL ? str : silc_get_real_name());
104   }
105  
106   /* username */
107   user_name = settings_get_str("user_name");
108   if (user_name == NULL || *user_name == '\0') {
109     str = g_getenv("SILCUSER");
110     if (!str)
111       str = g_getenv("IRCUSER");
112     settings_set_str("user_name",
113                      str != NULL ? str : silc_get_username());
114
115     user_name = settings_get_str("user_name");
116   }
117
118   /* nick */
119   nick = settings_get_str("nick");
120   if (nick == NULL || *nick == '\0') {
121     str = g_getenv("SILCNICK");
122     if (!str)
123       str = g_getenv("IRCNICK");
124     settings_set_str("nick", str != NULL ? str : user_name);
125     
126     nick = settings_get_str("nick");
127   }
128                 
129   /* alternate nick */
130   set = settings_get_str("alternate_nick");
131   if (set == NULL || *set == '\0') {
132     str = g_strconcat(nick, "_", NULL);
133     settings_set_str("alternate_nick", str);
134     g_free(str);
135   }
136
137   /* host name */
138   set = settings_get_str("hostname");
139   if (set == NULL || *set == '\0') {
140     str = g_getenv("SILCHOST");
141     if (!str)
142       str = g_getenv("IRCHOST");
143     if (str != NULL)
144       settings_set_str("hostname", str);
145   }
146 }
147
148 /* Log callbacks */
149
150 static bool silc_log_misc(SilcLogType type, char *message, void *context)
151 {
152   fprintf(stderr, "%s\n", message);
153   return TRUE;
154 }
155
156 static void silc_nickname_format_parse(const char *nickname,
157                                        char **ret_nickname)
158 {
159   silc_parse_userfqdn(nickname, ret_nickname, NULL);
160 }
161
162 static void silc_register_cipher(SilcClient client, const char *cipher)
163 {
164   int i;
165
166   if (cipher) {
167     for (i = 0; silc_default_ciphers[i].name; i++)
168       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
169         silc_cipher_register(&(silc_default_ciphers[i]));
170         break;
171       }
172     
173     if (!silc_cipher_is_supported(cipher)) {
174       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
175       exit(1);
176     }
177   }
178
179   /* Register other defaults */
180   silc_cipher_register_default();
181 }
182
183 static void silc_register_hash(SilcClient client, const char *hash)
184 {
185   int i;
186
187   if (hash) {
188     for (i = 0; silc_default_hash[i].name; i++)
189       if (!strcmp(silc_default_hash[i].name, hash)) {
190         silc_hash_register(&(silc_default_hash[i]));
191         break;
192       }
193     
194     if (!silc_hash_is_supported(hash)) {
195       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
196       exit(1);
197     }
198   }
199
200   /* Register other defaults */
201   silc_hash_register_default();
202 }
203
204 static void silc_register_hmac(SilcClient client, const char *hmac)
205 {
206   int i;
207
208   if (hmac) {
209     for (i = 0; silc_default_hmacs[i].name; i++)
210       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
211         silc_hmac_register(&(silc_default_hmacs[i]));
212         break;
213       }
214     
215     if (!silc_hmac_is_supported(hmac)) {
216       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
217       exit(1);
218     }
219   }
220
221   /* Register other defaults */
222   silc_hmac_register_default();
223 }
224
225 /* Finalize init. Init finish signal calls this. */
226
227 void silc_opt_callback(poptContext con, 
228                        enum poptCallbackReason reason,
229                        const struct poptOption *opt,
230                        const char *arg, void *data)
231 {
232   if (strcmp(opt->longName, "show-key") == 0) {
233     /* Dump the key */
234     silc_cipher_register_default();
235     silc_pkcs_register_default();
236     silc_hash_register_default();
237     silc_hmac_register_default();
238     silc_client_show_key((char *)arg);
239     exit(0);
240   }
241
242   if (strcmp(opt->longName, "list-ciphers") == 0) {
243     silc_cipher_register_default();
244     silc_client_list_ciphers();
245     exit(0);
246   }
247
248   if (strcmp(opt->longName, "list-hash-funcs") == 0) {
249     silc_hash_register_default();
250     silc_client_list_hash_funcs();
251     exit(0);
252   }
253
254   if (strcmp(opt->longName, "list-hmacs") == 0) {
255     silc_hmac_register_default();
256     silc_client_list_hmacs();
257     exit(0);
258   }
259
260   if (strcmp(opt->longName, "list-pkcs") == 0) {
261     silc_pkcs_register_default();
262     silc_client_list_pkcs();
263     exit(0);
264   }
265
266   if (strcmp(opt->longName, "debug") == 0) {
267     silc_debug = TRUE;
268     silc_debug_hexdump = TRUE;
269     silc_log_set_debug_string(arg);
270     silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
271     silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
272     silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
273     silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
274 #ifndef SILC_DEBUG
275     fprintf(stdout, 
276             "Run-time debugging is not enabled. To enable it recompile\n"
277             "the client with --enable-debug configuration option.\n");
278     sleep(1);
279 #endif
280   }
281 }
282
283 static void sig_init_read_settings(void)
284 {
285   if (opt_create_keypair) {
286     /* Create new key pair and exit */
287     silc_cipher_register_default();
288     silc_pkcs_register_default();
289     silc_hash_register_default();
290     silc_hmac_register_default();
291     silc_client_create_key_pair(opt_pkcs, opt_bits, 
292                                 NULL, NULL, NULL, NULL, NULL);
293     exit(0);
294   }
295 }
296
297 /* Init SILC. Called from src/fe-text/silc.c */
298
299 void silc_core_init(void)
300 {
301   static struct poptOption silc_options[] = {
302     { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
303     { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
304       "Show the contents of the public key", "FILE" },
305     { "list-ciphers", 'c', POPT_ARG_NONE, NULL, 0,
306       "List supported ciphers", NULL },
307     { "list-hash-funcs", 'H', POPT_ARG_NONE, NULL, 0,
308       "List supported hash functions", NULL },
309     { "list-hmacs", 'M', POPT_ARG_NONE, NULL, 0,
310       "List supported HMACs", NULL },
311     { "list-pkcs", 'P', POPT_ARG_NONE, NULL, 0,
312       "List supported PKCSs", NULL },
313     { "debug", 'd', POPT_ARG_STRING, NULL, 0,
314       "Enable debugging", "STRING" },
315     { NULL, '\0', 0, NULL }
316   };
317
318   static struct poptOption options[] = {
319     { NULL, '\0', POPT_ARG_INCLUDE_TABLE, silc_options, 0, NULL, NULL },
320     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0,
321       "Create new public key pair", NULL },
322     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
323       "Set the PKCS of the public key pair", "PKCS" },
324     { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
325       "Set the length of the public key pair", "VALUE" },
326     { NULL, '\0', 0, NULL }
327   };
328
329   CHAT_PROTOCOL_REC *rec;
330   SilcClientParams params;
331   const char *def_cipher, *def_hash, *def_hmac;
332
333   args_register(options);
334   signal_add("irssi init read settings", (SIGNAL_FUNC) sig_init_read_settings);
335
336   /* Settings */
337   settings_add_bool("server", "skip_motd", FALSE);
338   settings_add_str("server", "alternate_nick", NULL);
339   settings_add_bool("server", "use_auto_addr", FALSE);
340   settings_add_str("server", "auto_bind_ip", "");
341   settings_add_str("server", "auto_public_ip", "");
342   settings_add_int("server", "auto_bind_port", 0);
343   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
344   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
345   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
346   settings_add_int("server", "key_exchange_timeout_secs", 120);
347   settings_add_int("server", "key_exchange_rekey_secs", 3600);
348   settings_add_int("server", "connauth_request_secs", 2);
349
350   silc_init_userinfo();
351
352   /* Initialize client parameters */
353   memset(&params, 0, sizeof(params));
354   strcat(params.nickname_format, "%n@%h%a");
355   params.nickname_parse = silc_nickname_format_parse;
356   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
357   params.connauth_request_secs = settings_get_int("connauth_request_secs");
358
359   /* Allocate SILC client */
360   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
361
362   /* Get the ciphers and stuff from config file */
363   def_cipher = settings_get_str("crypto_default_cipher");
364   def_hash = settings_get_str("crypto_default_hash");
365   def_hmac = settings_get_str("crypto_default_hmac");
366   silc_register_cipher(silc_client, def_cipher);
367   silc_register_hash(silc_client, def_hash);
368   silc_register_hmac(silc_client, def_hmac);
369   silc_pkcs_register_default();
370
371   /* Get user information */
372   silc_client->username = g_strdup(settings_get_str("user_name"));
373   silc_client->nickname = g_strdup(settings_get_str("nick"));
374   silc_client->hostname = silc_net_localhost();
375   silc_client->realname = g_strdup(settings_get_str("real_name"));
376
377   /* Check ~/.silc directory and public and private keys */
378   if (silc_client_check_silc_dir() == FALSE) {
379     idletag = -1;
380     return;
381   }
382
383   /* Load public and private key */
384   if (silc_client_load_keys(silc_client) == FALSE) {
385     idletag = -1;
386     return;
387   }
388
389   /* Initialize the SILC client */
390   if (!silc_client_init(silc_client)) {
391     idletag = -1;
392     return;
393   }
394
395   /* Register SILC to the irssi */
396   rec = g_new0(CHAT_PROTOCOL_REC, 1);
397   rec->name = "SILC";
398   rec->fullname = "Secure Internet Live Conferencing";
399   rec->chatnet = "silcnet";
400   rec->create_chatnet = create_chatnet;
401   rec->create_server_setup = create_server_setup;
402   rec->create_channel_setup = create_channel_setup;
403   rec->create_server_connect = create_server_connect;
404   rec->destroy_server_connect = destroy_server_connect;
405   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
406     silc_server_connect; 
407   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
408     silc_channel_create;
409   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
410     silc_query_create;
411   
412   chat_protocol_register(rec);
413   g_free(rec);
414
415   silc_server_init();
416   silc_channels_init();
417   silc_queries_init();
418   silc_expandos_init();
419
420   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
421
422   module_register("silc", "core");
423 }
424
425 /* Deinit SILC. Called from src/fe-text/silc.c */
426
427 void silc_core_deinit(void)
428 {
429   if (idletag != -1) {
430     signal_emit("chat protocol deinit", 1,
431                 chat_protocol_find("SILC"));
432     signal_remove("irssi init read settings", 
433                   (SIGNAL_FUNC) sig_init_read_settings);
434     
435     silc_server_deinit();
436     silc_channels_deinit();
437     silc_queries_deinit();
438     silc_expandos_deinit();
439     
440     chat_protocol_unregister("SILC");
441     
442     g_source_remove(idletag);
443   }
444   
445   g_free(silc_client->username);
446   g_free(silc_client->realname);
447   silc_client_free(silc_client);
448 }