1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 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 | * Copyright 2002-2004 Wilmer van der Gaast <lintux@lintux.cx> |
---|
17 | */ |
---|
18 | |
---|
19 | /* |
---|
20 | This program is free software; you can redistribute it and/or modify |
---|
21 | it under the terms of the GNU General Public License as published by |
---|
22 | the Free Software Foundation; either version 2 of the License, or |
---|
23 | (at your option) any later version. |
---|
24 | |
---|
25 | This program is distributed in the hope that it will be useful, |
---|
26 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
27 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
28 | GNU General Public License for more details. |
---|
29 | |
---|
30 | You should have received a copy of the GNU General Public License with |
---|
31 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
32 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
33 | Suite 330, Boston, MA 02111-1307 USA |
---|
34 | */ |
---|
35 | |
---|
36 | #define BITLBEE_CORE |
---|
37 | #include "nogaim.h" |
---|
38 | #include <ctype.h> |
---|
39 | #include <iconv.h> |
---|
40 | |
---|
41 | struct prpl *proto_prpl[PROTO_MAX]; |
---|
42 | char proto_name[PROTO_MAX][8] = { "TOC", "OSCAR", "YAHOO", "ICQ", "MSN", "", "", "", "JABBER", "", "", "", "", "", "", "" }; |
---|
43 | |
---|
44 | static char *proto_away_alias[7][5] = |
---|
45 | { |
---|
46 | { "Away from computer", "Away", "Extended away", NULL }, |
---|
47 | { "NA", "N/A", "Not available", NULL }, |
---|
48 | { "Busy", "Do not disturb", "DND", "Occupied", NULL }, |
---|
49 | { "Be right back", "BRB", NULL }, |
---|
50 | { "On the phone", "Phone", "On phone", NULL }, |
---|
51 | { "Out to lunch", "Lunch", "Food", NULL }, |
---|
52 | { NULL } |
---|
53 | }; |
---|
54 | static char *proto_away_alias_find( GList *gcm, char *away ); |
---|
55 | |
---|
56 | static int remove_chat_buddy_silent( struct conversation *b, char *handle ); |
---|
57 | |
---|
58 | GSList *connections; |
---|
59 | |
---|
60 | |
---|
61 | /* nogaim.c */ |
---|
62 | |
---|
63 | void nogaim_init() |
---|
64 | { |
---|
65 | proto_prpl[PROTO_MSN] = g_new0 ( struct prpl, 1 ); |
---|
66 | #ifdef WITH_MSN |
---|
67 | msn_init( proto_prpl[PROTO_MSN] ); |
---|
68 | #endif |
---|
69 | |
---|
70 | proto_prpl[PROTO_OSCAR] = g_new0( struct prpl, 1 ); |
---|
71 | #ifdef WITH_OSCAR |
---|
72 | oscar_init( proto_prpl[PROTO_OSCAR] ); |
---|
73 | #endif |
---|
74 | |
---|
75 | proto_prpl[PROTO_YAHOO] = g_new0( struct prpl, 1 ); |
---|
76 | #ifdef WITH_YAHOO |
---|
77 | byahoo_init( proto_prpl[PROTO_YAHOO] ); |
---|
78 | #endif |
---|
79 | |
---|
80 | proto_prpl[PROTO_JABBER] = g_new0( struct prpl, 1 ); |
---|
81 | #ifdef WITH_JABBER |
---|
82 | jabber_init( proto_prpl[PROTO_JABBER] ); |
---|
83 | #endif |
---|
84 | } |
---|
85 | |
---|
86 | GSList *get_connections() { return connections; } |
---|
87 | |
---|
88 | int proto_away( struct gaim_connection *gc, char *away ) |
---|
89 | { |
---|
90 | GList *m, *ms; |
---|
91 | char *s; |
---|
92 | |
---|
93 | if( !away ) away = ""; |
---|
94 | ms = m = gc->prpl->away_states( gc ); |
---|
95 | |
---|
96 | while( m ) |
---|
97 | { |
---|
98 | if( *away ) |
---|
99 | { |
---|
100 | if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) |
---|
101 | break; |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | if( g_strcasecmp( m->data, "Available" ) == 0 ) |
---|
106 | break; |
---|
107 | if( g_strcasecmp( m->data, "Online" ) == 0 ) |
---|
108 | break; |
---|
109 | } |
---|
110 | m = m->next; |
---|
111 | } |
---|
112 | |
---|
113 | if( m ) |
---|
114 | { |
---|
115 | gc->prpl->set_away( gc, m->data, *away ? away : NULL ); |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
119 | s = proto_away_alias_find( ms, away ); |
---|
120 | if( s ) |
---|
121 | { |
---|
122 | gc->prpl->set_away( gc, s, away ); |
---|
123 | if( set_getint( gc->irc, "debug" ) ) |
---|
124 | serv_got_crap( gc, "Setting away state to %s", s ); |
---|
125 | } |
---|
126 | else |
---|
127 | gc->prpl->set_away( gc, GAIM_AWAY_CUSTOM, away ); |
---|
128 | } |
---|
129 | |
---|
130 | g_list_free( ms ); |
---|
131 | |
---|
132 | return( 1 ); |
---|
133 | } |
---|
134 | |
---|
135 | static char *proto_away_alias_find( GList *gcm, char *away ) |
---|
136 | { |
---|
137 | GList *m; |
---|
138 | int i, j; |
---|
139 | |
---|
140 | for( i = 0; *proto_away_alias[i]; i ++ ) |
---|
141 | { |
---|
142 | for( j = 0; proto_away_alias[i][j]; j ++ ) |
---|
143 | if( g_strncasecmp( away, proto_away_alias[i][j], strlen( proto_away_alias[i][j] ) ) == 0 ) |
---|
144 | break; |
---|
145 | |
---|
146 | if( !proto_away_alias[i][j] ) /* If we reach the end, this row */ |
---|
147 | continue; /* is not what we want. Next! */ |
---|
148 | |
---|
149 | /* Now find an entry in this row which exists in gcm */ |
---|
150 | for( j = 0; proto_away_alias[i][j]; j ++ ) |
---|
151 | { |
---|
152 | m = gcm; |
---|
153 | while( m ) |
---|
154 | { |
---|
155 | if( g_strcasecmp( proto_away_alias[i][j], m->data ) == 0 ) |
---|
156 | return( proto_away_alias[i][j] ); |
---|
157 | m = m->next; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | return( NULL ); |
---|
163 | } |
---|
164 | |
---|
165 | /* multi.c */ |
---|
166 | |
---|
167 | struct gaim_connection *new_gaim_conn( struct aim_user *user ) |
---|
168 | { |
---|
169 | struct gaim_connection *gc; |
---|
170 | account_t *a; |
---|
171 | |
---|
172 | gc = g_new0( struct gaim_connection, 1 ); |
---|
173 | |
---|
174 | gc->protocol = user->protocol; |
---|
175 | gc->prpl = proto_prpl[gc->protocol]; |
---|
176 | g_snprintf( gc->username, sizeof( gc->username ), "%s", user->username ); |
---|
177 | g_snprintf( gc->password, sizeof( gc->password ), "%s", user->password ); |
---|
178 | /* [MD] BUGFIX: don't set gc->irc to the global IRC, but use the one from the struct aim_user. |
---|
179 | * This fixes daemon mode breakage where IRC doesn't point to the currently active connection. |
---|
180 | */ |
---|
181 | gc->irc=user->irc; |
---|
182 | |
---|
183 | connections = g_slist_append( connections, gc ); |
---|
184 | |
---|
185 | user->gc = gc; |
---|
186 | gc->user = user; |
---|
187 | |
---|
188 | // Find the account_t so we can set its gc pointer |
---|
189 | for( a = gc->irc->accounts; a; a = a->next ) |
---|
190 | if( ( struct aim_user * ) a->gc == user ) |
---|
191 | { |
---|
192 | a->gc = gc; |
---|
193 | break; |
---|
194 | } |
---|
195 | |
---|
196 | return( gc ); |
---|
197 | } |
---|
198 | |
---|
199 | void destroy_gaim_conn( struct gaim_connection *gc ) |
---|
200 | { |
---|
201 | account_t *a; |
---|
202 | |
---|
203 | /* Destroy the pointer to this connection from the account list */ |
---|
204 | for( a = gc->irc->accounts; a; a = a->next ) |
---|
205 | if( a->gc == gc ) |
---|
206 | { |
---|
207 | a->gc = NULL; |
---|
208 | break; |
---|
209 | } |
---|
210 | |
---|
211 | connections = g_slist_remove( connections, gc ); |
---|
212 | g_free( gc->user ); |
---|
213 | g_free( gc ); |
---|
214 | } |
---|
215 | |
---|
216 | void set_login_progress( struct gaim_connection *gc, int step, char *msg ) |
---|
217 | { |
---|
218 | serv_got_crap( gc, "Logging in: %s", msg ); |
---|
219 | } |
---|
220 | |
---|
221 | /* Errors *while* logging in */ |
---|
222 | void hide_login_progress( struct gaim_connection *gc, char *msg ) |
---|
223 | { |
---|
224 | serv_got_crap( gc, "Login error: %s", msg ); |
---|
225 | } |
---|
226 | |
---|
227 | /* Errors *after* logging in */ |
---|
228 | void hide_login_progress_error( struct gaim_connection *gc, char *msg ) |
---|
229 | { |
---|
230 | serv_got_crap( gc, "Logged out: %s", msg ); |
---|
231 | } |
---|
232 | |
---|
233 | void serv_got_crap( struct gaim_connection *gc, char *format, ... ) |
---|
234 | { |
---|
235 | va_list params; |
---|
236 | char text[1024], buf[1024]; |
---|
237 | char *msg; |
---|
238 | |
---|
239 | va_start( params, format ); |
---|
240 | g_vsnprintf( text, sizeof( text ), format, params ); |
---|
241 | va_end( params ); |
---|
242 | |
---|
243 | if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 && |
---|
244 | do_iconv( "UTF8", set_getstr( gc->irc, "charset" ), text, buf, 0, 1024 ) != -1 ) |
---|
245 | msg = buf; |
---|
246 | else |
---|
247 | msg = text; |
---|
248 | |
---|
249 | if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) || |
---|
250 | ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) ) |
---|
251 | strip_html( msg ); |
---|
252 | |
---|
253 | irc_usermsg( gc->irc, "%s(%s) - %s", proto_name[gc->protocol], gc->username, msg ); |
---|
254 | } |
---|
255 | |
---|
256 | static gboolean send_keepalive( gpointer d ) |
---|
257 | { |
---|
258 | struct gaim_connection *gc = d; |
---|
259 | |
---|
260 | if( gc->prpl && gc->prpl->keepalive ) |
---|
261 | gc->prpl->keepalive( gc ); |
---|
262 | |
---|
263 | return TRUE; |
---|
264 | } |
---|
265 | |
---|
266 | void account_online( struct gaim_connection *gc ) |
---|
267 | { |
---|
268 | user_t *u; |
---|
269 | |
---|
270 | /* MSN servers sometimes redirect you to a different server and do |
---|
271 | the whole login sequence again, so subsequent calls to this |
---|
272 | function should be handled correctly. (IOW, ignored) */ |
---|
273 | if( gc->flags & OPT_LOGGED_IN ) |
---|
274 | return; |
---|
275 | |
---|
276 | u = user_find( gc->irc, gc->irc->nick ); |
---|
277 | |
---|
278 | serv_got_crap( gc, "Logged in" ); |
---|
279 | |
---|
280 | gc->keepalive = g_timeout_add( 60000, send_keepalive, gc ); |
---|
281 | gc->flags |= OPT_LOGGED_IN; |
---|
282 | |
---|
283 | if( u && u->away ) proto_away( gc, u->away ); |
---|
284 | |
---|
285 | if( gc->protocol == PROTO_ICQ ) |
---|
286 | { |
---|
287 | for( u = gc->irc->users; u; u = u->next ) |
---|
288 | if( u->gc == gc ) |
---|
289 | break; |
---|
290 | |
---|
291 | if( u == NULL ) |
---|
292 | serv_got_crap( gc, "\x02""***\x02"" BitlBee now supports ICQ server-side contact lists. " |
---|
293 | "See \x02""help import_buddies\x02"" for more information." ); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | gboolean auto_reconnect( gpointer data ) |
---|
298 | { |
---|
299 | account_t *a = data; |
---|
300 | |
---|
301 | a->reconnect = 0; |
---|
302 | account_on( a->irc, a ); |
---|
303 | |
---|
304 | return( FALSE ); /* Only have to run the timeout once */ |
---|
305 | } |
---|
306 | |
---|
307 | void cancel_auto_reconnect( account_t *a ) |
---|
308 | { |
---|
309 | while( g_source_remove_by_user_data( (gpointer) a ) ); |
---|
310 | a->reconnect = 0; |
---|
311 | } |
---|
312 | |
---|
313 | void account_offline( struct gaim_connection *gc ) |
---|
314 | { |
---|
315 | gc->wants_to_die = TRUE; |
---|
316 | signoff( gc ); |
---|
317 | } |
---|
318 | |
---|
319 | void signoff( struct gaim_connection *gc ) |
---|
320 | { |
---|
321 | irc_t *irc = gc->irc; |
---|
322 | user_t *t, *u = irc->users; |
---|
323 | account_t *a; |
---|
324 | |
---|
325 | serv_got_crap( gc, "Signing off.." ); |
---|
326 | |
---|
327 | gaim_input_remove( gc->keepalive ); |
---|
328 | gc->keepalive = 0; |
---|
329 | gc->prpl->close( gc ); |
---|
330 | gaim_input_remove( gc->inpa ); |
---|
331 | |
---|
332 | while( u ) |
---|
333 | { |
---|
334 | if( u->gc == gc ) |
---|
335 | { |
---|
336 | t = u->next; |
---|
337 | user_del( irc, u->nick ); |
---|
338 | u = t; |
---|
339 | } |
---|
340 | else |
---|
341 | u = u->next; |
---|
342 | } |
---|
343 | |
---|
344 | query_del_by_gc( gc->irc, gc ); |
---|
345 | |
---|
346 | for( a = irc->accounts; a; a = a->next ) |
---|
347 | if( a->gc == gc ) |
---|
348 | break; |
---|
349 | |
---|
350 | if( !a ) |
---|
351 | { |
---|
352 | /* Uhm... This is very sick. */ |
---|
353 | } |
---|
354 | else if( !gc->wants_to_die && set_getint( irc, "auto_reconnect" ) ) |
---|
355 | { |
---|
356 | int delay = set_getint( irc, "auto_reconnect_delay" ); |
---|
357 | serv_got_crap( gc, "Reconnecting in %d seconds..", delay ); |
---|
358 | |
---|
359 | a->reconnect = 1; |
---|
360 | g_timeout_add( delay * 1000, auto_reconnect, a ); |
---|
361 | } |
---|
362 | |
---|
363 | destroy_gaim_conn( gc ); |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | /* dialogs.c */ |
---|
368 | |
---|
369 | void do_error_dialog( struct gaim_connection *gc, char *msg, char *title ) |
---|
370 | { |
---|
371 | serv_got_crap( gc, "Error: %s", msg ); |
---|
372 | } |
---|
373 | |
---|
374 | void do_ask_dialog( struct gaim_connection *gc, char *msg, void *data, void *doit, void *dont ) |
---|
375 | { |
---|
376 | query_add( gc->irc, gc, msg, doit, dont, data ); |
---|
377 | } |
---|
378 | |
---|
379 | |
---|
380 | /* list.c */ |
---|
381 | |
---|
382 | int bud_list_cache_exists( struct gaim_connection *gc ) |
---|
383 | { |
---|
384 | return( 0 ); |
---|
385 | } |
---|
386 | |
---|
387 | void do_import( struct gaim_connection *gc, void *null ) |
---|
388 | { |
---|
389 | return; |
---|
390 | } |
---|
391 | |
---|
392 | void add_buddy( struct gaim_connection *gc, char *group, char *handle, char *realname ) |
---|
393 | { |
---|
394 | user_t *u; |
---|
395 | char nick[MAX_NICK_LENGTH+1]; |
---|
396 | char *s; |
---|
397 | irc_t *irc = gc->irc; |
---|
398 | |
---|
399 | if( set_getint( irc, "debug" ) && 0 ) /* This message is too useless */ |
---|
400 | serv_got_crap( gc, "Receiving user add from handle: %s", handle ); |
---|
401 | |
---|
402 | if( user_findhandle( gc, handle ) ) |
---|
403 | { |
---|
404 | if( set_getint( irc, "debug" ) ) |
---|
405 | serv_got_crap( gc, "User already exists, ignoring add request: %s", handle ); |
---|
406 | |
---|
407 | return; |
---|
408 | |
---|
409 | /* Buddy seems to exist already. Let's ignore this request then... */ |
---|
410 | } |
---|
411 | |
---|
412 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
413 | strcpy( nick, nick_get( gc->irc, handle, gc->protocol, realname ) ); |
---|
414 | |
---|
415 | u = user_add( gc->irc, nick ); |
---|
416 | |
---|
417 | if( !realname || !*realname ) realname = nick; |
---|
418 | u->realname = g_strdup( realname ); |
---|
419 | |
---|
420 | if( ( s = strchr( handle, '@' ) ) ) |
---|
421 | { |
---|
422 | u->host = g_strdup( s + 1 ); |
---|
423 | u->user = g_strndup( handle, s - handle ); |
---|
424 | } |
---|
425 | else if( gc->user->proto_opt[0] && *gc->user->proto_opt[0] ) |
---|
426 | { |
---|
427 | u->host = g_strdup( gc->user->proto_opt[0] ); |
---|
428 | u->user = g_strdup( handle ); |
---|
429 | |
---|
430 | /* s/ /_/ ... important for AOL screennames */ |
---|
431 | for( s = u->user; *s; s ++ ) |
---|
432 | if( *s == ' ' ) |
---|
433 | *s = '_'; |
---|
434 | } |
---|
435 | else |
---|
436 | { |
---|
437 | u->host = g_strdup( proto_name[gc->user->protocol] ); |
---|
438 | u->user = g_strdup( handle ); |
---|
439 | } |
---|
440 | |
---|
441 | u->gc = gc; |
---|
442 | u->handle = g_strdup( handle ); |
---|
443 | u->send_handler = buddy_send_handler; |
---|
444 | u->last_typing_notice = 0; |
---|
445 | } |
---|
446 | |
---|
447 | struct buddy *find_buddy( struct gaim_connection *gc, char *handle ) |
---|
448 | { |
---|
449 | static struct buddy b[1]; |
---|
450 | user_t *u; |
---|
451 | |
---|
452 | u = user_findhandle( gc, handle ); |
---|
453 | |
---|
454 | if( !u ) |
---|
455 | return( NULL ); |
---|
456 | |
---|
457 | memset( b, 0, sizeof( b ) ); |
---|
458 | strncpy( b->name, handle, 80 ); |
---|
459 | strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); |
---|
460 | b->present = u->online; |
---|
461 | b->gc = u->gc; |
---|
462 | |
---|
463 | return( b ); |
---|
464 | } |
---|
465 | |
---|
466 | void do_export( struct gaim_connection *gc ) |
---|
467 | { |
---|
468 | return; |
---|
469 | } |
---|
470 | |
---|
471 | void signoff_blocked( struct gaim_connection *gc ) |
---|
472 | { |
---|
473 | return; /* Make all blocked users look invisible (TODO?) */ |
---|
474 | } |
---|
475 | |
---|
476 | |
---|
477 | void serv_buddy_rename( struct gaim_connection *gc, char *handle, char *realname ) |
---|
478 | { |
---|
479 | user_t *u = user_findhandle( gc, handle ); |
---|
480 | char *name, buf[1024]; |
---|
481 | |
---|
482 | if( !u ) return; |
---|
483 | |
---|
484 | /* Convert all UTF-8 */ |
---|
485 | if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 && |
---|
486 | do_iconv( "UTF-8", set_getstr( gc->irc, "charset" ), realname, buf, 0, sizeof( buf ) ) != -1 ) |
---|
487 | name = buf; |
---|
488 | else |
---|
489 | name = realname; |
---|
490 | |
---|
491 | if( g_strcasecmp( u->realname, name ) != 0 ) |
---|
492 | { |
---|
493 | if( u->realname != u->nick ) g_free( u->realname ); |
---|
494 | |
---|
495 | u->realname = g_strdup( name ); |
---|
496 | |
---|
497 | if( ( gc->flags & OPT_LOGGED_IN ) && set_getint( gc->irc, "display_namechanges" ) ) |
---|
498 | serv_got_crap( gc, "User `%s' changed name to `%s'", u->nick, u->realname ); |
---|
499 | } |
---|
500 | } |
---|
501 | |
---|
502 | |
---|
503 | /* prpl.c */ |
---|
504 | |
---|
505 | void show_got_added( struct gaim_connection *gc, char *id, char *handle, const char *realname, const char *msg ) |
---|
506 | { |
---|
507 | return; |
---|
508 | } |
---|
509 | |
---|
510 | |
---|
511 | /* server.c */ |
---|
512 | |
---|
513 | void serv_got_update( struct gaim_connection *gc, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps ) |
---|
514 | { |
---|
515 | user_t *u; |
---|
516 | int oa, oo; |
---|
517 | |
---|
518 | u = user_findhandle( gc, handle ); |
---|
519 | |
---|
520 | if( !u ) |
---|
521 | { |
---|
522 | if( g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "add" ) == 0 ) |
---|
523 | { |
---|
524 | add_buddy( gc, NULL, handle, NULL ); |
---|
525 | u = user_findhandle( gc, handle ); |
---|
526 | } |
---|
527 | else |
---|
528 | { |
---|
529 | if( set_getint( gc->irc, "debug" ) || g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "ignore" ) != 0 ) |
---|
530 | { |
---|
531 | serv_got_crap( gc, "serv_got_update() for handle %s:", handle ); |
---|
532 | serv_got_crap( gc, "loggedin = %d, type = %d", loggedin, type ); |
---|
533 | } |
---|
534 | |
---|
535 | return; |
---|
536 | } |
---|
537 | return; |
---|
538 | } |
---|
539 | |
---|
540 | oa = u->away != NULL; |
---|
541 | oo = u->online; |
---|
542 | |
---|
543 | if( u->away ) |
---|
544 | { |
---|
545 | g_free( u->away ); |
---|
546 | u->away = NULL; |
---|
547 | } |
---|
548 | |
---|
549 | if( loggedin && !u->online ) |
---|
550 | { |
---|
551 | irc_spawn( gc->irc, u ); |
---|
552 | u->online = 1; |
---|
553 | } |
---|
554 | else if( !loggedin && u->online ) |
---|
555 | { |
---|
556 | struct conversation *c; |
---|
557 | |
---|
558 | irc_kill( gc->irc, u ); |
---|
559 | u->online = 0; |
---|
560 | |
---|
561 | /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */ |
---|
562 | for( c = gc->conversations; c; c = c->next ) |
---|
563 | remove_chat_buddy_silent( c, handle ); |
---|
564 | } |
---|
565 | |
---|
566 | if( ( type & UC_UNAVAILABLE ) && ( gc->protocol == PROTO_OSCAR || gc->protocol == PROTO_TOC ) ) |
---|
567 | { |
---|
568 | u->away = g_strdup( "Away" ); |
---|
569 | } |
---|
570 | else if( ( type & UC_UNAVAILABLE ) && ( gc->protocol == PROTO_JABBER ) ) |
---|
571 | { |
---|
572 | if( type & UC_DND ) |
---|
573 | u->away = g_strdup( "Do Not Disturb" ); |
---|
574 | else if( type & UC_XA ) |
---|
575 | u->away = g_strdup( "Extended Away" ); |
---|
576 | else // if( type & UC_AWAY ) |
---|
577 | u->away = g_strdup( "Away" ); |
---|
578 | } |
---|
579 | else if( ( type & UC_UNAVAILABLE ) && gc->prpl->get_status_string ) |
---|
580 | { |
---|
581 | u->away = g_strdup( gc->prpl->get_status_string( gc, type ) ); |
---|
582 | } |
---|
583 | else |
---|
584 | u->away = NULL; |
---|
585 | |
---|
586 | /* LISPy... */ |
---|
587 | if( ( set_getint( gc->irc, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ |
---|
588 | ( u->online ) && /* Don't touch offline people */ |
---|
589 | ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ |
---|
590 | ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ |
---|
591 | { |
---|
592 | irc_write( gc->irc, ":%s!%s@%s MODE %s %cv %s", gc->irc->mynick, gc->irc->mynick, gc->irc->myhost, |
---|
593 | gc->irc->channel, u->away?'-':'+', u->nick ); |
---|
594 | } |
---|
595 | } |
---|
596 | |
---|
597 | void serv_got_im( struct gaim_connection *gc, char *handle, char *msg, guint32 flags, time_t mtime, gint len ) |
---|
598 | { |
---|
599 | irc_t *irc = gc->irc; |
---|
600 | user_t *u; |
---|
601 | char buf[8192]; |
---|
602 | |
---|
603 | u = user_findhandle( gc, handle ); |
---|
604 | |
---|
605 | if( !u ) |
---|
606 | { |
---|
607 | char *h = set_getstr( irc, "handle_unknown" ); |
---|
608 | |
---|
609 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
610 | { |
---|
611 | if( set_getint( irc, "debug" ) ) |
---|
612 | serv_got_crap( gc, "Ignoring message from unknown handle %s", handle ); |
---|
613 | |
---|
614 | return; |
---|
615 | } |
---|
616 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
617 | { |
---|
618 | int private = set_getint( irc, "private" ); |
---|
619 | |
---|
620 | if( h[3] ) |
---|
621 | { |
---|
622 | if( g_strcasecmp( h + 3, "_private" ) == 0 ) |
---|
623 | private = 1; |
---|
624 | else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) |
---|
625 | private = 0; |
---|
626 | } |
---|
627 | |
---|
628 | add_buddy( gc, NULL, handle, NULL ); |
---|
629 | u = user_findhandle( gc, handle ); |
---|
630 | u->is_private = private; |
---|
631 | } |
---|
632 | else |
---|
633 | { |
---|
634 | serv_got_crap( gc, "Message from unknown handle %s:", handle ); |
---|
635 | u = user_find( irc, irc->mynick ); |
---|
636 | } |
---|
637 | } |
---|
638 | |
---|
639 | if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) || |
---|
640 | ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) ) |
---|
641 | strip_html( msg ); |
---|
642 | |
---|
643 | if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 && |
---|
644 | do_iconv( "UTF-8", set_getstr( irc, "charset" ), msg, buf, 0, 8192 ) != -1 ) |
---|
645 | msg = buf; |
---|
646 | |
---|
647 | while( strlen( msg ) > 450 ) |
---|
648 | { |
---|
649 | char tmp, *nl; |
---|
650 | |
---|
651 | tmp = msg[450]; |
---|
652 | msg[450] = 0; |
---|
653 | |
---|
654 | /* If there's a newline in this string, split up there so we're not |
---|
655 | going to split up lines. If there isn't a newline, well, too bad. */ |
---|
656 | if( ( nl = strrchr( msg, '\n' ) ) ) |
---|
657 | *nl = 0; |
---|
658 | |
---|
659 | irc_msgfrom( irc, u->nick, msg ); |
---|
660 | |
---|
661 | msg[450] = tmp; |
---|
662 | |
---|
663 | /* Move on. */ |
---|
664 | if( nl ) |
---|
665 | { |
---|
666 | *nl = '\n'; |
---|
667 | msg = nl + 1; |
---|
668 | } |
---|
669 | else |
---|
670 | { |
---|
671 | msg += 450; |
---|
672 | } |
---|
673 | } |
---|
674 | irc_msgfrom( irc, u->nick, msg ); |
---|
675 | } |
---|
676 | |
---|
677 | void serv_got_typing( struct gaim_connection *gc, char *handle, int timeout ) |
---|
678 | { |
---|
679 | user_t *u; |
---|
680 | |
---|
681 | if( !set_getint( gc->irc, "typing_notice" ) ) |
---|
682 | return; |
---|
683 | |
---|
684 | if( ( u = user_findhandle( gc, handle ) ) ) |
---|
685 | irc_msgfrom( gc->irc, u->nick, "\1TYPING 1\1" ); |
---|
686 | } |
---|
687 | |
---|
688 | void serv_got_chat_left( struct gaim_connection *gc, int id ) |
---|
689 | { |
---|
690 | struct conversation *c, *l = NULL; |
---|
691 | GList *ir; |
---|
692 | |
---|
693 | if( set_getint( gc->irc, "debug" ) ) |
---|
694 | serv_got_crap( gc, "You were removed from conversation %d", (int) id ); |
---|
695 | |
---|
696 | for( c = gc->conversations; c && c->id != id; c = (l=c)->next ); |
---|
697 | |
---|
698 | if( c ) |
---|
699 | { |
---|
700 | if( c->joined ) |
---|
701 | { |
---|
702 | user_t *u, *r; |
---|
703 | |
---|
704 | r = user_find( gc->irc, gc->irc->mynick ); |
---|
705 | irc_privmsg( gc->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); |
---|
706 | |
---|
707 | u = user_find( gc->irc, gc->irc->nick ); |
---|
708 | irc_kick( gc->irc, u, c->channel, r ); |
---|
709 | /* irc_part( gc->irc, u, c->channel ); */ |
---|
710 | } |
---|
711 | |
---|
712 | if( l ) |
---|
713 | l->next = c->next; |
---|
714 | else |
---|
715 | gc->conversations = c->next; |
---|
716 | |
---|
717 | for( ir = c->in_room; ir; ir = ir->next ) |
---|
718 | g_free( ir->data ); |
---|
719 | g_list_free( c->in_room ); |
---|
720 | g_free( c->channel ); |
---|
721 | g_free( c->title ); |
---|
722 | g_free( c ); |
---|
723 | } |
---|
724 | } |
---|
725 | |
---|
726 | void serv_got_chat_in( struct gaim_connection *gc, int id, char *who, int whisper, char *msg, time_t mtime ) |
---|
727 | { |
---|
728 | struct conversation *c; |
---|
729 | user_t *u; |
---|
730 | char buf[8192]; |
---|
731 | |
---|
732 | /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ |
---|
733 | if( g_strcasecmp( who, gc->user->username ) == 0 ) |
---|
734 | return; |
---|
735 | |
---|
736 | u = user_findhandle( gc, who ); |
---|
737 | for( c = gc->conversations; c && c->id != id; c = c->next ); |
---|
738 | |
---|
739 | if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) || |
---|
740 | ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) ) |
---|
741 | strip_html( msg ); |
---|
742 | |
---|
743 | if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 && |
---|
744 | do_iconv( "UTF-8", set_getstr( gc->irc, "charset" ), msg, buf, 0, 8192 ) != -1 ) |
---|
745 | msg = buf; |
---|
746 | |
---|
747 | if( c && u ) |
---|
748 | irc_privmsg( gc->irc, u, "PRIVMSG", c->channel, "", msg ); |
---|
749 | else |
---|
750 | serv_got_crap( gc, "Message from/to conversation %s@%d (unknown conv/user): %s", who, id, msg ); |
---|
751 | } |
---|
752 | |
---|
753 | struct conversation *serv_got_joined_chat( struct gaim_connection *gc, int id, char *handle ) |
---|
754 | { |
---|
755 | struct conversation *c; |
---|
756 | char *s; |
---|
757 | |
---|
758 | /* This one just creates the conversation structure, user won't see anything yet */ |
---|
759 | |
---|
760 | if( gc->conversations ) |
---|
761 | { |
---|
762 | for( c = gc->conversations; c->next; c = c->next ); |
---|
763 | c = c->next = g_new0( struct conversation, 1 ); |
---|
764 | } |
---|
765 | else |
---|
766 | gc->conversations = c = g_new0( struct conversation, 1); |
---|
767 | |
---|
768 | c->id = id; |
---|
769 | c->gc = gc; |
---|
770 | c->title = g_strdup( handle ); |
---|
771 | |
---|
772 | s = g_new( char, 16 ); |
---|
773 | sprintf( s, "&chat_%03d", gc->irc->c_id++ ); |
---|
774 | c->channel = g_strdup( s ); |
---|
775 | g_free( s ); |
---|
776 | |
---|
777 | if( set_getint( gc->irc, "debug" ) ) |
---|
778 | serv_got_crap( gc, "Creating new conversation: (id=%d,handle=%s)", id, handle ); |
---|
779 | |
---|
780 | return( c ); |
---|
781 | } |
---|
782 | |
---|
783 | void serv_finish_login( struct gaim_connection *gc ) |
---|
784 | { |
---|
785 | return; |
---|
786 | } |
---|
787 | |
---|
788 | |
---|
789 | /* buddy_chat.c */ |
---|
790 | |
---|
791 | void add_chat_buddy( struct conversation *b, char *handle ) |
---|
792 | { |
---|
793 | user_t *u = user_findhandle( b->gc, handle ); |
---|
794 | int me = 0; |
---|
795 | |
---|
796 | if( set_getint( b->gc->irc, "debug" ) ) |
---|
797 | serv_got_crap( b->gc, "User %s added to conversation %d", handle, b->id ); |
---|
798 | |
---|
799 | /* It might be yourself! */ |
---|
800 | if( b->gc->prpl->cmp_buddynames( handle, b->gc->user->username ) == 0 ) |
---|
801 | { |
---|
802 | u = user_find( b->gc->irc, b->gc->irc->nick ); |
---|
803 | if( !b->joined ) |
---|
804 | irc_join( b->gc->irc, u, b->channel ); |
---|
805 | b->joined = me = 1; |
---|
806 | } |
---|
807 | |
---|
808 | /* Most protocols allow people to join, even when they're not in |
---|
809 | your contact list. Try to handle that here */ |
---|
810 | if( !u ) |
---|
811 | { |
---|
812 | add_buddy( b->gc, NULL, handle, NULL ); |
---|
813 | u = user_findhandle( b->gc, handle ); |
---|
814 | } |
---|
815 | |
---|
816 | /* Add the handle to the room userlist, if it's not 'me' */ |
---|
817 | if( !me ) |
---|
818 | { |
---|
819 | if( b->joined ) |
---|
820 | irc_join( b->gc->irc, u, b->channel ); |
---|
821 | b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); |
---|
822 | } |
---|
823 | } |
---|
824 | |
---|
825 | void remove_chat_buddy( struct conversation *b, char *handle, char *reason ) |
---|
826 | { |
---|
827 | user_t *u; |
---|
828 | int me = 0; |
---|
829 | |
---|
830 | if( set_getint( b->gc->irc, "debug" ) ) |
---|
831 | serv_got_crap( b->gc, "User %s removed from conversation %d (%s)", handle, b->id, reason ? reason : "" ); |
---|
832 | |
---|
833 | /* It might be yourself! */ |
---|
834 | if( g_strcasecmp( handle, b->gc->user->username ) == 0 ) |
---|
835 | { |
---|
836 | u = user_find( b->gc->irc, b->gc->irc->nick ); |
---|
837 | b->joined = 0; |
---|
838 | me = 1; |
---|
839 | } |
---|
840 | else |
---|
841 | { |
---|
842 | u = user_findhandle( b->gc, handle ); |
---|
843 | } |
---|
844 | |
---|
845 | if( remove_chat_buddy_silent( b, handle ) ) |
---|
846 | if( ( b->joined || me ) && u ) |
---|
847 | irc_part( b->gc->irc, u, b->channel ); |
---|
848 | } |
---|
849 | |
---|
850 | static int remove_chat_buddy_silent( struct conversation *b, char *handle ) |
---|
851 | { |
---|
852 | GList *i; |
---|
853 | |
---|
854 | /* Find the handle in the room userlist and shoot it */ |
---|
855 | i = b->in_room; |
---|
856 | while( i ) |
---|
857 | { |
---|
858 | if( g_strcasecmp( handle, i->data ) == 0 ) |
---|
859 | { |
---|
860 | g_free( i->data ); |
---|
861 | b->in_room = g_list_remove( b->in_room, i->data ); |
---|
862 | return( 1 ); |
---|
863 | } |
---|
864 | |
---|
865 | i = i->next; |
---|
866 | } |
---|
867 | |
---|
868 | return( 0 ); |
---|
869 | } |
---|
870 | |
---|
871 | |
---|
872 | /* prefs.c */ |
---|
873 | |
---|
874 | /* Necessary? */ |
---|
875 | void build_block_list() |
---|
876 | { |
---|
877 | return; |
---|
878 | } |
---|
879 | |
---|
880 | void build_allow_list() |
---|
881 | { |
---|
882 | return; |
---|
883 | } |
---|
884 | |
---|
885 | |
---|
886 | /* Misc. BitlBee stuff which shouldn't really be here */ |
---|
887 | |
---|
888 | struct conversation *conv_findchannel( char *channel ) |
---|
889 | { |
---|
890 | struct gaim_connection *gc; |
---|
891 | struct conversation *c; |
---|
892 | GSList *l; |
---|
893 | |
---|
894 | /* This finds the connection which has a conversation which belongs to this channel */ |
---|
895 | for( l = connections; l; l = l->next ) |
---|
896 | { |
---|
897 | gc = l->data; |
---|
898 | for( c = gc->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next ); |
---|
899 | if( c ) |
---|
900 | return( c ); |
---|
901 | } |
---|
902 | |
---|
903 | return( NULL ); |
---|
904 | } |
---|
905 | |
---|
906 | char *set_eval_away_devoice( irc_t *irc, set_t *set, char *value ) |
---|
907 | { |
---|
908 | int st; |
---|
909 | |
---|
910 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
911 | st = 1; |
---|
912 | else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
913 | st = 0; |
---|
914 | else if( sscanf( value, "%d", &st ) != 1 ) |
---|
915 | return( NULL ); |
---|
916 | |
---|
917 | st = st != 0; |
---|
918 | |
---|
919 | /* Horror.... */ |
---|
920 | |
---|
921 | if( st != set_getint( irc, "away_devoice" ) ) |
---|
922 | { |
---|
923 | char list[80] = ""; |
---|
924 | user_t *u = irc->users; |
---|
925 | int i = 0, count = 0; |
---|
926 | char pm; |
---|
927 | char v[80]; |
---|
928 | |
---|
929 | if( st ) |
---|
930 | pm = '+'; |
---|
931 | else |
---|
932 | pm = '-'; |
---|
933 | |
---|
934 | while( u ) |
---|
935 | { |
---|
936 | if( u->gc && u->online && !u->away ) |
---|
937 | { |
---|
938 | if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) |
---|
939 | { |
---|
940 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
941 | irc_write( irc, ":%s!%s@%s MODE %s %c%s%s", |
---|
942 | irc->mynick, irc->mynick, irc->myhost, |
---|
943 | irc->channel, pm, v, list ); |
---|
944 | |
---|
945 | *list = 0; |
---|
946 | count = 0; |
---|
947 | } |
---|
948 | |
---|
949 | sprintf( list + strlen( list ), " %s", u->nick ); |
---|
950 | count ++; |
---|
951 | } |
---|
952 | u = u->next; |
---|
953 | } |
---|
954 | |
---|
955 | /* $v = 'v' x $i */ |
---|
956 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
957 | irc_write( irc, ":%s!%s@%s MODE %s %c%s%s", irc->mynick, irc->mynick, irc->myhost, |
---|
958 | irc->channel, pm, v, list ); |
---|
959 | } |
---|
960 | |
---|
961 | return( set_eval_bool( irc, set, value ) ); |
---|
962 | } |
---|
963 | |
---|
964 | int serv_send_im( irc_t *irc, user_t *u, char *msg, int flags ) |
---|
965 | { |
---|
966 | char buf[8192]; |
---|
967 | |
---|
968 | if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 && |
---|
969 | do_iconv( set_getstr( irc, "charset" ), "UTF-8", msg, buf, 0, 8192 ) != -1 ) |
---|
970 | msg = buf; |
---|
971 | |
---|
972 | if( ( u->gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
973 | { |
---|
974 | char *html; |
---|
975 | |
---|
976 | html = escape_html( msg ); |
---|
977 | strncpy( buf, html, 8192 ); |
---|
978 | g_free( html ); |
---|
979 | |
---|
980 | msg = buf; |
---|
981 | } |
---|
982 | |
---|
983 | return( ((struct gaim_connection *)u->gc)->prpl->send_im( u->gc, u->handle, msg, strlen( msg ), flags ) ); |
---|
984 | } |
---|
985 | |
---|
986 | int serv_send_chat( irc_t *irc, struct gaim_connection *gc, int id, char *msg ) |
---|
987 | { |
---|
988 | char buf[8192]; |
---|
989 | |
---|
990 | if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 && |
---|
991 | do_iconv( set_getstr( irc, "charset" ), "UTF-8", msg, buf, 0, 8192 ) != -1 ) |
---|
992 | msg = buf; |
---|
993 | |
---|
994 | if( gc->flags & OPT_CONN_HTML) { |
---|
995 | char * html = escape_html(msg); |
---|
996 | strncpy(buf, html, 8192); |
---|
997 | g_free(html); |
---|
998 | } |
---|
999 | |
---|
1000 | return( gc->prpl->chat_send( gc, id, msg ) ); |
---|
1001 | } |
---|
1002 | |
---|
1003 | /* Convert from one charset to another. |
---|
1004 | |
---|
1005 | from_cs, to_cs: Source and destination charsets |
---|
1006 | src, dst: Source and destination strings |
---|
1007 | size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though. |
---|
1008 | maxbuf: Maximum number of bytes to write to dst |
---|
1009 | |
---|
1010 | Returns the number of bytes written to maxbuf or -1 on an error. |
---|
1011 | */ |
---|
1012 | signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf ) |
---|
1013 | { |
---|
1014 | iconv_t cd; |
---|
1015 | size_t res; |
---|
1016 | size_t inbytesleft, outbytesleft; |
---|
1017 | char *inbuf = src; |
---|
1018 | char *outbuf = dst; |
---|
1019 | |
---|
1020 | cd = iconv_open( to_cs, from_cs ); |
---|
1021 | if( cd == (iconv_t) -1 ) |
---|
1022 | return( -1 ); |
---|
1023 | |
---|
1024 | inbytesleft = size ? size : strlen( src ); |
---|
1025 | outbytesleft = maxbuf - 1; |
---|
1026 | res = iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ); |
---|
1027 | *outbuf = '\0'; |
---|
1028 | iconv_close( cd ); |
---|
1029 | |
---|
1030 | if( res == (size_t) -1 ) |
---|
1031 | return( -1 ); |
---|
1032 | else |
---|
1033 | return( outbuf - dst ); |
---|
1034 | } |
---|
1035 | |
---|
1036 | char *set_eval_charset( irc_t *irc, set_t *set, char *value ) |
---|
1037 | { |
---|
1038 | iconv_t cd; |
---|
1039 | |
---|
1040 | if ( g_strncasecmp( value, "none", 4 ) == 0 ) |
---|
1041 | return( value ); |
---|
1042 | |
---|
1043 | cd = iconv_open( "UTF-8", value ); |
---|
1044 | if( cd == (iconv_t) -1 ) |
---|
1045 | return( NULL ); |
---|
1046 | |
---|
1047 | iconv_close( cd ); |
---|
1048 | return( value ); |
---|
1049 | } |
---|