Added SILC Thread Queue API
[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 void silcnet_create(SILC_CHATNET_REC *rec)
35 {
36         g_return_if_fail(rec != NULL);
37
38         rec->chat_type = SILC_PROTOCOL;
39         chatnet_create((CHATNET_REC *) rec);
40 }
41
42 static void cmd_silcnet_list(void)
43 {
44         GString *str;
45         GSList *tmp;
46
47         str = g_string_new(NULL);
48         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, SILCTXT_SILCNET_HEADER);
49         for (tmp = chatnets; tmp != NULL; tmp = tmp->next) {
50                 SILC_CHATNET_REC *rec = tmp->data;
51
52                 if (!IS_SILCNET(rec))
53                         continue;
54
55                 g_string_truncate(str, 0);
56                 if (rec->nick != NULL)
57                         g_string_sprintfa(str, "nick: %s, ", rec->nick);
58                 if (rec->username != NULL)
59                         g_string_sprintfa(str, "username: %s, ", rec->username);
60                 if (rec->realname != NULL)
61                         g_string_sprintfa(str, "realname: %s, ", rec->realname);
62                 if (rec->own_host != NULL)
63                         g_string_sprintfa(str, "host: %s, ", rec->own_host);
64
65                 if (str->len > 1) g_string_truncate(str, str->len-2);
66                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
67                             SILCTXT_SILCNET_LINE, rec->name, str->str);
68         }
69         g_string_free(str, TRUE);
70         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, SILCTXT_SILCNET_FOOTER);
71 }
72
73 /* SYNTAX: SILCNET ADD [-nick <nick>] [-user <user>] [-realname <name>]
74                        [-host <host>] <name> */
75 static void cmd_silcnet_add(const char *data)
76 {
77         GHashTable *optlist;
78         char *name, *value;
79         void *free_arg;
80         SILC_CHATNET_REC *rec;
81
82         if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
83                             "silcnet add", &optlist, &name))
84                 return;
85         if (*name == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
86
87         rec = silcnet_find(name);
88         if (rec == NULL) {
89                 rec = g_new0(SILC_CHATNET_REC, 1);
90                 rec->name = g_strdup(name);
91         } else {
92                 if (g_hash_table_lookup(optlist, "nick")) g_free_and_null(rec->nick);
93                 if (g_hash_table_lookup(optlist, "user")) g_free_and_null(rec->username);
94                 if (g_hash_table_lookup(optlist, "realname")) g_free_and_null(rec->realname);
95                 if (g_hash_table_lookup(optlist, "host")) {
96                         g_free_and_null(rec->own_host);
97                         rec->own_ip4 = rec->own_ip6 = NULL;
98                 }
99         }
100
101
102         value = g_hash_table_lookup(optlist, "nick");
103         if (value != NULL && *value != '\0') rec->nick = g_strdup(value);
104         value = g_hash_table_lookup(optlist, "user");
105         if (value != NULL && *value != '\0') rec->username = g_strdup(value);
106         value = g_hash_table_lookup(optlist, "realname");
107         if (value != NULL && *value != '\0') rec->realname = g_strdup(value);
108
109         value = g_hash_table_lookup(optlist, "host");
110         if (value != NULL && *value != '\0') {
111                 rec->own_host = g_strdup(value);
112                 rec->own_ip4 = rec->own_ip6 = NULL;
113         }
114
115         silcnet_create(rec);
116         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, SILCTXT_SILCNET_ADDED, name);
117
118         cmd_params_free(free_arg);
119 }
120
121 /* SYNTAX: SILCNET REMOVE <silcnet> */
122 static void cmd_silcnet_remove(const char *data)
123 {
124         SILC_CHATNET_REC *rec;
125
126         if (*data == '\0') cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
127
128         rec = silcnet_find(data);
129         if (rec == NULL)
130                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, SILCTXT_SILCNET_NOT_FOUND, data);
131         else {
132                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, SILCTXT_SILCNET_REMOVED, data);
133                 chatnet_remove(CHATNET(rec));
134         }
135 }
136
137 static void cmd_silcnet(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
138 {
139         if (*data == '\0')
140                 cmd_silcnet_list();
141         else
142                 command_runsub("silcnet", data, server, item);
143 }
144
145 void fe_silcnet_init(void)
146 {
147         command_bind("silcnet", NULL, (SIGNAL_FUNC) cmd_silcnet);
148         command_bind("silcnet list", NULL, (SIGNAL_FUNC) cmd_silcnet_list);
149         command_bind("silcnet add", NULL, (SIGNAL_FUNC) cmd_silcnet_add);
150         command_bind("silcnet remove", NULL, (SIGNAL_FUNC) cmd_silcnet_remove);
151
152         command_set_options("silcnet add", "-nick -user -realname -host");
153 }
154
155 void fe_silcnet_deinit(void)
156 {
157         command_unbind("silcnet", (SIGNAL_FUNC) cmd_silcnet);
158         command_unbind("silcnet list", (SIGNAL_FUNC) cmd_silcnet_list);
159         command_unbind("silcnet add", (SIGNAL_FUNC) cmd_silcnet_add);
160         command_unbind("silcnet remove", (SIGNAL_FUNC) cmd_silcnet_remove);
161 }