Addes writerel command.
[silc.git] / apps / silcmap / silcmap_command.c
1 /*
2
3   silcmap_command.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 "silcmap.h"
23
24 /******* Command Script Parsing **********************************************/
25
26 SILC_CONFIG_CALLBACK(silc_map_cmd_server);
27 SILC_CONFIG_CALLBACK(silc_map_cmd_loadmap);
28 SILC_CONFIG_CALLBACK(silc_map_cmd_writemap);
29 SILC_CONFIG_CALLBACK(silc_map_cmd_writemaphtml);
30 SILC_CONFIG_CALLBACK(silc_map_cmd_writehtml);
31 SILC_CONFIG_CALLBACK(silc_map_cmd_writerel);
32 SILC_CONFIG_CALLBACK(silc_map_cmd_cut);
33 SILC_CONFIG_CALLBACK(silc_map_cmd_rectangle);
34 SILC_CONFIG_CALLBACK(silc_map_cmd_circle);
35 SILC_CONFIG_CALLBACK(silc_map_cmd_line);
36 SILC_CONFIG_CALLBACK(silc_map_cmd_text);
37
38 static const SilcConfigTable silc_map_table_loadmap[] =
39 {
40   { "filename", SILC_CONFIG_ARG_STR, silc_map_cmd_loadmap, NULL },
41   { NULL },
42 };
43
44 static const SilcConfigTable silc_map_table_writemap[] =
45 {
46   { "filename", SILC_CONFIG_ARG_STR, silc_map_cmd_writemap, NULL },
47   { NULL },
48 };
49
50 static const SilcConfigTable silc_map_table_writemaphtml[] =
51 {
52   { "filename", SILC_CONFIG_ARG_STR, silc_map_cmd_writemaphtml, NULL },
53   { "image", SILC_CONFIG_ARG_STR, silc_map_cmd_writemaphtml, NULL },
54   { "cut_lat", SILC_CONFIG_ARG_STRE, silc_map_cmd_writemaphtml, NULL },
55   { "cut_lon", SILC_CONFIG_ARG_STRE, silc_map_cmd_writemaphtml, NULL },
56   { NULL },
57 };
58
59 static const SilcConfigTable silc_map_table_writehtml[] =
60 {
61   { "filename", SILC_CONFIG_ARG_STR, silc_map_cmd_writehtml, NULL },
62   { "class", SILC_CONFIG_ARG_STRE, silc_map_cmd_writehtml, NULL },
63   { NULL },
64 };
65
66 static const SilcConfigTable silc_map_table_writerel[] =
67 {
68   { "filename", SILC_CONFIG_ARG_STR, silc_map_cmd_writerel, NULL },
69   { "class", SILC_CONFIG_ARG_STRE, silc_map_cmd_writerel, NULL },
70   { NULL },
71 };
72
73 static const SilcConfigTable silc_map_table_cut[] =
74 {
75   { "lat", SILC_CONFIG_ARG_STR, silc_map_cmd_cut, NULL },
76   { "lon", SILC_CONFIG_ARG_STR, silc_map_cmd_cut, NULL },
77   { "width", SILC_CONFIG_ARG_INT, silc_map_cmd_cut, NULL },
78   { "height", SILC_CONFIG_ARG_INT, silc_map_cmd_cut, NULL },
79   { "filename", SILC_CONFIG_ARG_STR, silc_map_cmd_cut, NULL },
80   { NULL },
81 };
82
83 static const SilcConfigTable silc_map_table_rectangle[] =
84 {
85   { "lat", SILC_CONFIG_ARG_STR, silc_map_cmd_rectangle, NULL },
86   { "lon", SILC_CONFIG_ARG_STR, silc_map_cmd_rectangle, NULL },
87   { "color", SILC_CONFIG_ARG_STR, silc_map_cmd_rectangle, NULL },
88   { "label", SILC_CONFIG_ARG_STR, silc_map_cmd_rectangle, NULL },
89   { "lposx", SILC_CONFIG_ARG_INT, silc_map_cmd_rectangle, NULL },
90   { "lposy", SILC_CONFIG_ARG_INT, silc_map_cmd_rectangle, NULL },
91   { "lcolor", SILC_CONFIG_ARG_STR, silc_map_cmd_rectangle, NULL },
92   { NULL },
93 };
94
95 static const SilcConfigTable silc_map_table_circle[] =
96 {
97   { "lat", SILC_CONFIG_ARG_STR, silc_map_cmd_circle, NULL },
98   { "lon", SILC_CONFIG_ARG_STR, silc_map_cmd_circle, NULL },
99   { "color", SILC_CONFIG_ARG_STR, silc_map_cmd_circle, NULL },
100   { "label", SILC_CONFIG_ARG_STR, silc_map_cmd_circle, NULL },
101   { "lposx", SILC_CONFIG_ARG_INT, silc_map_cmd_circle, NULL },
102   { "lposy", SILC_CONFIG_ARG_INT, silc_map_cmd_circle, NULL },
103   { "lcolor", SILC_CONFIG_ARG_STR, silc_map_cmd_circle, NULL },
104   { NULL },
105 };
106
107 static const SilcConfigTable silc_map_table_line[] =
108 {
109   { "a_lat", SILC_CONFIG_ARG_STR, silc_map_cmd_line, NULL },
110   { "a_lon", SILC_CONFIG_ARG_STR, silc_map_cmd_line, NULL },
111   { "b_lat", SILC_CONFIG_ARG_STR, silc_map_cmd_line, NULL },
112   { "b_lon", SILC_CONFIG_ARG_STR, silc_map_cmd_line, NULL },
113   { "width", SILC_CONFIG_ARG_STR, silc_map_cmd_line, NULL },
114   { "color", SILC_CONFIG_ARG_STR, silc_map_cmd_line, NULL },
115   { NULL },
116 };
117
118 static const SilcConfigTable silc_map_table_text[] =
119 {
120   { "lat", SILC_CONFIG_ARG_STR, silc_map_cmd_text, NULL },
121   { "lon", SILC_CONFIG_ARG_STR, silc_map_cmd_text, NULL },
122   { "color", SILC_CONFIG_ARG_STR, silc_map_cmd_text, NULL },
123   { "text", SILC_CONFIG_ARG_STR, silc_map_cmd_text, NULL },
124   { NULL },
125 };
126
127 static const SilcConfigTable silc_map_table_server[] =
128 {
129   /* Details */
130   { "hostname", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
131   { "ip", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
132   { "port", SILC_CONFIG_ARG_INT, silc_map_cmd_server, NULL },
133   { "public_key", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
134   { "country", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
135   { "city", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
136   { "admin", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
137   { "description", SILC_CONFIG_ARG_STRE, silc_map_cmd_server, NULL },
138   { "html_url", SILC_CONFIG_ARG_STRE, silc_map_cmd_server, NULL },
139
140   /* Connect params */
141   { "connect", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
142   { "connect_timeout", SILC_CONFIG_ARG_INT, silc_map_cmd_server, NULL },
143
144   /* Statistics */
145   { "starttime", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
146   { "uptime", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
147   { "clients", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
148   { "channels", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
149   { "server_ops", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
150   { "router_ops", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
151   { "cell_clients", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
152   { "cell_channels", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
153   { "cell_servers", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
154   { "all_clients", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
155   { "all_channels", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
156   { "all_servers", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
157   { "all_routers", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
158   { "all_server_ops", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
159   { "all_router_ops", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
160   { "motd", SILC_CONFIG_ARG_TOGGLE, silc_map_cmd_server, NULL },
161
162   /* Colors */
163   { "up_color", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
164   { "down_color", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
165   { "up_text_color", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
166   { "down_text_color", SILC_CONFIG_ARG_STR, silc_map_cmd_server, NULL },
167
168   /* Map commands */
169   { "cut", SILC_CONFIG_ARG_BLOCK,
170     silc_map_cmd_cut, silc_map_table_cut },
171   { "rectangle", SILC_CONFIG_ARG_BLOCK,
172     silc_map_cmd_rectangle, silc_map_table_rectangle },
173   { "circle", SILC_CONFIG_ARG_BLOCK,
174     silc_map_cmd_circle, silc_map_table_circle },
175   { "line", SILC_CONFIG_ARG_BLOCK,
176     silc_map_cmd_line, silc_map_table_line },
177   { "text", SILC_CONFIG_ARG_BLOCK,
178     silc_map_cmd_text, silc_map_table_text },
179   { NULL },
180 };
181
182 static const SilcConfigTable silc_map_table_main[] =
183 {
184   { "server", SILC_CONFIG_ARG_BLOCK,
185     silc_map_cmd_server, silc_map_table_server },
186   { "loadmap", SILC_CONFIG_ARG_BLOCK,
187     silc_map_cmd_loadmap, silc_map_table_loadmap },
188   { "writemap", SILC_CONFIG_ARG_BLOCK,
189     silc_map_cmd_writemap, silc_map_table_writemap },
190   { "writemaphtml", SILC_CONFIG_ARG_BLOCK,
191     silc_map_cmd_writemaphtml, silc_map_table_writemaphtml },
192   { "writehtml", SILC_CONFIG_ARG_BLOCK,
193     silc_map_cmd_writehtml, silc_map_table_writehtml },
194   { "writerel", SILC_CONFIG_ARG_BLOCK,
195     silc_map_cmd_writerel, silc_map_table_writerel },
196   { "cut", SILC_CONFIG_ARG_BLOCK,
197     silc_map_cmd_cut, silc_map_table_cut },
198   { "rectangle", SILC_CONFIG_ARG_BLOCK,
199     silc_map_cmd_rectangle, silc_map_table_rectangle },
200   { "circle", SILC_CONFIG_ARG_BLOCK,
201     silc_map_cmd_circle, silc_map_table_circle },
202   { "line", SILC_CONFIG_ARG_BLOCK,
203     silc_map_cmd_line, silc_map_table_line },
204   { "text", SILC_CONFIG_ARG_BLOCK,
205     silc_map_cmd_text, silc_map_table_text },
206   { NULL },
207 };
208
209 /* Command datas.  Used when command is outside server { } section. */
210 static char *filename = NULL;
211 static char *lat = NULL;
212 static char *lon = NULL;
213 static char *lat2 = NULL;
214 static char *lon2 = NULL;
215 static SilcUInt32 width = 0;
216 static SilcUInt32 height = 0;
217 static SilcInt16 r = 0;
218 static SilcInt16 g = 0;
219 static SilcInt16 b = 0;
220 static SilcInt16 lr = -1;
221 static SilcInt16 lg = -1;
222 static SilcInt16 lb = -1;
223 static char *text = NULL;
224 static SilcInt32 lposx = 0;
225 static SilcInt32 lposy = 0;
226 static bool color_set = FALSE;
227 static bool lcolor_set = FALSE;
228
229 /* Current server section. */
230 SilcMapConnection curr_conn = NULL;
231
232 /* Command: server, performs the connection to the remote server and
233    gathers statistical information. */
234
235 SILC_CONFIG_CALLBACK(silc_map_cmd_server)
236 {
237   SilcMap map = context;
238   int retval = SILC_CONFIG_OK;
239
240   if (!map->bitmap) {
241     fprintf(stderr, "You must call loadmap command before server command\n");
242     return SILC_CONFIG_ESILENT;
243   }
244
245   if (type == SILC_CONFIG_ARG_BLOCK) {
246     SILC_LOG_DEBUG(("Server config parsed"));
247
248     if (!curr_conn->ips) {
249       fprintf(stderr, "IP must be configured\n");
250       return SILC_CONFIG_EMISSFIELDS;
251     }
252     if (!curr_conn->hostnames) {
253       fprintf(stderr, "Hostname must be configured\n");
254       return SILC_CONFIG_EMISSFIELDS;
255     }
256     if (!curr_conn->port) {
257       fprintf(stderr, "Port must be configured\n");
258       return SILC_CONFIG_EMISSFIELDS;
259     }
260
261     /* The server data is now gathered.  We continue by creating the
262        connection to the server and executing the requested commands. */
263     silc_map_connect(map, curr_conn);
264     map->conns_num++;
265
266     /* Cleanup */
267     curr_conn = NULL;
268
269     return retval;
270   }
271
272   /* Mark the current parsed server */
273   if (!curr_conn) {
274     curr_conn = silc_calloc(1, sizeof(*curr_conn));
275     if (!curr_conn)
276       return SILC_CONFIG_ESILENT;
277
278     curr_conn->hostnames = silc_dlist_init();
279     curr_conn->ips = silc_dlist_init();
280     curr_conn->commands = silc_dlist_init();
281     curr_conn->map = map;
282     if (!map->conns)
283       map->conns = silc_dlist_init();
284     silc_dlist_add(map->conns, curr_conn);
285   }
286
287   if (!strcmp(name, "hostname")) {
288     silc_dlist_add(curr_conn->hostnames, strdup((char *)val));
289   } else if (!strcmp(name, "ip")) {
290     silc_dlist_add(curr_conn->ips, strdup((char *)val));
291   } else if (!strcmp(name, "port")) {
292     curr_conn->port = (SilcUInt32)*(int *)val;
293   } else if (!strcmp(name, "public_key")) {
294     curr_conn->public_key = strdup((char *)val);
295   } else if (!strcmp(name, "country")) {
296     curr_conn->country = strdup((char *)val);
297   } else if (!strcmp(name, "city")) {
298     curr_conn->city = strdup((char *)val);
299   } else if (!strcmp(name, "admin")) {
300     curr_conn->admin = strdup((char *)val);
301   } else if (!strcmp(name, "description")) {
302     curr_conn->description = strdup((char *)val);
303   } else if (!strcmp(name, "html_url")) {
304     curr_conn->html_url = strdup((char *)val);
305   } else if (!strcmp(name, "connect")) {
306     curr_conn->connect = (bool)*(int *)val;
307   } else if (!strcmp(name, "connect_timeout")) {
308     curr_conn->connect_timeout = (SilcUInt32)*(int *)val;
309   } else if (!strcmp(name, "starttime")) {
310     curr_conn->starttime = (bool)*(int *)val;
311   } else if (!strcmp(name, "uptime")) {
312     curr_conn->uptime = (bool)*(int *)val;
313   } else if (!strcmp(name, "clients")) {
314     curr_conn->clients = (bool)*(int *)val;
315   } else if (!strcmp(name, "channels")) {
316     curr_conn->channels = (bool)*(int *)val;
317   } else if (!strcmp(name, "server_ops")) {
318     curr_conn->server_ops = (bool)*(int *)val;
319   } else if (!strcmp(name, "router_ops")) {
320     curr_conn->router_ops = (bool)*(int *)val;
321   } else if (!strcmp(name, "cell_clients")) {
322     curr_conn->cell_clients = (bool)*(int *)val;
323   } else if (!strcmp(name, "cell_channels")) {
324     curr_conn->cell_channels = (bool)*(int *)val;
325   } else if (!strcmp(name, "cell_servers")) {
326     curr_conn->cell_servers = (bool)*(int *)val;
327   } else if (!strcmp(name, "all_clients")) {
328     curr_conn->all_clients = (bool)*(int *)val;
329   } else if (!strcmp(name, "all_channels")) {
330     curr_conn->all_channels = (bool)*(int *)val;
331   } else if (!strcmp(name, "all_servers")) {
332     curr_conn->all_servers = (bool)*(int *)val;
333   } else if (!strcmp(name, "all_routers")) {
334     curr_conn->all_routers = (bool)*(int *)val;
335   } else if (!strcmp(name, "all_server_ops")) {
336     curr_conn->all_server_ops = (bool)*(int *)val;
337   } else if (!strcmp(name, "all_router_ops")) {
338     curr_conn->all_router_ops = (bool)*(int *)val;
339   } else if (!strcmp(name, "motd")) {
340     curr_conn->motd = (bool)*(int *)val;
341   } else if (!strcmp(name, "up_color")) {
342     curr_conn->up_color = strdup((char *)val);
343   } else if (!strcmp(name, "down_color")) {
344     curr_conn->down_color = strdup((char *)val);
345   } else if (!strcmp(name, "up_text_color")) {
346     curr_conn->up_text_color = strdup((char *)val);
347   } else if (!strcmp(name, "down_text_color")) {
348     curr_conn->down_text_color = strdup((char *)val);
349   } else {
350     retval = SILC_CONFIG_ESILENT;
351   }
352
353   return retval;
354 }
355
356 /* Command: loadmap, loadmaps the bitmap map image. */
357
358 SILC_CONFIG_CALLBACK(silc_map_cmd_loadmap)
359 {
360   SilcMap map = context;
361   int retval = SILC_CONFIG_OK;
362
363   if (type == SILC_CONFIG_ARG_BLOCK) {
364     if (!filename)
365       return SILC_CONFIG_EMISSFIELDS;
366
367     SILC_LOG_DEBUG(("loadmap: file: %s", filename));
368
369     /* Destroy old bitmap if loadmaped */
370     silc_free(map->bitmap);
371
372     /* Loadmap the bitmap image */
373     if (!silc_map_load_ppm(map, filename))
374       retval = SILC_CONFIG_ESILENT;
375
376     /* Cleanup */
377     silc_free(filename);
378     filename = NULL;
379
380     return retval;
381   }
382
383   if (!strcmp(name, "filename"))
384     filename = strdup((char *)val);
385   else
386     retval = SILC_CONFIG_ESILENT;
387
388   return retval;
389 }
390
391 /* Command: writemap, writemap the map into bitmap file. */
392
393 SILC_CONFIG_CALLBACK(silc_map_cmd_writemap)
394 {
395   SilcMap map = context;
396   int retval = SILC_CONFIG_OK;
397
398   if (type == SILC_CONFIG_ARG_BLOCK) {
399     if (!filename)
400       return SILC_CONFIG_EMISSFIELDS;
401
402     SILC_LOG_DEBUG(("writemap: file: %s", filename));
403
404     /* Execute directly if there are no connections */
405     if (map->conns_num == 0) {
406       /* Writemap the map */
407       if (!silc_map_write_ppm(map, filename))
408         retval = SILC_CONFIG_ESILENT;
409     } else {
410       map->writemap.filename = strdup(filename);
411       map->writemap.writemap = TRUE;
412     }
413
414     /* Cleanup */
415     silc_free(filename);
416     filename = NULL;
417
418     return retval;
419   }
420
421   if (!strcmp(name, "filename"))
422     filename = strdup((char *)val);
423   else
424     retval = SILC_CONFIG_ESILENT;
425
426   return retval;
427 }
428
429 /* Command: writemaphtml, writes HTML map of the image map. */
430
431 SILC_CONFIG_CALLBACK(silc_map_cmd_writemaphtml)
432 {
433   SilcMap map = context;
434   int retval = SILC_CONFIG_OK;
435
436   if (type == SILC_CONFIG_ARG_BLOCK) {
437     int i;
438     if (!filename)
439       return SILC_CONFIG_EMISSFIELDS;
440
441     SILC_LOG_DEBUG(("writemaphtml: file: %s", filename));
442
443     /* Will generate HTML map page */
444     i = map->writemaphtml_count;
445     map->writemaphtml = silc_realloc(map->writemaphtml,
446                                      sizeof(*map->writemaphtml) * (i + 1));
447     map->writemaphtml[i].filename = filename;
448     map->writemaphtml[i].text = text;
449     if (lon)
450       map->writemaphtml[i].x = silc_map_lon2x(map, lon);
451     if (lat)
452       map->writemaphtml[i].y = silc_map_lat2y(map, lat);
453     map->writemaphtml[i].writemaphtml = TRUE;
454     map->writemaphtml_count++;
455
456     /* Clean up */
457     silc_free(lat);
458     silc_free(lon);
459     filename = NULL;
460     text = NULL;
461     lat = lon = NULL;
462
463     return retval;
464   }
465
466   if (!strcmp(name, "filename"))
467     filename = strdup((char *)val);
468   else if (!strcmp(name, "image"))
469     text = strdup((char *)val);
470   else if (!strcmp(name, "cut_lat"))
471     lat = strdup((char *)val);
472   else if (!strcmp(name, "cut_lon"))
473     lon = strdup((char *)val);
474   else
475     retval = SILC_CONFIG_ESILENT;
476
477   return retval;
478 }
479
480 /* Command: writehtml, writes the gathered data into HTML pages. */
481
482 SILC_CONFIG_CALLBACK(silc_map_cmd_writehtml)
483 {
484   SilcMap map = context;
485   int retval = SILC_CONFIG_OK;
486
487   if (type == SILC_CONFIG_ARG_BLOCK) {
488     if (!filename)
489       return SILC_CONFIG_EMISSFIELDS;
490
491     SILC_LOG_DEBUG(("writehtml: file: %s", filename));
492
493     /* Will generate HTML pages */
494     map->writehtml.filename = filename;
495     map->writehtml.text = text;         /* class */
496     map->writehtml.writehtml = TRUE;
497     filename = text = NULL;
498
499     return retval;
500   }
501   if (!strcmp(name, "filename"))
502     filename = strdup((char *)val);
503   else if (!strcmp(name, "class"))
504     text = strdup((char *)val);
505   else
506     retval = SILC_CONFIG_ESILENT;
507
508   return retval;
509 }
510
511 /* Command: writerel, writes the uptime reliability graph. */
512
513 SILC_CONFIG_CALLBACK(silc_map_cmd_writerel)
514 {
515   SilcMap map = context;
516   int retval = SILC_CONFIG_OK;
517
518   if (type == SILC_CONFIG_ARG_BLOCK) {
519     if (!filename)
520       return SILC_CONFIG_EMISSFIELDS;
521
522     SILC_LOG_DEBUG(("writerel: file: %s", filename));
523
524     /* Will generate uptime reliability graph */
525     map->writerel.filename = filename;
526     map->writerel.text = text;          /* class */
527     map->writerel.writerel = TRUE;
528     filename = text = NULL;
529
530     return retval;
531   }
532   if (!strcmp(name, "filename"))
533     filename = strdup((char *)val);
534   else if (!strcmp(name, "class"))
535     text = strdup((char *)val);
536   else
537     retval = SILC_CONFIG_ESILENT;
538
539   return retval;
540 }
541
542 /* Command: cut, cut's a specified area from the map. */
543
544 SILC_CONFIG_CALLBACK(silc_map_cmd_cut)
545 {
546   SilcMap map = context;
547   int retval = SILC_CONFIG_OK;
548   bool ret;
549
550   if (type == SILC_CONFIG_ARG_BLOCK) {
551     SilcMap map2;
552     int i;
553
554     if (!filename || !lat || !lon || !width || !height)
555       return SILC_CONFIG_EMISSFIELDS;
556
557     SILC_LOG_DEBUG(("cut: lat: %s lon: %s w: %ld h: %ld file: %s",
558                     lat, lon, width, height, filename));
559
560     /* Execute directly if not inside connection block */
561     if (!curr_conn) {
562       if (!map->conns_num) {
563         /* Before any connection blocks */
564
565         /* Cut the chunk from the map. */
566         ret = silc_map_cut(map, silc_map_lon2x(map, lon),
567                            silc_map_lat2y(map, lat),
568                            width, height, &map2);
569         if (ret) {
570           /* Writemap the chunk. */
571           ret = silc_map_write_ppm(map2, filename);
572           silc_map_free(map2);
573         }
574         if (!ret)
575           retval = SILC_CONFIG_ESILENT;
576       } else {
577         /* After all connection blocks */
578         i = map->cut_count;
579         map->cut = silc_realloc(map->cut, sizeof(*map->cut) * (i + 1));
580         map->cut[i].filename = strdup(filename);
581         map->cut[i].x = silc_map_lon2x(map, lon);
582         map->cut[i].y = silc_map_lat2y(map, lat);
583         map->cut[i].width = width;
584         map->cut[i].height = height;
585         map->cut[i].cut = TRUE;
586         map->cut_count++;
587       }
588     } else {
589       SilcMapCommand cmd = silc_calloc(1, sizeof(*cmd));
590       if (!cmd)
591         return SILC_CONFIG_ESILENT;
592
593       silc_dlist_add(curr_conn->commands, cmd);
594       cmd->filename = strdup(filename);
595       cmd->x = silc_map_lon2x(map, lon);
596       cmd->y = silc_map_lat2y(map, lat);
597       cmd->width = width;
598       cmd->height = height;
599       cmd->cut = TRUE;
600     }
601
602     /* Cleanup */
603     silc_free(filename);
604     silc_free(lat);
605     silc_free(lon);
606     filename = NULL;
607     lat = NULL;
608     lon = NULL;
609     width = 0;
610     height = 0;
611
612     return retval;
613   }
614
615   if (!strcmp(name, "lat"))
616     lat = strdup((char *)val);
617   else if (!strcmp(name, "lon"))
618     lon = strdup((char *)val);
619   else if (!strcmp(name, "width"))
620     width = (SilcUInt32)*(int *)val;
621   else if (!strcmp(name, "height"))
622     height = (SilcUInt32)*(int *)val;
623   else if (!strcmp(name, "filename"))
624     filename = strdup((char *)val);
625   else
626     retval = SILC_CONFIG_ESILENT;
627
628   return retval;
629 }
630
631 /* Command: rectangle, draws a rectangle on the map. */
632
633 SILC_CONFIG_CALLBACK(silc_map_cmd_rectangle)
634 {
635   SilcMap map = context;
636   int retval = SILC_CONFIG_OK;
637   bool ret;
638
639   if (type == SILC_CONFIG_ARG_BLOCK) {
640     if (!lat || !lon)
641       return SILC_CONFIG_EMISSFIELDS;
642
643     SILC_LOG_DEBUG(("rectangle: lat: %s lon: %s color: %d %d %d",
644                     lat, lon, r, g, b));
645
646     if (lr == -1) {
647       lr = r;
648       lg = g;
649       lb = b;
650     }
651
652     /* Execute directly if not for connection */
653     if (!curr_conn) {
654       /* Draw the rectangle */
655       ret = silc_map_draw_rectangle(map, silc_map_lon2x(map, lon),
656                                     silc_map_lat2y(map, lat),
657                                     r, g, b, text, lposx, lposy, lr, lg, lb);
658       if (!ret)
659         retval = SILC_CONFIG_ESILENT;
660     } else {
661       SilcMapCommand cmd = silc_calloc(1, sizeof(*cmd));
662       if (!cmd)
663         return SILC_CONFIG_ESILENT;
664
665       silc_dlist_add(curr_conn->commands, cmd);
666       cmd->r = r;
667       cmd->g = g;
668       cmd->b = b;
669       cmd->lr = lr;
670       cmd->lg = lg;
671       cmd->lb = lb;
672       cmd->x = silc_map_lon2x(map, lon);
673       cmd->y = silc_map_lat2y(map, lat);
674       cmd->text = text ? strdup(text) : NULL;
675       cmd->lposx = lposx;
676       cmd->lposy = lposy;
677       cmd->draw_rectangle = TRUE;
678       cmd->color_set = color_set;
679       cmd->lcolor_set = lcolor_set;
680     }
681
682     /* Cleanup */
683     silc_free(text);
684     silc_free(lat);
685     silc_free(lon);
686     text = NULL;
687     lat = NULL;
688     lon = NULL;
689     lposx = 0;
690     lposy = 0;
691     lr = lg = lb = -1;
692     color_set = lcolor_set = FALSE;
693
694     return retval;
695   }
696
697   if (!strcmp(name, "lat"))
698     lat = strdup((char *)val);
699   else if (!strcmp(name, "lon"))
700     lon = strdup((char *)val);
701   else if (!strcmp(name, "color")) {
702     if (!silc_map_parse_color((const char *)val, &r, &g, &b))
703       retval = SILC_CONFIG_ESILENT;
704     color_set = TRUE;
705   } else if (!strcmp(name, "label"))
706     text = strdup((char *)val);
707   else if (!strcmp(name, "lposx"))
708     lposx = (SilcInt32)*(int *)val;
709   else if (!strcmp(name, "lposy"))
710     lposy = (SilcInt32)*(int *)val;
711   else if (!strcmp(name, "lcolor")) {
712     if (!silc_map_parse_color((const char *)val, &lr, &lg, &lb))
713       retval = SILC_CONFIG_ESILENT;
714     lcolor_set = TRUE;
715  } else
716     retval = SILC_CONFIG_ESILENT;
717
718   return retval;
719 }
720
721 /* Command: circle, draws a circle on the map. */
722
723 SILC_CONFIG_CALLBACK(silc_map_cmd_circle)
724 {
725   SilcMap map = context;
726   int retval = SILC_CONFIG_OK;
727   bool ret;
728
729   if (type == SILC_CONFIG_ARG_BLOCK) {
730     if (!lat || !lon)
731       return SILC_CONFIG_EMISSFIELDS;
732
733     SILC_LOG_DEBUG(("circle: lat: %s lon: %s color: %d %d %d",
734                     lat, lon, r, g, b));
735
736     if (lr == -1) {
737       lr = r;
738       lg = g;
739       lb = b;
740     }
741
742     /* Execute directly if not for connection */
743     if (!curr_conn) {
744       /* Draw the circle */
745       ret = silc_map_draw_circle(map, silc_map_lon2x(map, lon),
746                                  silc_map_lat2y(map, lat),
747                                  r, g, b, text, lposx, lposy, lr, lg, lb);
748       if (!ret)
749         retval = SILC_CONFIG_ESILENT;
750     } else {
751       SilcMapCommand cmd = silc_calloc(1, sizeof(*cmd));
752       if (!cmd)
753         return SILC_CONFIG_ESILENT;
754
755       silc_dlist_add(curr_conn->commands, cmd);
756       cmd->r = r;
757       cmd->g = g;
758       cmd->b = b;
759       cmd->lr = lr;
760       cmd->lg = lg;
761       cmd->lb = lb;
762       cmd->x = silc_map_lon2x(map, lon);
763       cmd->y = silc_map_lat2y(map, lat);
764       cmd->text = text ? strdup(text) : NULL;
765       cmd->lposx = lposx;
766       cmd->lposy = lposy;
767       cmd->draw_circle = TRUE;
768       cmd->color_set = color_set;
769       cmd->lcolor_set = lcolor_set;
770     }
771
772     /* Cleanup */
773     silc_free(text);
774     silc_free(lat);
775     silc_free(lon);
776     text = NULL;
777     lat = NULL;
778     lon = NULL;
779     lposx = 0;
780     lposy = 0;
781     lr = lg = lb = -1;
782     color_set = lcolor_set = FALSE;
783
784     return retval;
785   }
786
787   if (!strcmp(name, "lat"))
788     lat = strdup((char *)val);
789   else if (!strcmp(name, "lon"))
790     lon = strdup((char *)val);
791   else if (!strcmp(name, "color")) {
792     if (!silc_map_parse_color((const char *)val, &r, &g, &b))
793       retval = SILC_CONFIG_ESILENT;
794     color_set = TRUE;
795   } else if (!strcmp(name, "label"))
796     text = strdup((char *)val);
797   else if (!strcmp(name, "lposx"))
798     lposx = (SilcInt32)*(int *)val;
799   else if (!strcmp(name, "lposy"))
800     lposy = (SilcInt32)*(int *)val;
801   else if (!strcmp(name, "lcolor")) {
802     if (!silc_map_parse_color((const char *)val, &lr, &lg, &lb))
803       retval = SILC_CONFIG_ESILENT;
804     lcolor_set = TRUE;
805   } else
806     retval = SILC_CONFIG_ESILENT;
807
808   return retval;
809 }
810
811 /* Command: line, draws a line between two points in the map. */
812
813 SILC_CONFIG_CALLBACK(silc_map_cmd_line)
814 {
815   SilcMap map = context;
816   int retval = SILC_CONFIG_OK;
817   bool ret;
818
819   if (type == SILC_CONFIG_ARG_BLOCK) {
820     if (!lat || !lon || !lat2 || !lon2)
821       return SILC_CONFIG_EMISSFIELDS;
822
823     SILC_LOG_DEBUG(("line: alat: %s alon: %s blat: %s blon: %s "
824                     "width: %ld color: %d %d %d",
825                     lat, lon, lat2, lon2, width, r, g, b));
826
827     if (!width)
828       width = 1;
829
830     /* Execute directly if not for connection */
831     if (!curr_conn) {
832       /* Draw the line */
833       ret = silc_map_draw_line(map, width,
834                                silc_map_lon2x(map, lon),
835                                silc_map_lat2y(map, lat),
836                                silc_map_lon2x(map, lon2),
837                                silc_map_lat2y(map, lat2),
838                                r, g, b);
839       if (!ret)
840         retval = SILC_CONFIG_ESILENT;
841     } else {
842       SilcMapCommand cmd = silc_calloc(1, sizeof(*cmd));
843       if (!cmd)
844         return SILC_CONFIG_ESILENT;
845
846       silc_dlist_add(curr_conn->commands, cmd);
847       cmd->r = r;
848       cmd->g = g;
849       cmd->b = b;
850       cmd->x = silc_map_lon2x(map, lon);
851       cmd->y = silc_map_lat2y(map, lat);
852       cmd->x2 = silc_map_lon2x(map, lon2);
853       cmd->y2 = silc_map_lat2y(map, lat2);
854       cmd->width = width;
855       cmd->draw_line = TRUE;
856       cmd->color_set = color_set;
857     }
858
859     /* Cleanup */
860     silc_free(lat);
861     silc_free(lon);
862     silc_free(lat2);
863     silc_free(lon2);
864     lat = NULL;
865     lon = NULL;
866     lat2 = NULL;
867     lon2 = NULL;
868     width = 0;
869     color_set = FALSE;
870
871     return retval;
872   }
873
874   if (!strcmp(name, "a_lat"))
875     lat = strdup((char *)val);
876   else if (!strcmp(name, "a_lon"))
877     lon = strdup((char *)val);
878   else if (!strcmp(name, "b_lat"))
879     lat2 = strdup((char *)val);
880   else if (!strcmp(name, "b_lon"))
881     lon2 = strdup((char *)val);
882   else if (!strcmp(name, "width"))
883     width = (SilcUInt32)*(int *)val;
884   else if (!strcmp(name, "color")) {
885     if (!silc_map_parse_color((const char *)val, &r, &g, &b))
886       retval = SILC_CONFIG_ESILENT;
887     color_set = TRUE;
888   } else
889     retval = SILC_CONFIG_ESILENT;
890
891   return retval;
892 }
893
894 /* Command: text, prints a text on the map. */
895
896 SILC_CONFIG_CALLBACK(silc_map_cmd_text)
897 {
898   SilcMap map = context;
899   int retval = SILC_CONFIG_OK;
900   bool ret;
901
902   if (type == SILC_CONFIG_ARG_BLOCK) {
903     if (!lat || !lon || !text)
904       return SILC_CONFIG_EMISSFIELDS;
905
906     SILC_LOG_DEBUG(("text: lat: %s lon: %s color: %d %d %d text: %s",
907                     lat, lon, r, g, b, text));
908
909     /* Execute directly if not for connection */
910     if (!curr_conn) {
911       /* Print the text */
912       ret = silc_map_draw_text(map, text,
913                                silc_map_lon2x(map, lon),
914                                silc_map_lat2y(map, lat),
915                                r, g, b);
916       if (!ret)
917         retval = SILC_CONFIG_ESILENT;
918     } else {
919       SilcMapCommand cmd = silc_calloc(1, sizeof(*cmd));
920       if (!cmd)
921         return SILC_CONFIG_ESILENT;
922
923       silc_dlist_add(curr_conn->commands, cmd);
924       cmd->r = r;
925       cmd->g = g;
926       cmd->b = b;
927       cmd->x = silc_map_lon2x(map, lon);
928       cmd->y = silc_map_lat2y(map, lat);
929       cmd->text = text ? strdup(text) : NULL;
930       cmd->draw_text = TRUE;
931       cmd->color_set = color_set;
932     }
933
934     /* Cleanup */
935     silc_free(text);
936     silc_free(lat);
937     silc_free(lon);
938     text = NULL;
939     lat = NULL;
940     lon = NULL;
941     color_set = FALSE;
942
943     return retval;
944   }
945
946   if (!strcmp(name, "lat"))
947     lat = strdup((char *)val);
948   else if (!strcmp(name, "lon"))
949     lon = strdup((char *)val);
950   else if (!strcmp(name, "color")) {
951     if (!silc_map_parse_color((const char *)val, &r, &g, &b))
952       retval = SILC_CONFIG_ESILENT;
953     color_set = TRUE;
954   } else if (!strcmp(name, "text"))
955     text = strdup((char *)val);
956   else
957     retval = SILC_CONFIG_ESILENT;
958
959   return retval;
960 }
961
962 /* Parses the commands from the file `filename'. */
963
964 bool silc_map_commands_parse(SilcMap map, const char *filename)
965 {
966   SilcConfigEntity ent;
967   SilcConfigFile *file;
968   bool retval = TRUE;
969   int ret;
970
971   SILC_LOG_DEBUG(("Parsing commands"));
972
973   /* Open commands file */
974   file = silc_config_open(filename);
975   if (!file) {
976     fprintf(stderr, "Cannot open commands file '%s'\n", filename);
977     return FALSE;
978   }
979
980   /* Parse the commands */
981   ent = silc_config_init(file);
982   silc_config_register_table(ent, silc_map_table_main, map);
983   ret = silc_config_main(ent);
984
985   SILC_LOG_DEBUG(("Parsing status: %s", silc_config_strerror(ret)));
986
987   if (ret && ret != SILC_CONFIG_ESILENT) {
988     fprintf(stderr, "Error parsing commands: %s, line %ld\n",
989             silc_config_strerror(ret), silc_config_get_line(file));
990     retval = FALSE;
991   }
992
993   /* Cleanup */
994   silc_config_close(file);
995   return retval;
996 }