[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 | |
---|
[764b163d] | 40 | static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ); |
---|
[b7d3cc34] | 41 | |
---|
| 42 | GSList *connections; |
---|
| 43 | |
---|
[65e2ce1] | 44 | #ifdef WITH_PLUGINS |
---|
[7b23afd] | 45 | gboolean load_plugin(char *path) |
---|
| 46 | { |
---|
| 47 | void (*init_function) (void); |
---|
| 48 | |
---|
| 49 | GModule *mod = g_module_open(path, G_MODULE_BIND_LAZY); |
---|
| 50 | |
---|
| 51 | if(!mod) { |
---|
[8ad90fb] | 52 | log_message(LOGLVL_ERROR, "Can't find `%s', not loading (%s)\n", path, g_module_error()); |
---|
[7b23afd] | 53 | return FALSE; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | if(!g_module_symbol(mod,"init_plugin",(gpointer *) &init_function)) { |
---|
| 57 | log_message(LOGLVL_WARNING, "Can't find function `init_plugin' in `%s'\n", path); |
---|
| 58 | return FALSE; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | init_function(); |
---|
| 62 | |
---|
| 63 | return TRUE; |
---|
| 64 | } |
---|
[b7d3cc34] | 65 | |
---|
[65e2ce1] | 66 | void load_plugins(void) |
---|
| 67 | { |
---|
| 68 | GDir *dir; |
---|
| 69 | GError *error = NULL; |
---|
| 70 | |
---|
[4bfca70] | 71 | dir = g_dir_open(global.conf->plugindir, 0, &error); |
---|
[65e2ce1] | 72 | |
---|
| 73 | if (dir) { |
---|
| 74 | const gchar *entry; |
---|
| 75 | char *path; |
---|
| 76 | |
---|
| 77 | while ((entry = g_dir_read_name(dir))) { |
---|
[4bfca70] | 78 | path = g_build_filename(global.conf->plugindir, entry, NULL); |
---|
[65e2ce1] | 79 | if(!path) { |
---|
| 80 | log_message(LOGLVL_WARNING, "Can't build path for %s\n", entry); |
---|
| 81 | continue; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | load_plugin(path); |
---|
| 85 | |
---|
| 86 | g_free(path); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | g_dir_close(dir); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | #endif |
---|
[b7d3cc34] | 93 | |
---|
| 94 | /* nogaim.c */ |
---|
| 95 | |
---|
[7b23afd] | 96 | GList *protocols = NULL; |
---|
| 97 | |
---|
| 98 | void register_protocol (struct prpl *p) |
---|
| 99 | { |
---|
[90cd6c4] | 100 | int i; |
---|
| 101 | gboolean refused = global.conf->protocols != NULL; |
---|
| 102 | |
---|
| 103 | for (i = 0; global.conf->protocols && global.conf->protocols[i]; i++) |
---|
| 104 | { |
---|
| 105 | if (g_strcasecmp(p->name, global.conf->protocols[i]) == 0) |
---|
| 106 | refused = FALSE; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | if (refused) |
---|
| 110 | log_message(LOGLVL_WARNING, "Protocol %s disabled\n", p->name); |
---|
| 111 | else |
---|
| 112 | protocols = g_list_append(protocols, p); |
---|
[7b23afd] | 113 | } |
---|
| 114 | |
---|
| 115 | struct prpl *find_protocol(const char *name) |
---|
| 116 | { |
---|
| 117 | GList *gl; |
---|
| 118 | for (gl = protocols; gl; gl = gl->next) |
---|
| 119 | { |
---|
| 120 | struct prpl *proto = gl->data; |
---|
| 121 | if(!g_strcasecmp(proto->name, name)) |
---|
| 122 | return proto; |
---|
| 123 | } |
---|
| 124 | return NULL; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /* nogaim.c */ |
---|
[b7d3cc34] | 128 | void nogaim_init() |
---|
| 129 | { |
---|
[0da65d5] | 130 | extern void msn_initmodule(); |
---|
| 131 | extern void oscar_initmodule(); |
---|
| 132 | extern void byahoo_initmodule(); |
---|
| 133 | extern void jabber_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 | |
---|
[65e2ce1] | 151 | #ifdef WITH_PLUGINS |
---|
| 152 | load_plugins(); |
---|
[b7d3cc34] | 153 | #endif |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | GSList *get_connections() { return connections; } |
---|
| 157 | |
---|
| 158 | /* multi.c */ |
---|
| 159 | |
---|
[84b045d] | 160 | struct im_connection *imcb_new( account_t *acc ) |
---|
[b7d3cc34] | 161 | { |
---|
[0da65d5] | 162 | struct im_connection *ic; |
---|
[b7d3cc34] | 163 | |
---|
[0da65d5] | 164 | ic = g_new0( struct im_connection, 1 ); |
---|
[b7d3cc34] | 165 | |
---|
[0da65d5] | 166 | ic->irc = acc->irc; |
---|
| 167 | ic->acc = acc; |
---|
| 168 | acc->ic = ic; |
---|
[b7d3cc34] | 169 | |
---|
[0da65d5] | 170 | connections = g_slist_append( connections, ic ); |
---|
[b7d3cc34] | 171 | |
---|
[0da65d5] | 172 | return( ic ); |
---|
[b7d3cc34] | 173 | } |
---|
| 174 | |
---|
[aef4828] | 175 | void imc_free( struct im_connection *ic ) |
---|
[b7d3cc34] | 176 | { |
---|
| 177 | account_t *a; |
---|
| 178 | |
---|
| 179 | /* Destroy the pointer to this connection from the account list */ |
---|
[0da65d5] | 180 | for( a = ic->irc->accounts; a; a = a->next ) |
---|
| 181 | if( a->ic == ic ) |
---|
[b7d3cc34] | 182 | { |
---|
[0da65d5] | 183 | a->ic = NULL; |
---|
[b7d3cc34] | 184 | break; |
---|
| 185 | } |
---|
| 186 | |
---|
[0da65d5] | 187 | connections = g_slist_remove( connections, ic ); |
---|
| 188 | g_free( ic ); |
---|
[b7d3cc34] | 189 | } |
---|
| 190 | |
---|
[aef4828] | 191 | static void serv_got_crap( struct im_connection *ic, char *format, ... ) |
---|
[b7d3cc34] | 192 | { |
---|
| 193 | va_list params; |
---|
[e27661d] | 194 | char *text; |
---|
[dfde8e0] | 195 | account_t *a; |
---|
[b7d3cc34] | 196 | |
---|
| 197 | va_start( params, format ); |
---|
[e27661d] | 198 | text = g_strdup_vprintf( format, params ); |
---|
[b7d3cc34] | 199 | va_end( params ); |
---|
| 200 | |
---|
[10a96f4] | 201 | if( ( g_strcasecmp( set_getstr( &ic->irc->b->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 202 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->b->set, "strip_html" ) ) ) |
---|
[e27661d] | 203 | strip_html( text ); |
---|
[b7d3cc34] | 204 | |
---|
[dfde8e0] | 205 | /* Try to find a different connection on the same protocol. */ |
---|
[0da65d5] | 206 | for( a = ic->irc->accounts; a; a = a->next ) |
---|
| 207 | if( a->prpl == ic->acc->prpl && a->ic != ic ) |
---|
[dfde8e0] | 208 | break; |
---|
| 209 | |
---|
[e27661d] | 210 | /* If we found one, include the screenname in the message. */ |
---|
[dfde8e0] | 211 | if( a ) |
---|
[c2fb3809] | 212 | irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text ); |
---|
[dfde8e0] | 213 | else |
---|
[0da65d5] | 214 | irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text ); |
---|
[7b07dc6] | 215 | |
---|
[e27661d] | 216 | g_free( text ); |
---|
[b7d3cc34] | 217 | } |
---|
| 218 | |
---|
[84b045d] | 219 | void imcb_log( struct im_connection *ic, char *format, ... ) |
---|
[aef4828] | 220 | { |
---|
| 221 | va_list params; |
---|
| 222 | char *text; |
---|
| 223 | |
---|
| 224 | va_start( params, format ); |
---|
| 225 | text = g_strdup_vprintf( format, params ); |
---|
| 226 | va_end( params ); |
---|
| 227 | |
---|
| 228 | if( ic->flags & OPT_LOGGED_IN ) |
---|
| 229 | serv_got_crap( ic, "%s", text ); |
---|
| 230 | else |
---|
| 231 | serv_got_crap( ic, "Logging in: %s", text ); |
---|
| 232 | |
---|
| 233 | g_free( text ); |
---|
| 234 | } |
---|
| 235 | |
---|
[84b045d] | 236 | void imcb_error( struct im_connection *ic, char *format, ... ) |
---|
[aef4828] | 237 | { |
---|
| 238 | va_list params; |
---|
| 239 | char *text; |
---|
| 240 | |
---|
| 241 | va_start( params, format ); |
---|
| 242 | text = g_strdup_vprintf( format, params ); |
---|
| 243 | va_end( params ); |
---|
| 244 | |
---|
| 245 | if( ic->flags & OPT_LOGGED_IN ) |
---|
| 246 | serv_got_crap( ic, "Error: %s", text ); |
---|
| 247 | else |
---|
| 248 | serv_got_crap( ic, "Couldn't log in: %s", text ); |
---|
| 249 | |
---|
| 250 | g_free( text ); |
---|
| 251 | } |
---|
| 252 | |
---|
[ba9edaa] | 253 | static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond ) |
---|
[b7d3cc34] | 254 | { |
---|
[0da65d5] | 255 | struct im_connection *ic = d; |
---|
[b7d3cc34] | 256 | |
---|
[0da65d5] | 257 | if( ic->acc->prpl->keepalive ) |
---|
| 258 | ic->acc->prpl->keepalive( ic ); |
---|
[b7d3cc34] | 259 | |
---|
| 260 | return TRUE; |
---|
| 261 | } |
---|
| 262 | |
---|
[84b045d] | 263 | void imcb_connected( struct im_connection *ic ) |
---|
[b7d3cc34] | 264 | { |
---|
[3611717] | 265 | irc_t *irc = ic->irc; |
---|
| 266 | struct chat *c; |
---|
[b7d3cc34] | 267 | |
---|
| 268 | /* MSN servers sometimes redirect you to a different server and do |
---|
[84c1a0a] | 269 | the whole login sequence again, so these "late" calls to this |
---|
[b7d3cc34] | 270 | function should be handled correctly. (IOW, ignored) */ |
---|
[0da65d5] | 271 | if( ic->flags & OPT_LOGGED_IN ) |
---|
[b7d3cc34] | 272 | return; |
---|
| 273 | |
---|
[84b045d] | 274 | imcb_log( ic, "Logged in" ); |
---|
[b7d3cc34] | 275 | |
---|
[0da65d5] | 276 | ic->keepalive = b_timeout_add( 60000, send_keepalive, ic ); |
---|
| 277 | ic->flags |= OPT_LOGGED_IN; |
---|
[b7d3cc34] | 278 | |
---|
[58adb7e] | 279 | /* Necessary to send initial presence status, even if we're not away. */ |
---|
| 280 | imc_away_send_update( ic ); |
---|
[280e655] | 281 | |
---|
| 282 | /* Apparently we're connected successfully, so reset the |
---|
| 283 | exponential backoff timer. */ |
---|
| 284 | ic->acc->auto_reconnect_delay = 0; |
---|
[3611717] | 285 | |
---|
[10a96f4] | 286 | /* |
---|
[3611717] | 287 | for( c = irc->chatrooms; c; c = c->next ) |
---|
| 288 | { |
---|
| 289 | if( c->acc != ic->acc ) |
---|
| 290 | continue; |
---|
| 291 | |
---|
| 292 | if( set_getbool( &c->set, "auto_join" ) ) |
---|
[94acdd0] | 293 | chat_join( irc, c, NULL ); |
---|
[3611717] | 294 | } |
---|
[10a96f4] | 295 | */ |
---|
[b7d3cc34] | 296 | } |
---|
| 297 | |
---|
[ba9edaa] | 298 | gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond ) |
---|
[b7d3cc34] | 299 | { |
---|
| 300 | account_t *a = data; |
---|
| 301 | |
---|
| 302 | a->reconnect = 0; |
---|
| 303 | account_on( a->irc, a ); |
---|
| 304 | |
---|
| 305 | return( FALSE ); /* Only have to run the timeout once */ |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | void cancel_auto_reconnect( account_t *a ) |
---|
| 309 | { |
---|
[c98be00] | 310 | b_event_remove( a->reconnect ); |
---|
[b7d3cc34] | 311 | a->reconnect = 0; |
---|
| 312 | } |
---|
| 313 | |
---|
[c2fb3809] | 314 | void imc_logout( struct im_connection *ic, int allow_reconnect ) |
---|
[b7d3cc34] | 315 | { |
---|
[0da65d5] | 316 | irc_t *irc = ic->irc; |
---|
[c9c7ca7] | 317 | user_t *t, *u; |
---|
[b7d3cc34] | 318 | account_t *a; |
---|
[4230221] | 319 | int delay; |
---|
[b7d3cc34] | 320 | |
---|
[8d74291] | 321 | /* Nested calls might happen sometimes, this is probably the best |
---|
| 322 | place to catch them. */ |
---|
[0da65d5] | 323 | if( ic->flags & OPT_LOGGING_OUT ) |
---|
[8d74291] | 324 | return; |
---|
[66f783f] | 325 | else |
---|
[0da65d5] | 326 | ic->flags |= OPT_LOGGING_OUT; |
---|
[8d74291] | 327 | |
---|
[84b045d] | 328 | imcb_log( ic, "Signing off.." ); |
---|
[fb62f81f] | 329 | |
---|
[0da65d5] | 330 | b_event_remove( ic->keepalive ); |
---|
| 331 | ic->keepalive = 0; |
---|
| 332 | ic->acc->prpl->logout( ic ); |
---|
| 333 | b_event_remove( ic->inpa ); |
---|
[b7d3cc34] | 334 | |
---|
[c0c43fb] | 335 | g_free( ic->away ); |
---|
| 336 | ic->away = NULL; |
---|
| 337 | |
---|
[c9c7ca7] | 338 | u = irc->users; |
---|
[b7d3cc34] | 339 | while( u ) |
---|
| 340 | { |
---|
[0da65d5] | 341 | if( u->ic == ic ) |
---|
[b7d3cc34] | 342 | { |
---|
| 343 | t = u->next; |
---|
| 344 | user_del( irc, u->nick ); |
---|
| 345 | u = t; |
---|
| 346 | } |
---|
| 347 | else |
---|
| 348 | u = u->next; |
---|
| 349 | } |
---|
| 350 | |
---|
[0da65d5] | 351 | query_del_by_conn( ic->irc, ic ); |
---|
[b7d3cc34] | 352 | |
---|
| 353 | for( a = irc->accounts; a; a = a->next ) |
---|
[0da65d5] | 354 | if( a->ic == ic ) |
---|
[b7d3cc34] | 355 | break; |
---|
| 356 | |
---|
| 357 | if( !a ) |
---|
| 358 | { |
---|
| 359 | /* Uhm... This is very sick. */ |
---|
| 360 | } |
---|
[10a96f4] | 361 | else if( allow_reconnect && set_getbool( &irc->b->set, "auto_reconnect" ) && |
---|
[4230221] | 362 | set_getbool( &a->set, "auto_reconnect" ) && |
---|
| 363 | ( delay = account_reconnect_delay( a ) ) > 0 ) |
---|
[b7d3cc34] | 364 | { |
---|
[84b045d] | 365 | imcb_log( ic, "Reconnecting in %d seconds..", delay ); |
---|
[c98be00] | 366 | a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a ); |
---|
[b7d3cc34] | 367 | } |
---|
| 368 | |
---|
[aef4828] | 369 | imc_free( ic ); |
---|
[b7d3cc34] | 370 | } |
---|
| 371 | |
---|
| 372 | |
---|
| 373 | /* dialogs.c */ |
---|
| 374 | |
---|
[9143aeb] | 375 | void imcb_ask( struct im_connection *ic, char *msg, void *data, |
---|
| 376 | query_callback doit, query_callback dont ) |
---|
[b7d3cc34] | 377 | { |
---|
[0da65d5] | 378 | query_add( ic->irc, ic, msg, doit, dont, data ); |
---|
[b7d3cc34] | 379 | } |
---|
| 380 | |
---|
| 381 | |
---|
| 382 | /* list.c */ |
---|
| 383 | |
---|
[c6ca3ee] | 384 | void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group ) |
---|
[b7d3cc34] | 385 | { |
---|
| 386 | user_t *u; |
---|
[f0cb961] | 387 | char nick[MAX_NICK_LENGTH+1], *s; |
---|
[0da65d5] | 388 | irc_t *irc = ic->irc; |
---|
[b7d3cc34] | 389 | |
---|
[0da65d5] | 390 | if( user_findhandle( ic, handle ) ) |
---|
[b7d3cc34] | 391 | { |
---|
[10a96f4] | 392 | if( set_getbool( &irc->b->set, "debug" ) ) |
---|
[84b045d] | 393 | imcb_log( ic, "User already exists, ignoring add request: %s", handle ); |
---|
[b7d3cc34] | 394 | |
---|
| 395 | return; |
---|
| 396 | |
---|
[f0cb961] | 397 | /* Buddy seems to exist already. Let's ignore this request then... |
---|
| 398 | Eventually subsequent calls to this function *should* be possible |
---|
| 399 | when a buddy is in multiple groups. But for now BitlBee doesn't |
---|
| 400 | even support groups so let's silently ignore this for now. */ |
---|
[b7d3cc34] | 401 | } |
---|
| 402 | |
---|
| 403 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
[d323394c] | 404 | strcpy( nick, nick_get( ic->acc, handle ) ); |
---|
[b7d3cc34] | 405 | |
---|
[0da65d5] | 406 | u = user_add( ic->irc, nick ); |
---|
[b7d3cc34] | 407 | |
---|
[f0cb961] | 408 | // if( !realname || !*realname ) realname = nick; |
---|
| 409 | // u->realname = g_strdup( realname ); |
---|
[b7d3cc34] | 410 | |
---|
| 411 | if( ( s = strchr( handle, '@' ) ) ) |
---|
| 412 | { |
---|
| 413 | u->host = g_strdup( s + 1 ); |
---|
| 414 | u->user = g_strndup( handle, s - handle ); |
---|
| 415 | } |
---|
[0da65d5] | 416 | else if( ic->acc->server ) |
---|
[b7d3cc34] | 417 | { |
---|
[f0cb961] | 418 | u->host = g_strdup( ic->acc->server ); |
---|
[b7d3cc34] | 419 | u->user = g_strdup( handle ); |
---|
| 420 | |
---|
| 421 | /* s/ /_/ ... important for AOL screennames */ |
---|
| 422 | for( s = u->user; *s; s ++ ) |
---|
| 423 | if( *s == ' ' ) |
---|
| 424 | *s = '_'; |
---|
| 425 | } |
---|
| 426 | else |
---|
| 427 | { |
---|
[0da65d5] | 428 | u->host = g_strdup( ic->acc->prpl->name ); |
---|
[b7d3cc34] | 429 | u->user = g_strdup( handle ); |
---|
| 430 | } |
---|
| 431 | |
---|
[0da65d5] | 432 | u->ic = ic; |
---|
[b7d3cc34] | 433 | u->handle = g_strdup( handle ); |
---|
[9b8a38b] | 434 | if( group ) u->group = g_strdup( group ); |
---|
[b7d3cc34] | 435 | u->send_handler = buddy_send_handler; |
---|
| 436 | u->last_typing_notice = 0; |
---|
| 437 | } |
---|
| 438 | |
---|
[f0cb961] | 439 | struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle ) |
---|
[b7d3cc34] | 440 | { |
---|
| 441 | static struct buddy b[1]; |
---|
| 442 | user_t *u; |
---|
| 443 | |
---|
[0da65d5] | 444 | u = user_findhandle( ic, handle ); |
---|
[b7d3cc34] | 445 | |
---|
| 446 | if( !u ) |
---|
| 447 | return( NULL ); |
---|
[9624fdf] | 448 | |
---|
[b7d3cc34] | 449 | memset( b, 0, sizeof( b ) ); |
---|
| 450 | strncpy( b->name, handle, 80 ); |
---|
| 451 | strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); |
---|
| 452 | b->present = u->online; |
---|
[0da65d5] | 453 | b->ic = u->ic; |
---|
[b7d3cc34] | 454 | |
---|
| 455 | return( b ); |
---|
| 456 | } |
---|
| 457 | |
---|
[c6ca3ee] | 458 | void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *realname ) |
---|
[b7d3cc34] | 459 | { |
---|
[0da65d5] | 460 | user_t *u = user_findhandle( ic, handle ); |
---|
[286b28e] | 461 | char *set; |
---|
[b7d3cc34] | 462 | |
---|
[f0cb961] | 463 | if( !u || !realname ) return; |
---|
[b7d3cc34] | 464 | |
---|
[e27661d] | 465 | if( g_strcasecmp( u->realname, realname ) != 0 ) |
---|
[b7d3cc34] | 466 | { |
---|
| 467 | if( u->realname != u->nick ) g_free( u->realname ); |
---|
| 468 | |
---|
[e27661d] | 469 | u->realname = g_strdup( realname ); |
---|
[b7d3cc34] | 470 | |
---|
[10a96f4] | 471 | if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->b->set, "display_namechanges" ) ) |
---|
[84b045d] | 472 | imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname ); |
---|
[b7d3cc34] | 473 | } |
---|
[286b28e] | 474 | |
---|
| 475 | set = set_getstr( &ic->acc->set, "nick_source" ); |
---|
| 476 | if( strcmp( set, "handle" ) != 0 ) |
---|
| 477 | { |
---|
| 478 | char *name = g_strdup( realname ); |
---|
| 479 | |
---|
| 480 | if( strcmp( set, "first_name" ) == 0 ) |
---|
| 481 | { |
---|
| 482 | int i; |
---|
| 483 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
| 484 | name[i] = '\0'; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | imcb_buddy_nick_hint( ic, handle, name ); |
---|
| 488 | |
---|
| 489 | g_free( name ); |
---|
| 490 | } |
---|
[b7d3cc34] | 491 | } |
---|
| 492 | |
---|
[c6ca3ee] | 493 | void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group ) |
---|
[998b103] | 494 | { |
---|
| 495 | user_t *u; |
---|
| 496 | |
---|
| 497 | if( ( u = user_findhandle( ic, handle ) ) ) |
---|
| 498 | user_del( ic->irc, u->nick ); |
---|
| 499 | } |
---|
| 500 | |
---|
[d06eabf] | 501 | /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM |
---|
| 502 | modules to suggest a nickname for a handle. */ |
---|
[fb00989] | 503 | void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick ) |
---|
[d06eabf] | 504 | { |
---|
| 505 | user_t *u = user_findhandle( ic, handle ); |
---|
[43d8cc5] | 506 | char newnick[MAX_NICK_LENGTH+1], *orig_nick; |
---|
[d06eabf] | 507 | |
---|
[e0e2a71] | 508 | if( u && !u->online && !nick_saved( ic->acc, handle ) ) |
---|
[d06eabf] | 509 | { |
---|
| 510 | /* Only do this if the person isn't online yet (which should |
---|
| 511 | be the case if we just added it) and if the user hasn't |
---|
| 512 | assigned a nickname to this buddy already. */ |
---|
| 513 | |
---|
[e0e2a71] | 514 | strncpy( newnick, nick, MAX_NICK_LENGTH ); |
---|
| 515 | newnick[MAX_NICK_LENGTH] = 0; |
---|
[d06eabf] | 516 | |
---|
| 517 | /* Some processing to make sure this string is a valid IRC nickname. */ |
---|
| 518 | nick_strip( newnick ); |
---|
[10a96f4] | 519 | if( set_getbool( &ic->irc->b->set, "lcnicks" ) ) |
---|
[d06eabf] | 520 | nick_lc( newnick ); |
---|
| 521 | |
---|
[1962ac1] | 522 | if( strcmp( u->nick, newnick ) != 0 ) |
---|
| 523 | { |
---|
| 524 | /* Only do this if newnick is different from the current one. |
---|
| 525 | If rejoining a channel, maybe we got this nick already |
---|
| 526 | (and dedupe would only add an underscore. */ |
---|
| 527 | nick_dedupe( ic->acc, handle, newnick ); |
---|
| 528 | |
---|
| 529 | /* u->nick will be freed halfway the process, so it can't be |
---|
| 530 | passed as an argument. */ |
---|
| 531 | orig_nick = g_strdup( u->nick ); |
---|
| 532 | user_rename( ic->irc, orig_nick, newnick ); |
---|
| 533 | g_free( orig_nick ); |
---|
| 534 | } |
---|
[d06eabf] | 535 | } |
---|
| 536 | } |
---|
[b7d3cc34] | 537 | |
---|
| 538 | |
---|
[fa295e36] | 539 | struct imcb_ask_cb_data |
---|
[7bf0f5f0] | 540 | { |
---|
[0da65d5] | 541 | struct im_connection *ic; |
---|
[7bf0f5f0] | 542 | char *handle; |
---|
| 543 | }; |
---|
| 544 | |
---|
[fa295e36] | 545 | static void imcb_ask_auth_cb_no( void *data ) |
---|
[7bf0f5f0] | 546 | { |
---|
[fa295e36] | 547 | struct imcb_ask_cb_data *cbd = data; |
---|
| 548 | |
---|
| 549 | cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle ); |
---|
| 550 | |
---|
| 551 | g_free( cbd->handle ); |
---|
| 552 | g_free( cbd ); |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | static void imcb_ask_auth_cb_yes( void *data ) |
---|
| 556 | { |
---|
| 557 | struct imcb_ask_cb_data *cbd = data; |
---|
| 558 | |
---|
| 559 | cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle ); |
---|
| 560 | |
---|
| 561 | g_free( cbd->handle ); |
---|
| 562 | g_free( cbd ); |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname ) |
---|
| 566 | { |
---|
| 567 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
| 568 | char *s, *realname_ = NULL; |
---|
| 569 | |
---|
| 570 | if( realname != NULL ) |
---|
| 571 | realname_ = g_strdup_printf( " (%s)", realname ); |
---|
| 572 | |
---|
| 573 | s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.", |
---|
| 574 | handle, realname_ ?: "" ); |
---|
| 575 | |
---|
| 576 | g_free( realname_ ); |
---|
| 577 | |
---|
| 578 | data->ic = ic; |
---|
| 579 | data->handle = g_strdup( handle ); |
---|
| 580 | query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data ); |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | |
---|
| 584 | static void imcb_ask_add_cb_no( void *data ) |
---|
| 585 | { |
---|
| 586 | g_free( ((struct imcb_ask_cb_data*)data)->handle ); |
---|
[7bf0f5f0] | 587 | g_free( data ); |
---|
| 588 | } |
---|
| 589 | |
---|
[fa295e36] | 590 | static void imcb_ask_add_cb_yes( void *data ) |
---|
[7bf0f5f0] | 591 | { |
---|
[fa295e36] | 592 | struct imcb_ask_cb_data *cbd = data; |
---|
[7bf0f5f0] | 593 | |
---|
[fa295e36] | 594 | cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL ); |
---|
[9143aeb] | 595 | |
---|
[fa295e36] | 596 | return imcb_ask_add_cb_no( data ); |
---|
[7bf0f5f0] | 597 | } |
---|
| 598 | |
---|
[fa295e36] | 599 | void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname ) |
---|
[b7d3cc34] | 600 | { |
---|
[fa295e36] | 601 | struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); |
---|
[7bf0f5f0] | 602 | char *s; |
---|
| 603 | |
---|
| 604 | /* TODO: Make a setting for this! */ |
---|
[0da65d5] | 605 | if( user_findhandle( ic, handle ) != NULL ) |
---|
[7bf0f5f0] | 606 | return; |
---|
| 607 | |
---|
| 608 | s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle ); |
---|
| 609 | |
---|
[0da65d5] | 610 | data->ic = ic; |
---|
[7bf0f5f0] | 611 | data->handle = g_strdup( handle ); |
---|
[fa295e36] | 612 | query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data ); |
---|
[b7d3cc34] | 613 | } |
---|
| 614 | |
---|
| 615 | |
---|
| 616 | /* server.c */ |
---|
| 617 | |
---|
[6bbb939] | 618 | void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) |
---|
[b7d3cc34] | 619 | { |
---|
| 620 | user_t *u; |
---|
| 621 | int oa, oo; |
---|
| 622 | |
---|
[6bbb939] | 623 | u = user_findhandle( ic, (char*) handle ); |
---|
[b7d3cc34] | 624 | |
---|
| 625 | if( !u ) |
---|
| 626 | { |
---|
[10a96f4] | 627 | if( g_strcasecmp( set_getstr( &ic->irc->b->set, "handle_unknown" ), "add" ) == 0 ) |
---|
[b7d3cc34] | 628 | { |
---|
[f0cb961] | 629 | imcb_add_buddy( ic, (char*) handle, NULL ); |
---|
[6bbb939] | 630 | u = user_findhandle( ic, (char*) handle ); |
---|
[b7d3cc34] | 631 | } |
---|
| 632 | else |
---|
| 633 | { |
---|
[10a96f4] | 634 | if( set_getbool( &ic->irc->b->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->b->set, "handle_unknown" ), "ignore" ) != 0 ) |
---|
[b7d3cc34] | 635 | { |
---|
[6bbb939] | 636 | imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle ); |
---|
| 637 | imcb_log( ic, "flags = %d, state = %s, message = %s", flags, |
---|
| 638 | state ? state : "NULL", message ? message : "NULL" ); |
---|
[b7d3cc34] | 639 | } |
---|
| 640 | |
---|
| 641 | return; |
---|
| 642 | } |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | oa = u->away != NULL; |
---|
| 646 | oo = u->online; |
---|
| 647 | |
---|
[449a51d] | 648 | g_free( u->away ); |
---|
| 649 | g_free( u->status_msg ); |
---|
| 650 | u->away = u->status_msg = NULL; |
---|
[b7d3cc34] | 651 | |
---|
[6bbb939] | 652 | if( ( flags & OPT_LOGGED_IN ) && !u->online ) |
---|
[b7d3cc34] | 653 | { |
---|
[0da65d5] | 654 | irc_spawn( ic->irc, u ); |
---|
[b7d3cc34] | 655 | u->online = 1; |
---|
| 656 | } |
---|
[6bbb939] | 657 | else if( !( flags & OPT_LOGGED_IN ) && u->online ) |
---|
[b7d3cc34] | 658 | { |
---|
[0da65d5] | 659 | struct groupchat *c; |
---|
[b7d3cc34] | 660 | |
---|
[0da65d5] | 661 | irc_kill( ic->irc, u ); |
---|
[b7d3cc34] | 662 | u->online = 0; |
---|
| 663 | |
---|
[e35d1a1] | 664 | /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */ |
---|
| 665 | for( c = ic->groupchats; c; c = c->next ) |
---|
[764b163d] | 666 | remove_chat_buddy_silent( c, handle ); |
---|
[b7d3cc34] | 667 | } |
---|
| 668 | |
---|
[6bbb939] | 669 | if( flags & OPT_AWAY ) |
---|
[b7d3cc34] | 670 | { |
---|
[6bbb939] | 671 | if( state && message ) |
---|
| 672 | { |
---|
| 673 | u->away = g_strdup_printf( "%s (%s)", state, message ); |
---|
| 674 | } |
---|
| 675 | else if( state ) |
---|
| 676 | { |
---|
| 677 | u->away = g_strdup( state ); |
---|
| 678 | } |
---|
| 679 | else if( message ) |
---|
| 680 | { |
---|
| 681 | u->away = g_strdup( message ); |
---|
| 682 | } |
---|
| 683 | else |
---|
| 684 | { |
---|
| 685 | u->away = g_strdup( "Away" ); |
---|
| 686 | } |
---|
[b7d3cc34] | 687 | } |
---|
[449a51d] | 688 | else |
---|
| 689 | { |
---|
| 690 | u->status_msg = g_strdup( message ); |
---|
| 691 | } |
---|
[b7d3cc34] | 692 | |
---|
| 693 | /* LISPy... */ |
---|
[10a96f4] | 694 | if( ( set_getbool( &ic->irc->b->set, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ |
---|
[b7d3cc34] | 695 | ( u->online ) && /* Don't touch offline people */ |
---|
| 696 | ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ |
---|
| 697 | ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ |
---|
| 698 | { |
---|
[1186382] | 699 | char *from; |
---|
| 700 | |
---|
[10a96f4] | 701 | if( set_getbool( &ic->irc->b->set, "simulate_netsplit" ) ) |
---|
[1186382] | 702 | { |
---|
| 703 | from = g_strdup( ic->irc->myhost ); |
---|
| 704 | } |
---|
| 705 | else |
---|
| 706 | { |
---|
| 707 | from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick, |
---|
| 708 | ic->irc->myhost ); |
---|
| 709 | } |
---|
| 710 | irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel, |
---|
| 711 | u->away?'-':'+', u->nick ); |
---|
| 712 | g_free( from ); |
---|
[b7d3cc34] | 713 | } |
---|
| 714 | } |
---|
| 715 | |
---|
[c6ca3ee] | 716 | void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) |
---|
[b7d3cc34] | 717 | { |
---|
[0da65d5] | 718 | irc_t *irc = ic->irc; |
---|
[d444c09] | 719 | char *wrapped; |
---|
[b7d3cc34] | 720 | user_t *u; |
---|
| 721 | |
---|
[0da65d5] | 722 | u = user_findhandle( ic, handle ); |
---|
[b7d3cc34] | 723 | |
---|
| 724 | if( !u ) |
---|
| 725 | { |
---|
[10a96f4] | 726 | char *h = set_getstr( &irc->b->set, "handle_unknown" ); |
---|
[b7d3cc34] | 727 | |
---|
| 728 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
| 729 | { |
---|
[10a96f4] | 730 | if( set_getbool( &irc->b->set, "debug" ) ) |
---|
[84b045d] | 731 | imcb_log( ic, "Ignoring message from unknown handle %s", handle ); |
---|
[b7d3cc34] | 732 | |
---|
| 733 | return; |
---|
| 734 | } |
---|
| 735 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
| 736 | { |
---|
[10a96f4] | 737 | int private = set_getbool( &irc->b->set, "private" ); |
---|
[b7d3cc34] | 738 | |
---|
| 739 | if( h[3] ) |
---|
| 740 | { |
---|
| 741 | if( g_strcasecmp( h + 3, "_private" ) == 0 ) |
---|
| 742 | private = 1; |
---|
| 743 | else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) |
---|
| 744 | private = 0; |
---|
| 745 | } |
---|
| 746 | |
---|
[f0cb961] | 747 | imcb_add_buddy( ic, handle, NULL ); |
---|
[0da65d5] | 748 | u = user_findhandle( ic, handle ); |
---|
[b7d3cc34] | 749 | u->is_private = private; |
---|
| 750 | } |
---|
| 751 | else |
---|
| 752 | { |
---|
[84b045d] | 753 | imcb_log( ic, "Message from unknown handle %s:", handle ); |
---|
[b7d3cc34] | 754 | u = user_find( irc, irc->mynick ); |
---|
| 755 | } |
---|
| 756 | } |
---|
| 757 | |
---|
[10a96f4] | 758 | if( ( g_strcasecmp( set_getstr( &ic->irc->b->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 759 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->b->set, "strip_html" ) ) ) |
---|
[b7d3cc34] | 760 | strip_html( msg ); |
---|
| 761 | |
---|
[d444c09] | 762 | wrapped = word_wrap( msg, 425 ); |
---|
| 763 | irc_msgfrom( irc, u->nick, wrapped ); |
---|
| 764 | g_free( wrapped ); |
---|
[b7d3cc34] | 765 | } |
---|
| 766 | |
---|
[52744f8] | 767 | void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) |
---|
[b7d3cc34] | 768 | { |
---|
| 769 | user_t *u; |
---|
| 770 | |
---|
[10a96f4] | 771 | if( !set_getbool( &ic->irc->b->set, "typing_notice" ) ) |
---|
[b7d3cc34] | 772 | return; |
---|
| 773 | |
---|
[9624fdf] | 774 | if( ( u = user_findhandle( ic, handle ) ) ) |
---|
| 775 | { |
---|
| 776 | char buf[256]; |
---|
| 777 | |
---|
| 778 | g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 ); |
---|
| 779 | irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf ); |
---|
[e7f46c5] | 780 | } |
---|
[b7d3cc34] | 781 | } |
---|
| 782 | |
---|
[94acdd0] | 783 | struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ) |
---|
[83ba3e5] | 784 | { |
---|
| 785 | struct groupchat *c; |
---|
| 786 | |
---|
| 787 | /* This one just creates the conversation structure, user won't see anything yet */ |
---|
| 788 | |
---|
| 789 | if( ic->groupchats ) |
---|
| 790 | { |
---|
| 791 | for( c = ic->groupchats; c->next; c = c->next ); |
---|
| 792 | c = c->next = g_new0( struct groupchat, 1 ); |
---|
| 793 | } |
---|
| 794 | else |
---|
| 795 | ic->groupchats = c = g_new0( struct groupchat, 1 ); |
---|
| 796 | |
---|
| 797 | c->ic = ic; |
---|
| 798 | c->title = g_strdup( handle ); |
---|
| 799 | c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ ); |
---|
| 800 | c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
| 801 | |
---|
[10a96f4] | 802 | if( set_getbool( &ic->irc->b->set, "debug" ) ) |
---|
[83ba3e5] | 803 | imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); |
---|
| 804 | |
---|
| 805 | return c; |
---|
| 806 | } |
---|
| 807 | |
---|
[e35d1a1] | 808 | void imcb_chat_free( struct groupchat *c ) |
---|
[b7d3cc34] | 809 | { |
---|
[0da65d5] | 810 | struct im_connection *ic = c->ic; |
---|
[e35d1a1] | 811 | struct groupchat *l; |
---|
[b7d3cc34] | 812 | GList *ir; |
---|
| 813 | |
---|
[10a96f4] | 814 | if( set_getbool( &ic->irc->b->set, "debug" ) ) |
---|
[56f260a] | 815 | imcb_log( ic, "You were removed from conversation %p", c ); |
---|
[b7d3cc34] | 816 | |
---|
| 817 | if( c ) |
---|
| 818 | { |
---|
| 819 | if( c->joined ) |
---|
| 820 | { |
---|
| 821 | user_t *u, *r; |
---|
| 822 | |
---|
[0da65d5] | 823 | r = user_find( ic->irc, ic->irc->mynick ); |
---|
| 824 | irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); |
---|
[b7d3cc34] | 825 | |
---|
[0da65d5] | 826 | u = user_find( ic->irc, ic->irc->nick ); |
---|
| 827 | irc_kick( ic->irc, u, c->channel, r ); |
---|
| 828 | /* irc_part( ic->irc, u, c->channel ); */ |
---|
[b7d3cc34] | 829 | } |
---|
| 830 | |
---|
[e35d1a1] | 831 | /* Find the previous chat in the linked list. */ |
---|
| 832 | for( l = ic->groupchats; l && l->next != c; l = l->next ); |
---|
| 833 | |
---|
[b7d3cc34] | 834 | if( l ) |
---|
| 835 | l->next = c->next; |
---|
| 836 | else |
---|
[e35d1a1] | 837 | ic->groupchats = c->next; |
---|
[b7d3cc34] | 838 | |
---|
| 839 | for( ir = c->in_room; ir; ir = ir->next ) |
---|
| 840 | g_free( ir->data ); |
---|
| 841 | g_list_free( c->in_room ); |
---|
| 842 | g_free( c->channel ); |
---|
| 843 | g_free( c->title ); |
---|
[83ba3e5] | 844 | g_free( c->topic ); |
---|
[b7d3cc34] | 845 | g_free( c ); |
---|
| 846 | } |
---|
| 847 | } |
---|
| 848 | |
---|
[c6ca3ee] | 849 | void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) |
---|
[b7d3cc34] | 850 | { |
---|
[0da65d5] | 851 | struct im_connection *ic = c->ic; |
---|
[d444c09] | 852 | char *wrapped; |
---|
[b7d3cc34] | 853 | user_t *u; |
---|
| 854 | |
---|
| 855 | /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ |
---|
[c2fb3809] | 856 | if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
[b7d3cc34] | 857 | return; |
---|
| 858 | |
---|
[0da65d5] | 859 | u = user_findhandle( ic, who ); |
---|
[b7d3cc34] | 860 | |
---|
[10a96f4] | 861 | if( ( g_strcasecmp( set_getstr( &ic->irc->b->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 862 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->b->set, "strip_html" ) ) ) |
---|
[b7d3cc34] | 863 | strip_html( msg ); |
---|
| 864 | |
---|
[d444c09] | 865 | wrapped = word_wrap( msg, 425 ); |
---|
[b7d3cc34] | 866 | if( c && u ) |
---|
[d444c09] | 867 | { |
---|
| 868 | irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", wrapped ); |
---|
| 869 | } |
---|
[b7d3cc34] | 870 | else |
---|
[d444c09] | 871 | { |
---|
[56f260a] | 872 | imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped ); |
---|
[d444c09] | 873 | } |
---|
| 874 | g_free( wrapped ); |
---|
[b7d3cc34] | 875 | } |
---|
| 876 | |
---|
[31e5846] | 877 | void imcb_chat_log( struct groupchat *c, char *format, ... ) |
---|
| 878 | { |
---|
| 879 | irc_t *irc = c->ic->irc; |
---|
| 880 | va_list params; |
---|
| 881 | char *text; |
---|
| 882 | user_t *u; |
---|
| 883 | |
---|
| 884 | va_start( params, format ); |
---|
| 885 | text = g_strdup_vprintf( format, params ); |
---|
| 886 | va_end( params ); |
---|
| 887 | |
---|
| 888 | u = user_find( irc, irc->mynick ); |
---|
| 889 | |
---|
| 890 | irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text ); |
---|
| 891 | |
---|
| 892 | g_free( text ); |
---|
| 893 | } |
---|
| 894 | |
---|
[ef5c185] | 895 | void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) |
---|
[50e1776] | 896 | { |
---|
| 897 | struct im_connection *ic = c->ic; |
---|
| 898 | user_t *u = NULL; |
---|
| 899 | |
---|
| 900 | if( who == NULL) |
---|
[ef5c185] | 901 | u = user_find( ic->irc, ic->irc->mynick ); |
---|
[50e1776] | 902 | else if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
[ef5c185] | 903 | u = user_find( ic->irc, ic->irc->nick ); |
---|
[50e1776] | 904 | else |
---|
| 905 | u = user_findhandle( ic, who ); |
---|
| 906 | |
---|
[10a96f4] | 907 | if( ( g_strcasecmp( set_getstr( &ic->irc->b->set, "strip_html" ), "always" ) == 0 ) || |
---|
| 908 | ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->b->set, "strip_html" ) ) ) |
---|
[50e1776] | 909 | strip_html( topic ); |
---|
| 910 | |
---|
| 911 | g_free( c->topic ); |
---|
| 912 | c->topic = g_strdup( topic ); |
---|
| 913 | |
---|
| 914 | if( c->joined && u ) |
---|
| 915 | irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic ); |
---|
| 916 | } |
---|
| 917 | |
---|
[b7d3cc34] | 918 | |
---|
| 919 | /* buddy_chat.c */ |
---|
| 920 | |
---|
[c6ca3ee] | 921 | void imcb_chat_add_buddy( struct groupchat *b, const char *handle ) |
---|
[b7d3cc34] | 922 | { |
---|
[0da65d5] | 923 | user_t *u = user_findhandle( b->ic, handle ); |
---|
[b7d3cc34] | 924 | int me = 0; |
---|
| 925 | |
---|
[10a96f4] | 926 | if( set_getbool( &b->ic->irc->b->set, "debug" ) ) |
---|
[56f260a] | 927 | imcb_log( b->ic, "User %s added to conversation %p", handle, b ); |
---|
[b7d3cc34] | 928 | |
---|
| 929 | /* It might be yourself! */ |
---|
[c2fb3809] | 930 | if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 ) |
---|
[b7d3cc34] | 931 | { |
---|
[0da65d5] | 932 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
[b7d3cc34] | 933 | if( !b->joined ) |
---|
[0da65d5] | 934 | irc_join( b->ic->irc, u, b->channel ); |
---|
[b7d3cc34] | 935 | b->joined = me = 1; |
---|
| 936 | } |
---|
| 937 | |
---|
| 938 | /* Most protocols allow people to join, even when they're not in |
---|
| 939 | your contact list. Try to handle that here */ |
---|
| 940 | if( !u ) |
---|
| 941 | { |
---|
[f0cb961] | 942 | imcb_add_buddy( b->ic, handle, NULL ); |
---|
[0da65d5] | 943 | u = user_findhandle( b->ic, handle ); |
---|
[b7d3cc34] | 944 | } |
---|
| 945 | |
---|
| 946 | /* Add the handle to the room userlist, if it's not 'me' */ |
---|
| 947 | if( !me ) |
---|
| 948 | { |
---|
| 949 | if( b->joined ) |
---|
[0da65d5] | 950 | irc_join( b->ic->irc, u, b->channel ); |
---|
[b7d3cc34] | 951 | b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); |
---|
| 952 | } |
---|
| 953 | } |
---|
| 954 | |
---|
[2d317bb] | 955 | /* This function is one BIG hack... :-( EREWRITE */ |
---|
[c6ca3ee] | 956 | void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason ) |
---|
[b7d3cc34] | 957 | { |
---|
| 958 | user_t *u; |
---|
| 959 | int me = 0; |
---|
| 960 | |
---|
[10a96f4] | 961 | if( set_getbool( &b->ic->irc->b->set, "debug" ) ) |
---|
[56f260a] | 962 | imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" ); |
---|
[b7d3cc34] | 963 | |
---|
| 964 | /* It might be yourself! */ |
---|
[c2fb3809] | 965 | if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) |
---|
[b7d3cc34] | 966 | { |
---|
[2d317bb] | 967 | if( b->joined == 0 ) |
---|
| 968 | return; |
---|
| 969 | |
---|
[0da65d5] | 970 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
[b7d3cc34] | 971 | b->joined = 0; |
---|
| 972 | me = 1; |
---|
| 973 | } |
---|
| 974 | else |
---|
| 975 | { |
---|
[0da65d5] | 976 | u = user_findhandle( b->ic, handle ); |
---|
[b7d3cc34] | 977 | } |
---|
| 978 | |
---|
[2d317bb] | 979 | if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) ) |
---|
| 980 | irc_part( b->ic->irc, u, b->channel ); |
---|
[b7d3cc34] | 981 | } |
---|
| 982 | |
---|
[764b163d] | 983 | static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ) |
---|
[b7d3cc34] | 984 | { |
---|
| 985 | GList *i; |
---|
| 986 | |
---|
| 987 | /* Find the handle in the room userlist and shoot it */ |
---|
| 988 | i = b->in_room; |
---|
| 989 | while( i ) |
---|
| 990 | { |
---|
| 991 | if( g_strcasecmp( handle, i->data ) == 0 ) |
---|
| 992 | { |
---|
| 993 | g_free( i->data ); |
---|
| 994 | b->in_room = g_list_remove( b->in_room, i->data ); |
---|
| 995 | return( 1 ); |
---|
| 996 | } |
---|
| 997 | |
---|
| 998 | i = i->next; |
---|
| 999 | } |
---|
| 1000 | |
---|
| 1001 | return( 0 ); |
---|
| 1002 | } |
---|
| 1003 | |
---|
| 1004 | |
---|
| 1005 | /* Misc. BitlBee stuff which shouldn't really be here */ |
---|
| 1006 | |
---|
[5c9512f] | 1007 | char *set_eval_away_devoice( set_t *set, char *value ) |
---|
[b7d3cc34] | 1008 | { |
---|
[5c9512f] | 1009 | irc_t *irc = set->data; |
---|
[b7d3cc34] | 1010 | int st; |
---|
| 1011 | |
---|
[7125cb3] | 1012 | if( !is_bool( value ) ) |
---|
| 1013 | return SET_INVALID; |
---|
[b7d3cc34] | 1014 | |
---|
[7125cb3] | 1015 | st = bool2int( value ); |
---|
[b7d3cc34] | 1016 | |
---|
| 1017 | /* Horror.... */ |
---|
| 1018 | |
---|
[10a96f4] | 1019 | if( st != set_getbool( &irc->b->set, "away_devoice" ) ) |
---|
[b7d3cc34] | 1020 | { |
---|
| 1021 | char list[80] = ""; |
---|
| 1022 | user_t *u = irc->users; |
---|
| 1023 | int i = 0, count = 0; |
---|
| 1024 | char pm; |
---|
| 1025 | char v[80]; |
---|
| 1026 | |
---|
| 1027 | if( st ) |
---|
| 1028 | pm = '+'; |
---|
| 1029 | else |
---|
| 1030 | pm = '-'; |
---|
| 1031 | |
---|
| 1032 | while( u ) |
---|
| 1033 | { |
---|
[0da65d5] | 1034 | if( u->ic && u->online && !u->away ) |
---|
[b7d3cc34] | 1035 | { |
---|
| 1036 | if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) |
---|
| 1037 | { |
---|
| 1038 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
[2087159] | 1039 | irc_write( irc, ":%s MODE %s %c%s%s", |
---|
| 1040 | irc->myhost, |
---|
[b7d3cc34] | 1041 | irc->channel, pm, v, list ); |
---|
| 1042 | |
---|
| 1043 | *list = 0; |
---|
| 1044 | count = 0; |
---|
| 1045 | } |
---|
| 1046 | |
---|
| 1047 | sprintf( list + strlen( list ), " %s", u->nick ); |
---|
| 1048 | count ++; |
---|
| 1049 | } |
---|
| 1050 | u = u->next; |
---|
| 1051 | } |
---|
| 1052 | |
---|
| 1053 | /* $v = 'v' x $i */ |
---|
| 1054 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
[2087159] | 1055 | irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, |
---|
[b7d3cc34] | 1056 | irc->channel, pm, v, list ); |
---|
| 1057 | } |
---|
| 1058 | |
---|
[7125cb3] | 1059 | return value; |
---|
[b7d3cc34] | 1060 | } |
---|
| 1061 | |
---|
[226fce1] | 1062 | |
---|
| 1063 | |
---|
| 1064 | |
---|
| 1065 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
| 1066 | them all from some wrappers. We'll start to define some down here: */ |
---|
| 1067 | |
---|
[84b045d] | 1068 | int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags ) |
---|
[b7d3cc34] | 1069 | { |
---|
[e27661d] | 1070 | char *buf = NULL; |
---|
| 1071 | int st; |
---|
[b7d3cc34] | 1072 | |
---|
[6bbb939] | 1073 | if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
[c572dd6] | 1074 | { |
---|
[e27661d] | 1075 | buf = escape_html( msg ); |
---|
[c572dd6] | 1076 | msg = buf; |
---|
[b7d3cc34] | 1077 | } |
---|
| 1078 | |
---|
[f6c963b] | 1079 | st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags ); |
---|
[e27661d] | 1080 | g_free( buf ); |
---|
| 1081 | |
---|
| 1082 | return st; |
---|
[b7d3cc34] | 1083 | } |
---|
| 1084 | |
---|
[84b045d] | 1085 | int imc_chat_msg( struct groupchat *c, char *msg, int flags ) |
---|
[b7d3cc34] | 1086 | { |
---|
[e27661d] | 1087 | char *buf = NULL; |
---|
[b7d3cc34] | 1088 | |
---|
[6bbb939] | 1089 | if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
[e27661d] | 1090 | { |
---|
| 1091 | buf = escape_html( msg ); |
---|
[b7d3cc34] | 1092 | msg = buf; |
---|
| 1093 | } |
---|
| 1094 | |
---|
[f6c963b] | 1095 | c->ic->acc->prpl->chat_msg( c, msg, flags ); |
---|
[e27661d] | 1096 | g_free( buf ); |
---|
| 1097 | |
---|
[0da65d5] | 1098 | return 1; |
---|
[b7d3cc34] | 1099 | } |
---|
[226fce1] | 1100 | |
---|
[34fbbf9] | 1101 | static char *imc_away_state_find( GList *gcm, char *away, char **message ); |
---|
[226fce1] | 1102 | |
---|
[58adb7e] | 1103 | int imc_away_send_update( struct im_connection *ic ) |
---|
[226fce1] | 1104 | { |
---|
[3e1ef92c] | 1105 | char *away, *msg = NULL; |
---|
[226fce1] | 1106 | |
---|
[58adb7e] | 1107 | away = set_getstr( &ic->acc->set, "away" ) ? |
---|
[10a96f4] | 1108 | : set_getstr( &ic->irc->b->set, "away" ); |
---|
[34fbbf9] | 1109 | if( away && *away ) |
---|
[226fce1] | 1110 | { |
---|
[34fbbf9] | 1111 | GList *m = ic->acc->prpl->away_states( ic ); |
---|
[58adb7e] | 1112 | msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; |
---|
| 1113 | away = imc_away_state_find( m, away, &msg ) ? : m->data; |
---|
| 1114 | } |
---|
| 1115 | else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE ) |
---|
| 1116 | { |
---|
| 1117 | away = NULL; |
---|
| 1118 | msg = set_getstr( &ic->acc->set, "status" ) ? |
---|
[10a96f4] | 1119 | : set_getstr( &ic->irc->b->set, "status" ); |
---|
[226fce1] | 1120 | } |
---|
| 1121 | |
---|
[58adb7e] | 1122 | ic->acc->prpl->set_away( ic, away, msg ); |
---|
[226fce1] | 1123 | |
---|
[34fbbf9] | 1124 | return 1; |
---|
[226fce1] | 1125 | } |
---|
| 1126 | |
---|
[84b045d] | 1127 | static char *imc_away_alias_list[8][5] = |
---|
[226fce1] | 1128 | { |
---|
| 1129 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
| 1130 | { "NA", "N/A", "Not available", NULL }, |
---|
| 1131 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
| 1132 | { "Be right back", "BRB", NULL }, |
---|
| 1133 | { "On the phone", "Phone", "On phone", NULL }, |
---|
| 1134 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
| 1135 | { "Invisible", "Hidden" }, |
---|
| 1136 | { NULL } |
---|
| 1137 | }; |
---|
| 1138 | |
---|
[34fbbf9] | 1139 | static char *imc_away_state_find( GList *gcm, char *away, char **message ) |
---|
[226fce1] | 1140 | { |
---|
| 1141 | GList *m; |
---|
| 1142 | int i, j; |
---|
| 1143 | |
---|
[34fbbf9] | 1144 | for( m = gcm; m; m = m->next ) |
---|
| 1145 | if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) |
---|
| 1146 | { |
---|
| 1147 | /* At least the Yahoo! module works better if message |
---|
| 1148 | contains no data unless it adds something to what |
---|
| 1149 | we have in state already. */ |
---|
| 1150 | if( strlen( m->data ) == strlen( away ) ) |
---|
| 1151 | *message = NULL; |
---|
| 1152 | |
---|
| 1153 | return m->data; |
---|
| 1154 | } |
---|
| 1155 | |
---|
[84b045d] | 1156 | for( i = 0; *imc_away_alias_list[i]; i ++ ) |
---|
[226fce1] | 1157 | { |
---|
[34fbbf9] | 1158 | int keep_message; |
---|
| 1159 | |
---|
[84b045d] | 1160 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
| 1161 | if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 ) |
---|
[34fbbf9] | 1162 | { |
---|
| 1163 | keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] ); |
---|
[226fce1] | 1164 | break; |
---|
[34fbbf9] | 1165 | } |
---|
[226fce1] | 1166 | |
---|
[84b045d] | 1167 | if( !imc_away_alias_list[i][j] ) /* If we reach the end, this row */ |
---|
[226fce1] | 1168 | continue; /* is not what we want. Next! */ |
---|
| 1169 | |
---|
| 1170 | /* Now find an entry in this row which exists in gcm */ |
---|
[84b045d] | 1171 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
[226fce1] | 1172 | { |
---|
[34fbbf9] | 1173 | for( m = gcm; m; m = m->next ) |
---|
[84b045d] | 1174 | if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 ) |
---|
[34fbbf9] | 1175 | { |
---|
| 1176 | if( !keep_message ) |
---|
| 1177 | *message = NULL; |
---|
| 1178 | |
---|
| 1179 | return imc_away_alias_list[i][j]; |
---|
| 1180 | } |
---|
[226fce1] | 1181 | } |
---|
[34fbbf9] | 1182 | |
---|
| 1183 | /* No need to look further, apparently this state doesn't |
---|
| 1184 | have any good alias for this protocol. */ |
---|
| 1185 | break; |
---|
[226fce1] | 1186 | } |
---|
| 1187 | |
---|
[34fbbf9] | 1188 | return NULL; |
---|
[226fce1] | 1189 | } |
---|
[da3b536] | 1190 | |
---|
[84b045d] | 1191 | void imc_add_allow( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 1192 | { |
---|
[0da65d5] | 1193 | if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
[da3b536] | 1194 | { |
---|
[0da65d5] | 1195 | ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) ); |
---|
[da3b536] | 1196 | } |
---|
| 1197 | |
---|
[0da65d5] | 1198 | ic->acc->prpl->add_permit( ic, handle ); |
---|
[da3b536] | 1199 | } |
---|
| 1200 | |
---|
[84b045d] | 1201 | void imc_rem_allow( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 1202 | { |
---|
| 1203 | GSList *l; |
---|
| 1204 | |
---|
[0da65d5] | 1205 | if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
[da3b536] | 1206 | { |
---|
| 1207 | g_free( l->data ); |
---|
[0da65d5] | 1208 | ic->permit = g_slist_delete_link( ic->permit, l ); |
---|
[da3b536] | 1209 | } |
---|
| 1210 | |
---|
[0da65d5] | 1211 | ic->acc->prpl->rem_permit( ic, handle ); |
---|
[da3b536] | 1212 | } |
---|
| 1213 | |
---|
[84b045d] | 1214 | void imc_add_block( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 1215 | { |
---|
[0da65d5] | 1216 | if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
[da3b536] | 1217 | { |
---|
[0da65d5] | 1218 | ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) ); |
---|
[da3b536] | 1219 | } |
---|
| 1220 | |
---|
[0da65d5] | 1221 | ic->acc->prpl->add_deny( ic, handle ); |
---|
[da3b536] | 1222 | } |
---|
| 1223 | |
---|
[84b045d] | 1224 | void imc_rem_block( struct im_connection *ic, char *handle ) |
---|
[da3b536] | 1225 | { |
---|
| 1226 | GSList *l; |
---|
| 1227 | |
---|
[0da65d5] | 1228 | if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
[da3b536] | 1229 | { |
---|
| 1230 | g_free( l->data ); |
---|
[0da65d5] | 1231 | ic->deny = g_slist_delete_link( ic->deny, l ); |
---|
[da3b536] | 1232 | } |
---|
| 1233 | |
---|
[0da65d5] | 1234 | ic->acc->prpl->rem_deny( ic, handle ); |
---|
[da3b536] | 1235 | } |
---|
[85023c6] | 1236 | |
---|
| 1237 | void imcb_clean_handle( struct im_connection *ic, char *handle ) |
---|
| 1238 | { |
---|
| 1239 | /* Accepts a handle and does whatever is necessary to make it |
---|
| 1240 | BitlBee-friendly. Currently this means removing everything |
---|
| 1241 | outside 33-127 (ASCII printable excl spaces), @ (only one |
---|
| 1242 | is allowed) and ! and : */ |
---|
| 1243 | char out[strlen(handle)+1]; |
---|
| 1244 | int s, d; |
---|
| 1245 | |
---|
| 1246 | s = d = 0; |
---|
| 1247 | while( handle[s] ) |
---|
| 1248 | { |
---|
| 1249 | if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' && |
---|
| 1250 | ( handle[s] & 0x80 ) == 0 ) |
---|
| 1251 | { |
---|
| 1252 | if( handle[s] == '@' ) |
---|
| 1253 | { |
---|
| 1254 | /* See if we got an @ already? */ |
---|
| 1255 | out[d] = 0; |
---|
| 1256 | if( strchr( out, '@' ) ) |
---|
| 1257 | continue; |
---|
| 1258 | } |
---|
| 1259 | |
---|
| 1260 | out[d++] = handle[s]; |
---|
| 1261 | } |
---|
| 1262 | s ++; |
---|
| 1263 | } |
---|
| 1264 | out[d] = handle[s]; |
---|
| 1265 | |
---|
| 1266 | strcpy( handle, out ); |
---|
| 1267 | } |
---|