1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2010 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Some glue to put the IRC and the IM stuff together. */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "bitlbee.h" |
---|
27 | #include "dcc.h" |
---|
28 | |
---|
29 | /* IM->IRC callbacks: Simple IM/buddy-related stuff. */ |
---|
30 | |
---|
31 | static const struct irc_user_funcs irc_user_im_funcs; |
---|
32 | |
---|
33 | static gboolean bee_irc_user_new( bee_t *bee, bee_user_t *bu ) |
---|
34 | { |
---|
35 | irc_user_t *iu; |
---|
36 | char nick[MAX_NICK_LENGTH+1], *s; |
---|
37 | |
---|
38 | memset( nick, 0, MAX_NICK_LENGTH + 1 ); |
---|
39 | strcpy( nick, nick_get( bu->ic->acc, bu->handle ) ); |
---|
40 | |
---|
41 | bu->ui_data = iu = irc_user_new( (irc_t*) bee->ui_data, nick ); |
---|
42 | iu->bu = bu; |
---|
43 | |
---|
44 | if( ( s = strchr( bu->handle, '@' ) ) ) |
---|
45 | { |
---|
46 | iu->host = g_strdup( s + 1 ); |
---|
47 | iu->user = g_strndup( bu->handle, s - bu->handle ); |
---|
48 | } |
---|
49 | else if( bu->ic->acc->server ) |
---|
50 | { |
---|
51 | iu->host = g_strdup( bu->ic->acc->server ); |
---|
52 | iu->user = g_strdup( bu->handle ); |
---|
53 | |
---|
54 | /* s/ /_/ ... important for AOL screennames */ |
---|
55 | for( s = iu->user; *s; s ++ ) |
---|
56 | if( *s == ' ' ) |
---|
57 | *s = '_'; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | iu->host = g_strdup( bu->ic->acc->prpl->name ); |
---|
62 | iu->user = g_strdup( bu->handle ); |
---|
63 | } |
---|
64 | |
---|
65 | if( set_getbool( &bee->set, "private" ) ) |
---|
66 | iu->flags |= IRC_USER_PRIVATE; |
---|
67 | |
---|
68 | iu->f = &irc_user_im_funcs; |
---|
69 | //iu->last_typing_notice = 0; |
---|
70 | |
---|
71 | return TRUE; |
---|
72 | } |
---|
73 | |
---|
74 | static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu ) |
---|
75 | { |
---|
76 | return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data ); |
---|
77 | } |
---|
78 | |
---|
79 | static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old ) |
---|
80 | { |
---|
81 | irc_t *irc = bee->ui_data; |
---|
82 | irc_user_t *iu = bu->ui_data; |
---|
83 | irc_channel_t *ic = irc->default_channel; |
---|
84 | |
---|
85 | /* Do this outside the if below since away state can change without |
---|
86 | the online state changing. */ |
---|
87 | iu->flags &= ~IRC_USER_AWAY; |
---|
88 | if( bu->flags & BEE_USER_AWAY || !( bu->flags & BEE_USER_ONLINE ) ) |
---|
89 | iu->flags |= IRC_USER_AWAY; |
---|
90 | |
---|
91 | if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) ) |
---|
92 | { |
---|
93 | if( bu->flags & BEE_USER_ONLINE ) |
---|
94 | { |
---|
95 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
96 | irc_send_num( irc, 600, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
97 | iu->host, (int) time( NULL ), "logged online" ); |
---|
98 | |
---|
99 | irc_channel_add_user( ic, iu ); |
---|
100 | |
---|
101 | if( set_getbool( &bee->set, "away_devoice" ) ) |
---|
102 | irc_channel_user_set_mode( ic, iu, ( bu->flags & BEE_USER_AWAY ) ? |
---|
103 | 0 : IRC_CHANNEL_USER_VOICE ); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | if( g_hash_table_lookup( irc->watches, iu->key ) ) |
---|
108 | irc_send_num( irc, 601, "%s %s %s %d :%s", iu->nick, iu->user, |
---|
109 | iu->host, (int) time( NULL ), "logged offline" ); |
---|
110 | |
---|
111 | irc_channel_del_user( ic, iu ); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | return TRUE; |
---|
116 | } |
---|
117 | |
---|
118 | static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
119 | { |
---|
120 | irc_t *irc = bee->ui_data; |
---|
121 | irc_channel_t *ic = irc->default_channel; |
---|
122 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
123 | char *dst, *prefix = NULL; |
---|
124 | char *wrapped, *ts = NULL; |
---|
125 | |
---|
126 | if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) ) |
---|
127 | ts = irc_format_timestamp( irc, sent_at ); |
---|
128 | |
---|
129 | if( iu->flags & IRC_USER_PRIVATE ) |
---|
130 | { |
---|
131 | dst = irc->user->nick; |
---|
132 | prefix = ts; |
---|
133 | ts = NULL; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | dst = ic->name; |
---|
138 | prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" ); |
---|
139 | } |
---|
140 | |
---|
141 | wrapped = word_wrap( msg, 425 ); |
---|
142 | irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix ); |
---|
143 | |
---|
144 | g_free( wrapped ); |
---|
145 | g_free( prefix ); |
---|
146 | g_free( ts ); |
---|
147 | |
---|
148 | return TRUE; |
---|
149 | } |
---|
150 | |
---|
151 | static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags ) |
---|
152 | { |
---|
153 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
154 | |
---|
155 | if( set_getbool( &bee->set, "typing_notice" ) ) |
---|
156 | irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick, |
---|
157 | "\001TYPING %d\001", ( flags >> 8 ) & 3 ); |
---|
158 | else |
---|
159 | return FALSE; |
---|
160 | |
---|
161 | return TRUE; |
---|
162 | } |
---|
163 | |
---|
164 | static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu ) |
---|
165 | { |
---|
166 | irc_user_t *iu = (irc_user_t *) bu->ui_data; |
---|
167 | irc_t *irc = (irc_t *) bee->ui_data; |
---|
168 | char *s; |
---|
169 | |
---|
170 | if( iu->fullname != iu->nick ) |
---|
171 | g_free( iu->fullname ); |
---|
172 | iu->fullname = g_strdup( bu->fullname ); |
---|
173 | |
---|
174 | /* Strip newlines (unlikely, but IRC-unfriendly so they must go) |
---|
175 | TODO(wilmer): Do the same with away msgs again! */ |
---|
176 | for( s = iu->fullname; *s; s ++ ) |
---|
177 | if( isspace( *s ) ) *s = ' '; |
---|
178 | |
---|
179 | if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) ) |
---|
180 | { |
---|
181 | char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname ); |
---|
182 | irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL ); |
---|
183 | } |
---|
184 | |
---|
185 | s = set_getstr( &bu->ic->acc->set, "nick_source" ); |
---|
186 | if( strcmp( s, "handle" ) != 0 ) |
---|
187 | { |
---|
188 | char *name = g_strdup( bu->fullname ); |
---|
189 | |
---|
190 | if( strcmp( s, "first_name" ) == 0 ) |
---|
191 | { |
---|
192 | int i; |
---|
193 | for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} |
---|
194 | name[i] = '\0'; |
---|
195 | } |
---|
196 | |
---|
197 | imcb_buddy_nick_hint( bu->ic, bu->handle, name ); |
---|
198 | |
---|
199 | g_free( name ); |
---|
200 | } |
---|
201 | |
---|
202 | return TRUE; |
---|
203 | } |
---|
204 | |
---|
205 | /* IRC->IM calls */ |
---|
206 | |
---|
207 | static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg ) |
---|
208 | { |
---|
209 | if( iu->bu ) |
---|
210 | return bee_user_msg( iu->irc->b, iu->bu, msg, 0 ); |
---|
211 | else |
---|
212 | return FALSE; |
---|
213 | } |
---|
214 | |
---|
215 | static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp ) |
---|
216 | { |
---|
217 | if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0 |
---|
218 | && g_strcasecmp( ctcp[1], "SEND" ) == 0 ) |
---|
219 | { |
---|
220 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request ) |
---|
221 | { |
---|
222 | file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp ); |
---|
223 | if ( ft ) |
---|
224 | iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle ); |
---|
225 | |
---|
226 | return TRUE; |
---|
227 | } |
---|
228 | } |
---|
229 | else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 ) |
---|
230 | { |
---|
231 | if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] ) |
---|
232 | { |
---|
233 | int st = ctcp[1][0]; |
---|
234 | if( st >= '0' && st <= '2' ) |
---|
235 | { |
---|
236 | st <<= 8; |
---|
237 | iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st ); |
---|
238 | } |
---|
239 | |
---|
240 | return TRUE; |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | return FALSE; |
---|
245 | } |
---|
246 | |
---|
247 | static const struct irc_user_funcs irc_user_im_funcs = { |
---|
248 | bee_irc_user_privmsg, |
---|
249 | bee_irc_user_ctcp, |
---|
250 | }; |
---|
251 | |
---|
252 | |
---|
253 | /* IM->IRC: Groupchats */ |
---|
254 | static const struct irc_channel_funcs irc_channel_im_chat_funcs; |
---|
255 | |
---|
256 | static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c ) |
---|
257 | { |
---|
258 | irc_t *irc = bee->ui_data; |
---|
259 | irc_channel_t *ic; |
---|
260 | char *topic; |
---|
261 | GSList *l; |
---|
262 | int i; |
---|
263 | |
---|
264 | /* Try to find a channel that expects to receive a groupchat. |
---|
265 | This flag is set by groupchat_stub_invite(). */ |
---|
266 | for( l = irc->channels; l; l = l->next ) |
---|
267 | { |
---|
268 | ic = l->data; |
---|
269 | if( ic->flags & IRC_CHANNEL_CHAT_PICKME ) |
---|
270 | break; |
---|
271 | } |
---|
272 | |
---|
273 | /* If we found none, just generate some stupid name. */ |
---|
274 | if( l == NULL ) for( i = 0; i <= 999; i ++ ) |
---|
275 | { |
---|
276 | char name[16]; |
---|
277 | sprintf( name, "&chat_%03d", i ); |
---|
278 | if( ( ic = irc_channel_new( irc, name ) ) ) |
---|
279 | break; |
---|
280 | } |
---|
281 | |
---|
282 | if( ic == NULL ) |
---|
283 | return FALSE; |
---|
284 | |
---|
285 | c->ui_data = ic; |
---|
286 | ic->data = c; |
---|
287 | ic->f = &irc_channel_im_chat_funcs; |
---|
288 | |
---|
289 | topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); |
---|
290 | irc_channel_set_topic( ic, topic, irc->root ); |
---|
291 | g_free( topic ); |
---|
292 | |
---|
293 | return TRUE; |
---|
294 | } |
---|
295 | |
---|
296 | static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c ) |
---|
297 | { |
---|
298 | irc_channel_t *ic = c->ui_data; |
---|
299 | |
---|
300 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
301 | irc_channel_printf( ic, "Cleaning up channel, bye!" ); |
---|
302 | |
---|
303 | irc_channel_free( ic ); |
---|
304 | |
---|
305 | return TRUE; |
---|
306 | } |
---|
307 | |
---|
308 | static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text ) |
---|
309 | { |
---|
310 | irc_channel_t *ic = c->ui_data; |
---|
311 | |
---|
312 | irc_channel_printf( ic, "%s", text ); |
---|
313 | |
---|
314 | return TRUE; |
---|
315 | } |
---|
316 | |
---|
317 | static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at ) |
---|
318 | { |
---|
319 | irc_t *irc = bee->ui_data; |
---|
320 | irc_user_t *iu = bu->ui_data; |
---|
321 | irc_channel_t *ic = c->ui_data; |
---|
322 | char *ts = NULL; |
---|
323 | |
---|
324 | if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) ) |
---|
325 | ts = irc_format_timestamp( irc, sent_at ); |
---|
326 | |
---|
327 | irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts ); |
---|
328 | g_free( ts ); |
---|
329 | |
---|
330 | return TRUE; |
---|
331 | } |
---|
332 | |
---|
333 | static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
334 | { |
---|
335 | irc_t *irc = bee->ui_data; |
---|
336 | |
---|
337 | irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data ); |
---|
338 | |
---|
339 | return TRUE; |
---|
340 | } |
---|
341 | |
---|
342 | static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu ) |
---|
343 | { |
---|
344 | irc_t *irc = bee->ui_data; |
---|
345 | |
---|
346 | irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data ); |
---|
347 | |
---|
348 | return TRUE; |
---|
349 | } |
---|
350 | |
---|
351 | static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu ) |
---|
352 | { |
---|
353 | irc_t *irc = bee->ui_data; |
---|
354 | irc_user_t *iu; |
---|
355 | |
---|
356 | if( bu == NULL ) |
---|
357 | iu = irc->root; |
---|
358 | else if( bu == bee->user ) |
---|
359 | iu = irc->user; |
---|
360 | else |
---|
361 | iu = bu->ui_data; |
---|
362 | |
---|
363 | irc_channel_set_topic( c->ui_data, new, iu ); |
---|
364 | |
---|
365 | return TRUE; |
---|
366 | } |
---|
367 | |
---|
368 | static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name ) |
---|
369 | { |
---|
370 | irc_t *irc = bee->ui_data; |
---|
371 | irc_channel_t *ic = c->ui_data; |
---|
372 | char stripped[MAX_NICK_LENGTH+1], *full_name; |
---|
373 | |
---|
374 | /* Don't rename a channel if the user's in it already. */ |
---|
375 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
376 | return FALSE; |
---|
377 | |
---|
378 | strncpy( stripped, name, MAX_NICK_LENGTH ); |
---|
379 | stripped[MAX_NICK_LENGTH] = '\0'; |
---|
380 | nick_strip( stripped ); |
---|
381 | if( set_getbool( &bee->set, "lcnicks" ) ) |
---|
382 | nick_lc( stripped ); |
---|
383 | |
---|
384 | full_name = g_strdup_printf( "&%s", stripped ); |
---|
385 | |
---|
386 | if( stripped[0] && irc_channel_by_name( irc, full_name ) == NULL ) |
---|
387 | { |
---|
388 | g_free( ic->name ); |
---|
389 | ic->name = full_name; |
---|
390 | } |
---|
391 | else |
---|
392 | { |
---|
393 | g_free( full_name ); |
---|
394 | } |
---|
395 | |
---|
396 | return TRUE; |
---|
397 | } |
---|
398 | |
---|
399 | /* IRC->IM */ |
---|
400 | static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg ) |
---|
401 | { |
---|
402 | struct groupchat *c = ic->data; |
---|
403 | |
---|
404 | bee_chat_msg( ic->irc->b, c, msg, 0 ); |
---|
405 | |
---|
406 | return TRUE; |
---|
407 | |
---|
408 | } |
---|
409 | |
---|
410 | static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg ) |
---|
411 | { |
---|
412 | struct groupchat *c = ic->data; |
---|
413 | |
---|
414 | if( c->ic->acc->prpl->chat_leave ) |
---|
415 | c->ic->acc->prpl->chat_leave( c ); |
---|
416 | |
---|
417 | return TRUE; |
---|
418 | |
---|
419 | } |
---|
420 | |
---|
421 | static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new ) |
---|
422 | { |
---|
423 | return TRUE; |
---|
424 | } |
---|
425 | |
---|
426 | static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu ) |
---|
427 | { |
---|
428 | struct groupchat *c = ic->data; |
---|
429 | |
---|
430 | if( iu->bu->ic != c->ic ) |
---|
431 | irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name ); |
---|
432 | else if( c->ic->acc->prpl->chat_invite ) |
---|
433 | c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL ); |
---|
434 | else |
---|
435 | irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name ); |
---|
436 | |
---|
437 | return TRUE; |
---|
438 | } |
---|
439 | |
---|
440 | static const struct irc_channel_funcs irc_channel_im_chat_funcs = { |
---|
441 | bee_irc_channel_chat_privmsg, |
---|
442 | NULL, /* join */ |
---|
443 | bee_irc_channel_chat_part, |
---|
444 | bee_irc_channel_chat_topic, |
---|
445 | bee_irc_channel_chat_invite, |
---|
446 | }; |
---|
447 | |
---|
448 | |
---|
449 | /* IM->IRC: File transfers */ |
---|
450 | static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ) |
---|
451 | { |
---|
452 | return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size ); |
---|
453 | } |
---|
454 | |
---|
455 | static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft ) |
---|
456 | { |
---|
457 | return dccs_recv_start( ft ); |
---|
458 | } |
---|
459 | |
---|
460 | static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft ) |
---|
461 | { |
---|
462 | return dcc_close( ft ); |
---|
463 | } |
---|
464 | |
---|
465 | static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file ) |
---|
466 | { |
---|
467 | dcc_file_transfer_t *df = file->priv; |
---|
468 | |
---|
469 | if( file->bytes_transferred >= file->file_size ) |
---|
470 | dcc_finish( file ); |
---|
471 | else |
---|
472 | df->proto_finished = TRUE; |
---|
473 | } |
---|
474 | |
---|
475 | const struct bee_ui_funcs irc_ui_funcs = { |
---|
476 | bee_irc_user_new, |
---|
477 | bee_irc_user_free, |
---|
478 | bee_irc_user_fullname, |
---|
479 | bee_irc_user_status, |
---|
480 | bee_irc_user_msg, |
---|
481 | bee_irc_user_typing, |
---|
482 | |
---|
483 | bee_irc_chat_new, |
---|
484 | bee_irc_chat_free, |
---|
485 | bee_irc_chat_log, |
---|
486 | bee_irc_chat_msg, |
---|
487 | bee_irc_chat_add_user, |
---|
488 | bee_irc_chat_remove_user, |
---|
489 | bee_irc_chat_topic, |
---|
490 | bee_irc_chat_name_hint, |
---|
491 | |
---|
492 | bee_irc_ft_in_start, |
---|
493 | bee_irc_ft_out_start, |
---|
494 | bee_irc_ft_close, |
---|
495 | bee_irc_ft_finished, |
---|
496 | }; |
---|