source: irc_im.c @ 9052bc1

Last change on this file since 9052bc1 was 9052bc1, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-30T23:15:44Z

Flush channels when the user leaves them. Also, don't update a control
channel if the user isn't in it.

  • Property mode set to 100644
File size: 20.4 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;
[92c8d41]36        irc_t *irc = (irc_t*) bee->ui_data;
[81e04e1]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       
[92c8d41]42        bu->ui_data = iu = irc_user_new( irc, nick );
[d860a8d]43        iu->bu = bu;
[81e04e1]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       
[ad404ab]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 )
[92c8d41]71                        iu->last_channel = NULL;
[ad404ab]72                else if( strcmp( s, "add_channel" ) == 0 )
[92c8d41]73                        iu->last_channel = irc->default_channel;
[ad404ab]74        }
75       
[81e04e1]76        iu->f = &irc_user_im_funcs;
77       
78        return TRUE;
79}
80
[d860a8d]81static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu )
82{
[eabc9d2]83        return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data );
[d860a8d]84}
[81e04e1]85
[d860a8d]86static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old )
87{
[231b08b]88        irc_t *irc = bee->ui_data;
[003a12b]89        irc_user_t *iu = bu->ui_data;
[231b08b]90       
[eb50495]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       
[231b08b]97        if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) )
98        {
99                if( bu->flags & BEE_USER_ONLINE )
[003a12b]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                }
[231b08b]105                else
[003a12b]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                }
[231b08b]111        }
112       
[1c8e5f7]113        /* Reset this one since the info may have changed. */
114        iu->away_reply_timeout = 0;
115       
[13c1a9f]116        bee_irc_channel_update( irc, NULL, iu );
117       
[d860a8d]118        return TRUE;
119}
[81e04e1]120
[13c1a9f]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.. */
[9052bc1]133                        if( ic->f == irc->default_channel->f &&
134                            ( ic->flags & IRC_CHANNEL_JOINED ) )
[13c1a9f]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;
[a067771]158        else if( icc->type == IRC_CC_TYPE_ACCOUNT )
159                show = iu->bu->ic->acc == icc->account;
[7a6ba50]160        else if( icc->type == IRC_CC_TYPE_PROTOCOL )
161                show = iu->bu->ic->acc->prpl == icc->protocol;
[13c1a9f]162       
163        if( !show )
164        {
[18da20b]165                irc_channel_del_user( ic, iu, FALSE, NULL );
[13c1a9f]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 );
[0e8b3e8]174                else
175                        irc_channel_user_set_mode( ic, iu, 0 );
[13c1a9f]176        }
177}
178
[f012a9f]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;
[21c87a7]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 );
[f012a9f]188       
[92c8d41]189        if( iu->last_channel )
[f012a9f]190        {
[92c8d41]191                dst = iu->last_channel->name;
192                prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts ? : "" );
[f012a9f]193        }
194        else
195        {
[92c8d41]196                dst = irc->user->nick;
197                prefix = ts;
198                ts = NULL;
[f012a9f]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 );
[21c87a7]206        g_free( ts );
[f012a9f]207       
208        return TRUE;
209}
210
[573dab0]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
[6ef9065]224static gboolean bee_irc_user_nick_hint( bee_t *bee, bee_user_t *bu, const char *hint );
225
[1d39159]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               
[6ef9065]259                bee_irc_user_nick_hint( bee, bu, name );
[1d39159]260               
261                g_free( name );
262        }
263       
264        return TRUE;
265}
266
[6ef9065]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
[7e83e8e4]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
[e4816ea]318/* IRC->IM calls */
319
[619dd18]320static gboolean bee_irc_user_privmsg_cb( gpointer data, gint fd, b_input_condition cond );
321
[e4816ea]322static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg )
323{
[1c8e5f7]324        const char *away;
325       
[619dd18]326        if( iu->bu == NULL )
[e4816ea]327                return FALSE;
[1c8e5f7]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" ) )
[619dd18]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;
[e4816ea]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
[aea8b68]410
[e4816ea]411/* IM->IRC: Groupchats */
[5a75d15]412const struct irc_channel_funcs irc_channel_im_chat_funcs;
[a87754b]413
414static gboolean bee_irc_chat_new( bee_t *bee, struct groupchat *c )
[aea8b68]415{
416        irc_t *irc = bee->ui_data;
417        irc_channel_t *ic;
418        char *topic;
[eb37735]419        GSList *l;
[aea8b68]420        int i;
421       
[eb37735]422        /* Try to find a channel that expects to receive a groupchat.
423           This flag is set by groupchat_stub_invite(). */
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 ++ )
[aea8b68]433        {
434                char name[16];
[5a75d15]435                sprintf( name, "#chat_%03d", i );
[aea8b68]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
[a87754b]453static gboolean bee_irc_chat_free( bee_t *bee, struct groupchat *c )
[aea8b68]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       
[5a75d15]460        ic->data = NULL;
[1c40aa7]461        irc_channel_del_user( ic, ic->irc->user, FALSE, "Chatroom closed by server" );
[aea8b68]462       
463        return TRUE;
464}
465
[a87754b]466static gboolean bee_irc_chat_log( bee_t *bee, struct groupchat *c, const char *text )
[aea8b68]467{
[27e2c66]468        irc_channel_t *ic = c->ui_data;
469       
470        irc_channel_printf( ic, "%s", text );
[b17ce85]471       
472        return TRUE;
[aea8b68]473}
474
[a87754b]475static gboolean bee_irc_chat_msg( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at )
[aea8b68]476{
[27e2c66]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;
[aea8b68]489}
490
[a87754b]491static gboolean bee_irc_chat_add_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
[aea8b68]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 );
[b17ce85]496       
497        return TRUE;
[aea8b68]498}
499
[a87754b]500static gboolean bee_irc_chat_remove_user( bee_t *bee, struct groupchat *c, bee_user_t *bu )
[aea8b68]501{
[b17ce85]502        irc_t *irc = bee->ui_data;
503       
[1c40aa7]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. */
[18da20b]507        irc_channel_del_user( c->ui_data, bu == bee->user ? irc->user : bu->ui_data, FALSE, NULL );
[b17ce85]508       
509        return TRUE;
[aea8b68]510}
511
[9e27f18]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
[d343eaa]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;
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';
[134a02c]541        irc_channel_name_strip( stripped );
[d343eaa]542        if( set_getbool( &bee->set, "lcnicks" ) )
543                nick_lc( stripped );
544       
[134a02c]545        full_name = g_strdup_printf( "#%s", stripped );
[d343eaa]546       
547        if( stripped[0] && irc_channel_by_name( irc, full_name ) == NULL )
548        {
549                g_free( ic->name );
550                ic->name = full_name;
551        }
552        else
553        {
554                g_free( full_name );
555        }
556       
557        return TRUE;
558}
559
[a87754b]560/* IRC->IM */
[619dd18]561static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond );
562
[a87754b]563static gboolean bee_irc_channel_chat_privmsg( irc_channel_t *ic, const char *msg )
564{
565        struct groupchat *c = ic->data;
566       
[5a75d15]567        if( c == NULL )
568                return FALSE;
[619dd18]569        else if( set_getbool( &ic->irc->b->set, "paste_buffer" ) )
570        {
571                int delay;
572               
573                if( ic->pastebuf == NULL )
574                        ic->pastebuf = g_string_new( msg );
575                else
576                {
577                        b_event_remove( ic->pastebuf_timer );
578                        g_string_append_printf( ic->pastebuf, "\n%s", msg );
579                }
580               
581                if( ( delay = set_getint( &ic->irc->b->set, "paste_buffer_delay" ) ) <= 5 )
582                        delay *= 1000;
583               
584                ic->pastebuf_timer = b_timeout_add( delay, bee_irc_channel_chat_privmsg_cb, ic );
585               
586                return TRUE;
587        }
588        else
589                bee_chat_msg( ic->irc->b, c, msg, 0 );
[a87754b]590       
591        return TRUE;
[5a75d15]592}
593
[619dd18]594static gboolean bee_irc_channel_chat_privmsg_cb( gpointer data, gint fd, b_input_condition cond )
595{
596        irc_channel_t *ic = data;
597       
598        bee_chat_msg( ic->irc->b, ic->data, ic->pastebuf->str, 0 );
599       
600        g_string_free( ic->pastebuf, TRUE );
601        ic->pastebuf = 0;
602        ic->pastebuf_timer = 0;
603       
604        return FALSE;
605}
606
[5a75d15]607static gboolean bee_irc_channel_chat_join( irc_channel_t *ic )
608{
609        char *acc_s, *room;
610        account_t *acc;
[a87754b]611       
[5a75d15]612        if( strcmp( set_getstr( &ic->set, "chat_type" ), "room" ) != 0 )
613                return TRUE;
614       
615        if( ( acc_s = set_getstr( &ic->set, "account" ) ) &&
616            ( room = set_getstr( &ic->set, "room" ) ) &&
617            ( acc = account_get( ic->irc->b, acc_s ) ) &&
618            acc->ic && acc->prpl->chat_join )
619        {
620                char *nick;
621               
622                if( !( nick = set_getstr( &ic->set, "nick" ) ) )
623                        nick = ic->irc->user->nick;
624               
625                ic->flags |= IRC_CHANNEL_CHAT_PICKME;
626                acc->prpl->chat_join( acc->ic, room, nick, NULL );
627                ic->flags &= ~IRC_CHANNEL_CHAT_PICKME;
628               
629                return FALSE;
630        }
631        else
632        {
633                irc_send_num( ic->irc, 403, "%s :Can't join channel, account offline?", ic->name );
634                return FALSE;
635        }
[a87754b]636}
637
[bfb99ee]638static gboolean bee_irc_channel_chat_part( irc_channel_t *ic, const char *msg )
639{
640        struct groupchat *c = ic->data;
641       
[5a75d15]642        if( c && c->ic->acc->prpl->chat_leave )
[bfb99ee]643                c->ic->acc->prpl->chat_leave( c );
644       
645        return TRUE;
646}
647
[9e27f18]648static gboolean bee_irc_channel_chat_topic( irc_channel_t *ic, const char *new )
649{
[4469e7e]650        struct groupchat *c = ic->data;
[5a75d15]651       
652        if( c == NULL )
653                return FALSE;
[4469e7e]654       
655        if( c->ic->acc->prpl->chat_topic == NULL )
656                irc_send_num( ic->irc, 482, "%s :IM network does not support channel topics", ic->name );
657        else
658        {
[5a75d15]659                /* TODO: Need more const goodness here, sigh */
660                char *topic = g_strdup( new );
[4469e7e]661                c->ic->acc->prpl->chat_topic( c, topic );
[5a75d15]662                g_free( topic );
[4469e7e]663                return TRUE;
664        }
665               
666        return FALSE;
[9e27f18]667}
668
[66b9e36a]669static gboolean bee_irc_channel_chat_invite( irc_channel_t *ic, irc_user_t *iu )
670{
671        struct groupchat *c = ic->data;
[5a75d15]672        bee_user_t *bu = iu->bu;
673       
674        if( bu == NULL )
675                return FALSE;
[66b9e36a]676       
[5a75d15]677        if( c )
678        {
679                if( iu->bu->ic != c->ic )
680                        irc_send_num( ic->irc, 482, "%s :Can't mix different IM networks in one groupchat", ic->name );
681                else if( c->ic->acc->prpl->chat_invite )
682                        c->ic->acc->prpl->chat_invite( c, iu->bu->handle, NULL );
683                else
684                        irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name );
685        }
686        else if( bu->ic->acc->prpl->chat_with &&
687                 strcmp( set_getstr( &ic->set, "chat_type" ), "groupchat" ) == 0 )
688        {
689                ic->flags |= IRC_CHANNEL_CHAT_PICKME;
690                iu->bu->ic->acc->prpl->chat_with( bu->ic, bu->handle );
691                ic->flags &= ~IRC_CHANNEL_CHAT_PICKME;
692        }
[66b9e36a]693        else
[5a75d15]694        {
[66b9e36a]695                irc_send_num( ic->irc, 482, "%s :IM protocol does not support room invitations", ic->name );
[5a75d15]696        }
[66b9e36a]697       
698        return TRUE;
699}
700
[5a75d15]701static char *set_eval_room_account( set_t *set, char *value );
[1c40aa7]702static char *set_eval_chat_type( set_t *set, char *value );
[5a75d15]703
704static gboolean bee_irc_channel_init( irc_channel_t *ic )
705{
706        set_add( &ic->set, "account", NULL, set_eval_room_account, ic );
[1c40aa7]707        set_add( &ic->set, "chat_type", "groupchat", set_eval_chat_type, ic );
[5a75d15]708        set_add( &ic->set, "nick", NULL, NULL, ic );
709        set_add( &ic->set, "room", NULL, NULL, ic );
710       
[1c40aa7]711        /* chat_type == groupchat */
712        ic->flags |= IRC_CHANNEL_TEMP;
713       
[5a75d15]714        return TRUE;
715}
716
717static char *set_eval_room_account( set_t *set, char *value )
718{
719        struct irc_channel *ic = set->data;
720        account_t *acc;
721       
722        if( !( acc = account_get( ic->irc->b, value ) ) )
723                return SET_INVALID;
724        else if( !acc->prpl->chat_join )
725        {
726                irc_usermsg( ic->irc, "Named chatrooms not supported on that account." );
727                return SET_INVALID;
728        }
729       
730        return g_strdup_printf( "%s(%s)", acc->prpl->name, acc->user );
731}
732
[1c40aa7]733static char *set_eval_chat_type( set_t *set, char *value )
734{
735        struct irc_channel *ic = set->data;
736       
737        if( strcmp( value, "groupchat" ) == 0 )
738                ic->flags |= IRC_CHANNEL_TEMP;
739        else if( strcmp( value, "room" ) == 0 )
740                ic->flags &= ~IRC_CHANNEL_TEMP;
741        else
742                return NULL;
743       
744        return value;
745}
746
[5a75d15]747static gboolean bee_irc_channel_free( irc_channel_t *ic )
748{
749        set_del( &ic->set, "account" );
750        set_del( &ic->set, "chat_type" );
751        set_del( &ic->set, "nick" );
752        set_del( &ic->set, "room" );
753       
[1c40aa7]754        ic->flags &= ~IRC_CHANNEL_TEMP;
755       
[5a75d15]756        return TRUE;
757}
758
759const struct irc_channel_funcs irc_channel_im_chat_funcs = {
[a87754b]760        bee_irc_channel_chat_privmsg,
[5a75d15]761        bee_irc_channel_chat_join,
[bfb99ee]762        bee_irc_channel_chat_part,
[9e27f18]763        bee_irc_channel_chat_topic,
[66b9e36a]764        bee_irc_channel_chat_invite,
[5a75d15]765
766        bee_irc_channel_init,
767        bee_irc_channel_free,
[a87754b]768};
769
[aea8b68]770
[e4816ea]771/* IM->IRC: File transfers */
[17a6ee9]772static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size )
773{
774        return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size );
775}
776
[a87754b]777static gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft )
[17a6ee9]778{
779        return dccs_recv_start( ft );
780}
781
[a87754b]782static void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft )
[17a6ee9]783{
784        return dcc_close( ft );
785}
786
[a87754b]787static void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file )
[17a6ee9]788{
789        dcc_file_transfer_t *df = file->priv;
790
791        if( file->bytes_transferred >= file->file_size )
792                dcc_finish( file );
793        else
794                df->proto_finished = TRUE;
795}
796
[d860a8d]797const struct bee_ui_funcs irc_ui_funcs = {
[81e04e1]798        bee_irc_user_new,
[d860a8d]799        bee_irc_user_free,
[1d39159]800        bee_irc_user_fullname,
[6ef9065]801        bee_irc_user_nick_hint,
[7e83e8e4]802        bee_irc_user_group,
[d860a8d]803        bee_irc_user_status,
[f012a9f]804        bee_irc_user_msg,
[573dab0]805        bee_irc_user_typing,
[17a6ee9]806       
[aea8b68]807        bee_irc_chat_new,
808        bee_irc_chat_free,
[27e2c66]809        bee_irc_chat_log,
810        bee_irc_chat_msg,
[aea8b68]811        bee_irc_chat_add_user,
[b17ce85]812        bee_irc_chat_remove_user,
[9e27f18]813        bee_irc_chat_topic,
[d343eaa]814        bee_irc_chat_name_hint,
[aea8b68]815       
[17a6ee9]816        bee_irc_ft_in_start,
817        bee_irc_ft_out_start,
818        bee_irc_ft_close,
819        bee_irc_ft_finished,
[81e04e1]820};
Note: See TracBrowser for help on using the repository browser.