source: irc_im.c @ f5d87ea

Last change on this file since f5d87ea was 7e83e8e4, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-05T00:35:17Z

Inform the UI about group changes. This is important if the user has
group channels.

  • Property mode set to 100644
File size: 14.0 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 );
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        }
172}
173
174static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at )
175{
176        irc_t *irc = bee->ui_data;
177        irc_channel_t *ic = irc->default_channel;
178        irc_user_t *iu = (irc_user_t *) bu->ui_data;
179        char *dst, *prefix = NULL;
180        char *wrapped, *ts = NULL;
181       
182        if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) )
183                ts = irc_format_timestamp( irc, sent_at );
184       
185        if( iu->flags & IRC_USER_PRIVATE )
186        {
187                dst = irc->user->nick;
188                prefix = ts;
189                ts = NULL;
190        }
191        else
192        {
193                dst = ic->name;
194                prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" );
195        }
196       
197        wrapped = word_wrap( msg, 425 );
198        irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix );
199       
200        g_free( wrapped );
201        g_free( prefix );
202        g_free( ts );
203       
204        return TRUE;
205}
206
207static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags )
208{
209        irc_t *irc = (irc_t *) bee->ui_data;
210       
211        if( set_getbool( &bee->set, "typing_notice" ) )
212                irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick,
213                                "\001TYPING %d\001", ( flags >> 8 ) & 3 );
214        else
215                return FALSE;
216       
217        return TRUE;
218}
219
220static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu )
221{
222        irc_user_t *iu = (irc_user_t *) bu->ui_data;
223        irc_t *irc = (irc_t *) bee->ui_data;
224        char *s;
225       
226        if( iu->fullname != iu->nick )
227                g_free( iu->fullname );
228        iu->fullname = g_strdup( bu->fullname );
229       
230        /* Strip newlines (unlikely, but IRC-unfriendly so they must go)
231           TODO(wilmer): Do the same with away msgs again! */
232        for( s = iu->fullname; *s; s ++ )
233                if( isspace( *s ) ) *s = ' ';
234       
235        if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) )
236        {
237                char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname );
238                irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL );
239        }
240       
241        s = set_getstr( &bu->ic->acc->set, "nick_source" );
242        if( strcmp( s, "handle" ) != 0 )
243        {
244                char *name = g_strdup( bu->fullname );
245               
246                if( strcmp( s, "first_name" ) == 0 )
247                {
248                        int i;
249                        for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {}
250                        name[i] = '\0';
251                }
252               
253                imcb_buddy_nick_hint( bu->ic, bu->handle, name );
254               
255                g_free( name );
256        }
257       
258        return TRUE;
259}
260
261static gboolean bee_irc_user_group( bee_t *bee, bee_user_t *bu )
262{
263        irc_user_t *iu = (irc_user_t *) bu->ui_data;
264        irc_t *irc = (irc_t *) bee->ui_data;
265       
266        bee_irc_channel_update( irc, NULL, iu );
267       
268        return TRUE;
269}
270
271/* IRC->IM calls */
272
273static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg )
274{
275        if( iu->bu )
276                return bee_user_msg( iu->irc->b, iu->bu, msg, 0 );
277        else
278                return FALSE;
279}
280
281static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp )
282{
283        if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0
284                    && g_strcasecmp( ctcp[1], "SEND" ) == 0 )
285        {
286                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request )
287                {
288                        file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp );
289                        if ( ft )
290                                iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle );
291                       
292                        return TRUE;
293                }
294        }
295        else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 )
296        {
297                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] )
298                {
299                        int st = ctcp[1][0];
300                        if( st >= '0' && st <= '2' )
301                        {
302                                st <<= 8;
303                                iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st );
304                        }
305                       
306                        return TRUE;
307                }
308        }
309       
310        return FALSE;
311}
312
313static const struct irc_user_funcs irc_user_im_funcs = {
314        bee_irc_user_privmsg,
315        bee_irc_user_ctcp,
316};
317
318
319/* IM->IRC: Groupchats */
320static const struct irc_channel_funcs irc_channel_im_chat_funcs;
321
322static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c )
323{
324        irc_t *irc = bee->ui_data;
325        irc_channel_t *ic;
326        char *topic;
327        GSList *l;
328        int i;
329       
330        /* Try to find a channel that expects to receive a groupchat.
331           This flag is set by groupchat_stub_invite(). */
332        for( l = irc->channels; l; l = l->next )
333        {
334                ic = l->data;
335                if( ic->flags & IRC_CHANNEL_CHAT_PICKME )
336                        break;
337        }
338       
339        /* If we found none, just generate some stupid name. */
340        if( l == NULL ) for( i = 0; i <= 999; i ++ )
341        {
342                char name[16];
343                sprintf( name, "&chat_%03d", i );
344                if( ( ic = irc_channel_new( irc, name ) ) )
345                        break;
346        }
347       
348        if( ic == NULL )
349                return FALSE;
350       
351        c->ui_data = ic;
352        ic->data = c;
353        ic->f = &irc_channel_im_chat_funcs;
354       
355        topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
356        irc_channel_set_topic( ic, topic, irc->root );
357        g_free( topic );
358       
359        return TRUE;
360}
361
362static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c )
363{
364        irc_channel_t *ic = c->ui_data;
365       
366        if( ic->flags & IRC_CHANNEL_JOINED )
367                irc_channel_printf( ic, "Cleaning up channel, bye!" );
368       
369        irc_channel_free( ic );
370       
371        return TRUE;
372}
373
374static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text )
375{
376        irc_channel_t *ic = c->ui_data;
377       
378        irc_channel_printf( ic, "%s", text );
379       
380        return TRUE;
381}
382
383static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at )
384{
385        irc_t *irc = bee->ui_data;
386        irc_user_t *iu = bu->ui_data;
387        irc_channel_t *ic = c->ui_data;
388        char *ts = NULL;
389       
390        if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) )
391                ts = irc_format_timestamp( irc, sent_at );
392       
393        irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts );
394        g_free( ts );
395       
396        return TRUE;
397}
398
399static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
400{
401        irc_t *irc = bee->ui_data;
402       
403        irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data );
404       
405        return TRUE;
406}
407
408static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
409{
410        irc_t *irc = bee->ui_data;
411       
412        irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data );
413       
414        return TRUE;
415}
416
417static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu )
418{
419        irc_t *irc = bee->ui_data;
420        irc_user_t *iu;
421       
422        if( bu == NULL )
423                iu = irc->root;
424        else if( bu == bee->user )
425                iu = irc->user;
426        else
427                iu = bu->ui_data;
428       
429        irc_channel_set_topic( c->ui_data, new, iu );
430       
431        return TRUE;
432}
433
434static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name )
435{
436        irc_t *irc = bee->ui_data;
437        irc_channel_t *ic = c->ui_data;
438        char stripped[MAX_NICK_LENGTH+1], *full_name;
439       
440        /* Don't rename a channel if the user's in it already. */
441        if( ic->flags & IRC_CHANNEL_JOINED )
442                return FALSE;
443       
444        strncpy( stripped, name, MAX_NICK_LENGTH );
445        stripped[MAX_NICK_LENGTH] = '\0';
446        nick_strip( stripped );
447        if( set_getbool( &bee->set, "lcnicks" ) )
448                nick_lc( stripped );
449       
450        full_name = g_strdup_printf( "&%s", stripped );
451       
452        if( stripped[0] && irc_channel_by_name( irc, full_name ) == NULL )
453        {
454                g_free( ic->name );
455                ic->name = full_name;
456        }
457        else
458        {
459                g_free( full_name );
460        }
461       
462        return TRUE;
463}
464
465/* IRC->IM */
466static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg )
467{
468        struct groupchat *c = ic->data;
469       
470        bee_chat_msg( ic->irc->b, c, msg, 0 );
471       
472        return TRUE;
473       
474}
475
476static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg )
477{
478        struct groupchat *c = ic->data;
479       
480        if( c->ic->acc->prpl->chat_leave )
481                c->ic->acc->prpl->chat_leave( c );
482       
483        return TRUE;
484       
485}
486
487static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new )
488{
489        struct groupchat *c = ic->data;
490        char *topic = g_strdup( new ); /* TODO: Need more const goodness here, sigh */
491       
492        if( c->ic->acc->prpl->chat_topic == NULL )
493                irc_send_num( ic->irc, 482, "%s :IM network does not support channel topics", ic->name );
494        else
495        {
496                c->ic->acc->prpl->chat_topic( c, topic );
497                return TRUE;
498        }
499               
500        return FALSE;
501}
502
503static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu )
504{
505        struct groupchat *c = ic->data;
506       
507        if( iu->bu->ic != c->ic )
508                irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name );
509        else if( c->ic->acc->prpl->chat_invite )
510                c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL );
511        else
512                irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name );
513       
514        return TRUE;
515}
516
517static const struct irc_channel_funcs irc_channel_im_chat_funcs = {
518        bee_irc_channel_chat_privmsg,
519        NULL, /* join */
520        bee_irc_channel_chat_part,
521        bee_irc_channel_chat_topic,
522        bee_irc_channel_chat_invite,
523};
524
525
526/* IM->IRC: File transfers */
527static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size )
528{
529        return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size );
530}
531
532static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft )
533{
534        return dccs_recv_start( ft );
535}
536
537static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft )
538{
539        return dcc_close( ft );
540}
541
542static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file )
543{
544        dcc_file_transfer_t *df = file->priv;
545
546        if( file->bytes_transferred >= file->file_size )
547                dcc_finish( file );
548        else
549                df->proto_finished = TRUE;
550}
551
552const struct bee_ui_funcs irc_ui_funcs = {
553        bee_irc_user_new,
554        bee_irc_user_free,
555        bee_irc_user_fullname,
556        bee_irc_user_group,
557        bee_irc_user_status,
558        bee_irc_user_msg,
559        bee_irc_user_typing,
560       
561        bee_irc_chat_new,
562        bee_irc_chat_free,
563        bee_irc_chat_log,
564        bee_irc_chat_msg,
565        bee_irc_chat_add_user,
566        bee_irc_chat_remove_user,
567        bee_irc_chat_topic,
568        bee_irc_chat_name_hint,
569       
570        bee_irc_ft_in_start,
571        bee_irc_ft_out_start,
572        bee_irc_ft_close,
573        bee_irc_ft_finished,
574};
Note: See TracBrowser for help on using the repository browser.