1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2006 Wilmer van der Gaast and others * |
---|
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 | |
---|
38 | static int remove_chat_buddy_silent( struct groupchat *b, char *handle ); |
---|
39 | |
---|
40 | GSList *connections; |
---|
41 | |
---|
42 | #ifdef WITH_PLUGINS |
---|
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) { |
---|
50 | log_message(LOGLVL_ERROR, "Can't find `%s', not loading", path); |
---|
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 | } |
---|
63 | |
---|
64 | void load_plugins(void) |
---|
65 | { |
---|
66 | GDir *dir; |
---|
67 | GError *error = NULL; |
---|
68 | |
---|
69 | dir = g_dir_open(global.conf->plugindir, 0, &error); |
---|
70 | |
---|
71 | if (dir) { |
---|
72 | const gchar *entry; |
---|
73 | char *path; |
---|
74 | |
---|
75 | while ((entry = g_dir_read_name(dir))) { |
---|
76 | path = g_build_filename(global.conf->plugindir, entry, NULL); |
---|
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 |
---|
91 | |
---|
92 | /* nogaim.c */ |
---|
93 | |
---|
94 | GList *protocols = NULL; |
---|
95 | |
---|
96 | void register_protocol (struct prpl *p) |
---|
97 | { |
---|
98 | protocols = g_list_append(protocols, p); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | struct prpl *find_protocol(const char *name) |
---|
103 | { |
---|
104 | GList *gl; |
---|
105 | for (gl = protocols; gl; gl = gl->next) |
---|
106 | { |
---|
107 | struct prpl *proto = gl->data; |
---|
108 | if(!g_strcasecmp(proto->name, name)) |
---|
109 | return proto; |
---|
110 | } |
---|
111 | return NULL; |
---|
112 | } |
---|
113 | |
---|
114 | /* nogaim.c */ |
---|
115 | void nogaim_init() |
---|
116 | { |
---|
117 | extern void msn_initmodule(); |
---|
118 | extern void oscar_initmodule(); |
---|
119 | extern void byahoo_initmodule(); |
---|
120 | extern void jabber_initmodule(); |
---|
121 | |
---|
122 | #ifdef WITH_MSN |
---|
123 | msn_initmodule(); |
---|
124 | #endif |
---|
125 | |
---|
126 | #ifdef WITH_OSCAR |
---|
127 | oscar_initmodule(); |
---|
128 | #endif |
---|
129 | |
---|
130 | #ifdef WITH_YAHOO |
---|
131 | byahoo_initmodule(); |
---|
132 | #endif |
---|
133 | |
---|
134 | #ifdef WITH_JABBER |
---|
135 | jabber_initmodule(); |
---|
136 | #endif |
---|
137 | |
---|
138 | #ifdef WITH_PLUGINS |
---|
139 | load_plugins(); |
---|
140 | #endif |
---|
141 | } |
---|
142 | |
---|
143 | GSList *get_connections() { return connections; } |
---|
144 | |
---|
145 | /* multi.c */ |
---|
146 | |
---|
147 | struct im_connection *imcb_new( account_t *acc ) |
---|
148 | { |
---|
149 | struct im_connection *ic; |
---|
150 | |
---|
151 | ic = g_new0( struct im_connection, 1 ); |
---|
152 | |
---|
153 | ic->irc = acc->irc; |
---|
154 | ic->acc = acc; |
---|
155 | acc->ic = ic; |
---|
156 | |
---|
157 | connections = g_slist_append( connections, ic ); |
---|
158 | |
---|
159 | return( ic ); |
---|
160 | } |
---|
161 | |
---|
162 | void imc_free( struct im_connection *ic ) |
---|
163 | { |
---|
164 | account_t *a; |
---|
165 | |
---|
166 | /* Destroy the pointer to this connection from the account list */ |
---|
167 | for( a = ic->irc->accounts; a; a = a->next ) |
---|
168 | if( a->ic == ic ) |
---|
169 | { |
---|
170 | a->ic = NULL; |
---|
171 | break; |
---|
172 | } |
---|
173 | |
---|
174 | connections = g_slist_remove( connections, ic ); |
---|
175 | g_free( ic ); |
---|
176 | } |
---|
177 | |
---|
178 | static void serv_got_crap( struct im_connection *ic, char *format, ... ) |
---|
179 | { |
---|
180 | va_list params; |
---|
181 | char *text; |
---|
182 | account_t *a; |
---|
183 | |
---|
184 | va_start( params, format ); |
---|
185 | text = g_strdup_vprintf( format, params ); |
---|
186 | va_end( params ); |
---|
187 | |
---|
188 | if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || |
---|
189 | ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) |
---|
190 | strip_html( text ); |
---|
191 | |
---|
192 | /* Try to find a different connection on the same protocol. */ |
---|
193 | for( a = ic->irc->accounts; a; a = a->next ) |
---|
194 | if( a->prpl == ic->acc->prpl && a->ic != ic ) |
---|
195 | break; |
---|
196 | |
---|
197 | /* If we found one, include the screenname in the message. */ |
---|
198 | if( a ) |
---|
199 | irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text ); |
---|
200 | else |
---|
201 | irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text ); |
---|
202 | |
---|
203 | g_free( text ); |
---|
204 | } |
---|
205 | |
---|
206 | void imcb_log( struct im_connection *ic, char *format, ... ) |
---|
207 | { |
---|
208 | va_list params; |
---|
209 | char *text; |
---|
210 | |
---|
211 | va_start( params, format ); |
---|
212 | text = g_strdup_vprintf( format, params ); |
---|
213 | va_end( params ); |
---|
214 | |
---|
215 | if( ic->flags & OPT_LOGGED_IN ) |
---|
216 | serv_got_crap( ic, "%s", text ); |
---|
217 | else |
---|
218 | serv_got_crap( ic, "Logging in: %s", text ); |
---|
219 | |
---|
220 | g_free( text ); |
---|
221 | } |
---|
222 | |
---|
223 | void imcb_error( struct im_connection *ic, char *format, ... ) |
---|
224 | { |
---|
225 | va_list params; |
---|
226 | char *text; |
---|
227 | |
---|
228 | va_start( params, format ); |
---|
229 | text = g_strdup_vprintf( format, params ); |
---|
230 | va_end( params ); |
---|
231 | |
---|
232 | if( ic->flags & OPT_LOGGED_IN ) |
---|
233 | serv_got_crap( ic, "Error: %s", text ); |
---|
234 | else |
---|
235 | serv_got_crap( ic, "Couldn't log in: %s", text ); |
---|
236 | |
---|
237 | g_free( text ); |
---|
238 | } |
---|
239 | |
---|
240 | static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond ) |
---|
241 | { |
---|
242 | struct im_connection *ic = d; |
---|
243 | |
---|
244 | if( ic->acc->prpl->keepalive ) |
---|
245 | ic->acc->prpl->keepalive( ic ); |
---|
246 | |
---|
247 | return TRUE; |
---|
248 | } |
---|
249 | |
---|
250 | void imcb_connected( struct im_connection *ic ) |
---|
251 | { |
---|
252 | user_t *u; |
---|
253 | |
---|
254 | /* MSN servers sometimes redirect you to a different server and do |
---|
255 | the whole login sequence again, so these "late" calls to this |
---|
256 | function should be handled correctly. (IOW, ignored) */ |
---|
257 | if( ic->flags & OPT_LOGGED_IN ) |
---|
258 | return; |
---|
259 | |
---|
260 | u = user_find( ic->irc, ic->irc->nick ); |
---|
261 | |
---|
262 | imcb_log( ic, "Logged in" ); |
---|
263 | |
---|
264 | ic->keepalive = b_timeout_add( 60000, send_keepalive, ic ); |
---|
265 | ic->flags |= OPT_LOGGED_IN; |
---|
266 | |
---|
267 | /* Also necessary when we're not away, at least for some of the |
---|
268 | protocols. */ |
---|
269 | imc_set_away( ic, u->away ); |
---|
270 | } |
---|
271 | |
---|
272 | gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond ) |
---|
273 | { |
---|
274 | account_t *a = data; |
---|
275 | |
---|
276 | a->reconnect = 0; |
---|
277 | account_on( a->irc, a ); |
---|
278 | |
---|
279 | return( FALSE ); /* Only have to run the timeout once */ |
---|
280 | } |
---|
281 | |
---|
282 | void cancel_auto_reconnect( account_t *a ) |
---|
283 | { |
---|
284 | /* while( b_event_remove_by_data( (gpointer) a ) ); */ |
---|
285 | b_event_remove( a->reconnect ); |
---|
286 | a->reconnect = 0; |
---|
287 | } |
---|
288 | |
---|
289 | void imc_logout( struct im_connection *ic, int allow_reconnect ) |
---|
290 | { |
---|
291 | irc_t *irc = ic->irc; |
---|
292 | user_t *t, *u = irc->users; |
---|
293 | account_t *a; |
---|
294 | |
---|
295 | /* Nested calls might happen sometimes, this is probably the best |
---|
296 | place to catch them. */ |
---|
297 | if( ic->flags & OPT_LOGGING_OUT ) |
---|
298 | return; |
---|
299 | else |
---|
300 | ic->flags |= OPT_LOGGING_OUT; |
---|
301 | |
---|
302 | imcb_log( ic, "Signing off.." ); |
---|
303 | |
---|
304 | b_event_remove( ic->keepalive ); |
---|
305 | ic->keepalive = 0; |
---|
306 | ic->acc->prpl->logout( ic ); |
---|
307 | b_event_remove( ic->inpa ); |
---|
308 | |
---|
309 | while( u ) |
---|
310 | { |
---|
311 | if( u->ic == ic ) |
---|
312 | { |
---|
313 | t = u->next; |
---|
314 | user_del( irc, u->nick ); |
---|
315 | u = t; |
---|
316 | } |
---|
317 | else |
---|
318 | u = u->next; |
---|
319 | } |
---|
320 | |
---|
321 | query_del_by_conn( ic->irc, ic ); |
---|
322 | |
---|
323 | for( a = irc->accounts; a; a = a->next ) |
---|
324 | if( a->ic == ic ) |
---|
325 | break; |
---|
326 | |
---|
327 | if( !a ) |
---|
328 | { |
---|
329 | /* Uhm... This is very sick. */ |
---|
330 | } |
---|
331 | else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) && |
---|
332 | set_getbool( &a->set, "auto_reconnect" ) ) |
---|
333 | { |
---|
334 | int delay = set_getint( &irc->set, "auto_reconnect_delay" ); |
---|
335 | |
---|
336 | imcb_log( ic, "Reconnecting in %d seconds..", delay ); |
---|
337 | a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a ); |
---|
338 | } |
---|
339 | |
---|
340 | imc_free( ic ); |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | /* dialogs.c */ |
---|
345 | |
---|
346 | void imcb_ask( struct im_connection *ic, char *msg, void *data, void *doit, void *dont ) |
---|
347 | { |
---|
348 | query_add( ic->irc, ic, msg, doit, dont, data ); |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | /* list.c */ |
---|
353 | |
---|
354 | void add_buddy( struct im_connection *ic, char *group, char *handle, char *realname ) |
---|
355 | { |
---|
356 | user_t *u; |
---|
357 | char nick[MAX_NICK_LENGTH+1]; |
---|
358 | char *s; |
---|
359 | irc_t *irc = ic->irc; |
---|
360 | |
---|
361 | if( set_getbool( &irc->set, "debug" ) && 0 ) /* This message is too useless */ |
---|
362 | imcb_log( ic, "Receiving user add from handle: %s", handle ); |
---|
363 | |
---|
364 | if( user_findhandle( ic, handle ) ) |
---|
365 | { |
---|
366 | if( set_getbool( &irc->set, "debug" ) ) |
---|
367 | imcb_log( ic, "User already exists, ignoring add request: %s", handle ); |
---|
368 | |
---|
369 | return; |
---|
370 | |
---|
371 | /* Buddy seems to exist already. Let's ignore this request then... */ |
---|
372 | } |
---|
373 | |
---|
374 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
375 | strcpy( nick, nick_get( ic->acc, handle, realname ) ); |
---|
376 | |
---|
377 | u = user_add( ic->irc, nick ); |
---|
378 | |
---|
379 | if( !realname || !*realname ) realname = nick; |
---|
380 | u->realname = g_strdup( realname ); |
---|
381 | |
---|
382 | if( ( s = strchr( handle, '@' ) ) ) |
---|
383 | { |
---|
384 | u->host = g_strdup( s + 1 ); |
---|
385 | u->user = g_strndup( handle, s - handle ); |
---|
386 | } |
---|
387 | else if( ic->acc->server ) |
---|
388 | { |
---|
389 | char *colon; |
---|
390 | |
---|
391 | if( ( colon = strchr( ic->acc->server, ':' ) ) ) |
---|
392 | u->host = g_strndup( ic->acc->server, |
---|
393 | colon - ic->acc->server ); |
---|
394 | else |
---|
395 | u->host = g_strdup( ic->acc->server ); |
---|
396 | |
---|
397 | u->user = g_strdup( handle ); |
---|
398 | |
---|
399 | /* s/ /_/ ... important for AOL screennames */ |
---|
400 | for( s = u->user; *s; s ++ ) |
---|
401 | if( *s == ' ' ) |
---|
402 | *s = '_'; |
---|
403 | } |
---|
404 | else |
---|
405 | { |
---|
406 | u->host = g_strdup( ic->acc->prpl->name ); |
---|
407 | u->user = g_strdup( handle ); |
---|
408 | } |
---|
409 | |
---|
410 | u->ic = ic; |
---|
411 | u->handle = g_strdup( handle ); |
---|
412 | if( group ) u->group = g_strdup( group ); |
---|
413 | u->send_handler = buddy_send_handler; |
---|
414 | u->last_typing_notice = 0; |
---|
415 | } |
---|
416 | |
---|
417 | struct buddy *find_buddy( struct im_connection *ic, char *handle ) |
---|
418 | { |
---|
419 | static struct buddy b[1]; |
---|
420 | user_t *u; |
---|
421 | |
---|
422 | u = user_findhandle( ic, handle ); |
---|
423 | |
---|
424 | if( !u ) |
---|
425 | return( NULL ); |
---|
426 | |
---|
427 | memset( b, 0, sizeof( b ) ); |
---|
428 | strncpy( b->name, handle, 80 ); |
---|
429 | strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); |
---|
430 | b->present = u->online; |
---|
431 | b->ic = u->ic; |
---|
432 | |
---|
433 | return( b ); |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | void serv_buddy_rename( struct im_connection *ic, char *handle, char *realname ) |
---|
438 | { |
---|
439 | user_t *u = user_findhandle( ic, handle ); |
---|
440 | |
---|
441 | if( !u ) return; |
---|
442 | |
---|
443 | if( g_strcasecmp( u->realname, realname ) != 0 ) |
---|
444 | { |
---|
445 | if( u->realname != u->nick ) g_free( u->realname ); |
---|
446 | |
---|
447 | u->realname = g_strdup( realname ); |
---|
448 | |
---|
449 | if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) ) |
---|
450 | imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname ); |
---|
451 | } |
---|
452 | } |
---|
453 | |
---|
454 | |
---|
455 | /* prpl.c */ |
---|
456 | |
---|
457 | struct show_got_added_data |
---|
458 | { |
---|
459 | struct im_connection *ic; |
---|
460 | char *handle; |
---|
461 | }; |
---|
462 | |
---|
463 | void show_got_added_no( gpointer w, struct show_got_added_data *data ) |
---|
464 | { |
---|
465 | g_free( data->handle ); |
---|
466 | g_free( data ); |
---|
467 | } |
---|
468 | |
---|
469 | void show_got_added_yes( gpointer w, struct show_got_added_data *data ) |
---|
470 | { |
---|
471 | data->ic->acc->prpl->add_buddy( data->ic, data->handle, NULL ); |
---|
472 | add_buddy( data->ic, NULL, data->handle, data->handle ); |
---|
473 | |
---|
474 | return show_got_added_no( w, data ); |
---|
475 | } |
---|
476 | |
---|
477 | void imcb_ask_add( struct im_connection *ic, char *handle, const char *realname ) |
---|
478 | { |
---|
479 | struct show_got_added_data *data = g_new0( struct show_got_added_data, 1 ); |
---|
480 | char *s; |
---|
481 | |
---|
482 | /* TODO: Make a setting for this! */ |
---|
483 | if( user_findhandle( ic, handle ) != NULL ) |
---|
484 | return; |
---|
485 | |
---|
486 | s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle ); |
---|
487 | |
---|
488 | data->ic = ic; |
---|
489 | data->handle = g_strdup( handle ); |
---|
490 | query_add( ic->irc, ic, s, show_got_added_yes, show_got_added_no, data ); |
---|
491 | } |
---|
492 | |
---|
493 | |
---|
494 | /* server.c */ |
---|
495 | |
---|
496 | void serv_got_update( struct im_connection *ic, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps ) |
---|
497 | { |
---|
498 | user_t *u; |
---|
499 | int oa, oo; |
---|
500 | |
---|
501 | u = user_findhandle( ic, handle ); |
---|
502 | |
---|
503 | if( !u ) |
---|
504 | { |
---|
505 | if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 ) |
---|
506 | { |
---|
507 | add_buddy( ic, NULL, handle, NULL ); |
---|
508 | u = user_findhandle( ic, handle ); |
---|
509 | } |
---|
510 | else |
---|
511 | { |
---|
512 | if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 ) |
---|
513 | { |
---|
514 | imcb_log( ic, "serv_got_update() for handle %s:", handle ); |
---|
515 | imcb_log( ic, "loggedin = %d, type = %d", loggedin, type ); |
---|
516 | } |
---|
517 | |
---|
518 | return; |
---|
519 | } |
---|
520 | /* Why did we have this here.... |
---|
521 | return; */ |
---|
522 | } |
---|
523 | |
---|
524 | oa = u->away != NULL; |
---|
525 | oo = u->online; |
---|
526 | |
---|
527 | if( u->away ) |
---|
528 | { |
---|
529 | g_free( u->away ); |
---|
530 | u->away = NULL; |
---|
531 | } |
---|
532 | |
---|
533 | if( loggedin && !u->online ) |
---|
534 | { |
---|
535 | irc_spawn( ic->irc, u ); |
---|
536 | u->online = 1; |
---|
537 | } |
---|
538 | else if( !loggedin && u->online ) |
---|
539 | { |
---|
540 | struct groupchat *c; |
---|
541 | |
---|
542 | irc_kill( ic->irc, u ); |
---|
543 | u->online = 0; |
---|
544 | |
---|
545 | /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */ |
---|
546 | for( c = ic->conversations; c; c = c->next ) |
---|
547 | remove_chat_buddy_silent( c, handle ); |
---|
548 | } |
---|
549 | |
---|
550 | if( ( type & UC_UNAVAILABLE ) && ( strcmp( ic->acc->prpl->name, "oscar" ) == 0 || strcmp( ic->acc->prpl->name, "icq" ) == 0 ) ) |
---|
551 | { |
---|
552 | u->away = g_strdup( "Away" ); |
---|
553 | } |
---|
554 | else if( ( type & UC_UNAVAILABLE ) && ic->acc->prpl->get_status_string ) |
---|
555 | { |
---|
556 | u->away = g_strdup( ic->acc->prpl->get_status_string( ic, type ) ); |
---|
557 | } |
---|
558 | else |
---|
559 | u->away = NULL; |
---|
560 | |
---|
561 | /* LISPy... */ |
---|
562 | if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ |
---|
563 | ( u->online ) && /* Don't touch offline people */ |
---|
564 | ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ |
---|
565 | ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ |
---|
566 | { |
---|
567 | irc_write( ic->irc, ":%s MODE %s %cv %s", ic->irc->myhost, |
---|
568 | ic->irc->channel, u->away?'-':'+', u->nick ); |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | void serv_got_im( struct im_connection *ic, char *handle, char *msg, guint32 flags, time_t mtime, gint len ) |
---|
573 | { |
---|
574 | irc_t *irc = ic->irc; |
---|
575 | user_t *u; |
---|
576 | |
---|
577 | u = user_findhandle( ic, handle ); |
---|
578 | |
---|
579 | if( !u ) |
---|
580 | { |
---|
581 | char *h = set_getstr( &irc->set, "handle_unknown" ); |
---|
582 | |
---|
583 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
584 | { |
---|
585 | if( set_getbool( &irc->set, "debug" ) ) |
---|
586 | imcb_log( ic, "Ignoring message from unknown handle %s", handle ); |
---|
587 | |
---|
588 | return; |
---|
589 | } |
---|
590 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
591 | { |
---|
592 | int private = set_getbool( &irc->set, "private" ); |
---|
593 | |
---|
594 | if( h[3] ) |
---|
595 | { |
---|
596 | if( g_strcasecmp( h + 3, "_private" ) == 0 ) |
---|
597 | private = 1; |
---|
598 | else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) |
---|
599 | private = 0; |
---|
600 | } |
---|
601 | |
---|
602 | add_buddy( ic, NULL, handle, NULL ); |
---|
603 | u = user_findhandle( ic, handle ); |
---|
604 | u->is_private = private; |
---|
605 | } |
---|
606 | else |
---|
607 | { |
---|
608 | imcb_log( ic, "Message from unknown handle %s:", handle ); |
---|
609 | u = user_find( irc, irc->mynick ); |
---|
610 | } |
---|
611 | } |
---|
612 | |
---|
613 | if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || |
---|
614 | ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) |
---|
615 | strip_html( msg ); |
---|
616 | |
---|
617 | while( strlen( msg ) > 425 ) |
---|
618 | { |
---|
619 | char tmp, *nl; |
---|
620 | |
---|
621 | tmp = msg[425]; |
---|
622 | msg[425] = 0; |
---|
623 | |
---|
624 | /* If there's a newline/space in this string, split up there, |
---|
625 | looks a bit prettier. */ |
---|
626 | if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) ) |
---|
627 | { |
---|
628 | msg[425] = tmp; |
---|
629 | tmp = *nl; |
---|
630 | *nl = 0; |
---|
631 | } |
---|
632 | |
---|
633 | irc_msgfrom( irc, u->nick, msg ); |
---|
634 | |
---|
635 | /* Move on. */ |
---|
636 | if( nl ) |
---|
637 | { |
---|
638 | *nl = tmp; |
---|
639 | msg = nl + 1; |
---|
640 | } |
---|
641 | else |
---|
642 | { |
---|
643 | msg[425] = tmp; |
---|
644 | msg += 425; |
---|
645 | } |
---|
646 | } |
---|
647 | irc_msgfrom( irc, u->nick, msg ); |
---|
648 | } |
---|
649 | |
---|
650 | void serv_got_typing( struct im_connection *ic, char *handle, int timeout, int type ) |
---|
651 | { |
---|
652 | user_t *u; |
---|
653 | |
---|
654 | if( !set_getbool( &ic->irc->set, "typing_notice" ) ) |
---|
655 | return; |
---|
656 | |
---|
657 | if( ( u = user_findhandle( ic, handle ) ) ) { |
---|
658 | /* If type is: |
---|
659 | * 0: user has stopped typing |
---|
660 | * 1: user is actively typing |
---|
661 | * 2: user has entered text, but is not actively typing |
---|
662 | */ |
---|
663 | if (type == 0 || type == 1 || type == 2) { |
---|
664 | char buf[256]; |
---|
665 | g_snprintf(buf, 256, "\1TYPING %d\1", type); |
---|
666 | irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf ); |
---|
667 | } |
---|
668 | } |
---|
669 | } |
---|
670 | |
---|
671 | void serv_got_chat_left( struct groupchat *c ) |
---|
672 | { |
---|
673 | struct im_connection *ic = c->ic; |
---|
674 | struct groupchat *l = NULL; |
---|
675 | GList *ir; |
---|
676 | |
---|
677 | if( set_getbool( &ic->irc->set, "debug" ) ) |
---|
678 | imcb_log( ic, "You were removed from conversation 0x%x", (int) c ); |
---|
679 | |
---|
680 | if( c ) |
---|
681 | { |
---|
682 | if( c->joined ) |
---|
683 | { |
---|
684 | user_t *u, *r; |
---|
685 | |
---|
686 | r = user_find( ic->irc, ic->irc->mynick ); |
---|
687 | irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); |
---|
688 | |
---|
689 | u = user_find( ic->irc, ic->irc->nick ); |
---|
690 | irc_kick( ic->irc, u, c->channel, r ); |
---|
691 | /* irc_part( ic->irc, u, c->channel ); */ |
---|
692 | } |
---|
693 | |
---|
694 | if( l ) |
---|
695 | l->next = c->next; |
---|
696 | else |
---|
697 | ic->conversations = c->next; |
---|
698 | |
---|
699 | for( ir = c->in_room; ir; ir = ir->next ) |
---|
700 | g_free( ir->data ); |
---|
701 | g_list_free( c->in_room ); |
---|
702 | g_free( c->channel ); |
---|
703 | g_free( c->title ); |
---|
704 | g_free( c ); |
---|
705 | } |
---|
706 | } |
---|
707 | |
---|
708 | void serv_got_chat_in( struct groupchat *c, char *who, int whisper, char *msg, time_t mtime ) |
---|
709 | { |
---|
710 | struct im_connection *ic = c->ic; |
---|
711 | user_t *u; |
---|
712 | |
---|
713 | /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ |
---|
714 | if( g_strcasecmp( who, ic->acc->user ) == 0 ) |
---|
715 | return; |
---|
716 | |
---|
717 | u = user_findhandle( ic, who ); |
---|
718 | |
---|
719 | if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || |
---|
720 | ( ( ic->flags & OPT_CONN_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) |
---|
721 | strip_html( msg ); |
---|
722 | |
---|
723 | if( c && u ) |
---|
724 | irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, "", msg ); |
---|
725 | else |
---|
726 | imcb_log( ic, "Message from/to conversation %s@0x%x (unknown conv/user): %s", who, (int) c, msg ); |
---|
727 | } |
---|
728 | |
---|
729 | struct groupchat *serv_got_joined_chat( struct im_connection *ic, char *handle ) |
---|
730 | { |
---|
731 | struct groupchat *c; |
---|
732 | |
---|
733 | /* This one just creates the conversation structure, user won't see anything yet */ |
---|
734 | |
---|
735 | if( ic->conversations ) |
---|
736 | { |
---|
737 | for( c = ic->conversations; c->next; c = c->next ); |
---|
738 | c = c->next = g_new0( struct groupchat, 1 ); |
---|
739 | } |
---|
740 | else |
---|
741 | ic->conversations = c = g_new0( struct groupchat, 1 ); |
---|
742 | |
---|
743 | c->ic = ic; |
---|
744 | c->title = g_strdup( handle ); |
---|
745 | c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ ); |
---|
746 | |
---|
747 | if( set_getbool( &ic->irc->set, "debug" ) ) |
---|
748 | imcb_log( ic, "Creating new conversation: (id=0x%x,handle=%s)", (int) c, handle ); |
---|
749 | |
---|
750 | return c; |
---|
751 | } |
---|
752 | |
---|
753 | |
---|
754 | /* buddy_chat.c */ |
---|
755 | |
---|
756 | void add_chat_buddy( struct groupchat *b, char *handle ) |
---|
757 | { |
---|
758 | user_t *u = user_findhandle( b->ic, handle ); |
---|
759 | int me = 0; |
---|
760 | |
---|
761 | if( set_getbool( &b->ic->irc->set, "debug" ) ) |
---|
762 | imcb_log( b->ic, "User %s added to conversation 0x%x", handle, (int) b ); |
---|
763 | |
---|
764 | /* It might be yourself! */ |
---|
765 | if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 ) |
---|
766 | { |
---|
767 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
768 | if( !b->joined ) |
---|
769 | irc_join( b->ic->irc, u, b->channel ); |
---|
770 | b->joined = me = 1; |
---|
771 | } |
---|
772 | |
---|
773 | /* Most protocols allow people to join, even when they're not in |
---|
774 | your contact list. Try to handle that here */ |
---|
775 | if( !u ) |
---|
776 | { |
---|
777 | add_buddy( b->ic, NULL, handle, NULL ); |
---|
778 | u = user_findhandle( b->ic, handle ); |
---|
779 | } |
---|
780 | |
---|
781 | /* Add the handle to the room userlist, if it's not 'me' */ |
---|
782 | if( !me ) |
---|
783 | { |
---|
784 | if( b->joined ) |
---|
785 | irc_join( b->ic->irc, u, b->channel ); |
---|
786 | b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | void remove_chat_buddy( struct groupchat *b, char *handle, char *reason ) |
---|
791 | { |
---|
792 | user_t *u; |
---|
793 | int me = 0; |
---|
794 | |
---|
795 | if( set_getbool( &b->ic->irc->set, "debug" ) ) |
---|
796 | imcb_log( b->ic, "User %s removed from conversation 0x%x (%s)", handle, (int) b, reason ? reason : "" ); |
---|
797 | |
---|
798 | /* It might be yourself! */ |
---|
799 | if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) |
---|
800 | { |
---|
801 | u = user_find( b->ic->irc, b->ic->irc->nick ); |
---|
802 | b->joined = 0; |
---|
803 | me = 1; |
---|
804 | } |
---|
805 | else |
---|
806 | { |
---|
807 | u = user_findhandle( b->ic, handle ); |
---|
808 | } |
---|
809 | |
---|
810 | if( remove_chat_buddy_silent( b, handle ) ) |
---|
811 | if( ( b->joined || me ) && u ) |
---|
812 | irc_part( b->ic->irc, u, b->channel ); |
---|
813 | } |
---|
814 | |
---|
815 | static int remove_chat_buddy_silent( struct groupchat *b, char *handle ) |
---|
816 | { |
---|
817 | GList *i; |
---|
818 | |
---|
819 | /* Find the handle in the room userlist and shoot it */ |
---|
820 | i = b->in_room; |
---|
821 | while( i ) |
---|
822 | { |
---|
823 | if( g_strcasecmp( handle, i->data ) == 0 ) |
---|
824 | { |
---|
825 | g_free( i->data ); |
---|
826 | b->in_room = g_list_remove( b->in_room, i->data ); |
---|
827 | return( 1 ); |
---|
828 | } |
---|
829 | |
---|
830 | i = i->next; |
---|
831 | } |
---|
832 | |
---|
833 | return( 0 ); |
---|
834 | } |
---|
835 | |
---|
836 | |
---|
837 | /* Misc. BitlBee stuff which shouldn't really be here */ |
---|
838 | |
---|
839 | struct groupchat *chat_by_channel( char *channel ) |
---|
840 | { |
---|
841 | struct im_connection *ic; |
---|
842 | struct groupchat *c; |
---|
843 | GSList *l; |
---|
844 | |
---|
845 | /* This finds the connection which has a conversation which belongs to this channel */ |
---|
846 | for( l = connections; l; l = l->next ) |
---|
847 | { |
---|
848 | ic = l->data; |
---|
849 | for( c = ic->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next ); |
---|
850 | if( c ) |
---|
851 | return c; |
---|
852 | } |
---|
853 | |
---|
854 | return NULL; |
---|
855 | } |
---|
856 | |
---|
857 | char *set_eval_away_devoice( set_t *set, char *value ) |
---|
858 | { |
---|
859 | irc_t *irc = set->data; |
---|
860 | int st; |
---|
861 | |
---|
862 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
863 | st = 1; |
---|
864 | else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
865 | st = 0; |
---|
866 | else if( sscanf( value, "%d", &st ) != 1 ) |
---|
867 | return( NULL ); |
---|
868 | |
---|
869 | st = st != 0; |
---|
870 | |
---|
871 | /* Horror.... */ |
---|
872 | |
---|
873 | if( st != set_getbool( &irc->set, "away_devoice" ) ) |
---|
874 | { |
---|
875 | char list[80] = ""; |
---|
876 | user_t *u = irc->users; |
---|
877 | int i = 0, count = 0; |
---|
878 | char pm; |
---|
879 | char v[80]; |
---|
880 | |
---|
881 | if( st ) |
---|
882 | pm = '+'; |
---|
883 | else |
---|
884 | pm = '-'; |
---|
885 | |
---|
886 | while( u ) |
---|
887 | { |
---|
888 | if( u->ic && u->online && !u->away ) |
---|
889 | { |
---|
890 | if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) |
---|
891 | { |
---|
892 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
893 | irc_write( irc, ":%s MODE %s %c%s%s", |
---|
894 | irc->myhost, |
---|
895 | irc->channel, pm, v, list ); |
---|
896 | |
---|
897 | *list = 0; |
---|
898 | count = 0; |
---|
899 | } |
---|
900 | |
---|
901 | sprintf( list + strlen( list ), " %s", u->nick ); |
---|
902 | count ++; |
---|
903 | } |
---|
904 | u = u->next; |
---|
905 | } |
---|
906 | |
---|
907 | /* $v = 'v' x $i */ |
---|
908 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
909 | irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, |
---|
910 | irc->channel, pm, v, list ); |
---|
911 | } |
---|
912 | |
---|
913 | return( set_eval_bool( set, value ) ); |
---|
914 | } |
---|
915 | |
---|
916 | |
---|
917 | |
---|
918 | |
---|
919 | /* The plan is to not allow straight calls to prpl functions anymore, but do |
---|
920 | them all from some wrappers. We'll start to define some down here: */ |
---|
921 | |
---|
922 | int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags ) |
---|
923 | { |
---|
924 | char *buf = NULL; |
---|
925 | int st; |
---|
926 | |
---|
927 | if( ( ic->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
928 | { |
---|
929 | buf = escape_html( msg ); |
---|
930 | msg = buf; |
---|
931 | } |
---|
932 | |
---|
933 | st = ic->acc->prpl->send_im( ic, handle, msg, flags ); |
---|
934 | g_free( buf ); |
---|
935 | |
---|
936 | return st; |
---|
937 | } |
---|
938 | |
---|
939 | int imc_chat_msg( struct groupchat *c, char *msg, int flags ) |
---|
940 | { |
---|
941 | char *buf = NULL; |
---|
942 | |
---|
943 | if( ( c->ic->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
944 | { |
---|
945 | buf = escape_html( msg ); |
---|
946 | msg = buf; |
---|
947 | } |
---|
948 | |
---|
949 | c->ic->acc->prpl->chat_send( c, msg, flags ); |
---|
950 | g_free( buf ); |
---|
951 | |
---|
952 | return 1; |
---|
953 | } |
---|
954 | |
---|
955 | static char *imc_away_alias_find( GList *gcm, char *away ); |
---|
956 | |
---|
957 | int imc_set_away( struct im_connection *ic, char *away ) |
---|
958 | { |
---|
959 | GList *m, *ms; |
---|
960 | char *s; |
---|
961 | |
---|
962 | if( !away ) away = ""; |
---|
963 | ms = m = ic->acc->prpl->away_states( ic ); |
---|
964 | |
---|
965 | while( m ) |
---|
966 | { |
---|
967 | if( *away ) |
---|
968 | { |
---|
969 | if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) |
---|
970 | break; |
---|
971 | } |
---|
972 | else |
---|
973 | { |
---|
974 | if( g_strcasecmp( m->data, "Available" ) == 0 ) |
---|
975 | break; |
---|
976 | if( g_strcasecmp( m->data, "Online" ) == 0 ) |
---|
977 | break; |
---|
978 | } |
---|
979 | m = m->next; |
---|
980 | } |
---|
981 | |
---|
982 | if( m ) |
---|
983 | { |
---|
984 | ic->acc->prpl->set_away( ic, m->data, *away ? away : NULL ); |
---|
985 | } |
---|
986 | else |
---|
987 | { |
---|
988 | s = imc_away_alias_find( ms, away ); |
---|
989 | if( s ) |
---|
990 | { |
---|
991 | ic->acc->prpl->set_away( ic, s, away ); |
---|
992 | if( set_getbool( &ic->irc->set, "debug" ) ) |
---|
993 | imcb_log( ic, "Setting away state to %s", s ); |
---|
994 | } |
---|
995 | else |
---|
996 | ic->acc->prpl->set_away( ic, GAIM_AWAY_CUSTOM, away ); |
---|
997 | } |
---|
998 | |
---|
999 | return( 1 ); |
---|
1000 | } |
---|
1001 | |
---|
1002 | static char *imc_away_alias_list[8][5] = |
---|
1003 | { |
---|
1004 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
1005 | { "NA", "N/A", "Not available", NULL }, |
---|
1006 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
1007 | { "Be right back", "BRB", NULL }, |
---|
1008 | { "On the phone", "Phone", "On phone", NULL }, |
---|
1009 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
1010 | { "Invisible", "Hidden" }, |
---|
1011 | { NULL } |
---|
1012 | }; |
---|
1013 | |
---|
1014 | static char *imc_away_alias_find( GList *gcm, char *away ) |
---|
1015 | { |
---|
1016 | GList *m; |
---|
1017 | int i, j; |
---|
1018 | |
---|
1019 | for( i = 0; *imc_away_alias_list[i]; i ++ ) |
---|
1020 | { |
---|
1021 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
1022 | if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 ) |
---|
1023 | break; |
---|
1024 | |
---|
1025 | if( !imc_away_alias_list[i][j] ) /* If we reach the end, this row */ |
---|
1026 | continue; /* is not what we want. Next! */ |
---|
1027 | |
---|
1028 | /* Now find an entry in this row which exists in gcm */ |
---|
1029 | for( j = 0; imc_away_alias_list[i][j]; j ++ ) |
---|
1030 | { |
---|
1031 | m = gcm; |
---|
1032 | while( m ) |
---|
1033 | { |
---|
1034 | if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 ) |
---|
1035 | return( imc_away_alias_list[i][j] ); |
---|
1036 | m = m->next; |
---|
1037 | } |
---|
1038 | } |
---|
1039 | } |
---|
1040 | |
---|
1041 | return( NULL ); |
---|
1042 | } |
---|
1043 | |
---|
1044 | void imc_add_allow( struct im_connection *ic, char *handle ) |
---|
1045 | { |
---|
1046 | if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
1047 | { |
---|
1048 | ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) ); |
---|
1049 | } |
---|
1050 | |
---|
1051 | ic->acc->prpl->add_permit( ic, handle ); |
---|
1052 | } |
---|
1053 | |
---|
1054 | void imc_rem_allow( struct im_connection *ic, char *handle ) |
---|
1055 | { |
---|
1056 | GSList *l; |
---|
1057 | |
---|
1058 | if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
1059 | { |
---|
1060 | g_free( l->data ); |
---|
1061 | ic->permit = g_slist_delete_link( ic->permit, l ); |
---|
1062 | } |
---|
1063 | |
---|
1064 | ic->acc->prpl->rem_permit( ic, handle ); |
---|
1065 | } |
---|
1066 | |
---|
1067 | void imc_add_block( struct im_connection *ic, char *handle ) |
---|
1068 | { |
---|
1069 | if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) |
---|
1070 | { |
---|
1071 | ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) ); |
---|
1072 | } |
---|
1073 | |
---|
1074 | ic->acc->prpl->add_deny( ic, handle ); |
---|
1075 | } |
---|
1076 | |
---|
1077 | void imc_rem_block( struct im_connection *ic, char *handle ) |
---|
1078 | { |
---|
1079 | GSList *l; |
---|
1080 | |
---|
1081 | if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) |
---|
1082 | { |
---|
1083 | g_free( l->data ); |
---|
1084 | ic->deny = g_slist_delete_link( ic->deny, l ); |
---|
1085 | } |
---|
1086 | |
---|
1087 | ic->acc->prpl->rem_deny( ic, handle ); |
---|
1088 | } |
---|