source: irc_im.c @ d33679e

Last change on this file since d33679e was 21c87a7, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-14T09:27:50Z

Merging loads of stuff from mainline.

  • Property mode set to 100644
File size: 6.9 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 */
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        iu->f = &irc_user_im_funcs;
69        //iu->last_typing_notice = 0;
70       
71        return TRUE;
72}
73
74static gboolean bee_irc_user_free( bee_t *bee, bee_user_t *bu )
75{
76        return irc_user_free( bee->ui_data, (irc_user_t *) bu->ui_data );
77}
78
79static gboolean bee_irc_user_status( bee_t *bee, bee_user_t *bu, bee_user_t *old )
80{
81        irc_t *irc = bee->ui_data;
82        irc_channel_t *ic = irc->channels->data; /* For now, just pick the first channel. */
83       
84        if( ( bu->flags & BEE_USER_ONLINE ) != ( old->flags & BEE_USER_ONLINE ) )
85        {
86                if( bu->flags & BEE_USER_ONLINE )
87                        irc_channel_add_user( ic, (irc_user_t*) bu->ui_data );
88                else
89                        irc_channel_del_user( ic, (irc_user_t*) bu->ui_data );
90        }
91       
92        return TRUE;
93}
94
95static gboolean bee_irc_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at )
96{
97        irc_t *irc = bee->ui_data;
98        irc_channel_t *ic = irc->channels->data;
99        irc_user_t *iu = (irc_user_t *) bu->ui_data;
100        char *dst, *prefix = NULL;
101        char *wrapped, *ts = NULL;
102       
103        if( sent_at > 0 && set_getbool( &irc->b->set, "display_timestamps" ) )
104                ts = irc_format_timestamp( irc, sent_at );
105       
106        if( iu->flags & IRC_USER_PRIVATE )
107        {
108                dst = irc->user->nick;
109                prefix = ts;
110                ts = NULL;
111        }
112        else
113        {
114                dst = ic->name;
115                prefix = g_strdup_printf( "%s%s%s", irc->user->nick, set_getstr( &bee->set, "to_char" ), ts );
116        }
117       
118        wrapped = word_wrap( msg, 425 );
119        irc_send_msg( iu, "PRIVMSG", dst, wrapped, prefix );
120       
121        g_free( wrapped );
122        g_free( prefix );
123        g_free( ts );
124       
125        return TRUE;
126}
127
128static gboolean bee_irc_user_typing( bee_t *bee, bee_user_t *bu, uint32_t flags )
129{
130        irc_t *irc = (irc_t *) bee->ui_data;
131       
132        if( set_getbool( &bee->set, "typing_notice" ) )
133                irc_send_msg_f( (irc_user_t *) bu->ui_data, "PRIVMSG", irc->user->nick,
134                                "\001TYPING %d\001", ( flags >> 8 ) & 3 );
135        else
136                return FALSE;
137       
138        return TRUE;
139}
140
141static gboolean bee_irc_user_fullname( bee_t *bee, bee_user_t *bu )
142{
143        irc_user_t *iu = (irc_user_t *) bu->ui_data;
144        irc_t *irc = (irc_t *) bee->ui_data;
145        char *s;
146       
147        if( iu->fullname != iu->nick )
148                g_free( iu->fullname );
149        iu->fullname = g_strdup( bu->fullname );
150       
151        /* Strip newlines (unlikely, but IRC-unfriendly so they must go)
152           TODO(wilmer): Do the same with away msgs again! */
153        for( s = iu->fullname; *s; s ++ )
154                if( isspace( *s ) ) *s = ' ';
155       
156        if( ( bu->ic->flags & OPT_LOGGED_IN ) && set_getbool( &bee->set, "display_namechanges" ) )
157        {
158                char *msg = g_strdup_printf( "<< \002BitlBee\002 - Changed name to `%s' >>", iu->fullname );
159                irc_send_msg( iu, "NOTICE", irc->user->nick, msg, NULL );
160        }
161       
162        s = set_getstr( &bu->ic->acc->set, "nick_source" );
163        if( strcmp( s, "handle" ) != 0 )
164        {
165                char *name = g_strdup( bu->fullname );
166               
167                if( strcmp( s, "first_name" ) == 0 )
168                {
169                        int i;
170                        for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {}
171                        name[i] = '\0';
172                }
173               
174                imcb_buddy_nick_hint( bu->ic, bu->handle, name );
175               
176                g_free( name );
177        }
178       
179        return TRUE;
180}
181
182/* File transfers */
183static file_transfer_t *bee_irc_ft_in_start( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size )
184{
185        return dccs_send_start( bu->ic, (irc_user_t *) bu->ui_data, file_name, file_size );
186}
187
188gboolean bee_irc_ft_out_start( struct im_connection *ic, file_transfer_t *ft )
189{
190        return dccs_recv_start( ft );
191}
192
193void bee_irc_ft_close( struct im_connection *ic, file_transfer_t *ft )
194{
195        return dcc_close( ft );
196}
197
198void bee_irc_ft_finished( struct im_connection *ic, file_transfer_t *file )
199{
200        dcc_file_transfer_t *df = file->priv;
201
202        if( file->bytes_transferred >= file->file_size )
203                dcc_finish( file );
204        else
205                df->proto_finished = TRUE;
206}
207
208const struct bee_ui_funcs irc_ui_funcs = {
209        bee_irc_user_new,
210        bee_irc_user_free,
211        bee_irc_user_fullname,
212        bee_irc_user_status,
213        bee_irc_user_msg,
214        bee_irc_user_typing,
215       
216        bee_irc_ft_in_start,
217        bee_irc_ft_out_start,
218        bee_irc_ft_close,
219        bee_irc_ft_finished,
220};
221
222
223/* IRC->IM calls */
224
225static gboolean bee_irc_user_privmsg( irc_user_t *iu, const char *msg )
226{
227        if( iu->bu )
228                return bee_user_msg( iu->irc->b, iu->bu, msg, 0 );
229        else
230                return FALSE;
231}
232
233static gboolean bee_irc_user_ctcp( irc_user_t *iu, char *const *ctcp )
234{
235        if( ctcp[1] && g_strcasecmp( ctcp[0], "DCC" ) == 0
236                    && g_strcasecmp( ctcp[1], "SEND" ) == 0 )
237        {
238                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->transfer_request )
239                {
240                        file_transfer_t *ft = dcc_request( iu->bu->ic, ctcp );
241                        if ( ft )
242                                iu->bu->ic->acc->prpl->transfer_request( iu->bu->ic, ft, iu->bu->handle );
243                       
244                        return TRUE;
245                }
246        }
247        else if( g_strcasecmp( ctcp[0], "TYPING" ) == 0 )
248        {
249                if( iu->bu && iu->bu->ic && iu->bu->ic->acc->prpl->send_typing && ctcp[1] )
250                {
251                        int st = ctcp[1][0];
252                        if( st >= '0' && st <= '2' )
253                        {
254                                st <<= 8;
255                                iu->bu->ic->acc->prpl->send_typing( iu->bu->ic, iu->bu->handle, st );
256                        }
257                       
258                        return TRUE;
259                }
260        }
261       
262        return FALSE;
263}
264
265static const struct irc_user_funcs irc_user_im_funcs = {
266        bee_irc_user_privmsg,
267        bee_irc_user_ctcp,
268};
Note: See TracBrowser for help on using the repository browser.