source: irc_im.c @ 3429b58

Last change on this file since 3429b58 was a067771, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-09T22:08:30Z

Per-account channels also exist now.

  • Property mode set to 100644
File size: 13.2 KB
Line 
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
31static const struct irc_user_funcs irc_user_im_funcs;
32
33static 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
74static 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
79static 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       
84        /* Do this outside the if below since away state can change without
85           the online state changing. */
86        iu->flags &= ~IRC_USER_AWAY;
87        if( bu->flags & BEE_USER_AWAY || !( bu->flags & BEE_USER_ONLINE ) )
88                iu->flags |= IRC_USER_AWAY;
89       
90        if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) )
91        {
92                if( bu->flags & BEE_USER_ONLINE )
93                {
94                        if( g_hash_table_lookup( irc->watches, iu->key ) )
95                                irc_send_num( irc, 600, "%s %s %s %d :%s", iu->nick, iu->user,
96                                              iu->host, (int) time( NULL ), "logged online" );
97                }
98                else
99                {
100                        if( g_hash_table_lookup( irc->watches, iu->key ) )
101                                irc_send_num( irc, 601, "%s %s %s %d :%s", iu->nick, iu->user,
102                                              iu->host, (int) time( NULL ), "logged offline" );
103                }
104        }
105       
106        bee_irc_channel_update( irc, NULL, iu );
107       
108        return TRUE;
109}
110
111void bee_irc_channel_update( irc_t *irc, irc_channel_t *ic, irc_user_t *iu )
112{
113        struct irc_control_channel *icc;
114        GSList *l;
115        gboolean show;
116       
117        if( ic == NULL )
118        {
119                for( l = irc->channels; l; l = l->next )
120                {
121                        ic = l->data;
122                        /* TODO: Just add a type flag or so.. */
123                        if( ic->f == irc->default_channel->f )
124                                bee_irc_channel_update( irc, ic, iu );
125                }
126                return;
127        }
128        if( iu == NULL )
129        {
130                for( l = irc->users; l; l = l->next )
131                {
132                        iu = l->data;
133                        if( iu->bu )
134                                bee_irc_channel_update( irc, ic, l->data );
135                }
136                return;
137        }
138       
139        icc = ic->data;
140       
141        if( !( iu->bu->flags & BEE_USER_ONLINE ) )
142                show = FALSE;
143        else if( icc->type == IRC_CC_TYPE_DEFAULT )
144                show = TRUE;
145        else if( icc->type == IRC_CC_TYPE_GROUP )
146                show = iu->bu->group == icc->group;
147        else if( icc->type == IRC_CC_TYPE_ACCOUNT )
148                show = iu->bu->ic->acc == icc->account;
149       
150        if( !show )
151        {
152                irc_channel_del_user( ic, iu );
153        }
154        else
155        {
156                irc_channel_add_user( ic, iu );
157               
158                if( set_getbool( &irc->b->set, "away_devoice" ) )
159                        irc_channel_user_set_mode( ic, iu, ( iu->bu->flags & BEE_USER_AWAY ) ?
160                                                   0 : IRC_CHANNEL_USER_VOICE );
161        }
162}
163
164static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at )
165{
166        irc_t *irc = bee->ui_data;
167        irc_channel_t *ic = irc->default_channel;
168        irc_user_t *iu = (irc_user_t *) bu->ui_data;
169        char *dst, *prefix = NULL;
170        char *wrapped, *ts = NULL;
171       
172        if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) )
173                ts = irc_format_timestamp( irc, sent_at );
174       
175        if( iu->flags & IRC_USER_PRIVATE )
176        {
177                dst = irc->user->nick;
178                prefix = ts;
179                ts = NULL;
180        }
181        else
182        {
183                dst = ic->name;
184                prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" );
185        }
186       
187        wrapped = word_wrap( msg, 425 );
188        irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix );
189       
190        g_free( wrapped );
191        g_free( prefix );
192        g_free( ts );
193       
194        return TRUE;
195}
196
197static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags )
198{
199        irc_t *irc = (irc_t *) bee->ui_data;
200       
201        if( set_getbool( &bee->set, "typing_notice" ) )
202                irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick,
203                                "\001TYPING %d\001", ( flags >> 8 ) & 3 );
204        else
205                return FALSE;
206       
207        return TRUE;
208}
209
210static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu )
211{
212        irc_user_t *iu = (irc_user_t *) bu->ui_data;
213        irc_t *irc = (irc_t *) bee->ui_data;
214        char *s;
215       
216        if( iu->fullname != iu->nick )
217                g_free( iu->fullname );
218        iu->fullname = g_strdup( bu->fullname );
219       
220        /* Strip newlines (unlikely, but IRC-unfriendly so they must go)
221           TODO(wilmer): Do the same with away msgs again! */
222        for( s = iu->fullname; *s; s ++ )
223                if( isspace( *s ) ) *s = ' ';
224       
225        if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) )
226        {
227                char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname );
228                irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL );
229        }
230       
231        s = set_getstr( &bu->ic->acc->set, "nick_source" );
232        if( strcmp( s, "handle" ) != 0 )
233        {
234                char *name = g_strdup( bu->fullname );
235               
236                if( strcmp( s, "first_name" ) == 0 )
237                {
238                        int i;
239                        for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {}
240                        name[i] = '\0';
241                }
242               
243                imcb_buddy_nick_hint( bu->ic, bu->handle, name );
244               
245                g_free( name );
246        }
247       
248        return TRUE;
249}
250
251/* IRC->IM calls */
252
253static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg )
254{
255        if( iu->bu )
256                return bee_user_msg( iu->irc->b, iu->bu, msg, 0 );
257        else
258                return FALSE;
259}
260
261static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp )
262{
263        if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0
264                    && g_strcasecmp( ctcp[1], "SEND" ) == 0 )
265        {
266                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request )
267                {
268                        file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp );
269                        if ( ft )
270                                iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle );
271                       
272                        return TRUE;
273                }
274        }
275        else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 )
276        {
277                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] )
278                {
279                        int st = ctcp[1][0];
280                        if( st >= '0' && st <= '2' )
281                        {
282                                st <<= 8;
283                                iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st );
284                        }
285                       
286                        return TRUE;
287                }
288        }
289       
290        return FALSE;
291}
292
293static const struct irc_user_funcs irc_user_im_funcs = {
294        bee_irc_user_privmsg,
295        bee_irc_user_ctcp,
296};
297
298
299/* IM->IRC: Groupchats */
300static const struct irc_channel_funcs irc_channel_im_chat_funcs;
301
302static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c )
303{
304        irc_t *irc = bee->ui_data;
305        irc_channel_t *ic;
306        char *topic;
307        GSList *l;
308        int i;
309       
310        /* Try to find a channel that expects to receive a groupchat.
311           This flag is set by groupchat_stub_invite(). */
312        for( l = irc->channels; l; l = l->next )
313        {
314                ic = l->data;
315                if( ic->flags & IRC_CHANNEL_CHAT_PICKME )
316                        break;
317        }
318       
319        /* If we found none, just generate some stupid name. */
320        if( l == NULL ) for( i = 0; i <= 999; i ++ )
321        {
322                char name[16];
323                sprintf( name, "&chat_%03d", i );
324                if( ( ic = irc_channel_new( irc, name ) ) )
325                        break;
326        }
327       
328        if( ic == NULL )
329                return FALSE;
330       
331        c->ui_data = ic;
332        ic->data = c;
333        ic->f = &irc_channel_im_chat_funcs;
334       
335        topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
336        irc_channel_set_topic( ic, topic, irc->root );
337        g_free( topic );
338       
339        return TRUE;
340}
341
342static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c )
343{
344        irc_channel_t *ic = c->ui_data;
345       
346        if( ic->flags & IRC_CHANNEL_JOINED )
347                irc_channel_printf( ic, "Cleaning up channel, bye!" );
348       
349        irc_channel_free( ic );
350       
351        return TRUE;
352}
353
354static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text )
355{
356        irc_channel_t *ic = c->ui_data;
357       
358        irc_channel_printf( ic, "%s", text );
359       
360        return TRUE;
361}
362
363static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at )
364{
365        irc_t *irc = bee->ui_data;
366        irc_user_t *iu = bu->ui_data;
367        irc_channel_t *ic = c->ui_data;
368        char *ts = NULL;
369       
370        if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) )
371                ts = irc_format_timestamp( irc, sent_at );
372       
373        irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts );
374        g_free( ts );
375       
376        return TRUE;
377}
378
379static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
380{
381        irc_t *irc = bee->ui_data;
382       
383        irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data );
384       
385        return TRUE;
386}
387
388static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
389{
390        irc_t *irc = bee->ui_data;
391       
392        irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data );
393       
394        return TRUE;
395}
396
397static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu )
398{
399        irc_t *irc = bee->ui_data;
400        irc_user_t *iu;
401       
402        if( bu == NULL )
403                iu = irc->root;
404        else if( bu == bee->user )
405                iu = irc->user;
406        else
407                iu = bu->ui_data;
408       
409        irc_channel_set_topic( c->ui_data, new, iu );
410       
411        return TRUE;
412}
413
414static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name )
415{
416        irc_t *irc = bee->ui_data;
417        irc_channel_t *ic = c->ui_data;
418        char stripped[MAX_NICK_LENGTH+1], *full_name;
419       
420        /* Don't rename a channel if the user's in it already. */
421        if( ic->flags & IRC_CHANNEL_JOINED )
422                return FALSE;
423       
424        strncpy( stripped, name, MAX_NICK_LENGTH );
425        stripped[MAX_NICK_LENGTH] = '\0';
426        nick_strip( stripped );
427        if( set_getbool( &bee->set, "lcnicks" ) )
428                nick_lc( stripped );
429       
430        full_name = g_strdup_printf( "&%s", stripped );
431       
432        if( stripped[0] && irc_channel_by_name( irc, full_name ) == NULL )
433        {
434                g_free( ic->name );
435                ic->name = full_name;
436        }
437        else
438        {
439                g_free( full_name );
440        }
441       
442        return TRUE;
443}
444
445/* IRC->IM */
446static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg )
447{
448        struct groupchat *c = ic->data;
449       
450        bee_chat_msg( ic->irc->b, c, msg, 0 );
451       
452        return TRUE;
453       
454}
455
456static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg )
457{
458        struct groupchat *c = ic->data;
459       
460        if( c->ic->acc->prpl->chat_leave )
461                c->ic->acc->prpl->chat_leave( c );
462       
463        return TRUE;
464       
465}
466
467static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new )
468{
469        return TRUE;
470}
471
472static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu )
473{
474        struct groupchat *c = ic->data;
475       
476        if( iu->bu->ic != c->ic )
477                irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name );
478        else if( c->ic->acc->prpl->chat_invite )
479                c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL );
480        else
481                irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name );
482       
483        return TRUE;
484}
485
486static const struct irc_channel_funcs irc_channel_im_chat_funcs = {
487        bee_irc_channel_chat_privmsg,
488        NULL, /* join */
489        bee_irc_channel_chat_part,
490        bee_irc_channel_chat_topic,
491        bee_irc_channel_chat_invite,
492};
493
494
495/* IM->IRC: File transfers */
496static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size )
497{
498        return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size );
499}
500
501static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft )
502{
503        return dccs_recv_start( ft );
504}
505
506static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft )
507{
508        return dcc_close( ft );
509}
510
511static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file )
512{
513        dcc_file_transfer_t *df = file->priv;
514
515        if( file->bytes_transferred >= file->file_size )
516                dcc_finish( file );
517        else
518                df->proto_finished = TRUE;
519}
520
521const struct bee_ui_funcs irc_ui_funcs = {
522        bee_irc_user_new,
523        bee_irc_user_free,
524        bee_irc_user_fullname,
525        bee_irc_user_status,
526        bee_irc_user_msg,
527        bee_irc_user_typing,
528       
529        bee_irc_chat_new,
530        bee_irc_chat_free,
531        bee_irc_chat_log,
532        bee_irc_chat_msg,
533        bee_irc_chat_add_user,
534        bee_irc_chat_remove_user,
535        bee_irc_chat_topic,
536        bee_irc_chat_name_hint,
537       
538        bee_irc_ft_in_start,
539        bee_irc_ft_out_start,
540        bee_irc_ft_close,
541        bee_irc_ft_finished,
542};
Note: See TracBrowser for help on using the repository browser.