Debian Codegraph @

src/fe-common/xmpp-completion.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2007 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <string.h>
19
20 #include "module.h"
21 #include "channels.h"
22 #include "channels-setup.h"
23 #include "misc.h"
24 #include "settings.h"
25 #include "signals.h"
26 #include "window-items.h"
27
28 #include "xmpp-servers.h"
29 #include "xmpp-commands.h"
30 #include "rosters-tools.h"
31 #include "tools.h"
32
•33 #if !defined(IRSSI_ABI_VERSION) || IRSSI_ABI_VERSION < 33
34 # define i_list_find_string glist_find_string
35 #endif
36
37 static char *
38 quoted_if_space(const char *name, const char *res)
39 {
40 if (res != NULL)
41 return g_utf8_strchr(res, -1, ' ') == NULL ?
42 g_strconcat(name, "/", res, (void *)NULL) :
43 g_strconcat("\"", name, "/", res, "\"", (void *)NULL);
44 else
45 return g_utf8_strchr(name, -1, ' ') == NULL ?
46 g_strdup(name) : g_strconcat("\"", name, "\"", (void *)NULL);
47 }
48
49 static GList *
50 get_resources(XMPP_SERVER_REC *server, const char *nick,
51 const char *resource_name, gboolean quoted)
52 {
53 GSList *rl;
54 GList *list;
55 XMPP_ROSTER_USER_REC *user;
56 XMPP_ROSTER_RESOURCE_REC *resource;
57 size_t len;
58
59 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
60 g_return_val_if_fail(nick != NULL, NULL);
61 len = resource_name != NULL ? strlen(resource_name) : 0;
62 list = NULL;
63 user = rosters_find_user(server->roster, nick, NULL, NULL);
64 if (user == NULL)
65 return NULL;
66 for(rl = user->resources; rl != NULL; rl = rl->next) {
67 resource = rl->data;
68 if (resource_name == NULL
69 || g_ascii_strncasecmp(resource->name, resource_name, len) == 0)
70 list = g_list_append(list, quoted ?
71 quoted_if_space(nick, resource->name) :
72 g_strconcat(nick, "/", resource->name, (void *)NULL));
73 }
74 return list;
75 }
76
77 static GList *
78 get_jids(XMPP_SERVER_REC *server, const char *jid)
79 {
80 GSList *gl, *ul;
81 GList *list, *list_case, *offlist, *offlist_case;
82 XMPP_ROSTER_USER_REC *user;
83 int len;
84
85 list = list_case = offlist = offlist_case = NULL;
86 len = strlen(jid);
87 for (gl = server->roster; gl != NULL; gl = gl->next) {
88 for (ul = ((XMPP_ROSTER_GROUP_REC *)gl->data)->users;
89 ul != NULL ; ul = ul->next) {
90 user = (XMPP_ROSTER_USER_REC *)ul->data;
91 if (strncmp(user->jid, jid, len) == 0) {
92 if (user->resources != NULL)
93 list = g_list_append(list,
94 g_strdup(user->jid));
95 else
96 offlist = g_list_append(offlist,
97 g_strdup(user->jid));
98 } else if (g_ascii_strncasecmp(user->jid, jid, len) == 0) {
99 if (user->resources != NULL)
100 list_case = g_list_append(list_case,
101 g_strdup(user->jid));
102 else
103 offlist_case = g_list_append(offlist_case,
104 g_strdup(user->jid));
105 }
106 }
107 }
108 /* TODO: temporary list of jids */
109 list = g_list_concat(list, list_case);
110 list = g_list_concat(list, offlist);
111 list = g_list_concat(list, offlist_case);
112 return list;
113 }
114
115 static GList *
116 get_nicks(XMPP_SERVER_REC *server, const char *nick, gboolean quoted,
117 gboolean complete_names)
118 {
119 GSList *gl, *ul;
120 GList *list;
121 XMPP_ROSTER_USER_REC *user;
122 char *jid, *resource;
123 int len;
124 gboolean pass2;
125
126 len = strlen(nick);
127 /* resources completion */
128 resource = xmpp_extract_resource(nick);
129 if (resource != NULL) {
130 jid = xmpp_strip_resource(nick);
131 list = get_resources(server, jid, resource, quoted);
132 g_free(resource);
133 g_free(jid);
134 return list;
135 }
136 list = NULL;
137 pass2 = FALSE;
138 again:
139 /* first complete with online contacts
140 * then complete with offline contacts */
141 for (gl = server->roster; gl != NULL; gl = gl->next) {
142 for (ul = ((XMPP_ROSTER_GROUP_REC *)gl->data)->users;
143 ul != NULL ; ul = ul->next) {
144 user = (XMPP_ROSTER_USER_REC *)ul->data;
145 if ((!pass2 && user->resources == NULL)
146 || (pass2 && user->resources != NULL))
147 continue;
148 if (complete_names && user->name != NULL
149 && g_ascii_strncasecmp(user->name, nick, len) == 0)
150 list = g_list_prepend(list, quoted ?
151 quoted_if_space(user->name, NULL) :
152 g_strdup(user->name));
153 if (g_ascii_strncasecmp(user->jid, nick, len) == 0)
154 list = g_list_prepend(list, quoted ?
155 quoted_if_space(user->jid, NULL) :
156 g_strdup(user->jid));
157 }
158 }
159 if ((pass2 = !pass2))
160 goto again;
161 /* TODO: rewrite this function */
162 return list;
163 }
164
165 static void
166 sig_complete_word(GList **list, WINDOW_REC *window, const char *word,
167 const char *linestart, int *want_space)
168 {
169 XMPP_SERVER_REC *server;
170
171 g_return_if_fail(list != NULL);
172 g_return_if_fail(window != NULL);
173 g_return_if_fail(word != NULL);
174 if ((server = XMPP_SERVER(window->active_server)) == NULL)
175 return;
176 if (g_ascii_strncasecmp(linestart,
177 settings_get_str("cmdchars"), 1) == 0) {
178 *list = g_list_concat(*list, get_nicks(server, *word == '"' ?
179 word+1 : word , TRUE, TRUE));
180 } else if (!IS_CHANNEL(window->active))
181 *list = g_list_concat(*list, get_nicks(server, word, FALSE,
182 TRUE));
183 }
184
185 static void
186 sig_complete_command_roster_group(GList **list, WINDOW_REC *window,
187 const char *word, const char *args, int *want_space)
188 {
189 GSList *gl;
190 XMPP_SERVER_REC *server;
191 XMPP_ROSTER_GROUP_REC *group;
192 int len;
193 char **tmp;
194
195 g_return_if_fail(list != NULL);
196 g_return_if_fail(window != NULL);
197 g_return_if_fail(word != NULL);
198 g_return_if_fail(args != NULL);
199 server = XMPP_SERVER(window->active_server);
200 if (server == NULL)
201 return;
202 len = strlen(word);
203 tmp = g_strsplit(args, " ", 2);
204 /* complete nicks */
205 if (tmp[0] == NULL)
206 *list = g_list_concat(*list, get_nicks(server, *word == '"' ?
207 word+1 : word , TRUE, FALSE));
208 /* complete groups */
209 else if (tmp[0] != NULL && tmp[1] == NULL) {
210 for (gl = server->roster; gl != NULL; gl = gl->next) {
211 group = (XMPP_ROSTER_GROUP_REC *)gl->data;
212 if (group->name != NULL &&
213 g_ascii_strncasecmp(word, group->name, len) == 0)
214 *list = g_list_append(*list,
215 g_strdup(group->name));
216 }
217 }
218 g_strfreev(tmp);
219 if (*list != NULL)
220 signal_stop();
221 }
222
223 static void
224 sig_complete_command_roster_others(GList **list, WINDOW_REC *window,
225 const char *word, const char *args, int *want_space)
226 {
227 XMPP_SERVER_REC *server;
228 char **tmp;
229
230 g_return_if_fail(list != NULL);
231 g_return_if_fail(window != NULL);
232 g_return_if_fail(word != NULL);
233 g_return_if_fail(args != NULL);
234 if ((server = XMPP_SERVER(window->active_server)) == NULL)
235 return;
236 tmp = g_strsplit(args, " ", 2);
237 /* complete nicks */
238 if (tmp[0] == NULL)
239 *list = g_list_concat(*list, get_nicks(server, *word == '"' ?
240 word+1 : word , TRUE, FALSE));
241 g_strfreev(tmp);
242 if (*list != NULL)
243 signal_stop();
244 }
245
246 static void
247 sig_complete_command_presence(GList **list, WINDOW_REC *window,
248 const char *word, const char *args, int *want_space)
249 {
250 XMPP_SERVER_REC *server;
251 char **tmp;
252
253 g_return_if_fail(list != NULL);
254 g_return_if_fail(window != NULL);
255 g_return_if_fail(word != NULL);
256 g_return_if_fail(args != NULL);
257 if ((server = XMPP_SERVER(window->active_server)) == NULL)
258 return;
259 tmp = g_strsplit(args, " ", 2);
260 /* complete nicks */
261 if (tmp[0] == NULL)
262 *list = g_list_concat(*list, get_jids(server, *word == '"' ?
263 word+1 : word));
264 g_strfreev(tmp);
265 if (*list != NULL)
266 signal_stop();
267 }
268
269 static GList *
270 get_channels(XMPP_SERVER_REC *server, const char *word)
271 {
272 GSList *tmp;
273 GList *list;
274 CHANNEL_REC *channel;
275 CHANNEL_SETUP_REC *channel_setup;
276 int len;
277
278 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
279 g_return_val_if_fail(word != NULL, NULL);
280 len = strlen(word);
281 list = NULL;
282 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
283 channel = CHANNEL(tmp->data);
284 if (channel != NULL &&
285 g_ascii_strncasecmp(channel->name, word, len) == 0)
286 list = g_list_append(list, g_strdup(channel->name));
287 }
288 for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) {
289 channel_setup = tmp->data;
290 if ((PROTO_CHECK_CAST(channel_setup, CHANNEL_SETUP_REC,
291 chat_type, XMPP_PROTOCOL_NAME)
292 || *channel_setup->name != '#')
293 && g_ascii_strncasecmp(channel_setup->name, word, len) == 0
•294 && i_list_find_string(list, channel_setup->name) == NULL)
295 list = g_list_append(list,
296 g_strdup(channel_setup->name));
297 }
298 return list;
299 }
300
301 static void
302 sig_complete_command_channels(GList **list, WINDOW_REC *window,
303 const char *word, const char *args, int *want_space)
304 {
305 XMPP_SERVER_REC *server;
306
307 g_return_if_fail(list != NULL);
308 g_return_if_fail(window != NULL);
309 g_return_if_fail(word != NULL);
310 server = XMPP_SERVER(window->active_server);
311 if (server == NULL)
312 return;
313 *list = get_channels(server, word);
314 if (*list != NULL)
315 signal_stop();
316 }
317
318 static void
319 sig_complete_command_invite(GList **list, WINDOW_REC *window,
320 const char *word, const char *args, int *want_space)
321 {
322 XMPP_SERVER_REC *server;
323 char **tmp;
324
325 g_return_if_fail(list != NULL);
326 g_return_if_fail(window != NULL);
327 g_return_if_fail(word != NULL);
328 server = XMPP_SERVER(window->active_server);
329 if (server == NULL)
330 return;
331 /* complete channels */
332 tmp = g_strsplit(args, " ", 2);
333 if (tmp[0] != NULL && tmp[1] == NULL)
334 *list = get_channels(server, word);
335 g_strfreev(tmp);
336 if (*list != NULL)
337 signal_stop();
338 }
339
340 static void
341 sig_complete_command_away(GList **list, WINDOW_REC *window,
342 const char *word, const char *args, int *want_space)
343 {
344 XMPP_SERVER_REC *server;
345 int len;
346
347 g_return_if_fail(list != NULL);
348 g_return_if_fail(window != NULL);
349 g_return_if_fail(word != NULL);
350 server = XMPP_SERVER(window->active_server);
351 if (server == NULL)
352 return;
353 len = strlen(word);
354 if (g_ascii_strncasecmp(word,
355 xmpp_presence_show[XMPP_PRESENCE_AWAY], len) == 0)
356 *list = g_list_append(*list,
357 g_strdup(xmpp_presence_show[XMPP_PRESENCE_AWAY]));
358 if (g_ascii_strncasecmp(word,
359 xmpp_presence_show[XMPP_PRESENCE_XA], len) == 0)
360 *list = g_list_append(*list,
361 g_strdup(xmpp_presence_show[XMPP_PRESENCE_XA]));
362 if (g_ascii_strncasecmp(word,
363 xmpp_presence_show[XMPP_PRESENCE_DND], len) == 0)
364 *list = g_list_append(*list,
365 g_strdup(xmpp_presence_show[XMPP_PRESENCE_DND]));
366 if (g_ascii_strncasecmp(word,
367 xmpp_presence_show[XMPP_PRESENCE_CHAT], len) == 0)
368 *list = g_list_append(*list,
369 g_strdup(xmpp_presence_show[XMPP_PRESENCE_CHAT]));
370 if (g_ascii_strncasecmp(word,
371 xmpp_presence_show[XMPP_PRESENCE_ONLINE], len) == 0)
372 *list = g_list_append(*list, g_strdup("online"));
373 signal_stop();
374 }
375
376 void
377 xmpp_completion_init(void)
378 {
379 signal_add("complete word", sig_complete_word);
380 signal_add("complete command roster group",
381 sig_complete_command_roster_group);
382 signal_add("complete command roster add",
383 sig_complete_command_roster_others);
384 signal_add("complete command roster remove",
385 sig_complete_command_roster_others);
386 signal_add("complete command roster name",
387 sig_complete_command_roster_others);
388 signal_add("complete command presence accept",
389 sig_complete_command_presence);
390 signal_add("complete command presence deny",
391 sig_complete_command_presence);
392 signal_add("complete command presence subscribe",
393 sig_complete_command_presence);
394 signal_add("complete command presence unsubscribe",
395 sig_complete_command_presence);
396 signal_add("complete command join", sig_complete_command_channels);
397 signal_add("complete command part", sig_complete_command_channels);
398 signal_add("complete command invite", sig_complete_command_invite);
399 signal_add("complete command away", sig_complete_command_away);
400 }
401
402 void
403 xmpp_completion_deinit(void)
404 {
405 signal_remove("complete word", sig_complete_word);
406 signal_remove("complete command roster group",
407 sig_complete_command_roster_group);
408 signal_remove("complete command roster add",
409 sig_complete_command_roster_others);
410 signal_remove("complete command roster remove",
411 sig_complete_command_roster_others);
412 signal_remove("complete command roster name",
413 sig_complete_command_roster_others);
414 signal_remove("complete command presence accept",
415 sig_complete_command_presence);
416 signal_remove("complete command presence deny",
417 sig_complete_command_presence);
418 signal_remove("complete command presence subscribe",
419 sig_complete_command_presence);
420 signal_remove("complete command presence unsubscribe",
421 sig_complete_command_presence);
422 signal_remove("complete command join", sig_complete_command_channels);
423 signal_remove("complete command part", sig_complete_command_channels);
424 signal_remove("complete command invite", sig_complete_command_invite);
425 signal_remove("complete command away", sig_complete_command_away);
426 }