5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 Pekka Riikonen
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.
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.
20 #include "silcincludes.h"
21 #include "silcclient.h"
22 #include "silcversion.h"
25 /* Allocates new SilcMap context and the SilcClient in it. */
27 SilcMap silc_map_alloc(const char *conffile)
29 SilcMap map = silc_calloc(1, sizeof(*map));
34 map->client = silc_client_alloc(&silc_map_client_ops, NULL, NULL, NULL);
40 map->client->username = strdup("silcmap");
41 map->client->hostname = silc_net_localhost();
42 map->client->realname = strdup("silcmap");
45 if (!silc_client_init(map->client)) {
46 silc_client_free(map->client);
51 /* Load new key pair if it exists, create if it doesn't. */
52 if (!silc_load_key_pair("silcmap.pub", "silcmap.prv", "",
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, "",
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);
70 map->conffile = strdup(conffile);
75 /* Free the SilcMap context and all data in it. */
77 void silc_map_free(SilcMap map)
79 SilcMapConnection mapconn;
83 silc_free(map->conffile);
84 silc_free(map->bitmap);
87 silc_free(map->client->username);
88 silc_free(map->client->realname);
89 silc_free(map->client->hostname);
90 silc_client_free(map->client);
94 silc_dlist_start(map->conns);
95 while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
96 silc_dlist_start(mapconn->hostnames);
97 while ((h = silc_dlist_get(mapconn->hostnames)) != SILC_LIST_END)
99 silc_dlist_uninit(mapconn->hostnames);
101 silc_dlist_start(mapconn->ips);
102 while ((h = silc_dlist_get(mapconn->ips)) != SILC_LIST_END)
104 silc_dlist_uninit(mapconn->ips);
106 silc_dlist_start(mapconn->commands);
107 while ((cmd = silc_dlist_get(mapconn->commands)) != SILC_LIST_END) {
108 silc_free(cmd->filename);
109 silc_free(cmd->text);
112 silc_dlist_uninit(mapconn->commands);
114 silc_free(mapconn->public_key);
115 silc_free(mapconn->country);
116 silc_free(mapconn->city);
117 silc_free(mapconn->admin);
118 silc_free(mapconn->description);
119 silc_free(mapconn->writemaphtml_url);
120 silc_free(mapconn->up_color);
121 silc_free(mapconn->up_text_color);
122 silc_free(mapconn->down_color);
123 silc_free(mapconn->down_text_color);
124 silc_free(mapconn->data.motd);
127 silc_dlist_uninit(map->conns);
130 silc_free(map->writemap.filename);
131 silc_free(map->writehtml.filename);
132 silc_free(map->writehtml.text);
133 silc_free(map->writemaphtml.filename);
134 silc_free(map->writemaphtml.text);
135 silc_free(map->cut.filename);
140 /* Starts the actual silcmap by parsing the commands script. */
142 SILC_TASK_CALLBACK(silc_map_start)
144 SilcMap map = context;
146 /* Load default font */
147 silc_map_load_font(map, "default.fnt");
149 /* Start command parsing. Most of the commands are executed when they
150 are parsed so most of the real magic happens here. */
151 if (!silc_map_commands_parse(map, map->conffile)) {
153 silc_schedule_stop(map->client->schedule);
157 /* Long command line options */
158 static struct option long_opts[] =
160 { "config-file", 1, NULL, 'f' },
161 { "debug", 2, NULL, 'd' },
162 { "help", 0, NULL, 'h' },
163 { "version", 0, NULL,'V' },
168 static void silc_map_usage(void)
171 "Usage: silcmap [options]\n"
173 " Generic Options:\n"
174 " -f --config-file=FILE Alternate SILC Map configuration file\n"
175 " -d --debug=string Enable debugging\n"
176 " -h --help Display this message and exit\n"
177 " -V --version Display version and exit\n"
182 int main(int argc, char **argv)
185 int opt, option_index;
186 char *filename = NULL;
189 while ((opt = getopt_long(argc, argv, "f:d:hV",
190 long_opts, &option_index)) != EOF) {
196 printf("SILC Map, version %s\n", silc_dist_version);
197 printf("(c) 2003 Pekka Riikonen <priikone@silcnet.org>\n");
203 silc_debug_hexdump = TRUE;
205 silc_log_set_debug_string(optarg);
206 silc_log_quick = TRUE;
209 "Run-time debugging is not enabled. To enable it recompile\n"
210 "the server with --enable-debug configuration option.\n");
214 filename = strdup(optarg);
223 /* Allocate map context */
225 filename = strdup("silcmap.conf");
226 map = silc_map_alloc(filename);
230 /* Schedule for command script parsing */
231 silc_schedule_task_add(map->client->schedule, 0,
232 silc_map_start, map, 0, 1,
233 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
235 /* Run the silcmap client */
236 silc_client_run(map->client);
239 silc_client_stop(map->client);