| 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 |
static char * |
| 34 |
quoted_if_space(const char *name, const char *res) |
| 35 |
{ |
| 36 |
if (res != NULL) |
| 37 |
return g_utf8_strchr(res, -1, ' ') == NULL ? |
| 38 |
g_strconcat(name, "/", res, (void *)NULL) : |
| 39 |
g_strconcat("\"", name, "/", res, "\"", (void *)NULL); |
| 40 |
else |
| 41 |
return g_utf8_strchr(name, -1, ' ') == NULL ? |
| 42 |
g_strdup(name) : g_strconcat("\"", name, "\"", (void *)NULL); |
| 43 |
} |
| 44 |
|
| 45 |
static GList * |
| 46 |
get_resources(XMPP_SERVER_REC *server, const char *nick, |
| 47 |
const char *resource_name, gboolean quoted) |
| 48 |
{ |
| 49 |
GSList *rl; |
| 50 |
GList *list; |
| 51 |
XMPP_ROSTER_USER_REC *user; |
| 52 |
XMPP_ROSTER_RESOURCE_REC *resource; |
| 53 |
size_t len; |
| 54 |
|
| 55 |
g_return_val_if_fail(IS_XMPP_SERVER(server), NULL); |
| 56 |
g_return_val_if_fail(nick != NULL, NULL); |
| 57 |
len = resource_name != NULL ? strlen(resource_name) : 0; |
| 58 |
list = NULL; |
| 59 |
user = rosters_find_user(server->roster, nick, NULL, NULL); |
| 60 |
if (user == NULL) |
| 61 |
return NULL; |
| 62 |
for(rl = user->resources; rl != NULL; rl = rl->next) { |
| 63 |
resource = rl->data; |
| 64 |
if (resource_name == NULL |
| 65 |
|| g_ascii_strncasecmp(resource->name, resource_name, len) == 0) |
| 66 |
list = g_list_append(list, quoted ? |
| 67 |
quoted_if_space(nick, resource->name) : |
| 68 |
g_strconcat(nick, "/", resource->name, (void *)NULL)); |
| 69 |
} |
| 70 |
return list; |
| 71 |
} |
| 72 |
|
| 73 |
static GList * |
| 74 |
get_jids(XMPP_SERVER_REC *server, const char *jid) |
| 75 |
{ |
| 76 |
GSList *gl, *ul; |
| 77 |
GList *list, *list_case, *offlist, *offlist_case; |
| 78 |
XMPP_ROSTER_USER_REC *user; |
| 79 |
int len; |
| 80 |
|
| 81 |
list = list_case = offlist = offlist_case = NULL; |
| 82 |
len = strlen(jid); |
| 83 |
for (gl = server->roster; gl != NULL; gl = gl->next) { |
| 84 |
for (ul = ((XMPP_ROSTER_GROUP_REC *)gl->data)->users; |
| 85 |
ul != NULL ; ul = ul->next) { |
| 86 |
user = (XMPP_ROSTER_USER_REC *)ul->data; |
| 87 |
if (strncmp(user->jid, jid, len) == 0) { |
| 88 |
if (user->resources != NULL) |
| 89 |
list = g_list_append(list, |
| 90 |
g_strdup(user->jid)); |
| 91 |
else |
| 92 |
offlist = g_list_append(offlist, |
| 93 |
g_strdup(user->jid)); |
| 94 |
} else if (g_ascii_strncasecmp(user->jid, jid, len) == 0) { |
| 95 |
if (user->resources != NULL) |
| 96 |
list_case = g_list_append(list_case, |
| 97 |
g_strdup(user->jid)); |
| 98 |
else |
| 99 |
offlist_case = g_list_append(offlist_case, |
| 100 |
g_strdup(user->jid)); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
/* TODO: temporary list of jids */ |
| 105 |
list = g_list_concat(list, list_case); |
| 106 |
list = g_list_concat(list, offlist); |
| 107 |
list = g_list_concat(list, offlist_case); |
| 108 |
return list; |
| 109 |
} |
| 110 |
|
| 111 |
static GList * |
| 112 |
get_nicks(XMPP_SERVER_REC *server, const char *nick, gboolean quoted, |
| 113 |
gboolean complete_names) |
| 114 |
{ |
| 115 |
GSList *gl, *ul; |
| 116 |
GList *list; |
| 117 |
XMPP_ROSTER_USER_REC *user; |
| 118 |
char *jid, *resource; |
| 119 |
int len; |
| 120 |
gboolean pass2; |
| 121 |
|
| 122 |
len = strlen(nick); |
| 123 |
/* resources completion */ |
| 124 |
resource = xmpp_extract_resource(nick); |
| 125 |
if (resource != NULL) { |
| 126 |
jid = xmpp_strip_resource(nick); |
| 127 |
list = get_resources(server, jid, resource, quoted); |
| 128 |
g_free(resource); |
| 129 |
g_free(jid); |
| 130 |
return list; |
| 131 |
} |
| 132 |
list = NULL; |
| 133 |
pass2 = FALSE; |
| 134 |
again: |
| 135 |
/* first complete with online contacts |
| 136 |
* then complete with offline contacts */ |
| 137 |
for (gl = server->roster; gl != NULL; gl = gl->next) { |
| 138 |
for (ul = ((XMPP_ROSTER_GROUP_REC *)gl->data)->users; |
| 139 |
ul != NULL ; ul = ul->next) { |
| 140 |
user = (XMPP_ROSTER_USER_REC *)ul->data; |
| 141 |
if ((!pass2 && user->resources == NULL) |
| 142 |
|| (pass2 && user->resources != NULL)) |
| 143 |
continue; |
| 144 |
if (complete_names && user->name != NULL |
| 145 |
&& g_ascii_strncasecmp(user->name, nick, len) == 0) |
| 146 |
list = g_list_prepend(list, quoted ? |
| 147 |
quoted_if_space(user->name, NULL) : |
| 148 |
g_strdup(user->name)); |
| 149 |
if (g_ascii_strncasecmp(user->jid, nick, len) == 0) |
| 150 |
list = g_list_prepend(list, quoted ? |
| 151 |
quoted_if_space(user->jid, NULL) : |
| 152 |
g_strdup(user->jid)); |
| 153 |
} |
| 154 |
} |
| 155 |
if ((pass2 = !pass2)) |
| 156 |
goto again; |
| 157 |
/* TODO: rewrite this function */ |
| 158 |
return list; |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
sig_complete_word(GList **list, WINDOW_REC *window, const char *word, |
| 163 |
const char *linestart, int *want_space) |
| 164 |
{ |
| 165 |
XMPP_SERVER_REC *server; |
| 166 |
|
| 167 |
g_return_if_fail(list != NULL); |
| 168 |
g_return_if_fail(window != NULL); |
| 169 |
g_return_if_fail(word != NULL); |
| 170 |
if ((server = XMPP_SERVER(window->active_server)) == NULL) |
| 171 |
return; |
| 172 |
if (g_ascii_strncasecmp(linestart, |
| 173 |
settings_get_str("cmdchars"), 1) == 0) { |
| 174 |
*list = g_list_concat(*list, get_nicks(server, *word == '"' ? |
| 175 |
word+1 : word , TRUE, TRUE)); |
| 176 |
} else if (!IS_CHANNEL(window->active)) |
| 177 |
*list = g_list_concat(*list, get_nicks(server, word, FALSE, |
| 178 |
TRUE)); |
| 179 |
} |
| 180 |
|
| 181 |
static void |
| 182 |
sig_complete_command_roster_group(GList **list, WINDOW_REC *window, |
| 183 |
const char *word, const char *args, int *want_space) |
| 184 |
{ |
| 185 |
GSList *gl; |
| 186 |
XMPP_SERVER_REC *server; |
| 187 |
XMPP_ROSTER_GROUP_REC *group; |
| 188 |
int len; |
| 189 |
char **tmp; |
| 190 |
|
| 191 |
g_return_if_fail(list != NULL); |
| 192 |
g_return_if_fail(window != NULL); |
| 193 |
g_return_if_fail(word != NULL); |
| 194 |
g_return_if_fail(args != NULL); |
| 195 |
server = XMPP_SERVER(window->active_server); |
| 196 |
if (server == NULL) |
| 197 |
return; |
| 198 |
len = strlen(word); |
| 199 |
tmp = g_strsplit(args, " ", 2); |
| 200 |
/* complete nicks */ |
| 201 |
if (tmp[0] == NULL) |
| 202 |
*list = g_list_concat(*list, get_nicks(server, *word == '"' ? |
| 203 |
word+1 : word , TRUE, FALSE)); |
| 204 |
/* complete groups */ |
| 205 |
else if (tmp[0] != NULL && tmp[1] == NULL) { |
| 206 |
for (gl = server->roster; gl != NULL; gl = gl->next) { |
| 207 |
group = (XMPP_ROSTER_GROUP_REC *)gl->data; |
| 208 |
if (group->name != NULL && |
| 209 |
g_ascii_strncasecmp(word, group->name, len) == 0) |
| 210 |
*list = g_list_append(*list, |
| 211 |
g_strdup(group->name)); |
| 212 |
} |
| 213 |
} |
| 214 |
g_strfreev(tmp); |
| 215 |
if (*list != NULL) |
| 216 |
signal_stop(); |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
sig_complete_command_roster_others(GList **list, WINDOW_REC *window, |
| 221 |
const char *word, const char *args, int *want_space) |
| 222 |
{ |
| 223 |
XMPP_SERVER_REC *server; |
| 224 |
char **tmp; |
| 225 |
|
| 226 |
g_return_if_fail(list != NULL); |
| 227 |
g_return_if_fail(window != NULL); |
| 228 |
g_return_if_fail(word != NULL); |
| 229 |
g_return_if_fail(args != NULL); |
| 230 |
if ((server = XMPP_SERVER(window->active_server)) == NULL) |
| 231 |
return; |
| 232 |
tmp = g_strsplit(args, " ", 2); |
| 233 |
/* complete nicks */ |
| 234 |
if (tmp[0] == NULL) |
| 235 |
*list = g_list_concat(*list, get_nicks(server, *word == '"' ? |
| 236 |
word+1 : word , TRUE, FALSE)); |
| 237 |
g_strfreev(tmp); |
| 238 |
if (*list != NULL) |
| 239 |
signal_stop(); |
| 240 |
} |
| 241 |
|
| 242 |
static void |
| 243 |
sig_complete_command_presence(GList **list, WINDOW_REC *window, |
| 244 |
const char *word, const char *args, int *want_space) |
| 245 |
{ |
| 246 |
XMPP_SERVER_REC *server; |
| 247 |
char **tmp; |
| 248 |
|
| 249 |
g_return_if_fail(list != NULL); |
| 250 |
g_return_if_fail(window != NULL); |
| 251 |
g_return_if_fail(word != NULL); |
| 252 |
g_return_if_fail(args != NULL); |
| 253 |
if ((server = XMPP_SERVER(window->active_server)) == NULL) |
| 254 |
return; |
| 255 |
tmp = g_strsplit(args, " ", 2); |
| 256 |
/* complete nicks */ |
| 257 |
if (tmp[0] == NULL) |
| 258 |
*list = g_list_concat(*list, get_jids(server, *word == '"' ? |
| 259 |
word+1 : word)); |
| 260 |
g_strfreev(tmp); |
| 261 |
if (*list != NULL) |
| 262 |
signal_stop(); |
| 263 |
} |
| 264 |
|
| 265 |
static GList * |
| 266 |
get_channels(XMPP_SERVER_REC *server, const char *word) |
| 267 |
{ |
| 268 |
GSList *tmp; |
| 269 |
GList *list; |
| 270 |
CHANNEL_REC *channel; |
| 271 |
CHANNEL_SETUP_REC *channel_setup; |
| 272 |
int len; |
| 273 |
|
| 274 |
g_return_val_if_fail(IS_XMPP_SERVER(server), NULL); |
| 275 |
g_return_val_if_fail(word != NULL, NULL); |
| 276 |
len = strlen(word); |
| 277 |
list = NULL; |
| 278 |
for (tmp = server->channels; tmp != NULL; tmp = tmp->next) { |
| 279 |
channel = CHANNEL(tmp->data); |
| 280 |
if (channel != NULL && |
| 281 |
g_ascii_strncasecmp(channel->name, word, len) == 0) |
| 282 |
list = g_list_append(list, g_strdup(channel->name)); |
| 283 |
} |
| 284 |
for (tmp = setupchannels; tmp != NULL; tmp = tmp->next) { |
| 285 |
channel_setup = tmp->data; |
| 286 |
if ((PROTO_CHECK_CAST(channel_setup, CHANNEL_SETUP_REC, |
| 287 |
chat_type, XMPP_PROTOCOL_NAME) |
| 288 |
|| *channel_setup->name != '#') |
| 289 |
&& g_ascii_strncasecmp(channel_setup->name, word, len) == 0 |
| 290 |
&& glist_find_string(list, channel_setup->name) == NULL) |
| 291 |
list = g_list_append(list, |
| 292 |
g_strdup(channel_setup->name)); |
| 293 |
} |
| 294 |
return list; |
| 295 |
} |
| 296 |
|
| 297 |
static void |
| 298 |
sig_complete_command_channels(GList **list, WINDOW_REC *window, |
| 299 |
const char *word, const char *args, int *want_space) |
| 300 |
{ |
| 301 |
XMPP_SERVER_REC *server; |
| 302 |
|
| 303 |
g_return_if_fail(list != NULL); |
| 304 |
g_return_if_fail(window != NULL); |
| 305 |
g_return_if_fail(word != NULL); |
| 306 |
server = XMPP_SERVER(window->active_server); |
| 307 |
if (server == NULL) |
| 308 |
return; |
| 309 |
*list = get_channels(server, word); |
| 310 |
if (*list != NULL) |
| 311 |
signal_stop(); |
| 312 |
} |
| 313 |
|
| 314 |
static void |
| 315 |
sig_complete_command_invite(GList **list, WINDOW_REC *window, |
| 316 |
const char *word, const char *args, int *want_space) |
| 317 |
{ |
| 318 |
XMPP_SERVER_REC *server; |
| 319 |
char **tmp; |
| 320 |
|
| 321 |
g_return_if_fail(list != NULL); |
| 322 |
g_return_if_fail(window != NULL); |
| 323 |
g_return_if_fail(word != NULL); |
| 324 |
server = XMPP_SERVER(window->active_server); |
| 325 |
if (server == NULL) |
| 326 |
return; |
| 327 |
/* complete channels */ |
| 328 |
tmp = g_strsplit(args, " ", 2); |
| 329 |
if (tmp[0] != NULL && tmp[1] == NULL) |
| 330 |
*list = get_channels(server, word); |
| 331 |
g_strfreev(tmp); |
| 332 |
if (*list != NULL) |
| 333 |
signal_stop(); |
| 334 |
} |
| 335 |
|
| 336 |
static void |
| 337 |
sig_complete_command_away(GList **list, WINDOW_REC *window, |
| 338 |
const char *word, const char *args, int *want_space) |
| 339 |
{ |
| 340 |
XMPP_SERVER_REC *server; |
| 341 |
int len; |
| 342 |
|
| 343 |
g_return_if_fail(list != NULL); |
| 344 |
g_return_if_fail(window != NULL); |
| 345 |
g_return_if_fail(word != NULL); |
| 346 |
server = XMPP_SERVER(window->active_server); |
| 347 |
if (server == NULL) |
| 348 |
return; |
| 349 |
len = strlen(word); |
| 350 |
if (g_ascii_strncasecmp(word, |
| 351 |
xmpp_presence_show[XMPP_PRESENCE_AWAY], len) == 0) |
| 352 |
*list = g_list_append(*list, |
| 353 |
g_strdup(xmpp_presence_show[XMPP_PRESENCE_AWAY])); |
| 354 |
if (g_ascii_strncasecmp(word, |
| 355 |
xmpp_presence_show[XMPP_PRESENCE_XA], len) == 0) |
| 356 |
*list = g_list_append(*list, |
| 357 |
g_strdup(xmpp_presence_show[XMPP_PRESENCE_XA])); |
| 358 |
if (g_ascii_strncasecmp(word, |
| 359 |
xmpp_presence_show[XMPP_PRESENCE_DND], len) == 0) |
| 360 |
*list = g_list_append(*list, |
| 361 |
g_strdup(xmpp_presence_show[XMPP_PRESENCE_DND])); |
| 362 |
if (g_ascii_strncasecmp(word, |
| 363 |
xmpp_presence_show[XMPP_PRESENCE_CHAT], len) == 0) |
| 364 |
*list = g_list_append(*list, |
| 365 |
g_strdup(xmpp_presence_show[XMPP_PRESENCE_CHAT])); |
| 366 |
if (g_ascii_strncasecmp(word, |
| 367 |
xmpp_presence_show[XMPP_PRESENCE_ONLINE], len) == 0) |
| 368 |
*list = g_list_append(*list, g_strdup("online")); |
| 369 |
signal_stop(); |
| 370 |
} |
| 371 |
|
| 372 |
void |
| 373 |
xmpp_completion_init(void) |
| 374 |
{ |
| 375 |
signal_add("complete word", sig_complete_word); |
| 376 |
signal_add("complete command roster group", |
| 377 |
sig_complete_command_roster_group); |
| 378 |
signal_add("complete command roster add", |
| 379 |
sig_complete_command_roster_others); |
| 380 |
signal_add("complete command roster remove", |
| 381 |
sig_complete_command_roster_others); |
| 382 |
signal_add("complete command roster name", |
| 383 |
sig_complete_command_roster_others); |
| 384 |
signal_add("complete command presence accept", |
| 385 |
sig_complete_command_presence); |
| 386 |
signal_add("complete command presence deny", |
| 387 |
sig_complete_command_presence); |
| 388 |
signal_add("complete command presence subscribe", |
| 389 |
sig_complete_command_presence); |
| 390 |
signal_add("complete command presence unsubscribe", |
| 391 |
sig_complete_command_presence); |
| 392 |
signal_add("complete command join", sig_complete_command_channels); |
| 393 |
signal_add("complete command part", sig_complete_command_channels); |
| 394 |
signal_add("complete command invite", sig_complete_command_invite); |
| 395 |
signal_add("complete command away", sig_complete_command_away); |
| 396 |
} |
| 397 |
|
| 398 |
void |
| 399 |
xmpp_completion_deinit(void) |
| 400 |
{ |
| 401 |
signal_remove("complete word", sig_complete_word); |
| 402 |
signal_remove("complete command roster group", |
| 403 |
sig_complete_command_roster_group); |
| 404 |
signal_remove("complete command roster add", |
| 405 |
sig_complete_command_roster_others); |
| 406 |
signal_remove("complete command roster remove", |
| 407 |
sig_complete_command_roster_others); |
| 408 |
signal_remove("complete command roster name", |
| 409 |
sig_complete_command_roster_others); |
| 410 |
signal_remove("complete command presence accept", |
| 411 |
sig_complete_command_presence); |
| 412 |
signal_remove("complete command presence deny", |
| 413 |
sig_complete_command_presence); |
| 414 |
signal_remove("complete command presence subscribe", |
| 415 |
sig_complete_command_presence); |
| 416 |
signal_remove("complete command presence unsubscribe", |
| 417 |
sig_complete_command_presence); |
| 418 |
signal_remove("complete command join", sig_complete_command_channels); |
| 419 |
signal_remove("complete command part", sig_complete_command_channels); |
| 420 |
signal_remove("complete command invite", sig_complete_command_invite); |
| 421 |
signal_remove("complete command away", sig_complete_command_away); |
| 422 |
} |