[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; |
---|
[5ebff60] | 92 | |
---|
| 93 | void register_protocol(struct prpl *p) |
---|
[7b23afd] | 94 | { |
---|
[90cd6c4] | 95 | int i; |
---|
| 96 | gboolean refused = global.conf->protocols != NULL; |
---|
[5ebff60] | 97 | |
---|
| 98 | for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++) { |
---|
| 99 | if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0) { |
---|
[90cd6c4] | 100 | refused = FALSE; |
---|
[5ebff60] | 101 | } |
---|
| 102 | } |
---|
[90cd6c4] | 103 | |
---|
[5ebff60] | 104 | if (refused) { |
---|
[90cd6c4] | 105 | log_message(LOGLVL_WARNING, "Protocol %s disabled\n", p->name); |
---|
[5ebff60] | 106 | } else { |
---|
[90cd6c4] | 107 | protocols = g_list_append(protocols, p); |
---|
[5ebff60] | 108 | } |
---|
[7b23afd] | 109 | } |
---|
| 110 | |
---|
| 111 | struct prpl *find_protocol(const char *name) |
---|
| 112 | { |
---|
| 113 | GList *gl; |
---|
[5ebff60] | 114 | |
---|
| 115 | for (gl = protocols; gl; gl = gl->next) { |
---|
| 116 | struct prpl *proto = gl->data; |
---|
| 117 | |
---|
| 118 | if (g_strcasecmp(proto->name, name) == 0) { |
---|
[7b23afd] | 119 | return proto; |
---|
[5ebff60] | 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return NULL; |
---|
[7b23afd] | 124 | } |
---|
| 125 | |
---|
[b7d3cc34] | 126 | void nogaim_init() |
---|
| 127 | { |
---|
[0da65d5] | 128 | extern void msn_initmodule(); |
---|
| 129 | extern void oscar_initmodule(); |
---|
| 130 | extern void byahoo_initmodule(); |
---|
| 131 | extern void jabber_initmodule(); |
---|
[1b221e0] | 132 | extern void twitter_initmodule(); |
---|
[796da03] | 133 | extern void purple_initmodule(); |
---|
[66aefeb] | 134 | extern void rpc_initmodule(); |
---|
[7b23afd] | 135 | |
---|
[b7d3cc34] | 136 | #ifdef WITH_MSN |
---|
[0da65d5] | 137 | msn_initmodule(); |
---|
[b7d3cc34] | 138 | #endif |
---|
| 139 | |
---|
| 140 | #ifdef WITH_OSCAR |
---|
[0da65d5] | 141 | oscar_initmodule(); |
---|
[b7d3cc34] | 142 | #endif |
---|
[5ebff60] | 143 | |
---|
[b7d3cc34] | 144 | #ifdef WITH_YAHOO |
---|
[0da65d5] | 145 | byahoo_initmodule(); |
---|
[b7d3cc34] | 146 | #endif |
---|
[5ebff60] | 147 | |
---|
[b7d3cc34] | 148 | #ifdef WITH_JABBER |
---|
[0da65d5] | 149 | jabber_initmodule(); |
---|
[b7d3cc34] | 150 | #endif |
---|
[7b23afd] | 151 | |
---|
[1b221e0] | 152 | #ifdef WITH_TWITTER |
---|
| 153 | twitter_initmodule(); |
---|
| 154 | #endif |
---|
| 155 | |
---|
[796da03] | 156 | #ifdef WITH_PURPLE |
---|
| 157 | purple_initmodule(); |
---|
| 158 | #endif |
---|
[7b23afd] | 159 | |
---|
[66aefeb] | 160 | #ifdef WITH_RPC |
---|
| 161 | rpc_initmodule(); |
---|
| 162 | #endif |
---|
| 163 | |
---|
[65e2ce1] | 164 | #ifdef WITH_PLUGINS |
---|
| 165 | load_plugins(); |
---|
[b7d3cc34] | 166 | #endif |
---|
| 167 | } |
---|
| 168 | |
---|
[5ebff60] | 169 | GSList *get_connections() |
---|
| 170 | { |
---|
| 171 | return connections; |
---|
| 172 | } |
---|
[b7d3cc34] | 173 | |
---|
[5ebff60] | 174 | struct im_connection *imcb_new(account_t *acc) |
---|
[b7d3cc34] | 175 | { |
---|
[0da65d5] | 176 | struct im_connection *ic; |
---|
[5ebff60] | 177 | |
---|
| 178 | ic = g_new0(struct im_connection, 1); |
---|
| 179 | |
---|
[81e04e1] | 180 | ic->bee = acc->bee; |
---|
[0da65d5] | 181 | ic->acc = acc; |
---|
| 182 | acc->ic = ic; |
---|
[5ebff60] | 183 | |
---|
| 184 | connections = g_slist_append(connections, ic); |
---|
| 185 | |
---|
| 186 | return(ic); |
---|
[b7d3cc34] | 187 | } |
---|
| 188 | |
---|
[5ebff60] | 189 | void imc_free(struct im_connection *ic) |
---|
[b7d3cc34] | 190 | { |
---|
| 191 | account_t *a; |
---|
[5ebff60] | 192 | |
---|
[b7d3cc34] | 193 | /* Destroy the pointer to this connection from the account list */ |
---|
[5ebff60] | 194 | for (a = ic->bee->accounts; a; a = a->next) { |
---|
| 195 | if (a->ic == ic) { |
---|
[0da65d5] | 196 | a->ic = NULL; |
---|
[b7d3cc34] | 197 | break; |
---|
| 198 | } |
---|
[5ebff60] | 199 | } |
---|
| 200 | |
---|
| 201 | connections = g_slist_remove(connections, ic); |
---|
| 202 | g_free(ic); |
---|
[b7d3cc34] | 203 | } |
---|
| 204 | |
---|
[5ebff60] | 205 | static void serv_got_crap(struct im_connection *ic, char *format, ...) |
---|
[b7d3cc34] | 206 | { |
---|
| 207 | va_list params; |
---|
[e27661d] | 208 | char *text; |
---|
[dfde8e0] | 209 | account_t *a; |
---|
[5ebff60] | 210 | |
---|
| 211 | va_start(params, format); |
---|
| 212 | text = g_strdup_vprintf(format, params); |
---|
| 213 | va_end(params); |
---|
| 214 | |
---|
| 215 | if ((g_strcasecmp(set_getstr(&ic->bee->set, "strip_html"), "always") == 0) || |
---|
| 216 | ((ic->flags & OPT_DOES_HTML) && set_getbool(&ic->bee->set, "strip_html"))) { |
---|
| 217 | strip_html(text); |
---|
| 218 | } |
---|
| 219 | |
---|
[dfde8e0] | 220 | /* Try to find a different connection on the same protocol. */ |
---|
[5ebff60] | 221 | for (a = ic->bee->accounts; a; a = a->next) { |
---|
| 222 | if (a->prpl == ic->acc->prpl && a->ic != ic) { |
---|
[dfde8e0] | 223 | break; |
---|
[5ebff60] | 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
[e27661d] | 227 | /* If we found one, include the screenname in the message. */ |
---|
[5ebff60] | 228 | if (a) { |
---|
[81e04e1] | 229 | /* FIXME(wilmer): ui_log callback or so */ |
---|
[5ebff60] | 230 | irc_rootmsg(ic->bee->ui_data, "%s - %s", ic->acc->tag, text); |
---|
| 231 | } else { |
---|
| 232 | irc_rootmsg(ic->bee->ui_data, "%s - %s", ic->acc->prpl->name, text); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | g_free(text); |
---|
[b7d3cc34] | 236 | } |
---|
| 237 | |
---|
[5ebff60] | 238 | void imcb_log(struct im_connection *ic, char *format, ...) |
---|
[aef4828] | 239 | { |
---|
| 240 | va_list params; |
---|
| 241 | char *text; |
---|
[5ebff60] | 242 | |
---|
| 243 | va_start(params, format); |
---|
| 244 | text = g_strdup_vprintf(format, params); |
---|
| 245 | va_end(params); |
---|
| 246 | |
---|
| 247 | if (ic->flags & OPT_LOGGED_IN) { |
---|
| 248 | serv_got_crap(ic, "%s", text); |
---|
| 249 | } else { |
---|
| 250 | serv_got_crap(ic, "Logging in: %s", text); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | g_free(text); |
---|
[aef4828] | 254 | } |
---|
| 255 | |
---|
[5ebff60] | 256 | void imcb_error(struct im_connection *ic, char *format, ...) |
---|
[aef4828] | 257 | { |
---|
| 258 | va_list params; |
---|
| 259 | char *text; |
---|
[5ebff60] | 260 | |
---|
| 261 | va_start(params, format); |
---|
| 262 | text = g_strdup_vprintf(format, params); |
---|
| 263 | va_end(params); |
---|
| 264 | |
---|
| 265 | if (ic->flags & OPT_LOGGED_IN) { |
---|
| 266 | serv_got_crap(ic, "Error: %s", text); |
---|
| 267 | } else { |
---|
| 268 | serv_got_crap(ic, "Login error: %s", text); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | g_free(text); |
---|
[aef4828] | 272 | } |
---|
| 273 | |
---|
[5ebff60] | 274 | static gboolean send_keepalive(gpointer d, gint fd, b_input_condition cond) |
---|
[b7d3cc34] | 275 | { |
---|
[0da65d5] | 276 | struct im_connection *ic = d; |
---|
[5ebff60] | 277 | |
---|
| 278 | if ((ic->flags & OPT_PONGS) && !(ic->flags & OPT_PONGED)) { |
---|
[e132b60] | 279 | /* This protocol is expected to ack keepalives and hasn't |
---|
| 280 | since the last time we were here. */ |
---|
[5ebff60] | 281 | imcb_error(ic, "Connection timeout"); |
---|
| 282 | imc_logout(ic, TRUE); |
---|
[e132b60] | 283 | return FALSE; |
---|
| 284 | } |
---|
| 285 | ic->flags &= ~OPT_PONGED; |
---|
[5ebff60] | 286 | |
---|
| 287 | if (ic->acc->prpl->keepalive) { |
---|
| 288 | ic->acc->prpl->keepalive(ic); |
---|
| 289 | } |
---|
| 290 | |
---|
[b7d3cc34] | 291 | return TRUE; |
---|
| 292 | } |
---|
| 293 | |
---|
[5ebff60] | 294 | void start_keepalives(struct im_connection *ic, int interval) |
---|
[e132b60] | 295 | { |
---|
[5ebff60] | 296 | b_event_remove(ic->keepalive); |
---|
| 297 | ic->keepalive = b_timeout_add(interval, send_keepalive, ic); |
---|
| 298 | |
---|
[e132b60] | 299 | /* Connecting successfully counts as a first successful pong. */ |
---|
[5ebff60] | 300 | if (ic->flags & OPT_PONGS) { |
---|
[e132b60] | 301 | ic->flags |= OPT_PONGED; |
---|
[5ebff60] | 302 | } |
---|
[e132b60] | 303 | } |
---|
| 304 | |
---|
[5ebff60] | 305 | void imcb_connected(struct im_connection *ic) |
---|
[b7d3cc34] | 306 | { |
---|
| 307 | /* MSN servers sometimes redirect you to a different server and do |
---|
[84c1a0a] | 308 | the whole login sequence again, so these "late" calls to this |
---|
[b7d3cc34] | 309 | function should be handled correctly. (IOW, ignored) */ |
---|
[5ebff60] | 310 | if (ic->flags & OPT_LOGGED_IN) { |
---|
[b7d3cc34] | 311 | return; |
---|
[5ebff60] | 312 | } |
---|
[11e7828] | 313 | |
---|
[8c3637b] | 314 | if ((ic->acc->flags & ACC_FLAG_LOCAL_CONTACTS) && |
---|
[eb89823] | 315 | !(ic->flags & OPT_LOCAL_CONTACTS_SENT) && |
---|
| 316 | ic->acc->prpl->add_buddy) { |
---|
[11e7828] | 317 | GHashTableIter nicks; |
---|
[8c3637b] | 318 | gpointer handle; |
---|
[5ebff60] | 319 | g_hash_table_iter_init(&nicks, ic->acc->nicks); |
---|
[8c3637b] | 320 | while (g_hash_table_iter_next(&nicks, &handle, NULL)) { |
---|
| 321 | ic->acc->prpl->add_buddy(ic, (char *) handle, NULL); |
---|
[11e7828] | 322 | } |
---|
| 323 | } |
---|
[5ebff60] | 324 | |
---|
| 325 | imcb_log(ic, "Logged in"); |
---|
| 326 | |
---|
[0da65d5] | 327 | ic->flags |= OPT_LOGGED_IN; |
---|
[5ebff60] | 328 | start_keepalives(ic, 60000); |
---|
| 329 | |
---|
[58adb7e] | 330 | /* Necessary to send initial presence status, even if we're not away. */ |
---|
[5ebff60] | 331 | imc_away_send_update(ic); |
---|
| 332 | |
---|
[280e655] | 333 | /* Apparently we're connected successfully, so reset the |
---|
| 334 | exponential backoff timer. */ |
---|
| 335 | ic->acc->auto_reconnect_delay = 0; |
---|
[5ebff60] | 336 | |
---|
| 337 | if (ic->bee->ui->imc_connected) { |
---|
| 338 | ic->bee->ui->imc_connected(ic); |
---|
| 339 | } |
---|
[b7d3cc34] | 340 | } |
---|
| 341 | |
---|
[5ebff60] | 342 | gboolean auto_reconnect(gpointer data, gint fd, b_input_condition cond) |
---|
[b7d3cc34] | 343 | { |
---|
| 344 | account_t *a = data; |
---|
[5ebff60] | 345 | |
---|
[b7d3cc34] | 346 | a->reconnect = 0; |
---|
[5ebff60] | 347 | account_on(a->bee, a); |
---|
| 348 | |
---|
| 349 | return(FALSE); /* Only have to run the timeout once */ |
---|
[b7d3cc34] | 350 | } |
---|
| 351 | |
---|
[5ebff60] | 352 | void cancel_auto_reconnect(account_t *a) |
---|
[b7d3cc34] | 353 | { |
---|
[5ebff60] | 354 | b_event_remove(a->reconnect); |
---|
[b7d3cc34] | 355 | a->reconnect = 0; |
---|
| 356 | } |
---|
| 357 | |
---|
[5ebff60] | 358 | void imc_logout(struct im_connection *ic, int allow_reconnect) |
---|
[b7d3cc34] | 359 | { |
---|
[81e04e1] | 360 | bee_t *bee = ic->bee; |
---|
[b7d3cc34] | 361 | account_t *a; |
---|
[81e04e1] | 362 | GSList *l; |
---|
[4230221] | 363 | int delay; |
---|
[5ebff60] | 364 | |
---|
[8d74291] | 365 | /* Nested calls might happen sometimes, this is probably the best |
---|
| 366 | place to catch them. */ |
---|
[5ebff60] | 367 | if (ic->flags & OPT_LOGGING_OUT) { |
---|
[8d74291] | 368 | return; |
---|
[5ebff60] | 369 | } else { |
---|
[0da65d5] | 370 | ic->flags |= OPT_LOGGING_OUT; |
---|
[5ebff60] | 371 | } |
---|
| 372 | |
---|
| 373 | if (ic->bee->ui->imc_disconnected) { |
---|
| 374 | ic->bee->ui->imc_disconnected(ic); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | imcb_log(ic, "Signing off.."); |
---|
| 378 | |
---|
[0dd6570] | 379 | /* TBH I don't remember anymore why I didn't just use ic->acc... */ |
---|
[5ebff60] | 380 | for (a = bee->accounts; a; a = a->next) { |
---|
| 381 | if (a->ic == ic) { |
---|
[0dd6570] | 382 | break; |
---|
[5ebff60] | 383 | } |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | if (a && !allow_reconnect && !(ic->flags & OPT_LOGGED_IN) && |
---|
| 387 | set_getbool(&a->set, "oauth")) { |
---|
[0dd6570] | 388 | /* If this account supports OAuth, we're not logged in yet and |
---|
| 389 | not allowed to retry, assume there were auth issues. Give a |
---|
| 390 | helpful message on what might be necessary to fix this. */ |
---|
[5ebff60] | 391 | imcb_log(ic, "If you're having problems logging in, try re-requesting " |
---|
| 392 | "an OAuth token: account %s set password \"\"", a->tag); |
---|
[0dd6570] | 393 | } |
---|
[5ebff60] | 394 | |
---|
| 395 | for (l = bee->users; l; ) { |
---|
[81e04e1] | 396 | bee_user_t *bu = l->data; |
---|
[eabc9d2] | 397 | GSList *next = l->next; |
---|
[5ebff60] | 398 | |
---|
| 399 | if (bu->ic == ic) { |
---|
| 400 | bee_user_free(bee, bu); |
---|
| 401 | } |
---|
| 402 | |
---|
[eabc9d2] | 403 | l = next; |
---|
[b7d3cc34] | 404 | } |
---|
[5ebff60] | 405 | |
---|
| 406 | b_event_remove(ic->keepalive); |
---|
[be7a180] | 407 | ic->keepalive = 0; |
---|
[5ebff60] | 408 | ic->acc->prpl->logout(ic); |
---|
| 409 | b_event_remove(ic->inpa); |
---|
| 410 | |
---|
| 411 | g_free(ic->away); |
---|
[be7a180] | 412 | ic->away = NULL; |
---|
[5ebff60] | 413 | |
---|
| 414 | query_del_by_conn((irc_t *) ic->bee->ui_data, ic); |
---|
| 415 | |
---|
[9076a1c] | 416 | /* Throw away groupchats owned by this account. Historically this was only |
---|
| 417 | ever done by IM modules which is a bug. But it gives them opportunity |
---|
| 418 | to clean up protocol-specific bits as well so keep it that way, just |
---|
| 419 | do another cleanup here as a fallback. Don't want to leave any dangling |
---|
| 420 | pointers! */ |
---|
| 421 | while (ic->groupchats) { |
---|
| 422 | imcb_chat_free(ic->groupchats->data); |
---|
| 423 | } |
---|
| 424 | |
---|
[5ebff60] | 425 | if (!a) { |
---|
[b7d3cc34] | 426 | /* Uhm... This is very sick. */ |
---|
[5ebff60] | 427 | } else if (allow_reconnect && set_getbool(&bee->set, "auto_reconnect") && |
---|
| 428 | set_getbool(&a->set, "auto_reconnect") && |
---|
| 429 | (delay = account_reconnect_delay(a)) > 0) { |
---|
| 430 | imcb_log(ic, "Reconnecting in %d seconds..", delay); |
---|
| 431 | a->reconnect = b_timeout_add(delay * 1000, auto_reconnect, a); |
---|
[b7d3cc34] | 432 | } |
---|
[5ebff60] | 433 | |
---|
| 434 | imc_free(ic); |
---|
[b7d3cc34] | 435 | } |
---|
| 436 | |
---|
[5ebff60] | 437 | void imcb_ask(struct im_connection *ic, char *msg, void *data, |
---|
| 438 | query_callback doit, query_callback dont) |
---|
[b7d3cc34] | 439 | { |
---|
[5ebff60] | 440 | query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, g_free, data); |
---|
[b7d3cc34] | 441 | } |
---|
| 442 | |
---|
[5ebff60] | 443 | void imcb_ask_with_free(struct im_connection *ic, char *msg, void *data, |
---|
| 444 | query_callback doit, query_callback dont, query_callback myfree) |
---|
[d0527c1] | 445 | { |
---|
[5ebff60] | 446 | query_add((irc_t *) ic->bee->ui_data, ic, msg, doit, dont, myfree, data); |
---|
[d0527c1] | 447 | } |
---|
| 448 | |
---|
[5ebff60] | 449 | void imcb_add_buddy(struct im_connection *ic, const char *handle, const char *group) |
---|
[b7d3cc34] | 450 | { |
---|
[81e04e1] | 451 | bee_user_t *bu; |
---|
| 452 | bee_t *bee = ic->bee; |
---|
[8b61469] | 453 | bee_group_t *oldg; |
---|
[5ebff60] | 454 | |
---|
| 455 | if (!(bu = bee_user_by_handle(bee, ic, handle))) { |
---|
| 456 | bu = bee_user_new(bee, ic, handle, 0); |
---|
| 457 | } |
---|
| 458 | |
---|
[8b61469] | 459 | oldg = bu->group; |
---|
[5ebff60] | 460 | bu->group = bee_group_by_name(bee, group, TRUE); |
---|
| 461 | |
---|
| 462 | if (bee->ui->user_group && bu->group != oldg) { |
---|
| 463 | bee->ui->user_group(bee, bu); |
---|
| 464 | } |
---|
[b7d3cc34] | 465 | } |
---|
| 466 | |
---|
[5ebff60] | 467 | void imcb_rename_buddy(struct im_connection *ic, const char *handle, const char *fullname) |
---|
[b7d3cc34] | 468 | { |
---|
[1d39159] | 469 | bee_t *bee = ic->bee; |
---|
[5ebff60] | 470 | bee_user_t *bu = bee_user_by_handle(bee, ic, handle); |
---|
| 471 | |
---|
| 472 | if (!bu || !fullname) { |
---|
| 473 | return; |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | if (!bu->fullname || strcmp(bu->fullname, fullname) != 0) { |
---|
| 477 | g_free(bu->fullname); |
---|
| 478 | bu->fullname = g_strdup(fullname); |
---|
| 479 | |
---|
| 480 | if (bee->ui->user_fullname) { |
---|
| 481 | bee->ui->user_fullname(bee, bu); |
---|
| 482 | } |
---|
[b7d3cc34] | 483 | } |
---|
| 484 | } |
---|
| 485 | |
---|
[5ebff60] | 486 | void imcb_remove_buddy(struct im_connection *ic, const char *handle, char *group) |
---|
[998b103] | 487 | { |
---|
[5ebff60] | 488 | bee_user_free(ic->bee, bee_user_by_handle(ic->bee, ic, handle)); |
---|
[998b103] | 489 | } |
---|
| 490 | |
---|
[d06eabf] | 491 | /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM |
---|
| 492 | modules to suggest a nickname for a handle. */ |
---|
[5ebff60] | 493 | void imcb_buddy_nick_hint(struct im_connection *ic, const char *handle, const char *nick) |
---|
[d06eabf] | 494 | { |
---|
[6ef9065] | 495 | bee_t *bee = ic->bee; |
---|
[5ebff60] | 496 | bee_user_t *bu = bee_user_by_handle(bee, ic, handle); |
---|
| 497 | |
---|
| 498 | if (!bu || !nick) { |
---|
| 499 | return; |
---|
| 500 | } |
---|
| 501 | |
---|
| 502 | g_free(bu->nick); |
---|
| 503 | bu->nick = g_strdup(nick); |
---|
| 504 | |
---|
| 505 | if (bee->ui->user_nick_hint) { |
---|
| 506 | bee->ui->user_nick_hint(bee, bu, nick); |
---|
| 507 | } |
---|
[d06eabf] | 508 | } |
---|
[b7d3cc34] | 509 | |
---|
[8c3637b] | 510 | /* Returns the local contacts for an IM account (based on assigned nicks). |
---|
| 511 | Linked list should be freed, the strings themselves not! So look at it |
---|
| 512 | like a GSList<const char*> I guess? Empty list means NULL retval (as |
---|
| 513 | always with GSList). */ |
---|
| 514 | GSList *imcb_get_local_contacts(struct im_connection *ic) |
---|
| 515 | { |
---|
| 516 | GHashTableIter nicks; |
---|
| 517 | GSList *ret = NULL; |
---|
| 518 | |
---|
[f16c64d] | 519 | if (!(ic->acc->flags & ACC_FLAG_LOCAL_CONTACTS)) { |
---|
| 520 | /* Only allow protocols that indicate local contact list |
---|
| 521 | support to use this function. */ |
---|
| 522 | return ret; |
---|
| 523 | } |
---|
| 524 | |
---|
[8c3637b] | 525 | g_hash_table_iter_init(&nicks, ic->acc->nicks); |
---|
| 526 | gpointer handle; |
---|
| 527 | while (g_hash_table_iter_next(&nicks, &handle, NULL)) { |
---|
| 528 | ret = g_slist_prepend(ret, (char *) handle); |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | /* If the protocol asked for the list, assume we won't have to send it |
---|
| 532 | anymore in imcb_connected(). */ |
---|
| 533 | ic->flags |= OPT_LOCAL_CONTACTS_SENT; |
---|
| 534 | |
---|
| 535 | return ret; |
---|
| 536 | } |
---|
| 537 | |
---|
[b7d3cc34] | 538 | |
---|
[5ebff60] | 539 | struct imcb_ask_cb_data { |
---|
[0da65d5] | 540 | struct im_connection *ic; |
---|
[7bf0f5f0] | 541 | char *handle; |
---|
| 542 | }; |
---|
| 543 | |
---|
[098a75b] | 544 | static void imcb_ask_cb_free(void *data) |
---|
| 545 | { |
---|
| 546 | struct imcb_ask_cb_data *cbd = data; |
---|
| 547 | |
---|
| 548 | g_free(cbd->handle); |
---|
| 549 | g_free(cbd); |
---|
| 550 | } |
---|
| 551 | |
---|
[5ebff60] | 552 | static void imcb_ask_auth_cb_no(void *data) |
---|
[7bf0f5f0] | 553 | { |
---|
[fa295e36] | 554 | struct imcb_ask_cb_data *cbd = data; |
---|
[5ebff60] | 555 | |
---|
| 556 | cbd->ic->acc->prpl->auth_deny(cbd->ic, cbd->handle); |
---|
| 557 | |
---|
[098a75b] | 558 | imcb_ask_cb_free(cbd); |
---|
[fa295e36] | 559 | } |
---|
| 560 | |
---|
[5ebff60] | 561 | static void imcb_ask_auth_cb_yes(void *data) |
---|
[fa295e36] | 562 | { |
---|
| 563 | struct imcb_ask_cb_data *cbd = data; |
---|
[5ebff60] | 564 | |
---|
| 565 | cbd->ic->acc->prpl->auth_allow(cbd->ic, cbd->handle); |
---|
| 566 | |
---|
[098a75b] | 567 | imcb_ask_cb_free(cbd); |
---|
[fa295e36] | 568 | } |
---|
| 569 | |
---|
[5ebff60] | 570 | void imcb_ask_auth(struct im_connection *ic, const char *handle, const char *realname) |
---|
[fa295e36] | 571 | { |
---|
[5ebff60] | 572 | struct imcb_ask_cb_data *data = g_new0(struct imcb_ask_cb_data, 1); |
---|
[fa295e36] | 573 | char *s, *realname_ = NULL; |
---|
[5ebff60] | 574 | |
---|
| 575 | if (realname != NULL) { |
---|
| 576 | realname_ = g_strdup_printf(" (%s)", realname); |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | s = g_strdup_printf("The user %s%s wants to add you to his/her buddy list.", |
---|
| 580 | handle, realname_ ? realname_ : ""); |
---|
| 581 | |
---|
| 582 | g_free(realname_); |
---|
| 583 | |
---|
[fa295e36] | 584 | data->ic = ic; |
---|
[5ebff60] | 585 | data->handle = g_strdup(handle); |
---|
| 586 | query_add((irc_t *) ic->bee->ui_data, ic, s, |
---|
[098a75b] | 587 | imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, imcb_ask_cb_free, data); |
---|
[fa295e36] | 588 | |
---|
[098a75b] | 589 | g_free(s); |
---|
[7bf0f5f0] | 590 | } |
---|
| 591 | |
---|
[5ebff60] | 592 | static void imcb_ask_add_cb_yes(void *data) |
---|
[7bf0f5f0] | 593 | { |
---|
[fa295e36] | 594 | struct imcb_ask_cb_data *cbd = data; |
---|
[5ebff60] | 595 | |
---|
[eb89823] | 596 | if (cbd->ic->acc->prpl->add_buddy) { |
---|
| 597 | cbd->ic->acc->prpl->add_buddy(cbd->ic, cbd->handle, NULL); |
---|
| 598 | } |
---|
[5ebff60] | 599 | |
---|
[098a75b] | 600 | imcb_ask_cb_free(data); |
---|
[7bf0f5f0] | 601 | } |
---|
| 602 | |
---|
[5ebff60] | 603 | void imcb_ask_add(struct im_connection *ic, const char *handle, const char *realname) |
---|
[b7d3cc34] | 604 | { |
---|
[098a75b] | 605 | struct imcb_ask_cb_data *data; |
---|
[7bf0f5f0] | 606 | char *s; |
---|
[5ebff60] | 607 | |
---|
[7bf0f5f0] | 608 | /* TODO: Make a setting for this! */ |
---|
[5ebff60] | 609 | if (bee_user_by_handle(ic->bee, ic, handle) != NULL) { |
---|
[7bf0f5f0] | 610 | return; |
---|
[5ebff60] | 611 | } |
---|
| 612 | |
---|
[098a75b] | 613 | data = g_new0(struct imcb_ask_cb_data, 1); |
---|
| 614 | |
---|
[5ebff60] | 615 | s = g_strdup_printf("The user %s is not in your buddy list yet. Do you want to add him/her now?", handle); |
---|
| 616 | |
---|
[0da65d5] | 617 | data->ic = ic; |
---|
[5ebff60] | 618 | data->handle = g_strdup(handle); |
---|
| 619 | query_add((irc_t *) ic->bee->ui_data, ic, s, |
---|
[098a75b] | 620 | imcb_ask_add_cb_yes, imcb_ask_cb_free, imcb_ask_cb_free, data); |
---|
| 621 | |
---|
| 622 | g_free(s); |
---|
[b7d3cc34] | 623 | } |
---|
| 624 | |
---|
[5ebff60] | 625 | struct bee_user *imcb_buddy_by_handle(struct im_connection *ic, const char *handle) |
---|
[81e04e1] | 626 | { |
---|
[5ebff60] | 627 | return bee_user_by_handle(ic->bee, ic, handle); |
---|
[b7d3cc34] | 628 | } |
---|
| 629 | |
---|
[226fce1] | 630 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
| 631 | them all from some wrappers. We'll start to define some down here: */ |
---|
| 632 | |
---|
[5ebff60] | 633 | int imc_chat_msg(struct groupchat *c, char *msg, int flags) |
---|
[b7d3cc34] | 634 | { |
---|
[e27661d] | 635 | char *buf = NULL; |
---|
[5ebff60] | 636 | |
---|
| 637 | if ((c->ic->flags & OPT_DOES_HTML) && (g_strncasecmp(msg, "<html>", 6) != 0)) { |
---|
| 638 | buf = escape_html(msg); |
---|
[b7d3cc34] | 639 | msg = buf; |
---|
| 640 | } |
---|
[5ebff60] | 641 | |
---|
| 642 | c->ic->acc->prpl->chat_msg(c, msg, flags); |
---|
| 643 | g_free(buf); |
---|
| 644 | |
---|
[0da65d5] | 645 | return 1; |
---|
[b7d3cc34] | 646 | } |
---|
[226fce1] | 647 | |
---|
[5ebff60] | 648 | static char *imc_away_state_find(GList *gcm, char *away, char **message); |
---|
[226fce1] | 649 | |
---|
[5ebff60] | 650 | int imc_away_send_update(struct im_connection *ic) |
---|
[226fce1] | 651 | { |
---|
[3e1ef92c] | 652 | char *away, *msg = NULL; |
---|
[5ebff60] | 653 | |
---|
| 654 | if (ic->acc->prpl->away_states == NULL || |
---|
| 655 | ic->acc->prpl->set_away == NULL) { |
---|
[91cec2f] | 656 | return 0; |
---|
[58adb7e] | 657 | } |
---|
[5ebff60] | 658 | |
---|
| 659 | away = set_getstr(&ic->acc->set, "away") ? |
---|
| 660 | : set_getstr(&ic->bee->set, "away"); |
---|
| 661 | if (away && *away) { |
---|
| 662 | GList *m = ic->acc->prpl->away_states(ic); |
---|
| 663 | msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; |
---|
| 664 | away = imc_away_state_find(m, away, &msg) ? : m->data; |
---|
| 665 | } else if (ic->acc->flags & ACC_FLAG_STATUS_MESSAGE) { |
---|
[58adb7e] | 666 | away = NULL; |
---|
[5ebff60] | 667 | msg = set_getstr(&ic->acc->set, "status") ? |
---|
| 668 | : set_getstr(&ic->bee->set, "status"); |
---|
[226fce1] | 669 | } |
---|
[5ebff60] | 670 | |
---|
| 671 | ic->acc->prpl->set_away(ic, away, msg); |
---|
| 672 | |
---|
[34fbbf9] | 673 | return 1; |
---|
[226fce1] | 674 | } |
---|
| 675 | |
---|
[84b045d] | 676 | static char *imc_away_alias_list[8][5] = |
---|
[226fce1] | 677 | { |
---|
| 678 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
| 679 | { "NA", "N/A", "Not available", NULL }, |
---|
| 680 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
| 681 | { "Be right back", "BRB", NULL }, |
---|
| 682 | { "On the phone", "Phone", "On phone", NULL }, |
---|
| 683 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
| 684 | { "Invisible", "Hidden" }, |
---|
| 685 | { NULL } |
---|
| 686 | }; |
---|
| 687 | |
---|
[5ebff60] | 688 | static char *imc_away_state_find(GList *gcm, char *away, char **message) |
---|
[226fce1] | 689 | { |
---|
| 690 | GList *m; |
---|
| 691 | int i, j; |
---|
[5ebff60] | 692 | |
---|
| 693 | for (m = gcm; m; m = m->next) { |
---|
| 694 | if (g_strncasecmp(m->data, away, strlen(m->data)) == 0) { |
---|
[34fbbf9] | 695 | /* At least the Yahoo! module works better if message |
---|
| 696 | contains no data unless it adds something to what |
---|
| 697 | we have in state already. */ |
---|
[5ebff60] | 698 | if (strlen(m->data) == strlen(away)) { |
---|
[34fbbf9] | 699 | *message = NULL; |
---|
[5ebff60] | 700 | } |
---|
| 701 | |
---|
[34fbbf9] | 702 | return m->data; |
---|
| 703 | } |
---|
[5ebff60] | 704 | } |
---|
| 705 | |
---|
| 706 | for (i = 0; *imc_away_alias_list[i]; i++) { |
---|
[34fbbf9] | 707 | int keep_message; |
---|
[5ebff60] | 708 | |
---|
| 709 | for (j = 0; imc_away_alias_list[i][j]; j++) { |
---|
| 710 | if (g_strncasecmp(away, imc_away_alias_list[i][j], strlen(imc_away_alias_list[i][j])) == 0) { |
---|
| 711 | keep_message = strlen(away) != strlen(imc_away_alias_list[i][j]); |
---|
[226fce1] | 712 | break; |
---|
[34fbbf9] | 713 | } |
---|
[5ebff60] | 714 | } |
---|
| 715 | |
---|
| 716 | if (!imc_away_alias_list[i][j]) { /* If we reach the end, this row */ |
---|
| 717 | continue; /* is not what we want. Next! */ |
---|
| 718 | |
---|
| 719 | } |
---|
[226fce1] | 720 | /* Now find an entry in this row which exists in gcm */ |
---|
[5ebff60] | 721 | for (j = 0; imc_away_alias_list[i][j]; j++) { |
---|
| 722 | for (m = gcm; m; m = m->next) { |
---|
| 723 | if (g_strcasecmp(imc_away_alias_list[i][j], m->data) == 0) { |
---|
| 724 | if (!keep_message) { |
---|
[34fbbf9] | 725 | *message = NULL; |
---|
[5ebff60] | 726 | } |
---|
| 727 | |
---|
[34fbbf9] | 728 | return imc_away_alias_list[i][j]; |
---|
| 729 | } |
---|
[5ebff60] | 730 | } |
---|
[226fce1] | 731 | } |
---|
[5ebff60] | 732 | |
---|
[34fbbf9] | 733 | /* No need to look further, apparently this state doesn't |
---|
| 734 | have any good alias for this protocol. */ |
---|
| 735 | break; |
---|
[226fce1] | 736 | } |
---|
[5ebff60] | 737 | |
---|
[34fbbf9] | 738 | return NULL; |
---|
[226fce1] | 739 | } |
---|
[da3b536] | 740 | |
---|
[5ebff60] | 741 | void imc_add_allow(struct im_connection *ic, char *handle) |
---|
[da3b536] | 742 | { |
---|
[5ebff60] | 743 | if (g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) { |
---|
| 744 | ic->permit = g_slist_prepend(ic->permit, g_strdup(handle)); |
---|
[da3b536] | 745 | } |
---|
[5ebff60] | 746 | |
---|
| 747 | ic->acc->prpl->add_permit(ic, handle); |
---|
[da3b536] | 748 | } |
---|
| 749 | |
---|
[5ebff60] | 750 | void imc_rem_allow(struct im_connection *ic, char *handle) |
---|
[da3b536] | 751 | { |
---|
| 752 | GSList *l; |
---|
[5ebff60] | 753 | |
---|
| 754 | if ((l = g_slist_find_custom(ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) { |
---|
| 755 | g_free(l->data); |
---|
| 756 | ic->permit = g_slist_delete_link(ic->permit, l); |
---|
[da3b536] | 757 | } |
---|
[5ebff60] | 758 | |
---|
| 759 | ic->acc->prpl->rem_permit(ic, handle); |
---|
[da3b536] | 760 | } |
---|
| 761 | |
---|
[5ebff60] | 762 | void imc_add_block(struct im_connection *ic, char *handle) |
---|
[da3b536] | 763 | { |
---|
[5ebff60] | 764 | if (g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp) == NULL) { |
---|
| 765 | ic->deny = g_slist_prepend(ic->deny, g_strdup(handle)); |
---|
[da3b536] | 766 | } |
---|
[5ebff60] | 767 | |
---|
| 768 | ic->acc->prpl->add_deny(ic, handle); |
---|
[da3b536] | 769 | } |
---|
| 770 | |
---|
[5ebff60] | 771 | void imc_rem_block(struct im_connection *ic, char *handle) |
---|
[da3b536] | 772 | { |
---|
| 773 | GSList *l; |
---|
[5ebff60] | 774 | |
---|
| 775 | if ((l = g_slist_find_custom(ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp))) { |
---|
| 776 | g_free(l->data); |
---|
| 777 | ic->deny = g_slist_delete_link(ic->deny, l); |
---|
[da3b536] | 778 | } |
---|
[5ebff60] | 779 | |
---|
| 780 | ic->acc->prpl->rem_deny(ic, handle); |
---|
[da3b536] | 781 | } |
---|
[85023c6] | 782 | |
---|
[5ebff60] | 783 | void imcb_clean_handle(struct im_connection *ic, char *handle) |
---|
[85023c6] | 784 | { |
---|
| 785 | /* Accepts a handle and does whatever is necessary to make it |
---|
| 786 | BitlBee-friendly. Currently this means removing everything |
---|
| 787 | outside 33-127 (ASCII printable excl spaces), @ (only one |
---|
| 788 | is allowed) and ! and : */ |
---|
[5ebff60] | 789 | char out[strlen(handle) + 1]; |
---|
[85023c6] | 790 | int s, d; |
---|
[5ebff60] | 791 | |
---|
[85023c6] | 792 | s = d = 0; |
---|
[5ebff60] | 793 | while (handle[s]) { |
---|
| 794 | if (handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' && |
---|
| 795 | (handle[s] & 0x80) == 0) { |
---|
| 796 | if (handle[s] == '@') { |
---|
[85023c6] | 797 | /* See if we got an @ already? */ |
---|
| 798 | out[d] = 0; |
---|
[5ebff60] | 799 | if (strchr(out, '@')) { |
---|
[85023c6] | 800 | continue; |
---|
[5ebff60] | 801 | } |
---|
[85023c6] | 802 | } |
---|
[5ebff60] | 803 | |
---|
[85023c6] | 804 | out[d++] = handle[s]; |
---|
| 805 | } |
---|
[5ebff60] | 806 | s++; |
---|
[85023c6] | 807 | } |
---|
| 808 | out[d] = handle[s]; |
---|
[5ebff60] | 809 | |
---|
| 810 | strcpy(handle, out); |
---|
[85023c6] | 811 | } |
---|