Merged silc_1_0_branch to trunk.
[crypto.git] / apps / irssi / src / fe-common / silc / fe-silcnet.c
1 /*
2  fe-silcnet.c : irssi
3
4     Copyright (C) 2000 Timo Sirainen
5     Copyright (C) 2003 Jochen Eisinger
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22 #include "module.h"
23 #include "module-formats.h"
24 #include "signals.h"
25 #include "commands.h"
26 #include "levels.h"
27 #include "misc.h"
28 #include "chatnets.h"
29
30 #include "silc-servers.h"
31 #include "silc-chatnets.h"
32 #include "printtext.h"
33
34 static void cmd_silcnet_list(void)
35 {
36         GString *str;
37         GSList *tmp;
38
39         str = g_string_new(NULL);
40         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, SILCTXT_SILCNET_HEADER);
41         for (tmp = chatnets; tmp != NULL; tmp = tmp->next) {
42                 SILC_CHATNET_REC *rec = tmp->data;
43
44                 if (!IS_SILCNET(rec))
45                         continue;
46
47                 g_string_truncate(str, 0);
48                 if (rec->nick != NULL)
49                         g_string_sprintfa(str, "nick: %s, ", rec->nick);
50                 if (rec->username != NULL)
51                         g_string_sprintfa(str, "username: %s, ", rec->username);
52                 if (rec->realname != NULL)
53                         g_string_sprintfa(str, "realname: %s, ", rec->realname);
54                 if (rec->own_host != NULL)
55                         g_string_sprintfa(str, "host: %s, ", rec->own_host);
56
57                 if (str->len > 1) g_string_truncate(str, str->len-2);
58                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
59                             SILCTXT_SILCNET_LINE, rec->name, str->str);
60         }
61         g_string_free(str, TRUE);
62         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, SILCTXT_SILCNET_FOOTER);
63 }
64
65 /* SYNTAX: SILCNET ADD [-nick <nick>] [-user <user>] [-realname <name>]
66                        [-host <host>] <name> */
67 static void cmd_silcnet_add(const char *data)
68 {
69         GHashTable *optlist;
70         char *name, *value;
71         void *free_arg;
72         SILC_CHATNET_REC *rec;
73
74         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
75                             "silcnet add", &optlist, &name))
76                 return;
77         if (*name == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
78
79         rec = silcnet_find(name);
80         if (rec == NULL) {
81                 rec = g_new0(SILC_CHATNET_REC, 1);
82                 rec->name = g_strdup(name);
83         } else {
84                 if (g_hash_table_lookup(optlist, "nick")) g_free_and_null(rec->nick);
85                 if (g_hash_table_lookup(optlist, "user")) g_free_and_null(rec->username);
86                 if (g_hash_table_lookup(optlist, "realname")) g_free_and_null(rec->realname);
87                 if (g_hash_table_lookup(optlist, "host")) {
88                         g_free_and_null(rec->own_host);
89                         rec->own_ip4 = rec->own_ip6 = NULL;
90                 }
91         }
92
93
94         value = g_hash_table_lookup(optlist, "nick");
95         if (value != NULL && *value != '\0') rec->nick = g_strdup(value);
96         value = g_hash_table_lookup(optlist, "user");
97         if (value != NULL && *value != '\0') rec->username = g_strdup(value);
98         value = g_hash_table_lookup(optlist, "realname");
99         if (value != NULL && *value != '\0') rec->realname = g_strdup(value);
100
101         value = g_hash_table_lookup(optlist, "host");
102         if (value != NULL && *value != '\0') {
103                 rec->own_host = g_strdup(value);
104                 rec->own_ip4 = rec->own_ip6 = NULL;
105         }
106
107         silcnet_create(rec);
108         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, SILCTXT_SILCNET_ADDED, name);
109
110         cmd_params_free(free_arg);
111 }
112
113 /* SYNTAX: SILCNET REMOVE <silcnet> */
114 static void cmd_silcnet_remove(const char *data)
115 {
116         SILC_CHATNET_REC *rec;
117
118         if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
119
120         rec = silcnet_find(data);
121         if (rec == NULL)
122                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, SILCTXT_SILCNET_NOT_FOUND, data);
123         else {
124                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, SILCTXT_SILCNET_REMOVED, data);
125                 chatnet_remove(CHATNET(rec));
126         }
127 }
128
129 static void cmd_silcnet(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
130 {
131         if (*data == '\0')
132                 cmd_silcnet_list();
133         else
134                 command_runsub("silcnet", data, server, item);
135 }
136
137 void fe_silcnet_init(void)
138 {
139         command_bind("silcnet", NULL, (SIGNAL_FUNC) cmd_silcnet);
140         command_bind("silcnet list", NULL, (SIGNAL_FUNC) cmd_silcnet_list);
141         command_bind("silcnet add", NULL, (SIGNAL_FUNC) cmd_silcnet_add);
142         command_bind("silcnet remove", NULL, (SIGNAL_FUNC) cmd_silcnet_remove);
143
144         command_set_options("silcnet add", "-nick -user -realname -host");
145 }
146
147 void fe_silcnet_deinit(void)
148 {
149         command_unbind("silcnet", (SIGNAL_FUNC) cmd_silcnet);
150         command_unbind("silcnet list", (SIGNAL_FUNC) cmd_silcnet_list);
151         command_unbind("silcnet add", (SIGNAL_FUNC) cmd_silcnet_add);
152         command_unbind("silcnet remove", (SIGNAL_FUNC) cmd_silcnet_remove);
153 }