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