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