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