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], acc_id[33]; |
---|
237 | char *msg; |
---|
238 | account_t *a; |
---|
239 | |
---|
240 | va_start( params, format ); |
---|
241 | g_vsnprintf( text, sizeof( text ), format, params ); |
---|
242 | va_end( params ); |
---|
243 | |
---|
244 | if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 && |
---|
245 | do_iconv( "UTF8", set_getstr( gc->irc, "charset" ), text, buf, 0, 1024 ) != -1 ) |
---|
246 | msg = buf; |
---|
247 | else |
---|
248 | msg = text; |
---|
249 | |
---|
250 | if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) || |
---|
251 | ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) ) |
---|
252 | strip_html( msg ); |
---|
253 | |
---|
254 | /* Try to find a different connection on the same protocol. */ |
---|
255 | for( a = gc->irc->accounts; a; a = a->next ) |
---|
256 | if( proto_prpl[a->protocol] == gc->prpl && a->gc != gc ) |
---|
257 | break; |
---|
258 | |
---|
259 | /* If we found one, add the screenname to the acc_id. */ |
---|
260 | if( a ) |
---|
261 | g_snprintf( acc_id, 32, "%s(%s)", proto_name[gc->protocol], gc->username ); |
---|
262 | else |
---|
263 | g_snprintf( acc_id, 32, "%s", proto_name[gc->protocol] ); |
---|
264 | |
---|
265 | irc_usermsg( gc->irc, "%s - %s", acc_id, msg ); |
---|
266 | } |
---|
267 | |
---|
268 | static gboolean send_keepalive( gpointer d ) |
---|
269 | { |
---|
270 | struct gaim_connection *gc = d; |
---|
271 | |
---|
272 | if( gc->prpl && gc->prpl->keepalive ) |
---|
273 | gc->prpl->keepalive( gc ); |
---|
274 | |
---|
275 | return TRUE; |
---|
276 | } |
---|
277 | |
---|
278 | void account_online( struct gaim_connection *gc ) |
---|
279 | { |
---|
280 | user_t *u; |
---|
281 | |
---|
282 | /* MSN servers sometimes redirect you to a different server and do |
---|
283 | the whole login sequence again, so subsequent calls to this |
---|
284 | function should be handled correctly. (IOW, ignored) */ |
---|
285 | if( gc->flags & OPT_LOGGED_IN ) |
---|
286 | return; |
---|
287 | |
---|
288 | u = user_find( gc->irc, gc->irc->nick ); |
---|
289 | |
---|
290 | serv_got_crap( gc, "Logged in" ); |
---|
291 | |
---|
292 | gc->keepalive = g_timeout_add( 60000, send_keepalive, gc ); |
---|
293 | gc->flags |= OPT_LOGGED_IN; |
---|
294 | |
---|
295 | if( u && u->away ) proto_away( gc, u->away ); |
---|
296 | |
---|
297 | if( gc->protocol == PROTO_ICQ ) |
---|
298 | { |
---|
299 | for( u = gc->irc->users; u; u = u->next ) |
---|
300 | if( u->gc == gc ) |
---|
301 | break; |
---|
302 | |
---|
303 | if( u == NULL ) |
---|
304 | serv_got_crap( gc, "\x02""***\x02"" BitlBee now supports ICQ server-side contact lists. " |
---|
305 | "See \x02""help import_buddies\x02"" for more information." ); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | gboolean auto_reconnect( gpointer data ) |
---|
310 | { |
---|
311 | account_t *a = data; |
---|
312 | |
---|
313 | a->reconnect = 0; |
---|
314 | account_on( a->irc, a ); |
---|
315 | |
---|
316 | return( FALSE ); /* Only have to run the timeout once */ |
---|
317 | } |
---|
318 | |
---|
319 | void cancel_auto_reconnect( account_t *a ) |
---|
320 | { |
---|
321 | while( g_source_remove_by_user_data( (gpointer) a ) ); |
---|
322 | a->reconnect = 0; |
---|
323 | } |
---|
324 | |
---|
325 | void account_offline( struct gaim_connection *gc ) |
---|
326 | { |
---|
327 | gc->wants_to_die = TRUE; |
---|
328 | signoff( gc ); |
---|
329 | } |
---|
330 | |
---|
331 | void signoff( struct gaim_connection *gc ) |
---|
332 | { |
---|
333 | irc_t *irc = gc->irc; |
---|
334 | user_t *t, *u = irc->users; |
---|
335 | account_t *a; |
---|
336 | |
---|
337 | serv_got_crap( gc, "Signing off.." ); |
---|
338 | |
---|
339 | gaim_input_remove( gc->keepalive ); |
---|
340 | gc->keepalive = 0; |
---|
341 | gc->prpl->close( gc ); |
---|
342 | gaim_input_remove( gc->inpa ); |
---|
343 | |
---|
344 | while( u ) |
---|
345 | { |
---|
346 | if( u->gc == gc ) |
---|
347 | { |
---|
348 | t = u->next; |
---|
349 | user_del( irc, u->nick ); |
---|
350 | u = t; |
---|
351 | } |
---|
352 | else |
---|
353 | u = u->next; |
---|
354 | } |
---|
355 | |
---|
356 | query_del_by_gc( gc->irc, gc ); |
---|
357 | |
---|
358 | for( a = irc->accounts; a; a = a->next ) |
---|
359 | if( a->gc == gc ) |
---|
360 | break; |
---|
361 | |
---|
362 | if( !a ) |
---|
363 | { |
---|
364 | /* Uhm... This is very sick. */ |
---|
365 | } |
---|
366 | else if( !gc->wants_to_die && set_getint( irc, "auto_reconnect" ) ) |
---|
367 | { |
---|
368 | int delay = set_getint( irc, "auto_reconnect_delay" ); |
---|
369 | serv_got_crap( gc, "Reconnecting in %d seconds..", delay ); |
---|
370 | |
---|
371 | a->reconnect = 1; |
---|
372 | g_timeout_add( delay * 1000, auto_reconnect, a ); |
---|
373 | } |
---|
374 | |
---|
375 | destroy_gaim_conn( gc ); |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | /* dialogs.c */ |
---|
380 | |
---|
381 | void do_error_dialog( struct gaim_connection *gc, char *msg, char *title ) |
---|
382 | { |
---|
383 | if( msg && title ) |
---|
384 | serv_got_crap( gc, "Error: %s: %s", title, msg ); |
---|
385 | else if( msg ) |
---|
386 | serv_got_crap( gc, "Error: %s", msg ); |
---|
387 | else if( title ) |
---|
388 | serv_got_crap( gc, "Error: %s", title ); |
---|
389 | else |
---|
390 | serv_got_crap( gc, "Error" ); |
---|
391 | } |
---|
392 | |
---|
393 | void do_ask_dialog( struct gaim_connection *gc, char *msg, void *data, void *doit, void *dont ) |
---|
394 | { |
---|
395 | query_add( gc->irc, gc, msg, doit, dont, data ); |
---|
396 | } |
---|
397 | |
---|
398 | |
---|
399 | /* list.c */ |
---|
400 | |
---|
401 | int bud_list_cache_exists( struct gaim_connection *gc ) |
---|
402 | { |
---|
403 | return( 0 ); |
---|
404 | } |
---|
405 | |
---|
406 | void do_import( struct gaim_connection *gc, void *null ) |
---|
407 | { |
---|
408 | return; |
---|
409 | } |
---|
410 | |
---|
411 | void add_buddy( struct gaim_connection *gc, char *group, char *handle, char *realname ) |
---|
412 | { |
---|
413 | user_t *u; |
---|
414 | char nick[MAX_NICK_LENGTH+1]; |
---|
415 | char *s; |
---|
416 | irc_t *irc = gc->irc; |
---|
417 | |
---|
418 | if( set_getint( irc, "debug" ) && 0 ) /* This message is too useless */ |
---|
419 | serv_got_crap( gc, "Receiving user add from handle: %s", handle ); |
---|
420 | |
---|
421 | if( user_findhandle( gc, handle ) ) |
---|
422 | { |
---|
423 | if( set_getint( irc, "debug" ) ) |
---|
424 | serv_got_crap( gc, "User already exists, ignoring add request: %s", handle ); |
---|
425 | |
---|
426 | return; |
---|
427 | |
---|
428 | /* Buddy seems to exist already. Let's ignore this request then... */ |
---|
429 | } |
---|
430 | |
---|
431 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
432 | strcpy( nick, nick_get( gc->irc, handle, gc->protocol, realname ) ); |
---|
433 | |
---|
434 | u = user_add( gc->irc, nick ); |
---|
435 | |
---|
436 | if( !realname || !*realname ) realname = nick; |
---|
437 | u->realname = g_strdup( realname ); |
---|
438 | |
---|
439 | if( ( s = strchr( handle, '@' ) ) ) |
---|
440 | { |
---|
441 | u->host = g_strdup( s + 1 ); |
---|
442 | u->user = g_strndup( handle, s - handle ); |
---|
443 | } |
---|
444 | else if( gc->user->proto_opt[0] && *gc->user->proto_opt[0] ) |
---|
445 | { |
---|
446 | u->host = g_strdup( gc->user->proto_opt[0] ); |
---|
447 | u->user = g_strdup( handle ); |
---|
448 | |
---|
449 | /* s/ /_/ ... important for AOL screennames */ |
---|
450 | for( s = u->user; *s; s ++ ) |
---|
451 | if( *s == ' ' ) |
---|
452 | *s = '_'; |
---|
453 | } |
---|
454 | else |
---|
455 | { |
---|
456 | u->host = g_strdup( proto_name[gc->user->protocol] ); |
---|
457 | u->user = g_strdup( handle ); |
---|
458 | } |
---|
459 | |
---|
460 | u->gc = gc; |
---|
461 | u->handle = g_strdup( handle ); |
---|
462 | u->send_handler = buddy_send_handler; |
---|
463 | u->last_typing_notice = 0; |
---|
464 | } |
---|
465 | |
---|
466 | struct buddy *find_buddy( struct gaim_connection *gc, char *handle ) |
---|
467 | { |
---|
468 | static struct buddy b[1]; |
---|
469 | user_t *u; |
---|
470 | |
---|
471 | u = user_findhandle( gc, handle ); |
---|
472 | |
---|
473 | if( !u ) |
---|
474 | return( NULL ); |
---|
475 | |
---|
476 | memset( b, 0, sizeof( b ) ); |
---|
477 | strncpy( b->name, handle, 80 ); |
---|
478 | strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); |
---|
479 | b->present = u->online; |
---|
480 | b->gc = u->gc; |
---|
481 | |
---|
482 | return( b ); |
---|
483 | } |
---|
484 | |
---|
485 | void do_export( struct gaim_connection *gc ) |
---|
486 | { |
---|
487 | return; |
---|
488 | } |
---|
489 | |
---|
490 | void signoff_blocked( struct gaim_connection *gc ) |
---|
491 | { |
---|
492 | return; /* Make all blocked users look invisible (TODO?) */ |
---|
493 | } |
---|
494 | |
---|
495 | |
---|
496 | void serv_buddy_rename( struct gaim_connection *gc, char *handle, char *realname ) |
---|
497 | { |
---|
498 | user_t *u = user_findhandle( gc, handle ); |
---|
499 | char *name, buf[1024]; |
---|
500 | |
---|
501 | if( !u ) return; |
---|
502 | |
---|
503 | /* Convert all UTF-8 */ |
---|
504 | if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 && |
---|
505 | do_iconv( "UTF-8", set_getstr( gc->irc, "charset" ), realname, buf, 0, sizeof( buf ) ) != -1 ) |
---|
506 | name = buf; |
---|
507 | else |
---|
508 | name = realname; |
---|
509 | |
---|
510 | if( g_strcasecmp( u->realname, name ) != 0 ) |
---|
511 | { |
---|
512 | if( u->realname != u->nick ) g_free( u->realname ); |
---|
513 | |
---|
514 | u->realname = g_strdup( name ); |
---|
515 | |
---|
516 | if( ( gc->flags & OPT_LOGGED_IN ) && set_getint( gc->irc, "display_namechanges" ) ) |
---|
517 | serv_got_crap( gc, "User `%s' changed name to `%s'", u->nick, u->realname ); |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | |
---|
522 | /* prpl.c */ |
---|
523 | |
---|
524 | void show_got_added( struct gaim_connection *gc, char *id, char *handle, const char *realname, const char *msg ) |
---|
525 | { |
---|
526 | return; |
---|
527 | } |
---|
528 | |
---|
529 | |
---|
530 | /* server.c */ |
---|
531 | |
---|
532 | void serv_got_update( struct gaim_connection *gc, char *handle, int loggedin, int evil, time_t signon, time_t idle, int type, guint caps ) |
---|
533 | { |
---|
534 | user_t *u; |
---|
535 | int oa, oo; |
---|
536 | |
---|
537 | u = user_findhandle( gc, handle ); |
---|
538 | |
---|
539 | if( !u ) |
---|
540 | { |
---|
541 | if( g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "add" ) == 0 ) |
---|
542 | { |
---|
543 | add_buddy( gc, NULL, handle, NULL ); |
---|
544 | u = user_findhandle( gc, handle ); |
---|
545 | } |
---|
546 | else |
---|
547 | { |
---|
548 | if( set_getint( gc->irc, "debug" ) || g_strcasecmp( set_getstr( gc->irc, "handle_unknown" ), "ignore" ) != 0 ) |
---|
549 | { |
---|
550 | serv_got_crap( gc, "serv_got_update() for handle %s:", handle ); |
---|
551 | serv_got_crap( gc, "loggedin = %d, type = %d", loggedin, type ); |
---|
552 | } |
---|
553 | |
---|
554 | return; |
---|
555 | } |
---|
556 | return; |
---|
557 | } |
---|
558 | |
---|
559 | oa = u->away != NULL; |
---|
560 | oo = u->online; |
---|
561 | |
---|
562 | if( u->away ) |
---|
563 | { |
---|
564 | g_free( u->away ); |
---|
565 | u->away = NULL; |
---|
566 | } |
---|
567 | |
---|
568 | if( loggedin && !u->online ) |
---|
569 | { |
---|
570 | irc_spawn( gc->irc, u ); |
---|
571 | u->online = 1; |
---|
572 | } |
---|
573 | else if( !loggedin && u->online ) |
---|
574 | { |
---|
575 | struct conversation *c; |
---|
576 | |
---|
577 | irc_kill( gc->irc, u ); |
---|
578 | u->online = 0; |
---|
579 | |
---|
580 | /* Remove him/her from the conversations to prevent PART messages after he/she QUIT already */ |
---|
581 | for( c = gc->conversations; c; c = c->next ) |
---|
582 | remove_chat_buddy_silent( c, handle ); |
---|
583 | } |
---|
584 | |
---|
585 | if( ( type & UC_UNAVAILABLE ) && ( gc->protocol == PROTO_OSCAR || gc->protocol == PROTO_TOC ) ) |
---|
586 | { |
---|
587 | u->away = g_strdup( "Away" ); |
---|
588 | } |
---|
589 | else if( ( type & UC_UNAVAILABLE ) && ( gc->protocol == PROTO_JABBER ) ) |
---|
590 | { |
---|
591 | if( type & UC_DND ) |
---|
592 | u->away = g_strdup( "Do Not Disturb" ); |
---|
593 | else if( type & UC_XA ) |
---|
594 | u->away = g_strdup( "Extended Away" ); |
---|
595 | else // if( type & UC_AWAY ) |
---|
596 | u->away = g_strdup( "Away" ); |
---|
597 | } |
---|
598 | else if( ( type & UC_UNAVAILABLE ) && gc->prpl->get_status_string ) |
---|
599 | { |
---|
600 | u->away = g_strdup( gc->prpl->get_status_string( gc, type ) ); |
---|
601 | } |
---|
602 | else |
---|
603 | u->away = NULL; |
---|
604 | |
---|
605 | /* LISPy... */ |
---|
606 | if( ( set_getint( gc->irc, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ |
---|
607 | ( u->online ) && /* Don't touch offline people */ |
---|
608 | ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ |
---|
609 | ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ |
---|
610 | { |
---|
611 | irc_write( gc->irc, ":%s!%s@%s MODE %s %cv %s", gc->irc->mynick, gc->irc->mynick, gc->irc->myhost, |
---|
612 | gc->irc->channel, u->away?'-':'+', u->nick ); |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | void serv_got_im( struct gaim_connection *gc, char *handle, char *msg, guint32 flags, time_t mtime, gint len ) |
---|
617 | { |
---|
618 | irc_t *irc = gc->irc; |
---|
619 | user_t *u; |
---|
620 | char buf[8192]; |
---|
621 | |
---|
622 | u = user_findhandle( gc, handle ); |
---|
623 | |
---|
624 | if( !u ) |
---|
625 | { |
---|
626 | char *h = set_getstr( irc, "handle_unknown" ); |
---|
627 | |
---|
628 | if( g_strcasecmp( h, "ignore" ) == 0 ) |
---|
629 | { |
---|
630 | if( set_getint( irc, "debug" ) ) |
---|
631 | serv_got_crap( gc, "Ignoring message from unknown handle %s", handle ); |
---|
632 | |
---|
633 | return; |
---|
634 | } |
---|
635 | else if( g_strncasecmp( h, "add", 3 ) == 0 ) |
---|
636 | { |
---|
637 | int private = set_getint( irc, "private" ); |
---|
638 | |
---|
639 | if( h[3] ) |
---|
640 | { |
---|
641 | if( g_strcasecmp( h + 3, "_private" ) == 0 ) |
---|
642 | private = 1; |
---|
643 | else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) |
---|
644 | private = 0; |
---|
645 | } |
---|
646 | |
---|
647 | add_buddy( gc, NULL, handle, NULL ); |
---|
648 | u = user_findhandle( gc, handle ); |
---|
649 | u->is_private = private; |
---|
650 | } |
---|
651 | else |
---|
652 | { |
---|
653 | serv_got_crap( gc, "Message from unknown handle %s:", handle ); |
---|
654 | u = user_find( irc, irc->mynick ); |
---|
655 | } |
---|
656 | } |
---|
657 | |
---|
658 | if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) || |
---|
659 | ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) ) |
---|
660 | strip_html( msg ); |
---|
661 | |
---|
662 | if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 && |
---|
663 | do_iconv( "UTF-8", set_getstr( irc, "charset" ), msg, buf, 0, 8192 ) != -1 ) |
---|
664 | msg = buf; |
---|
665 | |
---|
666 | while( strlen( msg ) > 425 ) |
---|
667 | { |
---|
668 | char tmp, *nl; |
---|
669 | |
---|
670 | tmp = msg[425]; |
---|
671 | msg[425] = 0; |
---|
672 | |
---|
673 | /* If there's a newline/space in this string, split up there, |
---|
674 | looks a bit prettier. */ |
---|
675 | if( ( nl = strrchr( msg, '\n' ) ) || ( nl = strrchr( msg, ' ' ) ) ) |
---|
676 | { |
---|
677 | msg[425] = tmp; |
---|
678 | tmp = *nl; |
---|
679 | *nl = 0; |
---|
680 | } |
---|
681 | |
---|
682 | irc_msgfrom( irc, u->nick, msg ); |
---|
683 | |
---|
684 | /* Move on. */ |
---|
685 | if( nl ) |
---|
686 | { |
---|
687 | *nl = tmp; |
---|
688 | msg = nl + 1; |
---|
689 | } |
---|
690 | else |
---|
691 | { |
---|
692 | msg[425] = tmp; |
---|
693 | msg += 425; |
---|
694 | } |
---|
695 | } |
---|
696 | irc_msgfrom( irc, u->nick, msg ); |
---|
697 | } |
---|
698 | |
---|
699 | void serv_got_typing( struct gaim_connection *gc, char *handle, int timeout ) |
---|
700 | { |
---|
701 | user_t *u; |
---|
702 | |
---|
703 | if( !set_getint( gc->irc, "typing_notice" ) ) |
---|
704 | return; |
---|
705 | |
---|
706 | if( ( u = user_findhandle( gc, handle ) ) ) |
---|
707 | irc_privmsg( gc->irc, u, "PRIVMSG", gc->irc->nick, NULL, "\1TYPING 1\1" ); |
---|
708 | } |
---|
709 | |
---|
710 | void serv_got_chat_left( struct gaim_connection *gc, int id ) |
---|
711 | { |
---|
712 | struct conversation *c, *l = NULL; |
---|
713 | GList *ir; |
---|
714 | |
---|
715 | if( set_getint( gc->irc, "debug" ) ) |
---|
716 | serv_got_crap( gc, "You were removed from conversation %d", (int) id ); |
---|
717 | |
---|
718 | for( c = gc->conversations; c && c->id != id; c = (l=c)->next ); |
---|
719 | |
---|
720 | if( c ) |
---|
721 | { |
---|
722 | if( c->joined ) |
---|
723 | { |
---|
724 | user_t *u, *r; |
---|
725 | |
---|
726 | r = user_find( gc->irc, gc->irc->mynick ); |
---|
727 | irc_privmsg( gc->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); |
---|
728 | |
---|
729 | u = user_find( gc->irc, gc->irc->nick ); |
---|
730 | irc_kick( gc->irc, u, c->channel, r ); |
---|
731 | /* irc_part( gc->irc, u, c->channel ); */ |
---|
732 | } |
---|
733 | |
---|
734 | if( l ) |
---|
735 | l->next = c->next; |
---|
736 | else |
---|
737 | gc->conversations = c->next; |
---|
738 | |
---|
739 | for( ir = c->in_room; ir; ir = ir->next ) |
---|
740 | g_free( ir->data ); |
---|
741 | g_list_free( c->in_room ); |
---|
742 | g_free( c->channel ); |
---|
743 | g_free( c->title ); |
---|
744 | g_free( c ); |
---|
745 | } |
---|
746 | } |
---|
747 | |
---|
748 | void serv_got_chat_in( struct gaim_connection *gc, int id, char *who, int whisper, char *msg, time_t mtime ) |
---|
749 | { |
---|
750 | struct conversation *c; |
---|
751 | user_t *u; |
---|
752 | char buf[8192]; |
---|
753 | |
---|
754 | /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ |
---|
755 | if( g_strcasecmp( who, gc->user->username ) == 0 ) |
---|
756 | return; |
---|
757 | |
---|
758 | u = user_findhandle( gc, who ); |
---|
759 | for( c = gc->conversations; c && c->id != id; c = c->next ); |
---|
760 | |
---|
761 | if( ( g_strcasecmp( set_getstr( gc->irc, "strip_html" ), "always" ) == 0 ) || |
---|
762 | ( ( gc->flags & OPT_CONN_HTML ) && set_getint( gc->irc, "strip_html" ) ) ) |
---|
763 | strip_html( msg ); |
---|
764 | |
---|
765 | if( g_strncasecmp( set_getstr( gc->irc, "charset" ), "none", 4 ) != 0 && |
---|
766 | do_iconv( "UTF-8", set_getstr( gc->irc, "charset" ), msg, buf, 0, 8192 ) != -1 ) |
---|
767 | msg = buf; |
---|
768 | |
---|
769 | if( c && u ) |
---|
770 | irc_privmsg( gc->irc, u, "PRIVMSG", c->channel, "", msg ); |
---|
771 | else |
---|
772 | serv_got_crap( gc, "Message from/to conversation %s@%d (unknown conv/user): %s", who, id, msg ); |
---|
773 | } |
---|
774 | |
---|
775 | struct conversation *serv_got_joined_chat( struct gaim_connection *gc, int id, char *handle ) |
---|
776 | { |
---|
777 | struct conversation *c; |
---|
778 | char *s; |
---|
779 | |
---|
780 | /* This one just creates the conversation structure, user won't see anything yet */ |
---|
781 | |
---|
782 | if( gc->conversations ) |
---|
783 | { |
---|
784 | for( c = gc->conversations; c->next; c = c->next ); |
---|
785 | c = c->next = g_new0( struct conversation, 1 ); |
---|
786 | } |
---|
787 | else |
---|
788 | gc->conversations = c = g_new0( struct conversation, 1); |
---|
789 | |
---|
790 | c->id = id; |
---|
791 | c->gc = gc; |
---|
792 | c->title = g_strdup( handle ); |
---|
793 | |
---|
794 | s = g_new( char, 16 ); |
---|
795 | sprintf( s, "&chat_%03d", gc->irc->c_id++ ); |
---|
796 | c->channel = g_strdup( s ); |
---|
797 | g_free( s ); |
---|
798 | |
---|
799 | if( set_getint( gc->irc, "debug" ) ) |
---|
800 | serv_got_crap( gc, "Creating new conversation: (id=%d,handle=%s)", id, handle ); |
---|
801 | |
---|
802 | return( c ); |
---|
803 | } |
---|
804 | |
---|
805 | void serv_finish_login( struct gaim_connection *gc ) |
---|
806 | { |
---|
807 | return; |
---|
808 | } |
---|
809 | |
---|
810 | |
---|
811 | /* buddy_chat.c */ |
---|
812 | |
---|
813 | void add_chat_buddy( struct conversation *b, char *handle ) |
---|
814 | { |
---|
815 | user_t *u = user_findhandle( b->gc, handle ); |
---|
816 | int me = 0; |
---|
817 | |
---|
818 | if( set_getint( b->gc->irc, "debug" ) ) |
---|
819 | serv_got_crap( b->gc, "User %s added to conversation %d", handle, b->id ); |
---|
820 | |
---|
821 | /* It might be yourself! */ |
---|
822 | if( b->gc->prpl->cmp_buddynames( handle, b->gc->user->username ) == 0 ) |
---|
823 | { |
---|
824 | u = user_find( b->gc->irc, b->gc->irc->nick ); |
---|
825 | if( !b->joined ) |
---|
826 | irc_join( b->gc->irc, u, b->channel ); |
---|
827 | b->joined = me = 1; |
---|
828 | } |
---|
829 | |
---|
830 | /* Most protocols allow people to join, even when they're not in |
---|
831 | your contact list. Try to handle that here */ |
---|
832 | if( !u ) |
---|
833 | { |
---|
834 | add_buddy( b->gc, NULL, handle, NULL ); |
---|
835 | u = user_findhandle( b->gc, handle ); |
---|
836 | } |
---|
837 | |
---|
838 | /* Add the handle to the room userlist, if it's not 'me' */ |
---|
839 | if( !me ) |
---|
840 | { |
---|
841 | if( b->joined ) |
---|
842 | irc_join( b->gc->irc, u, b->channel ); |
---|
843 | b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); |
---|
844 | } |
---|
845 | } |
---|
846 | |
---|
847 | void remove_chat_buddy( struct conversation *b, char *handle, char *reason ) |
---|
848 | { |
---|
849 | user_t *u; |
---|
850 | int me = 0; |
---|
851 | |
---|
852 | if( set_getint( b->gc->irc, "debug" ) ) |
---|
853 | serv_got_crap( b->gc, "User %s removed from conversation %d (%s)", handle, b->id, reason ? reason : "" ); |
---|
854 | |
---|
855 | /* It might be yourself! */ |
---|
856 | if( g_strcasecmp( handle, b->gc->user->username ) == 0 ) |
---|
857 | { |
---|
858 | u = user_find( b->gc->irc, b->gc->irc->nick ); |
---|
859 | b->joined = 0; |
---|
860 | me = 1; |
---|
861 | } |
---|
862 | else |
---|
863 | { |
---|
864 | u = user_findhandle( b->gc, handle ); |
---|
865 | } |
---|
866 | |
---|
867 | if( remove_chat_buddy_silent( b, handle ) ) |
---|
868 | if( ( b->joined || me ) && u ) |
---|
869 | irc_part( b->gc->irc, u, b->channel ); |
---|
870 | } |
---|
871 | |
---|
872 | static int remove_chat_buddy_silent( struct conversation *b, char *handle ) |
---|
873 | { |
---|
874 | GList *i; |
---|
875 | |
---|
876 | /* Find the handle in the room userlist and shoot it */ |
---|
877 | i = b->in_room; |
---|
878 | while( i ) |
---|
879 | { |
---|
880 | if( g_strcasecmp( handle, i->data ) == 0 ) |
---|
881 | { |
---|
882 | g_free( i->data ); |
---|
883 | b->in_room = g_list_remove( b->in_room, i->data ); |
---|
884 | return( 1 ); |
---|
885 | } |
---|
886 | |
---|
887 | i = i->next; |
---|
888 | } |
---|
889 | |
---|
890 | return( 0 ); |
---|
891 | } |
---|
892 | |
---|
893 | |
---|
894 | /* prefs.c */ |
---|
895 | |
---|
896 | /* Necessary? */ |
---|
897 | void build_block_list() |
---|
898 | { |
---|
899 | return; |
---|
900 | } |
---|
901 | |
---|
902 | void build_allow_list() |
---|
903 | { |
---|
904 | return; |
---|
905 | } |
---|
906 | |
---|
907 | |
---|
908 | /* Misc. BitlBee stuff which shouldn't really be here */ |
---|
909 | |
---|
910 | struct conversation *conv_findchannel( char *channel ) |
---|
911 | { |
---|
912 | struct gaim_connection *gc; |
---|
913 | struct conversation *c; |
---|
914 | GSList *l; |
---|
915 | |
---|
916 | /* This finds the connection which has a conversation which belongs to this channel */ |
---|
917 | for( l = connections; l; l = l->next ) |
---|
918 | { |
---|
919 | gc = l->data; |
---|
920 | for( c = gc->conversations; c && g_strcasecmp( c->channel, channel ) != 0; c = c->next ); |
---|
921 | if( c ) |
---|
922 | return( c ); |
---|
923 | } |
---|
924 | |
---|
925 | return( NULL ); |
---|
926 | } |
---|
927 | |
---|
928 | char *set_eval_away_devoice( irc_t *irc, set_t *set, char *value ) |
---|
929 | { |
---|
930 | int st; |
---|
931 | |
---|
932 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
933 | st = 1; |
---|
934 | else if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
935 | st = 0; |
---|
936 | else if( sscanf( value, "%d", &st ) != 1 ) |
---|
937 | return( NULL ); |
---|
938 | |
---|
939 | st = st != 0; |
---|
940 | |
---|
941 | /* Horror.... */ |
---|
942 | |
---|
943 | if( st != set_getint( irc, "away_devoice" ) ) |
---|
944 | { |
---|
945 | char list[80] = ""; |
---|
946 | user_t *u = irc->users; |
---|
947 | int i = 0, count = 0; |
---|
948 | char pm; |
---|
949 | char v[80]; |
---|
950 | |
---|
951 | if( st ) |
---|
952 | pm = '+'; |
---|
953 | else |
---|
954 | pm = '-'; |
---|
955 | |
---|
956 | while( u ) |
---|
957 | { |
---|
958 | if( u->gc && u->online && !u->away ) |
---|
959 | { |
---|
960 | if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) |
---|
961 | { |
---|
962 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
963 | irc_write( irc, ":%s!%s@%s MODE %s %c%s%s", |
---|
964 | irc->mynick, irc->mynick, irc->myhost, |
---|
965 | irc->channel, pm, v, list ); |
---|
966 | |
---|
967 | *list = 0; |
---|
968 | count = 0; |
---|
969 | } |
---|
970 | |
---|
971 | sprintf( list + strlen( list ), " %s", u->nick ); |
---|
972 | count ++; |
---|
973 | } |
---|
974 | u = u->next; |
---|
975 | } |
---|
976 | |
---|
977 | /* $v = 'v' x $i */ |
---|
978 | for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; |
---|
979 | irc_write( irc, ":%s!%s@%s MODE %s %c%s%s", irc->mynick, irc->mynick, irc->myhost, |
---|
980 | irc->channel, pm, v, list ); |
---|
981 | } |
---|
982 | |
---|
983 | return( set_eval_bool( irc, set, value ) ); |
---|
984 | } |
---|
985 | |
---|
986 | int serv_send_im( irc_t *irc, user_t *u, char *msg, int flags ) |
---|
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( ( u->gc->flags & OPT_CONN_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) |
---|
995 | { |
---|
996 | char *html; |
---|
997 | |
---|
998 | html = escape_html( msg ); |
---|
999 | strncpy( buf, html, 8192 ); |
---|
1000 | g_free( html ); |
---|
1001 | |
---|
1002 | msg = buf; |
---|
1003 | } |
---|
1004 | |
---|
1005 | return( ((struct gaim_connection *)u->gc)->prpl->send_im( u->gc, u->handle, msg, strlen( msg ), flags ) ); |
---|
1006 | } |
---|
1007 | |
---|
1008 | int serv_send_chat( irc_t *irc, struct gaim_connection *gc, int id, char *msg ) |
---|
1009 | { |
---|
1010 | char buf[8192]; |
---|
1011 | |
---|
1012 | if( g_strncasecmp( set_getstr( irc, "charset" ), "none", 4 ) != 0 && |
---|
1013 | do_iconv( set_getstr( irc, "charset" ), "UTF-8", msg, buf, 0, 8192 ) != -1 ) |
---|
1014 | msg = buf; |
---|
1015 | |
---|
1016 | if( gc->flags & OPT_CONN_HTML) { |
---|
1017 | char * html = escape_html(msg); |
---|
1018 | strncpy(buf, html, 8192); |
---|
1019 | g_free(html); |
---|
1020 | } |
---|
1021 | |
---|
1022 | return( gc->prpl->chat_send( gc, id, msg ) ); |
---|
1023 | } |
---|
1024 | |
---|
1025 | /* Convert from one charset to another. |
---|
1026 | |
---|
1027 | from_cs, to_cs: Source and destination charsets |
---|
1028 | src, dst: Source and destination strings |
---|
1029 | size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though. |
---|
1030 | maxbuf: Maximum number of bytes to write to dst |
---|
1031 | |
---|
1032 | Returns the number of bytes written to maxbuf or -1 on an error. |
---|
1033 | */ |
---|
1034 | signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf ) |
---|
1035 | { |
---|
1036 | iconv_t cd; |
---|
1037 | size_t res; |
---|
1038 | size_t inbytesleft, outbytesleft; |
---|
1039 | char *inbuf = src; |
---|
1040 | char *outbuf = dst; |
---|
1041 | |
---|
1042 | cd = iconv_open( to_cs, from_cs ); |
---|
1043 | if( cd == (iconv_t) -1 ) |
---|
1044 | return( -1 ); |
---|
1045 | |
---|
1046 | inbytesleft = size ? size : strlen( src ); |
---|
1047 | outbytesleft = maxbuf - 1; |
---|
1048 | res = iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ); |
---|
1049 | *outbuf = '\0'; |
---|
1050 | iconv_close( cd ); |
---|
1051 | |
---|
1052 | if( res == (size_t) -1 ) |
---|
1053 | return( -1 ); |
---|
1054 | else |
---|
1055 | return( outbuf - dst ); |
---|
1056 | } |
---|
1057 | |
---|
1058 | char *set_eval_charset( irc_t *irc, set_t *set, char *value ) |
---|
1059 | { |
---|
1060 | iconv_t cd; |
---|
1061 | |
---|
1062 | if ( g_strncasecmp( value, "none", 4 ) == 0 ) |
---|
1063 | return( value ); |
---|
1064 | |
---|
1065 | cd = iconv_open( "UTF-8", value ); |
---|
1066 | if( cd == (iconv_t) -1 ) |
---|
1067 | return( NULL ); |
---|
1068 | |
---|
1069 | iconv_close( cd ); |
---|
1070 | return( value ); |
---|
1071 | } |
---|