[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; |
---|
| 114 | for (gl = protocols; gl; gl = gl->next) |
---|
| 115 | { |
---|
| 116 | struct prpl *proto = gl->data; |
---|
| 117 | if(!g_strcasecmp(proto->name, name)) |
---|
| 118 | return proto; |
---|
| 119 | } |
---|
| 120 | return NULL; |
---|
| 121 | } |
---|
| 122 | |
---|
[b7d3cc34] | 123 | void nogaim_init() |
---|
| 124 | { |
---|
[0da65d5] | 125 | extern void msn_initmodule(); |
---|
| 126 | extern void oscar_initmodule(); |
---|
| 127 | extern void byahoo_initmodule(); |
---|
| 128 | extern void jabber_initmodule(); |
---|
[1b221e0] | 129 | extern void twitter_initmodule(); |
---|
[7b23afd] | 130 | |
---|
[b7d3cc34] | 131 | #ifdef WITH_MSN |
---|
[0da65d5] | 132 | msn_initmodule(); |
---|
[b7d3cc34] | 133 | #endif |
---|
| 134 | |
---|
| 135 | #ifdef WITH_OSCAR |
---|
[0da65d5] | 136 | oscar_initmodule(); |
---|
[b7d3cc34] | 137 | #endif |
---|
| 138 | |
---|
| 139 | #ifdef WITH_YAHOO |
---|
[0da65d5] | 140 | byahoo_initmodule(); |
---|
[b7d3cc34] | 141 | #endif |
---|
| 142 | |
---|
| 143 | #ifdef WITH_JABBER |
---|
[0da65d5] | 144 | jabber_initmodule(); |
---|
[b7d3cc34] | 145 | #endif |
---|
[7b23afd] | 146 | |
---|
[1b221e0] | 147 | #ifdef WITH_TWITTER |
---|
| 148 | twitter_initmodule(); |
---|
| 149 | #endif |
---|
| 150 | |
---|
[65e2ce1] | 151 | #ifdef WITH_PLUGINS |
---|
| 152 | load_plugins(); |
---|
[b7d3cc34] | 153 | #endif |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | GSList *get_connections() { return connections; } |
---|
| 157 | |
---|
[84b045d] | 158 | struct im_connection *imcb_new( account_t *acc ) |
---|
[b7d3cc34] | 159 | { |
---|
[0da65d5] | 160 | struct im_connection *ic; |
---|
[b7d3cc34] | 161 | |
---|
[0da65d5] | 162 | ic = g_new0( struct im_connection, 1 ); |
---|
[b7d3cc34] | 163 | |
---|
[81e04e1] | 164 | ic->bee = acc->bee; |
---|
[0da65d5] | 165 | ic->acc = acc; |
---|
| 166 | acc->ic = ic; |
---|
[b7d3cc34] | 167 | |
---|
[0da65d5] | 168 | connections = g_slist_append( connections, ic ); |
---|
[b7d3cc34] | 169 | |
---|
[0da65d5] | 170 | return( ic ); |
---|
[b7d3cc34] | 171 | } |
---|
| 172 | |
---|
[aef4828] | 173 | void imc_free( struct im_connection *ic ) |
---|
[b7d3cc34] | 174 | { |
---|
| 175 | account_t *a; |
---|
| 176 | |
---|
| 177 | /* Destroy the pointer to this connection from the account list */ |
---|
[81e04e1] | 178 | for( a = ic->bee->accounts; a; a = a->next ) |
---|
[0da65d5] | 179 | if( a->ic == ic ) |
---|
[b7d3cc34] | 180 | { |
---|
[0da65d5] | 181 | a->ic = NULL; |
---|
[b7d3cc34] | 182 | break; |
---|
| 183 | } |
---|
| 184 | |
---|
[0da65d5] | 185 | connections = g_slist_remove( connections, ic ); |
---|
| 186 | g_free( ic ); |
---|
[b7d3cc34] | 187 | } |
---|
| 188 | |
---|
[aef4828] | 189 | static void serv_got_crap( struct im_connection *ic, char *format, ... ) |
---|
[b7d3cc34] | 190 | { |
---|
| 191 | va_list params; |
---|
[e27661d] | 192 | char *text; |
---|
[dfde8e0] | 193 | account_t *a; |
---|
[b7d3cc34] | 194 | |
---|
| 195 | va_start( params, format ); |
---|
[e27661d] | 196 | text = g_strdup_vprintf( format, params ); |
---|
[b7d3cc34] | 197 | va_end( params ); |
---|
| 198 | |
---|
[81e04e1] | 199 | if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 200 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) |
---|
[e27661d] | 201 | strip_html( text ); |
---|
[b7d3cc34] | 202 | |
---|
[dfde8e0] | 203 | /* Try to find a different connection on the same protocol. */ |
---|
[81e04e1] | 204 | for( a = ic->bee->accounts; a; a = a->next ) |
---|
[0da65d5] | 205 | if( a->prpl == ic->acc->prpl && a->ic != ic ) |
---|
[dfde8e0] | 206 | break; |
---|
| 207 | |
---|
[e27661d] | 208 | /* If we found one, include the screenname in the message. */ |
---|
[dfde8e0] | 209 | if( a ) |
---|
[81e04e1] | 210 | /* FIXME(wilmer): ui_log callback or so */ |
---|
| 211 | irc_usermsg( ic->bee->ui_data, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text ); |
---|
[dfde8e0] | 212 | else |
---|
[81e04e1] | 213 | irc_usermsg( ic->bee->ui_data, "%s - %s", ic->acc->prpl->name, text ); |
---|
[7b07dc6] | 214 | |
---|
[e27661d] | 215 | g_free( text ); |
---|
[b7d3cc34] | 216 | } |
---|
| 217 | |
---|
[84b045d] | 218 | void imcb_log( struct im_connection *ic, char *format, ... ) |
---|
[aef4828] | 219 | { |
---|
| 220 | va_list params; |
---|
| 221 | char *text; |
---|
| 222 | |
---|
| 223 | va_start( params, format ); |
---|
| 224 | text = g_strdup_vprintf( format, params ); |
---|
| 225 | va_end( params ); |
---|
| 226 | |
---|
| 227 | if( ic->flags & OPT_LOGGED_IN ) |
---|
| 228 | serv_got_crap( ic, "%s", text ); |
---|
| 229 | else |
---|
| 230 | serv_got_crap( ic, "Logging in: %s", text ); |
---|
| 231 | |
---|
| 232 | g_free( text ); |
---|
| 233 | } |
---|
| 234 | |
---|
[84b045d] | 235 | void imcb_error( struct im_connection *ic, char *format, ... ) |
---|
[aef4828] | 236 | { |
---|
| 237 | va_list params; |
---|
| 238 | char *text; |
---|
| 239 | |
---|
| 240 | va_start( params, format ); |
---|
| 241 | text = g_strdup_vprintf( format, params ); |
---|
| 242 | va_end( params ); |
---|
| 243 | |
---|
| 244 | if( ic->flags & OPT_LOGGED_IN ) |
---|
| 245 | serv_got_crap( ic, "Error: %s", text ); |
---|
| 246 | else |
---|
| 247 | serv_got_crap( ic, "Couldn't log in: %s", text ); |
---|
| 248 | |
---|
| 249 | g_free( text ); |
---|
| 250 | } |
---|
| 251 | |
---|
[ba9edaa] | 252 | static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond ) |
---|
[b7d3cc34] | 253 | { |
---|
[0da65d5] | 254 | struct im_connection *ic = d; |
---|
[b7d3cc34] | 255 | |
---|
[0da65d5] | 256 | if( ic->acc->prpl->keepalive ) |
---|
| 257 | ic->acc->prpl->keepalive( ic ); |
---|
[b7d3cc34] | 258 | |
---|
| 259 | return TRUE; |
---|
| 260 | } |
---|
| 261 | |
---|
[84b045d] | 262 | void imcb_connected( struct im_connection *ic ) |
---|
[b7d3cc34] | 263 | { |
---|
| 264 | /* MSN servers sometimes redirect you to a different server and do |
---|
[84c1a0a] | 265 | the whole login sequence again, so these "late" calls to this |
---|
[b7d3cc34] | 266 | function should be handled correctly. (IOW, ignored) */ |
---|
[0da65d5] | 267 | if( ic->flags & OPT_LOGGED_IN ) |
---|
[b7d3cc34] | 268 | return; |
---|
| 269 | |
---|
[84b045d] | 270 | imcb_log( ic, "Logged in" ); |
---|
[b7d3cc34] | 271 | |
---|
[0da65d5] | 272 | ic->keepalive = b_timeout_add( 60000, send_keepalive, ic ); |
---|
| 273 | ic->flags |= OPT_LOGGED_IN; |
---|
[b7d3cc34] | 274 | |
---|
[58adb7e] | 275 | /* Necessary to send initial presence status, even if we're not away. */ |
---|
| 276 | imc_away_send_update( ic ); |
---|
[280e655] | 277 | |
---|
| 278 | /* Apparently we're connected successfully, so reset the |
---|
| 279 | exponential backoff timer. */ |
---|
| 280 | ic->acc->auto_reconnect_delay = 0; |
---|
[3611717] | 281 | |
---|
[10a96f4] | 282 | /* |
---|
[3611717] | 283 | for( c = irc->chatrooms; c; c = c->next ) |
---|
| 284 | { |
---|
| 285 | if( c->acc != ic->acc ) |
---|
| 286 | continue; |
---|
| 287 | |
---|
| 288 | if( set_getbool( &c->set, "auto_join" ) ) |
---|
[94acdd0] | 289 | chat_join( irc, c, NULL ); |
---|
[3611717] | 290 | } |
---|
[10a96f4] | 291 | */ |
---|
[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 | |
---|
[84b045d] | 324 | imcb_log( ic, "Signing off.." ); |
---|
[fb62f81f] | 325 | |
---|
[0da65d5] | 326 | b_event_remove( ic->keepalive ); |
---|
| 327 | ic->keepalive = 0; |
---|
| 328 | ic->acc->prpl->logout( ic ); |
---|
| 329 | b_event_remove( ic->inpa ); |
---|
[b7d3cc34] | 330 | |
---|
[c0c43fb] | 331 | g_free( ic->away ); |
---|
| 332 | ic->away = NULL; |
---|
| 333 | |
---|
[eabc9d2] | 334 | for( l = bee->users; l; ) |
---|
[b7d3cc34] | 335 | { |
---|
[81e04e1] | 336 | bee_user_t *bu = l->data; |
---|
[eabc9d2] | 337 | GSList *next = l->next; |
---|
[81e04e1] | 338 | |
---|
| 339 | if( bu->ic == ic ) |
---|
[eabc9d2] | 340 | bee_user_free( bee, bu ); |
---|
| 341 | |
---|
| 342 | l = next; |
---|
[b7d3cc34] | 343 | } |
---|
| 344 | |
---|
[3663bb3] | 345 | query_del_by_conn( (irc_t*) ic->bee->ui_data, ic ); |
---|
[b7d3cc34] | 346 | |
---|
[81e04e1] | 347 | for( a = bee->accounts; a; a = a->next ) |
---|
[0da65d5] | 348 | if( a->ic == ic ) |
---|
[b7d3cc34] | 349 | break; |
---|
| 350 | |
---|
| 351 | if( !a ) |
---|
| 352 | { |
---|
| 353 | /* Uhm... This is very sick. */ |
---|
| 354 | } |
---|
[81e04e1] | 355 | else if( allow_reconnect && set_getbool( &bee->set, "auto_reconnect" ) && |
---|
[4230221] | 356 | set_getbool( &a->set, "auto_reconnect" ) && |
---|
| 357 | ( delay = account_reconnect_delay( a ) ) > 0 ) |
---|
[b7d3cc34] | 358 | { |
---|
[84b045d] | 359 | imcb_log( ic, "Reconnecting in %d seconds..", delay ); |
---|
[c98be00] | 360 | a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a ); |
---|
[b7d3cc34] | 361 | } |
---|
| 362 | |
---|
[aef4828] | 363 | imc_free( ic ); |
---|
[b7d3cc34] | 364 | } |
---|
| 365 | |
---|
[9143aeb] | 366 | void imcb_ask( struct im_connection *ic, char *msg, void *data, |
---|
| 367 | query_callback doit, query_callback dont ) |
---|
[b7d3cc34] | 368 | { |
---|
[e00da63] | 369 | query_add( (irc_t *) ic->bee->ui_data, ic, msg, doit, dont, data ); |
---|
[b7d3cc34] | 370 | } |
---|
| 371 | |
---|
[c6ca3ee] | 372 | void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group ) |
---|
[b7d3cc34] | 373 | { |
---|
[81e04e1] | 374 | bee_user_t *bu; |
---|
| 375 | bee_t *bee = ic->bee; |
---|
[b7d3cc34] | 376 | |
---|
[4e608d6] | 377 | if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) |
---|
| 378 | bu = bee_user_new( bee, ic, handle ); |
---|
[b7d3cc34] | 379 | |
---|
[7aadd71] | 380 | bu->group = bee_group_by_name( bee, group, TRUE ); |
---|
[b7d3cc34] | 381 | } |
---|
| 382 | |
---|
[1d39159] | 383 | void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *fullname ) |
---|
[b7d3cc34] | 384 | { |
---|
[1d39159] | 385 | bee_t *bee = ic->bee; |
---|
| 386 | bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); |
---|
[b7d3cc34] | 387 | |
---|
[1d39159] | 388 | if( !bu || !fullname ) return; |
---|
[b7d3cc34] | 389 | |
---|
[17a6ee9] | 390 | if( !bu->fullname || strcmp( bu->fullname, fullname ) != 0 ) |
---|
[b7d3cc34] | 391 | { |
---|
[1d39159] | 392 | g_free( bu->fullname ); |
---|
| 393 | bu->fullname = g_strdup( fullname ); |
---|
[b7d3cc34] | 394 | |
---|
[1d39159] | 395 | if( bee->ui->user_fullname ) |
---|
| 396 | bee->ui->user_fullname( bee, bu ); |
---|
[b7d3cc34] | 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
[c6ca3ee] | 400 | void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group ) |
---|
[998b103] | 401 | { |
---|
[eabc9d2] | 402 | bee_user_free( ic->bee, bee_user_by_handle( ic->bee, ic, handle ) ); |
---|
[998b103] | 403 | } |
---|
| 404 | |
---|
[d06eabf] | 405 | /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM |
---|
| 406 | modules to suggest a nickname for a handle. */ |
---|
[fb00989] | 407 | void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick ) |
---|
[d06eabf] | 408 | { |
---|
[81e04e1] | 409 | #if 0 |
---|
[d06eabf] | 410 | user_t *u = user_findhandle( ic, handle ); |
---|
[43d8cc5] | 411 | char newnick[MAX_NICK_LENGTH+1], *orig_nick; |
---|
[d06eabf] | 412 | |
---|
[e0e2a71] | 413 | if( u && !u->online && !nick_saved( ic->acc, handle ) ) |
---|
[d06eabf] | 414 | { |
---|
| 415 | /* Only do this if the person isn't online yet (which should |
---|
| 416 | be the case if we just added it) and if the user hasn't |
---|
| 417 | assigned a nickname to this buddy already. */ |
---|
| 418 | |
---|
[e0e2a71] | 419 | strncpy( newnick, nick, MAX_NICK_LENGTH ); |
---|
| 420 | newnick[MAX_NICK_LENGTH] = 0; |
---|
[d06eabf] | 421 | |
---|
| 422 | /* Some processing to make sure this string is a valid IRC nickname. */ |
---|
| 423 | nick_strip( newnick ); |
---|
[81e04e1] | 424 | if( set_getbool( &ic->bee->set, "lcnicks" ) ) |
---|
[d06eabf] | 425 | nick_lc( newnick ); |
---|
| 426 | |
---|
[1962ac1] | 427 | if( strcmp( u->nick, newnick ) != 0 ) |
---|
| 428 | { |
---|
| 429 | /* Only do this if newnick is different from the current one. |
---|
| 430 | If rejoining a channel, maybe we got this nick already |
---|
| 431 | (and dedupe would only add an underscore. */ |
---|
| 432 | nick_dedupe( ic->acc, handle, newnick ); |
---|
| 433 | |
---|
| 434 | /* u->nick will be freed halfway the process, so it can't be |
---|
| 435 | passed as an argument. */ |
---|
| 436 | orig_nick = g_strdup( u->nick ); |
---|
| 437 | user_rename( ic->irc, orig_nick, newnick ); |
---|
| 438 | g_free( orig_nick ); |
---|
| 439 | } |
---|
[d06eabf] | 440 | } |
---|
[81e04e1] | 441 | #endif |
---|
[d06eabf] | 442 | } |
---|
[b7d3cc34] | 443 | |
---|
| 444 | |
---|
[fa295e36] | 445 | struct imcb_ask_cb_data |
---|
[7bf0f5f0] | 446 | { |
---|
[0da65d5] | 447 | struct im_connection *ic; |
---|
[7bf0f5f0] | 448 | char *handle; |
---|
| 449 | }; |
---|
| 450 | |
---|
[fa295e36] | 451 | static void imcb_ask_auth_cb_no( void *data ) |
---|
[7bf0f5f0] | 452 | { |
---|
[fa295e36] | 453 | struct imcb_ask_cb_data *cbd = data; |
---|
| 454 | |
---|
| 455 | cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle ); |
---|
| 456 | |
---|
| 457 | g_free( cbd->handle ); |
---|
| 458 | g_free( cbd ); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | static void imcb_ask_auth_cb_yes( void *data ) |
---|
| 462 | { |
---|
| 463 | struct imcb_ask_cb_data *cbd = data; |
---|
| 464 | |
---|
| 465 | cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle ); |
---|
| 466 | |
---|
| 467 | g_free( cbd->handle ); |
---|
| 468 | g_free( cbd ); |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname ) |
---|
| 472 | { |
---|
| 473 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
| 474 | char *s, *realname_ = NULL; |
---|
| 475 | |
---|
| 476 | if( realname != NULL ) |
---|
| 477 | realname_ = g_strdup_printf( " (%s)", realname ); |
---|
| 478 | |
---|
| 479 | s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.", |
---|
| 480 | handle, realname_ ?: "" ); |
---|
| 481 | |
---|
| 482 | g_free( realname_ ); |
---|
| 483 | |
---|
| 484 | data->ic = ic; |
---|
| 485 | data->handle = g_strdup( handle ); |
---|
[e00da63] | 486 | query_add( (irc_t *) ic->bee->ui_data, ic, s, |
---|
| 487 | imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data ); |
---|
[fa295e36] | 488 | } |
---|
| 489 | |
---|
| 490 | |
---|
| 491 | static void imcb_ask_add_cb_no( void *data ) |
---|
| 492 | { |
---|
| 493 | g_free( ((struct imcb_ask_cb_data*)data)->handle ); |
---|
[7bf0f5f0] | 494 | g_free( data ); |
---|
| 495 | } |
---|
| 496 | |
---|
[fa295e36] | 497 | static void imcb_ask_add_cb_yes( void *data ) |
---|
[7bf0f5f0] | 498 | { |
---|
[fa295e36] | 499 | struct imcb_ask_cb_data *cbd = data; |
---|
[7bf0f5f0] | 500 | |
---|
[fa295e36] | 501 | cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL ); |
---|
[9143aeb] | 502 | |
---|
[fa295e36] | 503 | return imcb_ask_add_cb_no( data ); |
---|
[7bf0f5f0] | 504 | } |
---|
| 505 | |
---|
[fa295e36] | 506 | void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname ) |
---|
[b7d3cc34] | 507 | { |
---|
[fa295e36] | 508 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
[7bf0f5f0] | 509 | char *s; |
---|
| 510 | |
---|
| 511 | /* TODO: Make a setting for this! */ |
---|
[e00da63] | 512 | if( bee_user_by_handle( ic->bee, ic, handle ) != NULL ) |
---|
[7bf0f5f0] | 513 | return; |
---|
| 514 | |
---|
| 515 | s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle ); |
---|
| 516 | |
---|
[0da65d5] | 517 | data->ic = ic; |
---|
[7bf0f5f0] | 518 | data->handle = g_strdup( handle ); |
---|
[e00da63] | 519 | query_add( (irc_t *) ic->bee->ui_data, ic, s, |
---|
| 520 | imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data ); |
---|
[b7d3cc34] | 521 | } |
---|
| 522 | |
---|
[81e04e1] | 523 | struct bee_user *imcb_buddy_by_handle( struct im_connection *ic, const char *handle ) |
---|
| 524 | { |
---|
| 525 | return bee_user_by_handle( ic->bee, ic, handle ); |
---|
[b7d3cc34] | 526 | } |
---|
| 527 | |
---|
| 528 | |
---|
| 529 | /* Misc. BitlBee stuff which shouldn't really be here */ |
---|
[81e04e1] | 530 | #if 0 |
---|
[5c9512f] | 531 | char *set_eval_away_devoice( set_t *set, char *value ) |
---|
[b7d3cc34] | 532 | { |
---|
[5c9512f] | 533 | irc_t *irc = set->data; |
---|
[b7d3cc34] | 534 | int st; |
---|
| 535 | |
---|
[7125cb3] | 536 | if( !is_bool( value ) ) |
---|
| 537 | return SET_INVALID; |
---|
[b7d3cc34] | 538 | |
---|
[7125cb3] | 539 | st = bool2int( value ); |
---|
[b7d3cc34] | 540 | |
---|
| 541 | /* Horror.... */ |
---|
| 542 | |
---|
[10a96f4] | 543 | if( st != set_getbool( &irc->b->set, "away_devoice" ) ) |
---|
[b7d3cc34] | 544 | { |
---|
| 545 | char list[80] = ""; |
---|
| 546 | user_t *u = irc->users; |
---|
| 547 | int i = 0, count = 0; |
---|
| 548 | char pm; |
---|
| 549 | char v[80]; |
---|
| 550 | |
---|
| 551 | if( st ) |
---|
| 552 | pm = '+'; |
---|
| 553 | else |
---|
| 554 | pm = '-'; |
---|
| 555 | |
---|
| 556 | while( u ) |
---|
| 557 | { |
---|
[0da65d5] | 558 | if( u->ic && u->online && !u->away ) |
---|
[b7d3cc34] | 559 | { |
---|
| 560 | if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) |
---|
| 561 | { |
---|
| 562 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
[2087159] | 563 | irc_write( irc, ":%s MODE %s %c%s%s", |
---|
| 564 | irc->myhost, |
---|
[b7d3cc34] | 565 | irc->channel, pm, v, list ); |
---|
| 566 | |
---|
| 567 | *list = 0; |
---|
| 568 | count = 0; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | sprintf( list + strlen( list ), " %s", u->nick ); |
---|
| 572 | count ++; |
---|
| 573 | } |
---|
| 574 | u = u->next; |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | /* $v = 'v' x $i */ |
---|
| 578 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
[2087159] | 579 | irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, |
---|
[b7d3cc34] | 580 | irc->channel, pm, v, list ); |
---|
| 581 | } |
---|
| 582 | |
---|
[7125cb3] | 583 | return value; |
---|
[b7d3cc34] | 584 | } |
---|
[81e04e1] | 585 | #endif |
---|
[226fce1] | 586 | |
---|
| 587 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
| 588 | them all from some wrappers. We'll start to define some down here: */ |
---|
| 589 | |
---|
[84b045d] | 590 | int imc_chat_msg( struct groupchat *c, char *msg, int flags ) |
---|
[b7d3cc34] | 591 | { |
---|
[e27661d] | 592 | char *buf = NULL; |
---|
[b7d3cc34] | 593 | |
---|
[6bbb939] | 594 | if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
[e27661d] | 595 | { |
---|
| 596 | buf = escape_html( msg ); |
---|
[b7d3cc34] | 597 | msg = buf; |
---|
| 598 | } |
---|
| 599 | |
---|
[f6c963b] | 600 | c->ic->acc->prpl->chat_msg( c, msg, flags ); |
---|
[e27661d] | 601 | g_free( buf ); |
---|
| 602 | |
---|
[0da65d5] | 603 | return 1; |
---|
[b7d3cc34] | 604 | } |
---|
[226fce1] | 605 | |
---|
[34fbbf9] | 606 | static char *imc_away_state_find( GList *gcm, char *away, char **message ); |
---|
[226fce1] | 607 | |
---|
[58adb7e] | 608 | int imc_away_send_update( struct im_connection *ic ) |
---|
[226fce1] | 609 | { |
---|
[3e1ef92c] | 610 | char *away, *msg = NULL; |
---|
[226fce1] | 611 | |
---|
[91cec2f] | 612 | if( ic->acc->prpl->away_states == NULL || |
---|
| 613 | ic->acc->prpl->set_away == NULL ) |
---|
| 614 | return 0; |
---|
| 615 | |
---|
[58adb7e] | 616 | away = set_getstr( &ic->acc->set, "away" ) ? |
---|
[81e04e1] | 617 | : set_getstr( &ic->bee->set, "away" ); |
---|
[34fbbf9] | 618 | if( away && *away ) |
---|
[226fce1] | 619 | { |
---|
[34fbbf9] | 620 | GList *m = ic->acc->prpl->away_states( ic ); |
---|
[58adb7e] | 621 | msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; |
---|
| 622 | away = imc_away_state_find( m, away, &msg ) ? : m->data; |
---|
| 623 | } |
---|
| 624 | else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE ) |
---|
| 625 | { |
---|
| 626 | away = NULL; |
---|
| 627 | msg = set_getstr( &ic->acc->set, "status" ) ? |
---|
[81e04e1] | 628 | : set_getstr( &ic->bee->set, "status" ); |
---|
[226fce1] | 629 | } |
---|
| 630 | |
---|
[58adb7e] | 631 | ic->acc->prpl->set_away( ic, away, msg ); |
---|
[226fce1] | 632 | |
---|
[34fbbf9] | 633 | return 1; |
---|
[226fce1] | 634 | } |
---|
| 635 | |
---|
[84b045d] | 636 | static char *imc_away_alias_list[8][5] = |
---|
[226fce1] | 637 | { |
---|
| 638 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
| 639 | { "NA", "N/A", "Not available", NULL }, |
---|
| 640 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
| 641 | { "Be right back", "BRB", NULL }, |
---|
| 642 | { "On the phone", "Phone", "On phone", NULL }, |
---|
| 643 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
| 644 | { "Invisible", "Hidden" }, |
---|
| 645 | { NULL } |
---|
| 646 | }; |
---|
| 647 | |
---|
[34fbbf9] | 648 | static char *imc_away_state_find( GList *gcm, char *away, char **message ) |
---|
[226fce1] | 649 | { |
---|
| 650 | GList *m; |
---|
| 651 | int i, j; |
---|
| 652 | |
---|
[34fbbf9] | 653 | for( m = gcm; m; m = m->next ) |
---|
| 654 | if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) |
---|
| 655 | { |
---|
| 656 | /* At least the Yahoo! module works better if message |
---|
| 657 | contains no data unless it adds something to what |
---|
| 658 | we have in state already. */ |
---|
| 659 | if( strlen( m->data ) == strlen( away ) ) |
---|
| 660 | *message = NULL; |
---|
| 661 | |
---|
| 662 | return m->data; |
---|
| 663 | } |
---|
| 664 | |
---|
[84b045d] | 665 | for( i = 0; *imc_away_alias_list[i]; i ++ ) |
---|
[226fce1] | 666 | { |
---|
[34fbbf9] | 667 | int keep_message; |
---|
| 668 | |
---|
[84b045d] | 669 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
| 670 | if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 ) |
---|
[34fbbf9] | 671 | { |
---|
| 672 | keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] ); |
---|
[226fce1] | 673 | break; |
---|
[34fbbf9] | 674 | } |
---|
[226fce1] | 675 | |
---|
[84b045d] | 676 | if( !imc_away_alias_list[i][j] ) /* If we reach the end, this row */ |
---|
[226fce1] | 677 | continue; /* is not what we want. Next! */ |
---|
| 678 | |
---|
| 679 | /* Now find an entry in this row which exists in gcm */ |
---|
[84b045d] | 680 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
[226fce1] | 681 | { |
---|
[34fbbf9] | 682 | for( m = gcm; m; m = m->next ) |
---|
[84b045d] | 683 | if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 ) |
---|
[34fbbf9] | 684 | { |
---|
| 685 | if( !keep_message ) |
---|
| 686 | *message = NULL; |
---|
| 687 | |
---|
| 688 | return imc_away_alias_list[i][j]; |
---|
| 689 | } |
---|
[226fce1] | 690 | } |
---|
[34fbbf9] | 691 | |
---|
| 692 | /* No need to look further, apparently this state doesn't |
---|
| 693 | have any good alias for this protocol. */ |
---|
| 694 | break; |
---|
[226fce1] | 695 | } |
---|
| 696 | |
---|
[34fbbf9] | 697 | return NULL; |
---|
[226fce1] | 698 | } |
---|
[da3b536] | 699 | |
---|
[84b045d] | 700 | void imc_add_allow( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 701 | { |
---|
[0da65d5] | 702 | if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
[da3b536] | 703 | { |
---|
[0da65d5] | 704 | ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) ); |
---|
[da3b536] | 705 | } |
---|
| 706 | |
---|
[0da65d5] | 707 | ic->acc->prpl->add_permit( ic, handle ); |
---|
[da3b536] | 708 | } |
---|
| 709 | |
---|
[84b045d] | 710 | void imc_rem_allow( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 711 | { |
---|
| 712 | GSList *l; |
---|
| 713 | |
---|
[0da65d5] | 714 | if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
[da3b536] | 715 | { |
---|
| 716 | g_free( l->data ); |
---|
[0da65d5] | 717 | ic->permit = g_slist_delete_link( ic->permit, l ); |
---|
[da3b536] | 718 | } |
---|
| 719 | |
---|
[0da65d5] | 720 | ic->acc->prpl->rem_permit( ic, handle ); |
---|
[da3b536] | 721 | } |
---|
| 722 | |
---|
[84b045d] | 723 | void imc_add_block( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 724 | { |
---|
[0da65d5] | 725 | if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
[da3b536] | 726 | { |
---|
[0da65d5] | 727 | ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) ); |
---|
[da3b536] | 728 | } |
---|
| 729 | |
---|
[0da65d5] | 730 | ic->acc->prpl->add_deny( ic, handle ); |
---|
[da3b536] | 731 | } |
---|
| 732 | |
---|
[84b045d] | 733 | void imc_rem_block( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 734 | { |
---|
| 735 | GSList *l; |
---|
| 736 | |
---|
[0da65d5] | 737 | if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
[da3b536] | 738 | { |
---|
| 739 | g_free( l->data ); |
---|
[0da65d5] | 740 | ic->deny = g_slist_delete_link( ic->deny, l ); |
---|
[da3b536] | 741 | } |
---|
| 742 | |
---|
[0da65d5] | 743 | ic->acc->prpl->rem_deny( ic, handle ); |
---|
[da3b536] | 744 | } |
---|
[85023c6] | 745 | |
---|
| 746 | void imcb_clean_handle( struct im_connection *ic, char *handle ) |
---|
| 747 | { |
---|
| 748 | /* Accepts a handle and does whatever is necessary to make it |
---|
| 749 | BitlBee-friendly. Currently this means removing everything |
---|
| 750 | outside 33-127 (ASCII printable excl spaces), @ (only one |
---|
| 751 | is allowed) and ! and : */ |
---|
| 752 | char out[strlen(handle)+1]; |
---|
| 753 | int s, d; |
---|
| 754 | |
---|
| 755 | s = d = 0; |
---|
| 756 | while( handle[s] ) |
---|
| 757 | { |
---|
| 758 | if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' && |
---|
| 759 | ( handle[s] & 0x80 ) == 0 ) |
---|
| 760 | { |
---|
| 761 | if( handle[s] == '@' ) |
---|
| 762 | { |
---|
| 763 | /* See if we got an @ already? */ |
---|
| 764 | out[d] = 0; |
---|
| 765 | if( strchr( out, '@' ) ) |
---|
| 766 | continue; |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | out[d++] = handle[s]; |
---|
| 770 | } |
---|
| 771 | s ++; |
---|
| 772 | } |
---|
| 773 | out[d] = handle[s]; |
---|
| 774 | |
---|
| 775 | strcpy( handle, out ); |
---|
| 776 | } |
---|