[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[0e788f5] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* |
---|
| 8 | * nogaim |
---|
| 9 | * |
---|
| 10 | * Gaim without gaim - for BitlBee |
---|
| 11 | * |
---|
| 12 | * This file contains functions called by the Gaim IM-modules. It's written |
---|
| 13 | * from scratch for BitlBee and doesn't contain any code from Gaim anymore |
---|
| 14 | * (except for the function names). |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | This program is free software; you can redistribute it and/or modify |
---|
| 19 | it under the terms of the GNU General Public License as published by |
---|
| 20 | the Free Software Foundation; either version 2 of the License, or |
---|
| 21 | (at your option) any later version. |
---|
| 22 | |
---|
| 23 | This program is distributed in the hope that it will be useful, |
---|
| 24 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 26 | GNU General Public License for more details. |
---|
| 27 | |
---|
| 28 | You should have received a copy of the GNU General Public License with |
---|
| 29 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
[6f10697] | 30 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 31 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 32 | */ |
---|
| 33 | |
---|
| 34 | #define BITLBEE_CORE |
---|
| 35 | #include <ctype.h> |
---|
| 36 | |
---|
[4cf80bb] | 37 | #include "nogaim.h" |
---|
| 38 | |
---|
[b7d3cc34] | 39 | GSList *connections; |
---|
| 40 | |
---|
[65e2ce1] | 41 | #ifdef WITH_PLUGINS |
---|
[7b23afd] | 42 | gboolean load_plugin(char *path) |
---|
| 43 | { |
---|
| 44 | void (*init_function) (void); |
---|
[5ebff60] | 45 | |
---|
[7b23afd] | 46 | GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY); |
---|
| 47 | |
---|
[5ebff60] | 48 | if (!mod) { |
---|
[8ad90fb] | 49 | log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error()); |
---|
[7b23afd] | 50 | return FALSE; |
---|
| 51 | } |
---|
| 52 | |
---|
[5ebff60] | 53 | if (!g_module_symbol(mod, "init_plugin", (gpointer *) &init_function)) { |
---|
[7b23afd] | 54 | log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path); |
---|
| 55 | return FALSE; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | init_function(); |
---|
| 59 | |
---|
| 60 | return TRUE; |
---|
| 61 | } |
---|
[b7d3cc34] | 62 | |
---|
[65e2ce1] | 63 | void load_plugins(void) |
---|
| 64 | { |
---|
| 65 | GDir *dir; |
---|
| 66 | GError *error = NULL; |
---|
| 67 | |
---|
[4bfca70] | 68 | dir = g_dir_open(global.conf->plugindir, 0, &error); |
---|
[65e2ce1] | 69 | |
---|
| 70 | if (dir) { |
---|
| 71 | const gchar *entry; |
---|
| 72 | char *path; |
---|
| 73 | |
---|
| 74 | while ((entry = g_dir_read_name(dir))) { |
---|
[4bfca70] | 75 | path = g_build_filename(global.conf->plugindir, entry, NULL); |
---|
[5ebff60] | 76 | if (!path) { |
---|
[65e2ce1] | 77 | log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry); |
---|
| 78 | continue; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | load_plugin(path); |
---|
| 82 | |
---|
| 83 | g_free(path); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | g_dir_close(dir); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | #endif |
---|
[b7d3cc34] | 90 | |
---|
[7b23afd] | 91 | GList *protocols = NULL; |
---|
[ad9ac5d] | 92 | GList *disabled_protocols = NULL; |
---|
[5ebff60] | 93 | |
---|
| 94 | void register_protocol(struct prpl *p) |
---|
[7b23afd] | 95 | { |
---|
[90cd6c4] | 96 | int i; |
---|
| 97 | gboolean refused = global.conf->protocols != NULL; |
---|
[5ebff60] | 98 | |
---|
| 99 | for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++) { |
---|
| 100 | if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0) { |
---|
[90cd6c4] | 101 | refused = FALSE; |
---|
[5ebff60] | 102 | } |
---|
| 103 | } |
---|
[90cd6c4] | 104 | |
---|
[5ebff60] | 105 | if (refused) { |
---|
[ad9ac5d] | 106 | disabled_protocols = g_list_append(disabled_protocols, p); |
---|
[5ebff60] | 107 | } else { |
---|
[90cd6c4] | 108 | protocols = g_list_append(protocols, p); |
---|
[5ebff60] | 109 | } |
---|
[7b23afd] | 110 | } |
---|
| 111 | |
---|
[ad9ac5d] | 112 | static int proto_name_cmp(const void *proto_, const void *name) |
---|
[7b23afd] | 113 | { |
---|
[ad9ac5d] | 114 | const struct prpl *proto = proto_; |
---|
| 115 | return g_strcasecmp(proto->name, name); |
---|
| 116 | } |
---|
[5ebff60] | 117 | |
---|
[ad9ac5d] | 118 | struct prpl *find_protocol(const char *name) |
---|
| 119 | { |
---|
| 120 | GList *gl = g_list_find_custom(protocols, name, proto_name_cmp); |
---|
| 121 | return gl ? gl->data: NULL; |
---|
| 122 | } |
---|
[5ebff60] | 123 | |
---|
[ad9ac5d] | 124 | gboolean is_protocol_disabled(const char *name) |
---|
| 125 | { |
---|
| 126 | return g_list_find_custom(disabled_protocols, name, proto_name_cmp) != NULL; |
---|
[7b23afd] | 127 | } |
---|
| 128 | |
---|
[b7d3cc34] | 129 | void nogaim_init() |
---|
| 130 | { |
---|
[0da65d5] | 131 | extern void msn_initmodule(); |
---|
| 132 | extern void oscar_initmodule(); |
---|
| 133 | extern void byahoo_initmodule(); |
---|
| 134 | extern void jabber_initmodule(); |
---|
[1b221e0] | 135 | extern void twitter_initmodule(); |
---|
[796da03] | 136 | extern void purple_initmodule(); |
---|
[7b23afd] | 137 | |
---|
[b7d3cc34] | 138 | #ifdef WITH_MSN |
---|
[0da65d5] | 139 | msn_initmodule(); |
---|
[b7d3cc34] | 140 | #endif |
---|
| 141 | |
---|
| 142 | #ifdef WITH_OSCAR |
---|
[0da65d5] | 143 | oscar_initmodule(); |
---|
[b7d3cc34] | 144 | #endif |
---|
[5ebff60] | 145 | |
---|
[b7d3cc34] | 146 | #ifdef WITH_YAHOO |
---|
[0da65d5] | 147 | byahoo_initmodule(); |
---|
[b7d3cc34] | 148 | #endif |
---|
[5ebff60] | 149 | |
---|
[b7d3cc34] | 150 | #ifdef WITH_JABBER |
---|
[0da65d5] | 151 | jabber_initmodule(); |
---|
[b7d3cc34] | 152 | #endif |
---|
[7b23afd] | 153 | |
---|
[1b221e0] | 154 | #ifdef WITH_TWITTER |
---|
| 155 | twitter_initmodule(); |
---|
| 156 | #endif |
---|
| 157 | |
---|
[796da03] | 158 | #ifdef WITH_PURPLE |
---|
| 159 | purple_initmodule(); |
---|
| 160 | #endif |
---|
[7b23afd] | 161 | |
---|
[65e2ce1] | 162 | #ifdef WITH_PLUGINS |
---|
| 163 | load_plugins(); |
---|
[b7d3cc34] | 164 | #endif |
---|
| 165 | } |
---|
| 166 | |
---|
[5ebff60] | 167 | GSList *get_connections() |
---|
| 168 | { |
---|
| 169 | return connections; |
---|
| 170 | } |
---|
[b7d3cc34] | 171 | |
---|
[5ebff60] | 172 | struct im_connection *imcb_new(account_t *acc) |
---|
[b7d3cc34] | 173 | { |
---|
[0da65d5] | 174 | struct im_connection *ic; |
---|
[5ebff60] | 175 | |
---|
| 176 | ic = g_new0(struct im_connection, 1); |
---|
| 177 | |
---|
[81e04e1] | 178 | ic->bee = acc->bee; |
---|
[0da65d5] | 179 | ic->acc = acc; |
---|
| 180 | acc->ic = ic; |
---|
[5ebff60] | 181 | |
---|
| 182 | connections = g_slist_append(connections, ic); |
---|
| 183 | |
---|
| 184 | return(ic); |
---|
[b7d3cc34] | 185 | } |
---|
| 186 | |
---|
[5ebff60] | 187 | void imc_free(struct im_connection *ic) |
---|
[b7d3cc34] | 188 | { |
---|
| 189 | account_t *a; |
---|
[5ebff60] | 190 | |
---|
[b7d3cc34] | 191 | /* Destroy the pointer to this connection from the account list */ |
---|
[5ebff60] | 192 | for (a = ic->bee->accounts; a; a = a->next) { |
---|
| 193 | if (a->ic == ic) { |
---|
[0da65d5] | 194 | a->ic = NULL; |
---|
[b7d3cc34] | 195 | break; |
---|
| 196 | } |
---|
[5ebff60] | 197 | } |
---|
| 198 | |
---|
| 199 | connections = g_slist_remove(connections, ic); |
---|
| 200 | g_free(ic); |
---|
[b7d3cc34] | 201 | } |
---|
| 202 | |
---|
[5ebff60] | 203 | static void serv_got_crap(struct im_connection *ic, char *format, ...) |
---|
[b7d3cc34] | 204 | { |
---|
| 205 | va_list params; |
---|
[e27661d] | 206 | char *text; |
---|
[dfde8e0] | 207 | account_t *a; |
---|
[5ebff60] | 208 | |
---|
| 209 | va_start(params, format); |
---|
| 210 | text = g_strdup_vprintf(format, params); |
---|
| 211 | va_end(params); |
---|
| 212 | |
---|
| 213 | if ((g_strcasecmp(set_getstr(&ic->bee->set, "strip_html"), "always") == 0) || |
---|
| 214 | ((ic->flags & OPT_DOES_HTML) && set_getbool(&ic->bee->set, "strip_html"))) { |
---|
| 215 | strip_html(text); |
---|
| 216 | } |
---|
| 217 | |
---|
[dfde8e0] | 218 | /* Try to find a different connection on the same protocol. */ |
---|
[5ebff60] | 219 | for (a = ic->bee->accounts; a; a = a->next) { |
---|
| 220 | if (a->prpl == ic->acc->prpl && a->ic != ic) { |
---|
[dfde8e0] | 221 | break; |
---|
[5ebff60] | 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
[e27661d] | 225 | /* If we found one, include the screenname in the message. */ |
---|
[5ebff60] | 226 | if (a) { |
---|
[81e04e1] | 227 | /* FIXME(wilmer): ui_log callback or so */ |
---|
[5ebff60] | 228 | irc_rootmsg(ic->bee->ui_data, "%s - %s", ic->acc->tag, text); |
---|
| 229 | } else { |
---|
| 230 | irc_rootmsg(ic->bee->ui_data, "%s - %s", ic->acc->prpl->name, text); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | g_free(text); |
---|
[b7d3cc34] | 234 | } |
---|
| 235 | |
---|
[5ebff60] | 236 | void imcb_log(struct im_connection *ic, char *format, ...) |
---|
[aef4828] | 237 | { |
---|
| 238 | va_list params; |
---|
| 239 | char *text; |
---|
[5ebff60] | 240 | |
---|
| 241 | va_start(params, format); |
---|
| 242 | text = g_strdup_vprintf(format, params); |
---|
| 243 | va_end(params); |
---|
| 244 | |
---|
| 245 | if (ic->flags & OPT_LOGGED_IN) { |
---|
| 246 | serv_got_crap(ic, "%s", text); |
---|
| 247 | } else { |
---|
| 248 | serv_got_crap(ic, "Logging in: %s", text); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | g_free(text); |
---|
[aef4828] | 252 | } |
---|
| 253 | |
---|
[5ebff60] | 254 | void imcb_error(struct im_connection *ic, char *format, ...) |
---|
[aef4828] | 255 | { |
---|
| 256 | va_list params; |
---|
| 257 | char *text; |
---|
[5ebff60] | 258 | |
---|
| 259 | va_start(params, format); |
---|
| 260 | text = g_strdup_vprintf(format, params); |
---|
| 261 | va_end(params); |
---|
| 262 | |
---|
| 263 | if (ic->flags & OPT_LOGGED_IN) { |
---|
| 264 | serv_got_crap(ic, "Error: %s", text); |
---|
| 265 | } else { |
---|
| 266 | serv_got_crap(ic, "Login error: %s", text); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | g_free(text); |
---|
[aef4828] | 270 | } |
---|
| 271 | |
---|
[5ebff60] | 272 | static gboolean send_keepalive(gpointer d, gint fd, b_input_condition cond) |
---|
[b7d3cc34] | 273 | { |
---|
[0da65d5] | 274 | struct im_connection *ic = d; |
---|
[5ebff60] | 275 | |
---|
| 276 | if ((ic->flags & OPT_PONGS) && !(ic->flags & OPT_PONGED)) { |
---|
[e132b60] | 277 | /* This protocol is expected to ack keepalives and hasn't |
---|
| 278 | since the last time we were here. */ |
---|
[5ebff60] | 279 | imcb_error(ic, "Connection timeout"); |
---|
| 280 | imc_logout(ic, TRUE); |
---|
[e132b60] | 281 | return FALSE; |
---|
| 282 | } |
---|
| 283 | ic->flags &= ~OPT_PONGED; |
---|
[5ebff60] | 284 | |
---|
| 285 | if (ic->acc->prpl->keepalive) { |
---|
| 286 | ic->acc->prpl->keepalive(ic); |
---|
| 287 | } |
---|
| 288 | |
---|
[b7d3cc34] | 289 | return TRUE; |
---|
| 290 | } |
---|
| 291 | |
---|
[5ebff60] | 292 | void start_keepalives(struct im_connection *ic, int interval) |
---|
[e132b60] | 293 | { |
---|
[5ebff60] | 294 | b_event_remove(ic->keepalive); |
---|
| 295 | ic->keepalive = b_timeout_add(interval, send_keepalive, ic); |
---|
| 296 | |
---|
[e132b60] | 297 | /* Connecting successfully counts as a first successful pong. */ |
---|
[5ebff60] | 298 | if (ic->flags & OPT_PONGS) { |
---|
[e132b60] | 299 | ic->flags |= OPT_PONGED; |
---|
[5ebff60] | 300 | } |
---|
[e132b60] | 301 | } |
---|
| 302 | |
---|
[5ebff60] | 303 | void imcb_connected(struct im_connection *ic) |
---|
[b7d3cc34] | 304 | { |
---|
| 305 | /* MSN servers sometimes redirect you to a different server and do |
---|
[84c1a0a] | 306 | the whole login sequence again, so these "late" calls to this |
---|
[b7d3cc34] | 307 | function should be handled correctly. (IOW, ignored) */ |
---|
[5ebff60] | 308 | if (ic->flags & OPT_LOGGED_IN) { |
---|
[b7d3cc34] | 309 | return; |
---|
[5ebff60] | 310 | } |
---|
[11e7828] | 311 | |
---|
[5ebff60] | 312 | if (ic->acc->flags & ACC_FLAG_LOCAL) { |
---|
[11e7828] | 313 | GHashTableIter nicks; |
---|
| 314 | gpointer k, v; |
---|
[5ebff60] | 315 | g_hash_table_iter_init(&nicks, ic->acc->nicks); |
---|
| 316 | while (g_hash_table_iter_next(&nicks, &k, &v)) { |
---|
| 317 | ic->acc->prpl->add_buddy(ic, (char *) k, NULL); |
---|
[11e7828] | 318 | } |
---|
| 319 | } |
---|
[5ebff60] | 320 | |
---|
| 321 | imcb_log(ic, "Logged in"); |
---|
| 322 | |
---|
[0da65d5] | 323 | ic->flags |= OPT_LOGGED_IN; |
---|
[5ebff60] | 324 | start_keepalives(ic, 60000); |
---|
| 325 | |
---|
[58adb7e] | 326 | /* Necessary to send initial presence status, even if we're not away. */ |
---|
[5ebff60] | 327 | imc_away_send_update(ic); |
---|
| 328 | |
---|
[280e655] | 329 | /* Apparently we're connected successfully, so reset the |
---|
| 330 | exponential backoff timer. */ |
---|
| 331 | ic->acc->auto_reconnect_delay = 0; |
---|
[5ebff60] | 332 | |
---|
| 333 | if (ic->bee->ui->imc_connected) { |
---|
| 334 | ic->bee->ui->imc_connected(ic); |
---|
| 335 | } |
---|
[b7d3cc34] | 336 | } |
---|
| 337 | |
---|
[5ebff60] | 338 | gboolean auto_reconnect(gpointer data, gint fd, b_input_condition cond) |
---|
[b7d3cc34] | 339 | { |
---|
| 340 | account_t *a = data; |
---|
[5ebff60] | 341 | |
---|
[b7d3cc34] | 342 | a->reconnect = 0; |
---|
[5ebff60] | 343 | account_on(a->bee, a); |
---|
| 344 | |
---|
| 345 | return(FALSE); /* Only have to run the timeout once */ |
---|
[b7d3cc34] | 346 | } |
---|
| 347 | |
---|
[5ebff60] | 348 | void cancel_auto_reconnect(account_t *a) |
---|
[b7d3cc34] | 349 | { |
---|
[5ebff60] | 350 | b_event_remove(a->reconnect); |
---|
[b7d3cc34] | 351 | a->reconnect = 0; |
---|
| 352 | } |
---|
| 353 | |
---|
[5ebff60] | 354 | void imc_logout(struct im_connection *ic, int allow_reconnect) |
---|
[b7d3cc34] | 355 | { |
---|
[81e04e1] | 356 | bee_t *bee = ic->bee; |
---|
[b7d3cc34] | 357 | account_t *a; |
---|
[81e04e1] | 358 | GSList *l; |
---|
[4230221] | 359 | int delay; |
---|
[5ebff60] | 360 | |
---|
[8d74291] | 361 | /* Nested calls might happen sometimes, this is probably the best |
---|
| 362 | place to catch them. */ |
---|
[5ebff60] | 363 | if (ic->flags & OPT_LOGGING_OUT) { |
---|
[8d74291] | 364 | return; |
---|
[5ebff60] | 365 | } else { |
---|
[0da65d5] | 366 | ic->flags |= OPT_LOGGING_OUT; |
---|
[5ebff60] | 367 | } |
---|
| 368 | |
---|
| 369 | if (ic->bee->ui->imc_disconnected) { |
---|
| 370 | ic->bee->ui->imc_disconnected(ic); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | imcb_log(ic, "Signing off.."); |
---|
| 374 | |
---|
[0dd6570] | 375 | /* TBH I don't remember anymore why I didn't just use ic->acc... */ |
---|
[5ebff60] | 376 | for (a = bee->accounts; a; a = a->next) { |
---|
| 377 | if (a->ic == ic) { |
---|
[0dd6570] | 378 | break; |
---|
[5ebff60] | 379 | } |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | if (a && !allow_reconnect && !(ic->flags & OPT_LOGGED_IN) && |
---|
| 383 | set_getbool(&a->set, "oauth")) { |
---|
[0dd6570] | 384 | /* If this account supports OAuth, we're not logged in yet and |
---|
| 385 | not allowed to retry, assume there were auth issues. Give a |
---|
| 386 | helpful message on what might be necessary to fix this. */ |
---|
[5ebff60] | 387 | imcb_log(ic, "If you're having problems logging in, try re-requesting " |
---|
| 388 | "an OAuth token: account %s set password \"\"", a->tag); |
---|
[0dd6570] | 389 | } |
---|
[5ebff60] | 390 | |
---|
| 391 | for (l = bee->users; l; ) { |
---|
[81e04e1] | 392 | bee_user_t *bu = l->data; |
---|
[eabc9d2] | 393 | GSList *next = l->next; |
---|
[5ebff60] | 394 | |
---|
| 395 | if (bu->ic == ic) { |
---|
| 396 | bee_user_free(bee, bu); |
---|
| 397 | } |
---|
| 398 | |
---|
[eabc9d2] | 399 | l = next; |
---|
[b7d3cc34] | 400 | } |
---|
[5ebff60] | 401 | |
---|
| 402 | b_event_remove(ic->keepalive); |
---|
[be7a180] | 403 | ic->keepalive = 0; |
---|
[5ebff60] | 404 | ic->acc->prpl->logout(ic); |
---|
| 405 | b_event_remove(ic->inpa); |
---|
| 406 | |
---|
| 407 | g_free(ic->away); |
---|
[be7a180] | 408 | ic->away = NULL; |
---|
[5ebff60] | 409 | |
---|
| 410 | query_del_by_conn((irc_t *) ic->bee->ui_data, ic); |
---|
| 411 | |
---|
| 412 | if (!a) { |
---|
[b7d3cc34] | 413 | /* Uhm... This is very sick. */ |
---|
[5ebff60] | 414 | } else if (allow_reconnect && set_getbool(&bee->set, "auto_reconnect") && |
---|
| 415 | set_getbool(&a->set, "auto_reconnect") && |
---|
| 416 | (delay = account_reconnect_delay(a)) > 0) { |
---|
| 417 | imcb_log(ic, "Reconnecting in %d seconds..", delay); |
---|
| 418 | a->reconnect = b_timeout_add(delay * 1000, auto_reconnect, a); |
---|
[b7d3cc34] | 419 | } |
---|
[5ebff60] | 420 | |
---|
| 421 | imc_free(ic); |
---|
[b7d3cc34] | 422 | } |
---|
| 423 | |
---|
[5ebff60] | 424 | void imcb_ask(struct im_connection *ic, char *msg, void *data, |
---|
| 425 | query_callback doit, query_callback dont) |
---|
[b7d3cc34] | 426 | { |
---|
[5ebff60] | 427 | query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, g_free, data); |
---|
[b7d3cc34] | 428 | } |
---|
| 429 | |
---|
[5ebff60] | 430 | void imcb_ask_with_free(struct im_connection *ic, char *msg, void *data, |
---|
| 431 | query_callback doit, query_callback dont, query_callback myfree) |
---|
[d0527c1] | 432 | { |
---|
[5ebff60] | 433 | query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, myfree, data); |
---|
[d0527c1] | 434 | } |
---|
| 435 | |
---|
[5ebff60] | 436 | void imcb_add_buddy(struct im_connection *ic, const char *handle, const char *group) |
---|
[b7d3cc34] | 437 | { |
---|
[81e04e1] | 438 | bee_user_t *bu; |
---|
| 439 | bee_t *bee = ic->bee; |
---|
[8b61469] | 440 | bee_group_t *oldg; |
---|
[5ebff60] | 441 | |
---|
| 442 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
| 443 | bu = bee_user_new(bee, ic, handle, 0); |
---|
| 444 | } |
---|
| 445 | |
---|
[8b61469] | 446 | oldg = bu->group; |
---|
[5ebff60] | 447 | bu->group = bee_group_by_name(bee, group, TRUE); |
---|
| 448 | |
---|
| 449 | if (bee->ui->user_group && bu->group != oldg) { |
---|
| 450 | bee->ui->user_group(bee, bu); |
---|
| 451 | } |
---|
[b7d3cc34] | 452 | } |
---|
| 453 | |
---|
[5ebff60] | 454 | void imcb_rename_buddy(struct im_connection *ic, const char *handle, const char *fullname) |
---|
[b7d3cc34] | 455 | { |
---|
[1d39159] | 456 | bee_t *bee = ic->bee; |
---|
[5ebff60] | 457 | bee_user_t *bu = bee_user_by_handle(bee, ic, handle); |
---|
| 458 | |
---|
| 459 | if (!bu || !fullname) { |
---|
| 460 | return; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | if (!bu->fullname || strcmp(bu->fullname, fullname) != 0) { |
---|
| 464 | g_free(bu->fullname); |
---|
| 465 | bu->fullname = g_strdup(fullname); |
---|
| 466 | |
---|
| 467 | if (bee->ui->user_fullname) { |
---|
| 468 | bee->ui->user_fullname(bee, bu); |
---|
| 469 | } |
---|
[b7d3cc34] | 470 | } |
---|
| 471 | } |
---|
| 472 | |
---|
[5ebff60] | 473 | void imcb_remove_buddy(struct im_connection *ic, const char *handle, char *group) |
---|
[998b103] | 474 | { |
---|
[5ebff60] | 475 | bee_user_free(ic->bee, bee_user_by_handle(ic->bee, ic, handle)); |
---|
[998b103] | 476 | } |
---|
| 477 | |
---|
[d06eabf] | 478 | /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM |
---|
| 479 | modules to suggest a nickname for a handle. */ |
---|
[5ebff60] | 480 | void imcb_buddy_nick_hint(struct im_connection *ic, const char *handle, const char *nick) |
---|
[d06eabf] | 481 | { |
---|
[6ef9065] | 482 | bee_t *bee = ic->bee; |
---|
[5ebff60] | 483 | bee_user_t *bu = bee_user_by_handle(bee, ic, handle); |
---|
| 484 | |
---|
| 485 | if (!bu || !nick) { |
---|
| 486 | return; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | g_free(bu->nick); |
---|
| 490 | bu->nick = g_strdup(nick); |
---|
| 491 | |
---|
| 492 | if (bee->ui->user_nick_hint) { |
---|
| 493 | bee->ui->user_nick_hint(bee, bu, nick); |
---|
| 494 | } |
---|
[d06eabf] | 495 | } |
---|
[b7d3cc34] | 496 | |
---|
| 497 | |
---|
[5ebff60] | 498 | struct imcb_ask_cb_data { |
---|
[0da65d5] | 499 | struct im_connection *ic; |
---|
[7bf0f5f0] | 500 | char *handle; |
---|
| 501 | }; |
---|
| 502 | |
---|
[098a75b] | 503 | static void imcb_ask_cb_free(void *data) |
---|
| 504 | { |
---|
| 505 | struct imcb_ask_cb_data *cbd = data; |
---|
| 506 | |
---|
| 507 | g_free(cbd->handle); |
---|
| 508 | g_free(cbd); |
---|
| 509 | } |
---|
| 510 | |
---|
[5ebff60] | 511 | static void imcb_ask_auth_cb_no(void *data) |
---|
[7bf0f5f0] | 512 | { |
---|
[fa295e36] | 513 | struct imcb_ask_cb_data *cbd = data; |
---|
[5ebff60] | 514 | |
---|
| 515 | cbd->ic->acc->prpl->auth_deny(cbd->ic, cbd->handle); |
---|
| 516 | |
---|
[098a75b] | 517 | imcb_ask_cb_free(cbd); |
---|
[fa295e36] | 518 | } |
---|
| 519 | |
---|
[5ebff60] | 520 | static void imcb_ask_auth_cb_yes(void *data) |
---|
[fa295e36] | 521 | { |
---|
| 522 | struct imcb_ask_cb_data *cbd = data; |
---|
[5ebff60] | 523 | |
---|
| 524 | cbd->ic->acc->prpl->auth_allow(cbd->ic, cbd->handle); |
---|
| 525 | |
---|
[098a75b] | 526 | imcb_ask_cb_free(cbd); |
---|
[fa295e36] | 527 | } |
---|
| 528 | |
---|
[5ebff60] | 529 | void imcb_ask_auth(struct im_connection *ic, const char *handle, const char *realname) |
---|
[fa295e36] | 530 | { |
---|
[5ebff60] | 531 | struct imcb_ask_cb_data *data = g_new0(struct imcb_ask_cb_data, 1); |
---|
[fa295e36] | 532 | char *s, *realname_ = NULL; |
---|
[5ebff60] | 533 | |
---|
| 534 | if (realname != NULL) { |
---|
| 535 | realname_ = g_strdup_printf(" (%s)", realname); |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | s = g_strdup_printf("The user %s%s wants to add you to his/her buddy list.", |
---|
| 539 | handle, realname_ ? realname_ : ""); |
---|
| 540 | |
---|
| 541 | g_free(realname_); |
---|
| 542 | |
---|
[fa295e36] | 543 | data->ic = ic; |
---|
[5ebff60] | 544 | data->handle = g_strdup(handle); |
---|
| 545 | query_add((irc_t *) ic->bee->ui_data, ic, s, |
---|
[098a75b] | 546 | imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, imcb_ask_cb_free, data); |
---|
[fa295e36] | 547 | |
---|
[098a75b] | 548 | g_free(s); |
---|
[7bf0f5f0] | 549 | } |
---|
| 550 | |
---|
[5ebff60] | 551 | static void imcb_ask_add_cb_yes(void *data) |
---|
[7bf0f5f0] | 552 | { |
---|
[fa295e36] | 553 | struct imcb_ask_cb_data *cbd = data; |
---|
[5ebff60] | 554 | |
---|
| 555 | cbd->ic->acc->prpl->add_buddy(cbd->ic, cbd->handle, NULL); |
---|
| 556 | |
---|
[098a75b] | 557 | imcb_ask_cb_free(data); |
---|
[7bf0f5f0] | 558 | } |
---|
| 559 | |
---|
[5ebff60] | 560 | void imcb_ask_add(struct im_connection *ic, const char *handle, const char *realname) |
---|
[b7d3cc34] | 561 | { |
---|
[098a75b] | 562 | struct imcb_ask_cb_data *data; |
---|
[7bf0f5f0] | 563 | char *s; |
---|
[5ebff60] | 564 | |
---|
[7bf0f5f0] | 565 | /* TODO: Make a setting for this! */ |
---|
[5ebff60] | 566 | if (bee_user_by_handle(ic->bee, ic, handle) != NULL) { |
---|
[7bf0f5f0] | 567 | return; |
---|
[5ebff60] | 568 | } |
---|
| 569 | |
---|
[098a75b] | 570 | data = g_new0(struct imcb_ask_cb_data, 1); |
---|
| 571 | |
---|
[5ebff60] | 572 | s = g_strdup_printf("The user %s is not in your buddy list yet. Do you want to add him/her now?", handle); |
---|
| 573 | |
---|
[0da65d5] | 574 | data->ic = ic; |
---|
[5ebff60] | 575 | data->handle = g_strdup(handle); |
---|
| 576 | query_add((irc_t *) ic->bee->ui_data, ic, s, |
---|
[098a75b] | 577 | imcb_ask_add_cb_yes, imcb_ask_cb_free, imcb_ask_cb_free, data); |
---|
| 578 | |
---|
| 579 | g_free(s); |
---|
[b7d3cc34] | 580 | } |
---|
| 581 | |
---|
[5ebff60] | 582 | struct bee_user *imcb_buddy_by_handle(struct im_connection *ic, const char *handle) |
---|
[81e04e1] | 583 | { |
---|
[5ebff60] | 584 | return bee_user_by_handle(ic->bee, ic, handle); |
---|
[b7d3cc34] | 585 | } |
---|
| 586 | |
---|
[226fce1] | 587 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
| 588 | them all from some wrappers. We'll start to define some down here: */ |
---|
| 589 | |
---|
[5ebff60] | 590 | int imc_chat_msg(struct groupchat *c, char *msg, int flags) |
---|
[b7d3cc34] | 591 | { |
---|
[e27661d] | 592 | char *buf = NULL; |
---|
[5ebff60] | 593 | |
---|
| 594 | if ((c->ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) { |
---|
| 595 | buf = escape_html(msg); |
---|
[b7d3cc34] | 596 | msg = buf; |
---|
| 597 | } |
---|
[5ebff60] | 598 | |
---|
| 599 | c->ic->acc->prpl->chat_msg(c, msg, flags); |
---|
| 600 | g_free(buf); |
---|
| 601 | |
---|
[0da65d5] | 602 | return 1; |
---|
[b7d3cc34] | 603 | } |
---|
[226fce1] | 604 | |
---|
[5ebff60] | 605 | static char *imc_away_state_find(GList *gcm, char *away, char **message); |
---|
[226fce1] | 606 | |
---|
[5ebff60] | 607 | int imc_away_send_update(struct im_connection *ic) |
---|
[226fce1] | 608 | { |
---|
[3e1ef92c] | 609 | char *away, *msg = NULL; |
---|
[5ebff60] | 610 | |
---|
| 611 | if (ic->acc->prpl->away_states == NULL || |
---|
| 612 | ic->acc->prpl->set_away == NULL) { |
---|
[91cec2f] | 613 | return 0; |
---|
[58adb7e] | 614 | } |
---|
[5ebff60] | 615 | |
---|
| 616 | away = set_getstr(&ic->acc->set, "away") ? |
---|
| 617 | : set_getstr(&ic->bee->set, "away"); |
---|
| 618 | if (away && *away) { |
---|
| 619 | GList *m = ic->acc->prpl->away_states(ic); |
---|
| 620 | msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; |
---|
| 621 | away = imc_away_state_find(m, away, &msg) ? : m->data; |
---|
| 622 | } else if (ic->acc->flags & ACC_FLAG_STATUS_MESSAGE) { |
---|
[58adb7e] | 623 | away = NULL; |
---|
[5ebff60] | 624 | msg = set_getstr(&ic->acc->set, "status") ? |
---|
| 625 | : set_getstr(&ic->bee->set, "status"); |
---|
[226fce1] | 626 | } |
---|
[5ebff60] | 627 | |
---|
| 628 | ic->acc->prpl->set_away(ic, away, msg); |
---|
| 629 | |
---|
[34fbbf9] | 630 | return 1; |
---|
[226fce1] | 631 | } |
---|
| 632 | |
---|
[84b045d] | 633 | static char *imc_away_alias_list[8][5] = |
---|
[226fce1] | 634 | { |
---|
| 635 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
| 636 | { "NA", "N/A", "Not available", NULL }, |
---|
| 637 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
| 638 | { "Be right back", "BRB", NULL }, |
---|
| 639 | { "On the phone", "Phone", "On phone", NULL }, |
---|
| 640 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
| 641 | { "Invisible", "Hidden" }, |
---|
| 642 | { NULL } |
---|
| 643 | }; |
---|
| 644 | |
---|
[5ebff60] | 645 | static char *imc_away_state_find(GList *gcm, char *away, char **message) |
---|
[226fce1] | 646 | { |
---|
| 647 | GList *m; |
---|
| 648 | int i, j; |
---|
[5ebff60] | 649 | |
---|
| 650 | for (m = gcm; m; m = m->next) { |
---|
| 651 | if (g_strncasecmp(m->data, away, strlen(m->data)) == 0) { |
---|
[34fbbf9] | 652 | /* At least the Yahoo! module works better if message |
---|
| 653 | contains no data unless it adds something to what |
---|
| 654 | we have in state already. */ |
---|
[5ebff60] | 655 | if (strlen(m->data) == strlen(away)) { |
---|
[34fbbf9] | 656 | *message = NULL; |
---|
[5ebff60] | 657 | } |
---|
| 658 | |
---|
[34fbbf9] | 659 | return m->data; |
---|
| 660 | } |
---|
[5ebff60] | 661 | } |
---|
| 662 | |
---|
| 663 | for (i = 0; *imc_away_alias_list[i]; i++) { |
---|
[34fbbf9] | 664 | int keep_message; |
---|
[5ebff60] | 665 | |
---|
| 666 | for (j = 0; imc_away_alias_list[i][j]; j++) { |
---|
| 667 | if (g_strncasecmp(away, imc_away_alias_list[i][j], strlen(imc_away_alias_list[i][j])) == 0) { |
---|
| 668 | keep_message = strlen(away) != strlen(imc_away_alias_list[i][j]); |
---|
[226fce1] | 669 | break; |
---|
[34fbbf9] | 670 | } |
---|
[5ebff60] | 671 | } |
---|
| 672 | |
---|
| 673 | if (!imc_away_alias_list[i][j]) { /* If we reach the end, this row */ |
---|
| 674 | continue; /* is not what we want. Next! */ |
---|
| 675 | |
---|
| 676 | } |
---|
[226fce1] | 677 | /* Now find an entry in this row which exists in gcm */ |
---|
[5ebff60] | 678 | for (j = 0; imc_away_alias_list[i][j]; j++) { |
---|
| 679 | for (m = gcm; m; m = m->next) { |
---|
| 680 | if (g_strcasecmp(imc_away_alias_list[i][j], m->data) == 0) { |
---|
| 681 | if (!keep_message) { |
---|
[34fbbf9] | 682 | *message = NULL; |
---|
[5ebff60] | 683 | } |
---|
| 684 | |
---|
[34fbbf9] | 685 | return imc_away_alias_list[i][j]; |
---|
| 686 | } |
---|
[5ebff60] | 687 | } |
---|
[226fce1] | 688 | } |
---|
[5ebff60] | 689 | |
---|
[34fbbf9] | 690 | /* No need to look further, apparently this state doesn't |
---|
| 691 | have any good alias for this protocol. */ |
---|
| 692 | break; |
---|
[226fce1] | 693 | } |
---|
[5ebff60] | 694 | |
---|
[34fbbf9] | 695 | return NULL; |
---|
[226fce1] | 696 | } |
---|
[da3b536] | 697 | |
---|
[5ebff60] | 698 | void imc_add_allow(struct im_connection *ic, char *handle) |
---|
[da3b536] | 699 | { |
---|
[5ebff60] | 700 | if (g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) { |
---|
| 701 | ic->permit = g_slist_prepend(ic->permit, g_strdup(handle)); |
---|
[da3b536] | 702 | } |
---|
[5ebff60] | 703 | |
---|
| 704 | ic->acc->prpl->add_permit(ic, handle); |
---|
[da3b536] | 705 | } |
---|
| 706 | |
---|
[5ebff60] | 707 | void imc_rem_allow(struct im_connection *ic, char *handle) |
---|
[da3b536] | 708 | { |
---|
| 709 | GSList *l; |
---|
[5ebff60] | 710 | |
---|
| 711 | if ((l = g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) { |
---|
| 712 | g_free(l->data); |
---|
| 713 | ic->permit = g_slist_delete_link(ic->permit, l); |
---|
[da3b536] | 714 | } |
---|
[5ebff60] | 715 | |
---|
| 716 | ic->acc->prpl->rem_permit(ic, handle); |
---|
[da3b536] | 717 | } |
---|
| 718 | |
---|
[5ebff60] | 719 | void imc_add_block(struct im_connection *ic, char *handle) |
---|
[da3b536] | 720 | { |
---|
[5ebff60] | 721 | if (g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) { |
---|
| 722 | ic->deny = g_slist_prepend(ic->deny, g_strdup(handle)); |
---|
[da3b536] | 723 | } |
---|
[5ebff60] | 724 | |
---|
| 725 | ic->acc->prpl->add_deny(ic, handle); |
---|
[da3b536] | 726 | } |
---|
| 727 | |
---|
[5ebff60] | 728 | void imc_rem_block(struct im_connection *ic, char *handle) |
---|
[da3b536] | 729 | { |
---|
| 730 | GSList *l; |
---|
[5ebff60] | 731 | |
---|
| 732 | if ((l = g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) { |
---|
| 733 | g_free(l->data); |
---|
| 734 | ic->deny = g_slist_delete_link(ic->deny, l); |
---|
[da3b536] | 735 | } |
---|
[5ebff60] | 736 | |
---|
| 737 | ic->acc->prpl->rem_deny(ic, handle); |
---|
[da3b536] | 738 | } |
---|
[85023c6] | 739 | |
---|
[d6e2aa8] | 740 | /* Deprecated: using this function resulted in merging several handles accidentally |
---|
| 741 | * Also the irc layer handles this decently nowadays */ |
---|
[5ebff60] | 742 | void imcb_clean_handle(struct im_connection *ic, char *handle) |
---|
[85023c6] | 743 | { |
---|
| 744 | } |
---|