source: irc_im.c @ 6a9d068

Last change on this file since 6a9d068 was 6a9d068, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-05-03T00:39:39Z

Restore away_devoice.

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