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