source: irc_im.c @ bc4b469

Last change on this file since bc4b469 was bc4b469, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-03T17:57:31Z

Initialize the show variable in channel_update. This shouldn't have caused
any troubles, only unpredictable behaviour for users trying out unimplemented
functionality.

  • Property mode set to 100644
File size: 21.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        irc_t *irc = (irc_t*) bee->ui_data;
37        char nick[MAX_NICK_LENGTH+1], *s;
38       
39        memset( nick, 0, MAX_NICK_LENGTH + 1 );
40        strcpy( nick, nick_get( bu->ic->acc, bu->handle ) );
41       
42        bu->ui_data = iu = irc_user_new( irc, nick );
43        iu->bu = bu;
44       
45        if( ( s = strchr( bu->handle, '@' ) ) )
46        {
47                iu->host = g_strdup( s + 1 );
48                iu->user = g_strndup( bu->handle, s - bu->handle );
49        }
50        else if( bu->ic->acc->server )
51        {
52                iu->host = g_strdup( bu->ic->acc->server );
53                iu->user = g_strdup( bu->handle );
54               
55                /* s/ /_/ ... important for AOL screennames */
56                for( s = iu->user; *s; s ++ )
57                        if( *s == ' ' )
58                                *s = '_';
59        }
60        else
61        {
62                iu->host = g_strdup( bu->ic->acc->prpl->name );
63                iu->user = g_strdup( bu->handle );
64        }
65       
66        if( bu->flags & BEE_USER_LOCAL )
67        {
68                char *s = set_getstr( &bee->set, "handle_unknown" );
69               
70                if( strcmp( s, "add_private" ) == 0 )
71                        iu->last_channel = NULL;
72                else if( strcmp( s, "add_channel" ) == 0 )
73                        iu->last_channel = irc->default_channel;
74        }
75       
76        iu->f = &irc_user_im_funcs;
77       
78        return TRUE;
79}
80
81static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu )
82{
83        return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data );
84}
85
86static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old )
87{
88        irc_t *irc = bee->ui_data;
89        irc_user_t *iu = bu->ui_data;
90       
91        /* Do this outside the if below since away state can change without
92           the online state changing. */
93        iu->flags &= ~IRC_USER_AWAY;
94        if( bu->flags & BEE_USER_AWAY || !( bu->flags & BEE_USER_ONLINE ) )
95                iu->flags |= IRC_USER_AWAY;
96       
97        if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) )
98        {
99                if( bu->flags & BEE_USER_ONLINE )
100                {
101                        if( g_hash_table_lookup( irc->watches, iu->key ) )
102                                irc_send_num( irc, 600, "%s %s %s %d :%s", iu->nick, iu->user,
103                                              iu->host, (int) time( NULL ), "logged online" );
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        }
112       
113        /* Reset this one since the info may have changed. */
114        iu->away_reply_timeout = 0;
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 = FALSE;
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                            ( ic->flags & IRC_CHANNEL_JOINED ) )
135                                bee_irc_channel_update( irc, ic, iu );
136                }
137                return;
138        }
139        if( iu == NULL )
140        {
141                for( l = irc->users; l; l = l->next )
142                {
143                        iu = l->data;
144                        if( iu->bu )
145                                bee_irc_channel_update( irc, ic, l->data );
146                }
147                return;
148        }
149       
150        icc = ic->data;
151       
152        if( !( iu->bu->flags & BEE_USER_ONLINE ) )
153                show = FALSE;
154        else if( icc->type == IRC_CC_TYPE_DEFAULT )
155                show = TRUE;
156        else if( icc->type == IRC_CC_TYPE_GROUP )
157                show = iu->bu->group == icc->group;
158        else if( icc->type == IRC_CC_TYPE_ACCOUNT )
159                show = iu->bu->ic->acc == icc->account;
160        else if( icc->type == IRC_CC_TYPE_PROTOCOL )
161                show = iu->bu->ic->acc->prpl == icc->protocol;
162       
163        if( !show )
164        {
165                irc_channel_del_user( ic, iu, FALSE, NULL );
166        }
167        else
168        {
169                irc_channel_add_user( ic, iu );
170               
171                if( set_getbool( &irc->b->set, "away_devoice" ) )
172                        irc_channel_user_set_mode( ic, iu, ( iu->bu->flags & BEE_USER_AWAY ) ?
173                                                   0 : IRC_CHANNEL_USER_VOICE );
174                else
175                        irc_channel_user_set_mode( ic, iu, 0 );
176        }
177}
178
179static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at )
180{
181        irc_t *irc = bee->ui_data;
182        irc_user_t *iu = (irc_user_t *) bu->ui_data;
183        char *dst, *prefix = NULL;
184        char *wrapped, *ts = NULL;
185       
186        if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) )
187                ts = irc_format_timestamp( irc, sent_at );
188       
189        if( iu->last_channel )
190        {
191                dst = iu->last_channel->name;
192                prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" );
193        }
194        else
195        {
196                dst = irc->user->nick;
197                prefix = ts;
198                ts = NULL;
199        }
200       
201        wrapped = word_wrap( msg, 425 );
202        irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix );
203       
204        g_free( wrapped );
205        g_free( prefix );
206        g_free( ts );
207       
208        return TRUE;
209}
210
211static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags )
212{
213        irc_t *irc = (irc_t *) bee->ui_data;
214       
215        if( set_getbool( &bee->set, "typing_notice" ) )
216                irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick,
217                                "\001TYPING %d\001", ( flags >> 8 ) & 3 );
218        else
219                return FALSE;
220       
221        return TRUE;
222}
223
224static gboolean bee_irc_user_nick_hint( bee_t *bee, bee_user_t *bu, const char *hint );
225
226static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu )
227{
228        irc_user_t *iu = (irc_user_t *) bu->ui_data;
229        irc_t *irc = (irc_t *) bee->ui_data;
230        char *s;
231       
232        if( iu->fullname != iu->nick )
233                g_free( iu->fullname );
234        iu->fullname = g_strdup( bu->fullname );
235       
236        /* Strip newlines (unlikely, but IRC-unfriendly so they must go)
237           TODO(wilmer): Do the same with away msgs again! */
238        for( s = iu->fullname; *s; s ++ )
239                if( isspace( *s ) ) *s = ' ';
240       
241        if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) )
242        {
243                char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname );
244                irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL );
245        }
246       
247        s = set_getstr( &bu->ic->acc->set, "nick_source" );
248        if( strcmp( s, "handle" ) != 0 )
249        {
250                char *name = g_strdup( bu->fullname );
251               
252                if( strcmp( s, "first_name" ) == 0 )
253                {
254                        int i;
255                        for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {}
256                        name[i] = '\0';
257                }
258               
259                bee_irc_user_nick_hint( bee, bu, name );
260               
261                g_free( name );
262        }
263       
264        return TRUE;
265}
266
267static gboolean bee_irc_user_nick_hint( bee_t *bee, bee_user_t *bu, const char *hint )
268{
269        irc_user_t *iu = bu->ui_data;
270        char newnick[MAX_NICK_LENGTH+1], *translit;
271       
272        if( bu->flags & BEE_USER_ONLINE )
273                /* Ignore if the user is visible already. */
274                return TRUE;
275       
276        if( nick_saved( bu->ic->acc, bu->handle ) )
277                /* The user already assigned a nickname to this person. */
278                return TRUE;
279       
280        /* Credits to Josay_ in #bitlbee for this idea. //TRANSLIT should
281           do lossy/approximate conversions, so letters with accents don't
282           just get stripped. Note that it depends on LC_CTYPE being set to
283           something other than C/POSIX. */
284        translit = g_convert( hint, -1, "ASCII//TRANSLIT//IGNORE", "UTF-8",
285                              NULL, NULL, NULL );
286       
287        strncpy( newnick, translit ? : hint, MAX_NICK_LENGTH );
288        newnick[MAX_NICK_LENGTH] = 0;
289        g_free( translit );
290       
291        /* Some processing to make sure this string is a valid IRC nickname. */
292        nick_strip( newnick );
293        if( set_getbool( &bee->set, "lcnicks" ) )
294                nick_lc( newnick );
295       
296        if( strcmp( iu->nick, newnick ) != 0 )
297        {
298                /* Only do this if newnick is different from the current one.
299                   If rejoining a channel, maybe we got this nick already
300                   (and dedupe would only add an underscore. */
301                nick_dedupe( bu->ic->acc, bu->handle, newnick );
302                irc_user_set_nick( iu, newnick );
303        }
304       
305        return TRUE;
306}
307
308static gboolean bee_irc_user_group( bee_t *bee, bee_user_t *bu )
309{
310        irc_user_t *iu = (irc_user_t *) bu->ui_data;
311        irc_t *irc = (irc_t *) bee->ui_data;
312       
313        bee_irc_channel_update( irc, NULL, iu );
314       
315        return TRUE;
316}
317
318/* IRC->IM calls */
319
320static gboolean bee_irc_user_privmsg_cb( gpointer data, gint fd, b_input_condition cond );
321
322static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg )
323{
324        const char *away;
325       
326        if( iu->bu == NULL )
327                return FALSE;
328       
329        if( ( away = irc_user_get_away( iu ) ) &&
330            time( NULL ) >= iu->away_reply_timeout )
331        {
332                irc_send_num( iu->irc, 301, "%s :%s", iu->nick, away );
333                iu->away_reply_timeout = time( NULL ) +
334                        set_getint( &iu->irc->b->set, "away_reply_timeout" );
335        }
336       
337        if( set_getbool( &iu->irc->b->set, "paste_buffer" ) )
338        {
339                int delay;
340               
341                if( iu->pastebuf == NULL )
342                        iu->pastebuf = g_string_new( msg );
343                else
344                {
345                        b_event_remove( iu->pastebuf_timer );
346                        g_string_append_printf( iu->pastebuf, "\n%s", msg );
347                }
348               
349                if( ( delay = set_getint( &iu->irc->b->set, "paste_buffer_delay" ) ) <= 5 )
350                        delay *= 1000;
351               
352                iu->pastebuf_timer = b_timeout_add( delay, bee_irc_user_privmsg_cb, iu );
353               
354                return TRUE;
355        }
356        else
357                return bee_user_msg( iu->irc->b, iu->bu, msg, 0 );
358}
359
360static gboolean bee_irc_user_privmsg_cb( gpointer data, gint fd, b_input_condition cond )
361{
362        irc_user_t *iu = data;
363       
364        bee_user_msg( iu->irc->b, iu->bu, iu->pastebuf->str, 0 );
365       
366        g_string_free( iu->pastebuf, TRUE );
367        iu->pastebuf = 0;
368        iu->pastebuf_timer = 0;
369       
370        return FALSE;
371}
372
373static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp )
374{
375        if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0
376                    && g_strcasecmp( ctcp[1], "SEND" ) == 0 )
377        {
378                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request )
379                {
380                        file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp );
381                        if ( ft )
382                                iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle );
383                       
384                        return TRUE;
385                }
386        }
387        else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 )
388        {
389                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] )
390                {
391                        int st = ctcp[1][0];
392                        if( st >= '0' && st <= '2' )
393                        {
394                                st <<= 8;
395                                iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st );
396                        }
397                       
398                        return TRUE;
399                }
400        }
401       
402        return FALSE;
403}
404
405static const struct irc_user_funcs irc_user_im_funcs = {
406        bee_irc_user_privmsg,
407        bee_irc_user_ctcp,
408};
409
410
411/* IM->IRC: Groupchats */
412const struct irc_channel_funcs irc_channel_im_chat_funcs;
413
414static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c )
415{
416        irc_t *irc = bee->ui_data;
417        irc_channel_t *ic;
418        char *topic;
419        GSList *l;
420        int i;
421       
422        /* Try to find a channel that expects to receive a groupchat.
423           This flag is set earlier in our current call trace. */
424        for( l = irc->channels; l; l = l->next )
425        {
426                ic = l->data;
427                if( ic->flags & IRC_CHANNEL_CHAT_PICKME )
428                        break;
429        }
430       
431        /* If we found none, just generate some stupid name. */
432        if( l == NULL ) for( i = 0; i <= 999; i ++ )
433        {
434                char name[16];
435                sprintf( name, "#chat_%03d", i );
436                if( ( ic = irc_channel_new( irc, name ) ) )
437                        break;
438        }
439       
440        if( ic == NULL )
441                return FALSE;
442       
443        c->ui_data = ic;
444        ic->data = c;
445       
446        topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title );
447        irc_channel_set_topic( ic, topic, irc->root );
448        g_free( topic );
449       
450        return TRUE;
451}
452
453static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c )
454{
455        irc_channel_t *ic = c->ui_data;
456       
457        if( ic->flags & IRC_CHANNEL_JOINED )
458                irc_channel_printf( ic, "Cleaning up channel, bye!" );
459       
460        ic->data = NULL;
461        irc_channel_del_user( ic, ic->irc->user, FALSE, "Chatroom closed by server" );
462       
463        return TRUE;
464}
465
466static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text )
467{
468        irc_channel_t *ic = c->ui_data;
469       
470        irc_channel_printf( ic, "%s", text );
471       
472        return TRUE;
473}
474
475static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at )
476{
477        irc_t *irc = bee->ui_data;
478        irc_user_t *iu = bu->ui_data;
479        irc_channel_t *ic = c->ui_data;
480        char *ts = NULL;
481       
482        if( sent_at > 0 && set_getbool( &bee->set, "display_timestamps" ) )
483                ts = irc_format_timestamp( irc, sent_at );
484       
485        irc_send_msg( iu, "PRIVMSG", ic->name, msg, ts );
486        g_free( ts );
487       
488        return TRUE;
489}
490
491static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
492{
493        irc_t *irc = bee->ui_data;
494       
495        irc_channel_add_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data );
496       
497        return TRUE;
498}
499
500static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
501{
502        irc_t *irc = bee->ui_data;
503       
504        /* TODO: Possible bug here: If a module removes $user here instead of just
505           using imcb_chat_free() and the channel was IRC_CHANNEL_TEMP, we get into
506           a broken state around here. */
507        irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data, FALSE, NULL );
508       
509        return TRUE;
510}
511
512static gboolean bee_irc_chat_topic( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu )
513{
514        irc_t *irc = bee->ui_data;
515        irc_user_t *iu;
516       
517        if( bu == NULL )
518                iu = irc->root;
519        else if( bu == bee->user )
520                iu = irc->user;
521        else
522                iu = bu->ui_data;
523       
524        irc_channel_set_topic( c->ui_data, new, iu );
525       
526        return TRUE;
527}
528
529static gboolean bee_irc_chat_name_hint( bee_t *bee, struct groupchat *c, const char *name )
530{
531        irc_t *irc = bee->ui_data;
532        irc_channel_t *ic = c->ui_data, *oic;
533        char stripped[MAX_NICK_LENGTH+1], *full_name;
534       
535        /* Don't rename a channel if the user's in it already. */
536        if( ic->flags & IRC_CHANNEL_JOINED )
537                return FALSE;
538       
539        strncpy( stripped, name, MAX_NICK_LENGTH );
540        stripped[MAX_NICK_LENGTH] = '\0';
541        irc_channel_name_strip( stripped );
542        if( set_getbool( &bee->set, "lcnicks" ) )
543                nick_lc( stripped );
544       
545        if( stripped[0] == '\0' )
546                return FALSE;
547       
548        full_name = g_strdup_printf( "#%s", stripped );
549        if( ( oic = irc_channel_by_name( irc, full_name ) ) )
550        {
551                char *type, *chat_type;
552               
553                type = set_getstr( &oic->set, "type" );
554                chat_type = set_getstr( &oic->set, "chat_type" );
555               
556                if( type && chat_type && oic->data == FALSE &&
557                    strcmp( type, "chat" ) == 0 &&
558                    strcmp( chat_type, "groupchat" ) == 0 )
559                {
560                        /* There's a channel with this name already, but it looks
561                           like it's not in use yet. Most likely the IRC client
562                           rejoined the channel after a reconnect. Remove it so
563                           we can reuse its name. */
564                        irc_channel_free( oic );
565                }
566                else
567                {
568                        g_free( full_name );
569                        return FALSE;
570                }
571        }
572       
573        g_free( ic->name );
574        ic->name = full_name;
575       
576        return TRUE;
577}
578
579/* IRC->IM */
580static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond );
581
582static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg )
583{
584        struct groupchat *c = ic->data;
585       
586        if( c == NULL )
587                return FALSE;
588        else if( set_getbool( &ic->irc->b->set, "paste_buffer" ) )
589        {
590                int delay;
591               
592                if( ic->pastebuf == NULL )
593                        ic->pastebuf = g_string_new( msg );
594                else
595                {
596                        b_event_remove( ic->pastebuf_timer );
597                        g_string_append_printf( ic->pastebuf, "\n%s", msg );
598                }
599               
600                if( ( delay = set_getint( &ic->irc->b->set, "paste_buffer_delay" ) ) <= 5 )
601                        delay *= 1000;
602               
603                ic->pastebuf_timer = b_timeout_add( delay, bee_irc_channel_chat_privmsg_cb, ic );
604               
605                return TRUE;
606        }
607        else
608                bee_chat_msg( ic->irc->b, c, msg, 0 );
609       
610        return TRUE;
611}
612
613static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond )
614{
615        irc_channel_t *ic = data;
616       
617        bee_chat_msg( ic->irc->b, ic->data, ic->pastebuf->str, 0 );
618       
619        g_string_free( ic->pastebuf, TRUE );
620        ic->pastebuf = 0;
621        ic->pastebuf_timer = 0;
622       
623        return FALSE;
624}
625
626static gboolean bee_irc_channel_chat_join( irc_channel_t *ic )
627{
628        char *acc_s, *room;
629        account_t *acc;
630       
631        if( strcmp( set_getstr( &ic->set, "chat_type" ), "room" ) != 0 )
632                return TRUE;
633       
634        if( ( acc_s = set_getstr( &ic->set, "account" ) ) &&
635            ( room = set_getstr( &ic->set, "room" ) ) &&
636            ( acc = account_get( ic->irc->b, acc_s ) ) &&
637            acc->ic && acc->prpl->chat_join )
638        {
639                char *nick;
640               
641                if( !( nick = set_getstr( &ic->set, "nick" ) ) )
642                        nick = ic->irc->user->nick;
643               
644                ic->flags |= IRC_CHANNEL_CHAT_PICKME;
645                acc->prpl->chat_join( acc->ic, room, nick, NULL );
646                ic->flags &= ~IRC_CHANNEL_CHAT_PICKME;
647               
648                return FALSE;
649        }
650        else
651        {
652                irc_send_num( ic->irc, 403, "%s :Can't join channel, account offline?", ic->name );
653                return FALSE;
654        }
655}
656
657static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg )
658{
659        struct groupchat *c = ic->data;
660       
661        if( c && c->ic->acc->prpl->chat_leave )
662                c->ic->acc->prpl->chat_leave( c );
663       
664        return TRUE;
665}
666
667static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new )
668{
669        struct groupchat *c = ic->data;
670       
671        if( c == NULL )
672                return FALSE;
673       
674        if( c->ic->acc->prpl->chat_topic == NULL )
675                irc_send_num( ic->irc, 482, "%s :IM network does not support channel topics", ic->name );
676        else
677        {
678                /* TODO: Need more const goodness here, sigh */
679                char *topic = g_strdup( new );
680                c->ic->acc->prpl->chat_topic( c, topic );
681                g_free( topic );
682                return TRUE;
683        }
684               
685        return FALSE;
686}
687
688static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu )
689{
690        struct groupchat *c = ic->data;
691        bee_user_t *bu = iu->bu;
692       
693        if( bu == NULL )
694                return FALSE;
695       
696        if( c )
697        {
698                if( iu->bu->ic != c->ic )
699                        irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name );
700                else if( c->ic->acc->prpl->chat_invite )
701                        c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL );
702                else
703                        irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name );
704        }
705        else if( bu->ic->acc->prpl->chat_with &&
706                 strcmp( set_getstr( &ic->set, "chat_type" ), "groupchat" ) == 0 )
707        {
708                ic->flags |= IRC_CHANNEL_CHAT_PICKME;
709                iu->bu->ic->acc->prpl->chat_with( bu->ic, bu->handle );
710                ic->flags &= ~IRC_CHANNEL_CHAT_PICKME;
711        }
712        else
713        {
714                irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name );
715        }
716       
717        return TRUE;
718}
719
720static char *set_eval_room_account( set_t *set, char *value );
721static char *set_eval_chat_type( set_t *set, char *value );
722
723static gboolean bee_irc_channel_init( irc_channel_t *ic )
724{
725        set_add( &ic->set, "account", NULL, set_eval_room_account, ic );
726        set_add( &ic->set, "chat_type", "groupchat", set_eval_chat_type, ic );
727        set_add( &ic->set, "nick", NULL, NULL, ic );
728        set_add( &ic->set, "room", NULL, NULL, ic );
729       
730        /* chat_type == groupchat */
731        ic->flags |= IRC_CHANNEL_TEMP;
732       
733        return TRUE;
734}
735
736static char *set_eval_room_account( set_t *set, char *value )
737{
738        struct irc_channel *ic = set->data;
739        account_t *acc;
740       
741        if( !( acc = account_get( ic->irc->b, value ) ) )
742                return SET_INVALID;
743        else if( !acc->prpl->chat_join )
744        {
745                irc_usermsg( ic->irc, "Named chatrooms not supported on that account." );
746                return SET_INVALID;
747        }
748       
749        return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user );
750}
751
752static char *set_eval_chat_type( set_t *set, char *value )
753{
754        struct irc_channel *ic = set->data;
755       
756        if( strcmp( value, "groupchat" ) == 0 )
757                ic->flags |= IRC_CHANNEL_TEMP;
758        else if( strcmp( value, "room" ) == 0 )
759                ic->flags &= ~IRC_CHANNEL_TEMP;
760        else
761                return NULL;
762       
763        return value;
764}
765
766static gboolean bee_irc_channel_free( irc_channel_t *ic )
767{
768        set_del( &ic->set, "account" );
769        set_del( &ic->set, "chat_type" );
770        set_del( &ic->set, "nick" );
771        set_del( &ic->set, "room" );
772       
773        ic->flags &= ~IRC_CHANNEL_TEMP;
774       
775        return TRUE;
776}
777
778const struct irc_channel_funcs irc_channel_im_chat_funcs = {
779        bee_irc_channel_chat_privmsg,
780        bee_irc_channel_chat_join,
781        bee_irc_channel_chat_part,
782        bee_irc_channel_chat_topic,
783        bee_irc_channel_chat_invite,
784
785        bee_irc_channel_init,
786        bee_irc_channel_free,
787};
788
789
790/* IM->IRC: File transfers */
791static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size )
792{
793        return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size );
794}
795
796static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft )
797{
798        return dccs_recv_start( ft );
799}
800
801static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft )
802{
803        return dcc_close( ft );
804}
805
806static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file )
807{
808        dcc_file_transfer_t *df = file->priv;
809
810        if( file->bytes_transferred >= file->file_size )
811                dcc_finish( file );
812        else
813                df->proto_finished = TRUE;
814}
815
816const struct bee_ui_funcs irc_ui_funcs = {
817        bee_irc_user_new,
818        bee_irc_user_free,
819        bee_irc_user_fullname,
820        bee_irc_user_nick_hint,
821        bee_irc_user_group,
822        bee_irc_user_status,
823        bee_irc_user_msg,
824        bee_irc_user_typing,
825       
826        bee_irc_chat_new,
827        bee_irc_chat_free,
828        bee_irc_chat_log,
829        bee_irc_chat_msg,
830        bee_irc_chat_add_user,
831        bee_irc_chat_remove_user,
832        bee_irc_chat_topic,
833        bee_irc_chat_name_hint,
834       
835        bee_irc_ft_in_start,
836        bee_irc_ft_out_start,
837        bee_irc_ft_close,
838        bee_irc_ft_finished,
839};
Note: See TracBrowser for help on using the repository browser.