source: irc_im.c @ 13c1a9f

Last change on this file since 13c1a9f was 13c1a9f, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-09T21:52:17Z

/join &groupname and all people in that group will be in that channel.

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