source: irc_im.c @ a4d920b

Last change on this file since a4d920b was 0e8b3e8, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-07T15:21:21Z

Changing away_devoice will change current voice statuses in all channels.

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