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