addition of silc.css
[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 "version_internal.h"
33 #include "version.h"
34
35 #include "signals.h"
36 #include "levels.h"
37 #include "settings.h"
38 #include "fe-common/core/printtext.h"
39 #include "fe-common/core/fe-channels.h"
40 #include "fe-common/core/keyboard.h"
41 #include "fe-common/silc/module-formats.h"
42
43 /* Command line option variables */
44 static bool opt_create_keypair = FALSE;
45 static bool opt_debug = FALSE;
46 static bool opt_list_ciphers = FALSE;
47 static bool opt_list_hash = FALSE;
48 static bool opt_list_hmac = FALSE;
49 static bool opt_list_pkcs = FALSE;
50 static bool opt_version = FALSE;
51 static char *opt_pkcs = NULL;
52 static char *opt_keyfile = NULL;
53 static int opt_bits = 0;
54
55 static int idletag;
56
57 SilcClient silc_client = NULL;
58 SilcClientConfig silc_config = NULL;
59 extern SilcClientOperations ops;
60 extern int silc_debug;
61 #ifdef SILC_SIM
62 /* SIM (SILC Module) table */
63 SilcSimContext **sims = NULL;
64 uint32 sims_count = 0;
65 #endif
66
67 static int my_silc_scheduler(void)
68 {
69   silc_schedule_one(silc_client->schedule, 0);
70   return 1;
71 }
72
73 static CHATNET_REC *create_chatnet(void)
74 {
75   return g_malloc0(sizeof(CHATNET_REC));
76 }
77
78 static SERVER_SETUP_REC *create_server_setup(void)
79 {
80   return g_malloc0(sizeof(SERVER_SETUP_REC));
81 }
82
83 static CHANNEL_SETUP_REC *create_channel_setup(void)
84 {
85   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
86 }
87
88 static SERVER_CONNECT_REC *create_server_connect(void)
89 {
90   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
91 }
92
93 /* Checks user information and saves them to the config file it they
94    do not exist there already. */
95
96 static void silc_init_userinfo(void)
97 {
98   const char *set, *nick, *user_name;
99   char *str;   
100         
101   /* check if nick/username/realname wasn't read from setup.. */
102   set = settings_get_str("real_name");
103   if (set == NULL || *set == '\0') {
104     str = g_getenv("SILCNAME");
105     if (!str)
106       str = g_getenv("IRCNAME");
107     settings_set_str("real_name",
108                      str != NULL ? str : g_get_real_name());
109   }
110  
111   /* username */
112   user_name = settings_get_str("user_name");
113   if (user_name == NULL || *user_name == '\0') {
114     str = g_getenv("SILCUSER");
115     if (!str)
116       str = g_getenv("IRCUSER");
117     settings_set_str("user_name",
118                      str != NULL ? str : g_get_user_name());
119     
120     user_name = settings_get_str("user_name");
121   }
122          
123   /* nick */
124   nick = settings_get_str("nick");
125   if (nick == NULL || *nick == '\0') {
126     str = g_getenv("SILCNICK");
127     if (!str)
128       str = g_getenv("IRCNICK");
129     settings_set_str("nick", str != NULL ? str : user_name);
130     
131     nick = settings_get_str("nick");
132   }
133                 
134   /* alternate nick */
135   set = settings_get_str("alternate_nick");
136   if (set == NULL || *set == '\0') {
137     if (strlen(nick) < 9)
138       str = g_strconcat(nick, "_", NULL);
139     else { 
140       str = g_strdup(nick);
141       str[strlen(str)-1] = '_';
142     }
143     settings_set_str("alternate_nick", str);
144     g_free(str);
145   }
146
147   /* host name */
148   set = settings_get_str("hostname");
149   if (set == NULL || *set == '\0') {
150     str = g_getenv("SILCHOST");
151     if (!str)
152       str = g_getenv("IRCHOST");
153     if (str != NULL)
154       settings_set_str("hostname", str);
155   }
156 }
157
158 /* Log callbacks */
159
160 static void silc_log_info(char *message)
161 {
162   fprintf(stderr, "%s\n", message);
163 }
164
165 static void silc_log_warning(char *message)
166 {
167   fprintf(stderr, "%s\n", message);
168 }
169
170 static void silc_log_error(char *message)
171 {
172   fprintf(stderr, "%s\n", message);
173 }
174
175 /* Init SILC. Called from src/fe-text/silc.c */
176
177 void silc_core_init(void)
178 {
179   static struct poptOption options[] = {
180     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0, 
181       "Create new public key pair", NULL },
182     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0, 
183       "Set the PKCS of the public key pair", "PKCS" },
184     { "bits", 0, POPT_ARG_INT, &opt_bits, 0, 
185       "Set the length of the public key pair", "VALUE" },
186     { "show-key", 'S', POPT_ARG_STRING, &opt_keyfile, 0, 
187       "Show the contents of the public key", "FILE" },
188     { "list-ciphers", 'C', POPT_ARG_NONE, &opt_list_ciphers, 0,
189       "List supported ciphers", NULL },
190     { "list-hash-funcs", 'H', POPT_ARG_NONE, &opt_list_hash, 0,
191       "List supported hash functions", NULL },
192     { "list-hmacs", 'H', POPT_ARG_NONE, &opt_list_hmac, 0,
193       "List supported HMACs", NULL },
194     { "list-pkcs", 'P', POPT_ARG_NONE, &opt_list_pkcs, 0,
195       "List supported PKCSs", NULL },
196     { "debug", 'd', POPT_ARG_NONE, &opt_debug, 0,
197       "Enable debugging", NULL },
198     { "version", 'V', POPT_ARG_NONE, &opt_version, 0,
199       "Show version", NULL },
200     { NULL, '\0', 0, NULL }
201   };
202
203   args_register(options);
204 }
205
206 static void silc_nickname_format_parse(const char *nickname,
207                                        char **ret_nickname)
208 {
209   silc_parse_userfqdn(nickname, ret_nickname, NULL);
210 }
211
212 /* Finalize init. Called from src/fe-text/silc.c */
213
214 void silc_core_init_finish(void)
215 {
216   CHAT_PROTOCOL_REC *rec;
217   SilcClientParams params;
218
219   if (opt_create_keypair == TRUE) {
220     /* Create new key pair and exit */
221     silc_cipher_register_default();
222     silc_pkcs_register_default();
223     silc_hash_register_default();
224     silc_hmac_register_default();
225     silc_client_create_key_pair(opt_pkcs, opt_bits, 
226                                 NULL, NULL, NULL, NULL, NULL);
227     exit(0);
228   }
229
230   if (opt_keyfile) {
231     /* Dump the key */
232     silc_cipher_register_default();
233     silc_pkcs_register_default();
234     silc_hash_register_default();
235     silc_hmac_register_default();
236     silc_client_show_key(opt_keyfile);
237     exit(0);
238   }
239
240   if (opt_list_ciphers) {
241     silc_cipher_register_default();
242     silc_client_list_ciphers();
243     exit(0);
244   }
245
246   if (opt_list_hash) {
247     silc_hash_register_default();
248     silc_client_list_hash_funcs();
249     exit(0);
250   }
251
252   if (opt_list_hmac) {
253     silc_hmac_register_default();
254     silc_client_list_hmacs();
255     exit(0);
256   }
257
258   if (opt_list_pkcs) {
259     silc_pkcs_register_default();
260     silc_client_list_pkcs();
261     exit(0);
262   }
263
264   if (opt_version) {
265     printf("SILC Secure Internet Live Conferencing, version %s "
266            "(base: SILC Toolkit %s)\n", silc_dist_version, silc_version);
267     printf("(c) 1997 - 2001 Pekka Riikonen <priikone@silcnet.org>\n");
268     exit(0); 
269   }
270
271   silc_debug = opt_debug;
272   silc_log_set_callbacks(silc_log_info, silc_log_warning,
273                          silc_log_error, NULL);
274
275   /* Do some irssi initializing */
276   settings_add_bool("server", "skip_motd", FALSE);
277   settings_add_str("server", "alternate_nick", NULL);
278   
279   /* Initialize the auto_addr variables Is "server" the best choice for
280    * this?  No existing category seems to apply.
281    */
282   
283   settings_add_bool("server", "use_auto_addr", FALSE);
284   settings_add_str("server", "auto_bind_ip", "");
285   settings_add_str("server", "auto_public_ip", "");
286   settings_add_int("server", "auto_bind_port", 0);
287                                 
288   silc_init_userinfo();
289
290   /* Initialize client parameters */
291   memset(&params, 0, sizeof(params));
292   strcat(params.nickname_format, "%n@%h%a");
293   params.nickname_parse = silc_nickname_format_parse;
294
295   /* Allocate SILC client */
296   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
297
298   /* Load local config file */
299   silc_config = silc_client_config_alloc(SILC_CLIENT_HOME_CONFIG_FILE);
300
301   /* Get user information */
302   silc_client->username = g_strdup(settings_get_str("user_name"));
303   silc_client->hostname = silc_net_localhost();
304   silc_client->realname = g_strdup(settings_get_str("real_name"));
305
306   /* Register all configured ciphers, PKCS and hash functions. */
307   if (silc_config) {
308     silc_config->client = silc_client;
309     if (!silc_client_config_register_ciphers(silc_config))
310       silc_cipher_register_default();
311     if (!silc_client_config_register_pkcs(silc_config))
312       silc_pkcs_register_default();
313     if (!silc_client_config_register_hashfuncs(silc_config))
314       silc_hash_register_default();
315     if (!silc_client_config_register_hmacs(silc_config))
316       silc_hmac_register_default();
317   } else {
318     /* Register default ciphers, pkcs, hash funtions and hmacs. */
319     silc_cipher_register_default();
320     silc_pkcs_register_default();
321     silc_hash_register_default();
322     silc_hmac_register_default();
323   }
324
325   /* Check ~/.silc directory and public and private keys */
326   if (silc_client_check_silc_dir() == FALSE) {
327     idletag = -1;
328     return;
329   }
330
331   /* Load public and private key */
332   if (silc_client_load_keys(silc_client) == FALSE) {
333     idletag = -1;
334     return;
335   }
336
337   /* Initialize the SILC client */
338   if (!silc_client_init(silc_client)) {
339     idletag = -1;
340     return;
341   }
342
343   /* Register SILC to the irssi */
344   rec = g_new0(CHAT_PROTOCOL_REC, 1);
345   rec->name = "SILC";
346   rec->fullname = "Secure Internet Live Conferencing";
347   rec->chatnet = "silcnet";
348   rec->create_chatnet = create_chatnet;
349   rec->create_server_setup = create_server_setup;
350   rec->create_channel_setup = create_channel_setup;
351   rec->create_server_connect = create_server_connect;
352   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
353     silc_server_connect; 
354   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
355     silc_channel_create;
356   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
357     silc_query_create;
358   
359   chat_protocol_register(rec);
360   g_free(rec);
361
362   silc_server_init();
363   silc_channels_init();
364   silc_queries_init();
365
366   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
367 }
368
369 /* Deinit SILC. Called from src/fe-text/silc.c */
370
371 void silc_core_deinit(void)
372 {
373   if (idletag != -1) {
374     signal_emit("chat protocol deinit", 1,
375                 chat_protocol_find("SILC"));
376     
377     silc_server_deinit();
378     silc_channels_deinit();
379     silc_queries_deinit();
380     
381     chat_protocol_unregister("SILC");
382     
383     g_source_remove(idletag);
384   }
385   
386   g_free(silc_client->username);
387   g_free(silc_client->realname);
388   silc_client_free(silc_client);
389 }