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