Added SILC Thread Queue API
[crypto.git] / apps / silcmap / silcmap.c
1 /*
2
3   silcmap.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silcincludes.h"
21 #include "silcclient.h"
22 #include "silcversion.h"
23 #include "silcmap.h"
24
25 /* Allocates new SilcMap context and the SilcClient in it. */
26
27 SilcMap silc_map_alloc(const char *conffile)
28 {
29   SilcMap map = silc_calloc(1, sizeof(*map));
30   if (!map)
31     return NULL;
32
33   /* Allocate client */
34   map->client = silc_client_alloc(&silc_map_client_ops, NULL, NULL, NULL);
35   if (!map->client) {
36     silc_free(map);
37     return NULL;
38   }
39
40   map->client->username = strdup("silcmap");
41   map->client->hostname = silc_net_localhost();
42   map->client->realname = strdup("silcmap");
43
44   /* Init the client */
45   if (!silc_client_init(map->client)) {
46     silc_client_free(map->client);
47     silc_free(map);
48     return NULL;
49   }
50
51   /* Load new key pair if it exists, create if it doesn't. */
52   if (!silc_load_key_pair("silcmap.pub", "silcmap.prv", "",
53                           &map->client->pkcs,
54                           &map->client->public_key,
55                           &map->client->private_key)) {
56     /* The keys don't exist.  Let's generate us a key pair then!  There's
57        nice ready routine for that too.  Let's do 1024 bit RSA key pair. */
58     if (!silc_create_key_pair("rsa", 1024, "silcmap.pub",
59                               "silcmap.prv", NULL, "",
60                               &map->client->pkcs,
61                               &map->client->public_key,
62                               &map->client->private_key, FALSE)) {
63       fprintf(stderr, "Could not create new key pair");
64       silc_client_free(map->client);
65       silc_free(map);
66       return NULL;
67     }
68   }
69
70   map->conffile = strdup(conffile);
71
72   return map;
73 }
74
75 /* Free the SilcMap context and all data in it. */
76
77 void silc_map_free(SilcMap map)
78 {
79   SilcMapConnection mapconn;
80   SilcMapCommand cmd;
81   char *h;
82   int i;
83
84   silc_free(map->conffile);
85   silc_free(map->bitmap);
86
87   if (map->client) {
88     silc_free(map->client->username);
89     silc_free(map->client->realname);
90     silc_free(map->client->hostname);
91     silc_client_free(map->client);
92   }
93
94   if (map->conns) {
95     silc_dlist_start(map->conns);
96     while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
97       silc_dlist_start(mapconn->hostnames);
98       while ((h = silc_dlist_get(mapconn->hostnames)) != SILC_LIST_END)
99         silc_free(h);
100       silc_dlist_uninit(mapconn->hostnames);
101
102       silc_dlist_start(mapconn->ips);
103       while ((h = silc_dlist_get(mapconn->ips)) != SILC_LIST_END)
104         silc_free(h);
105       silc_dlist_uninit(mapconn->ips);
106
107       silc_dlist_start(mapconn->commands);
108       while ((cmd = silc_dlist_get(mapconn->commands)) != SILC_LIST_END) {
109         silc_free(cmd->filename);
110         silc_free(cmd->text);
111         silc_free(cmd);
112       }
113       silc_dlist_uninit(mapconn->commands);
114
115       silc_free(mapconn->public_key);
116       silc_free(mapconn->country);
117       silc_free(mapconn->city);
118       silc_free(mapconn->admin);
119       silc_free(mapconn->description);
120       silc_free(mapconn->html_url);
121       silc_free(mapconn->up_color);
122       silc_free(mapconn->up_text_color);
123       silc_free(mapconn->down_color);
124       silc_free(mapconn->down_text_color);
125       silc_free(mapconn->data.motd);
126       memset(mapconn, 'F', sizeof(*mapconn));
127       silc_free(mapconn);
128     }
129     silc_dlist_uninit(map->conns);
130   }
131
132   for (i = 0; i < map->writemaphtml_count; i++) {
133     silc_free(map->writemaphtml[i].filename);
134     silc_free(map->writemaphtml[i].text);
135   }
136   silc_free(map->writemaphtml);
137
138   for (i = 0; i < map->cut_count; i++)
139     silc_free(map->cut[i].filename);
140   silc_free(map->cut);
141
142   silc_free(map->writemap.filename);
143   silc_free(map->writehtml.filename);
144   silc_free(map->writehtml.text);
145   silc_free(map->writerel.filename);
146   silc_free(map->writerel.text);
147
148   silc_free(map);
149 }
150
151 /* Starts the actual silcmap by parsing the commands script. */
152
153 SILC_TASK_CALLBACK(silc_map_start)
154 {
155   SilcMap map = context;
156
157   /* Load default font */
158   silc_map_load_font(map, "default.fnt");
159
160   /* Start command parsing.  Most of the commands are executed when they
161      are parsed so most of the real magic happens here. */
162   if (!silc_map_commands_parse(map, map->conffile)) {
163     /* Program stops */
164     silc_schedule_stop(map->client->schedule);
165   }
166 }
167
168 /* Long command line options */
169 static struct option long_opts[] =
170 {
171   { "config-file", 1, NULL, 'f' },
172   { "debug", 2, NULL, 'd' },
173   { "help", 0, NULL, 'h' },
174   { "version", 0, NULL,'V' },
175
176   { NULL, 0, NULL, 0 }
177 };
178
179 static void silc_map_usage(void)
180 {
181   printf(""
182 "Usage: silcmap [options]\n"
183 "\n"
184 "  Generic Options:\n"
185 "  -f  --config-file=FILE        Alternate SILC Map configuration file\n"
186 "  -d  --debug=string            Enable debugging\n"
187 "  -h  --help                    Display this message and exit\n"
188 "  -V  --version                 Display version and exit\n"
189 "\n");
190   exit(0);
191 }
192
193 int main(int argc, char **argv)
194 {
195   SilcMap map;
196   int opt, option_index;
197   char *filename = NULL;
198
199   if (argc > 1) {
200     while ((opt = getopt_long(argc, argv, "f:d:hV",
201                               long_opts, &option_index)) != EOF) {
202       switch(opt) {
203         case 'h':
204           silc_map_usage();
205           break;
206         case 'V':
207           printf("SILC Map, version %s\n", silc_dist_version);
208           printf("(c) 2003 Pekka Riikonen <priikone@silcnet.org>\n");
209           exit(0);
210           break;
211         case 'd':
212 #ifdef SILC_DEBUG
213           silc_debug = TRUE;
214           silc_debug_hexdump = TRUE;
215           if (optarg)
216             silc_log_set_debug_string(optarg);
217           silc_log_quick = TRUE;
218 #else
219           fprintf(stderr,
220                   "Run-time debugging is not enabled. To enable it recompile\n"
221                   "the server with --enable-debug configuration option.\n");
222 #endif
223           break;
224         case 'f':
225           filename = strdup(optarg);
226           break;
227         default:
228           silc_map_usage();
229           break;
230       }
231     }
232   }
233
234   /* Allocate map context */
235   if (!filename)
236     filename = strdup("silcmap.conf");
237   map = silc_map_alloc(filename);
238   if (!map)
239     return 1;
240
241   /* Schedule for command script parsing */
242   silc_schedule_task_add(map->client->schedule, 0,
243                          silc_map_start, map, 0, 1,
244                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
245
246   /* Run the silcmap client */
247   silc_client_run(map->client);
248
249   /* Cleanup */
250   silc_client_stop(map->client);
251   silc_map_free(map);
252   silc_free(filename);
253
254   return 0;
255 }