source: protocols/msn/msn.c @ b4e4b95

Last change on this file since b4e4b95 was 5a5c926, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-12T06:58:20Z

Fixed a (very rare, AFAIK) NULL-pointer dereference.

  • Property mode set to 100644
File size: 10.3 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* MSN module - Main file; functions to be called from BitlBee          */
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 "nogaim.h"
27#include "msn.h"
28
29static void msn_login( struct aim_user *acct )
30{
31        struct gaim_connection *gc = new_gaim_conn( acct );
32        struct msn_data *md = g_new0( struct msn_data, 1 );
33       
34        set_login_progress( gc, 1, "Connecting" );
35       
36        gc->proto_data = md;
37        md->fd = -1;
38       
39        if( strchr( acct->username, '@' ) == NULL )
40        {
41                hide_login_progress( gc, "Invalid account name" );
42                signoff( gc );
43                return;
44        }
45       
46        md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, gc );
47        if( md->fd < 0 )
48        {
49                hide_login_progress( gc, "Could not connect to server" );
50                signoff( gc );
51        }
52        else
53        {
54                md->gc = gc;
55                md->away_state = msn_away_state_list;
56               
57                msn_connections = g_slist_append( msn_connections, gc );
58        }
59}
60
61static void msn_close( struct gaim_connection *gc )
62{
63        struct msn_data *md = gc->proto_data;
64        GSList *l;
65       
66        if( md )
67        {
68                if( md->fd >= 0 )
69                        closesocket( md->fd );
70               
71                if( md->handler )
72                {
73                        if( md->handler->rxq ) g_free( md->handler->rxq );
74                        if( md->handler->cmd_text ) g_free( md->handler->cmd_text );
75                        g_free( md->handler );
76                }
77               
78                while( md->switchboards )
79                        msn_sb_destroy( md->switchboards->data );
80               
81                if( md->msgq )
82                {
83                        struct msn_message *m;
84                       
85                        for( l = md->msgq; l; l = l->next )
86                        {
87                                m = l->data;
88                       
89                                serv_got_crap( gc, "Warning: Closing down MSN connection with unsent message to %s, you'll have to resend it.", m->who );
90                                g_free( m->who );
91                                g_free( m->text );
92                                g_free( m );
93                        }
94                        g_slist_free( md->msgq );
95                }
96               
97                g_free( md );
98        }
99       
100        for( l = gc->permit; l; l = l->next )
101                g_free( l->data );
102        g_slist_free( gc->permit );
103       
104        for( l = gc->deny; l; l = l->next )
105                g_free( l->data );
106        g_slist_free( gc->deny );
107       
108        msn_connections = g_slist_remove( msn_connections, gc );
109}
110
111static int msn_send_im( struct gaim_connection *gc, char *who, char *message, int len, int away )
112{
113        struct msn_switchboard *sb;
114        struct msn_data *md = gc->proto_data;
115       
116        if( ( sb = msn_sb_by_handle( gc, who ) ) )
117        {
118                return( msn_sb_sendmessage( sb, message ) );
119        }
120        else
121        {
122                struct msn_message *m;
123                char buf[1024];
124               
125                /* Create a message. We have to arrange a usable switchboard, and send the message later. */
126                m = g_new0( struct msn_message, 1 );
127                m->who = g_strdup( who );
128                m->text = g_strdup( message );
129               
130                /* FIXME: *CHECK* the reliability of using spare sb's! */
131                if( ( sb = msn_sb_spare( gc ) ) )
132                {
133                        debug( "Trying to use a spare switchboard to message %s", who );
134                       
135                        sb->who = g_strdup( who );
136                        g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who );
137                        if( msn_sb_write( sb, buf, strlen( buf ) ) )
138                        {
139                                /* He/She should join the switchboard soon, let's queue the message. */
140                                sb->msgq = g_slist_append( sb->msgq, m );
141                                return( 1 );
142                        }
143                }
144               
145                debug( "Creating a new switchboard to message %s", who );
146               
147                /* If we reach this line, there was no spare switchboard, so let's make one. */
148                g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId );
149                if( !msn_write( gc, buf, strlen( buf ) ) )
150                {
151                        g_free( m->who );
152                        g_free( m->text );
153                        g_free( m );
154                       
155                        return( 0 );
156                }
157               
158                /* And queue the message to md. We'll pick it up when the switchboard comes up. */
159                md->msgq = g_slist_append( md->msgq, m );
160               
161                /* FIXME: If the switchboard creation fails, the message will not be sent. */
162               
163                return( 1 );
164        }
165       
166        return( 0 );
167}
168
169static GList *msn_away_states( struct gaim_connection *gc )
170{
171        GList *l = NULL;
172        int i;
173       
174        for( i = 0; msn_away_state_list[i].number > -1; i ++ )
175                l = g_list_append( l, (void*) msn_away_state_list[i].name );
176       
177        return( l );
178}
179
180static char *msn_get_status_string( struct gaim_connection *gc, int number )
181{
182        const struct msn_away_state *st = msn_away_state_by_number( number );
183       
184        if( st )
185                return( (char*) st->name );
186        else
187                return( "" );
188}
189
190static void msn_set_away( struct gaim_connection *gc, char *state, char *message )
191{
192        char buf[1024];
193        struct msn_data *md = gc->proto_data;
194        const struct msn_away_state *st;
195       
196        if( strcmp( state, GAIM_AWAY_CUSTOM ) == 0 )
197                st = msn_away_state_by_name( "Away" );
198        else
199                st = msn_away_state_by_name( state );
200       
201        if( !st ) st = msn_away_state_list;
202        md->away_state = st;
203       
204        g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, st->code );
205        msn_write( gc, buf, strlen( buf ) );
206}
207
208static void msn_set_info( struct gaim_connection *gc, char *info )
209{
210        int i;
211        char buf[1024], *fn, *s;
212        struct msn_data *md = gc->proto_data;
213       
214        if( strlen( info ) > 129 )
215        {
216                do_error_dialog( gc, "Maximum name length exceeded", "MSN" );
217                return;
218        }
219       
220        /* Of course we could use http_encode() here, but when we encode
221           every character, the server is less likely to complain about the
222           chosen name. However, the MSN server doesn't seem to like escaped
223           non-ASCII chars, so we keep those unescaped. */
224        s = fn = g_new0( char, strlen( info ) * 3 + 1 );
225        for( i = 0; info[i]; i ++ )
226                if( info[i] & 128 )
227                {
228                        *s = info[i];
229                        s ++;
230                }
231                else
232                {
233                        g_snprintf( s, 4, "%%%02X", info[i] );
234                        s += 3;
235                }
236       
237        g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, gc->username, fn );
238        msn_write( gc, buf, strlen( buf ) );
239        g_free( fn );
240}
241
242static void msn_get_info(struct gaim_connection *gc, char *who) 
243{
244        /* Just make an URL and let the user fetch the info */
245        serv_got_crap( gc, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who );
246}
247
248static void msn_add_buddy( struct gaim_connection *gc, char *who )
249{
250        msn_buddy_list_add( gc, "FL", who, who );
251}
252
253static void msn_remove_buddy( struct gaim_connection *gc, char *who, char *group )
254{
255        msn_buddy_list_remove( gc, "FL", who );
256}
257
258static int msn_chat_send( struct gaim_connection *gc, int id, char *message )
259{
260        struct msn_switchboard *sb = msn_sb_by_id( gc, id );
261       
262        if( sb )
263                return( msn_sb_sendmessage( sb, message ) );
264        else
265                return( 0 );
266}
267
268static void msn_chat_invite( struct gaim_connection *gc, int id, char *msg, char *who )
269{
270        struct msn_switchboard *sb = msn_sb_by_id( gc, id );
271        char buf[1024];
272       
273        if( sb )
274        {
275                g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who );
276                msn_sb_write( sb, buf, strlen( buf ) );
277        }
278}
279
280static void msn_chat_leave( struct gaim_connection *gc, int id )
281{
282        struct msn_switchboard *sb = msn_sb_by_id( gc, id );
283       
284        if( sb )
285                msn_sb_write( sb, "OUT\r\n", 5 );
286}
287
288static int msn_chat_open( struct gaim_connection *gc, char *who )
289{
290        struct msn_switchboard *sb;
291        struct msn_data *md = gc->proto_data;
292        char buf[1024];
293       
294        if( ( sb = msn_sb_by_handle( gc, who ) ) )
295        {
296                debug( "Converting existing switchboard to %s to a groupchat", who );
297                msn_sb_to_chat( sb );
298                return( 1 );
299        }
300        else
301        {
302                struct msn_message *m;
303               
304                if( ( sb = msn_sb_spare( gc ) ) )
305                {
306                        debug( "Trying to reuse an existing switchboard as a groupchat with %s", who );
307                        g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who );
308                        if( msn_sb_write( sb, buf, strlen( buf ) ) )
309                        {
310                                msn_sb_to_chat( sb );
311                                return( 1 );
312                        }
313                }
314               
315                /* If the stuff above failed for some reason: */
316                debug( "Creating a new switchboard to groupchat with %s", who );
317               
318                /* Request a new switchboard. */
319                g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId );
320                if( !msn_write( gc, buf, strlen( buf ) ) )
321                        return( 0 );
322               
323                /* Create a magic message. This is quite hackish, but who cares? :-P */
324                m = g_new0( struct msn_message, 1 );
325                m->who = g_strdup( who );
326                m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE );
327               
328                /* Queue the magic message and cross your fingers. */
329                md->msgq = g_slist_append( md->msgq, m );
330               
331                return( 1 );
332        }
333       
334        return( 0 );
335}
336
337static void msn_keepalive( struct gaim_connection *gc )
338{
339        msn_write( gc, "PNG\r\n", strlen( "PNG\r\n" ) );
340}
341
342static void msn_add_permit( struct gaim_connection *gc, char *who )
343{
344        msn_buddy_list_add( gc, "AL", who, who );
345}
346
347static void msn_rem_permit( struct gaim_connection *gc, char *who )
348{
349        msn_buddy_list_remove( gc, "AL", who );
350}
351
352static void msn_add_deny( struct gaim_connection *gc, char *who )
353{
354        struct msn_switchboard *sb;
355       
356        msn_buddy_list_add( gc, "BL", who, who );
357       
358        /* If there's still a conversation with this person, close it. */
359        if( ( sb = msn_sb_by_handle( gc, who ) ) )
360        {
361                msn_sb_destroy( sb );
362        }
363}
364
365static void msn_rem_deny( struct gaim_connection *gc, char *who )
366{
367        msn_buddy_list_remove( gc, "BL", who );
368}
369
370static int msn_send_typing( struct gaim_connection *gc, char *who, int typing )
371{
372        if( typing )
373                return( msn_send_im( gc, who, TYPING_NOTIFICATION_MESSAGE, strlen( TYPING_NOTIFICATION_MESSAGE ), 0 ) );
374        else
375                return( 1 );
376}
377
378void msn_init()
379{
380        struct prpl *ret = g_new0(struct prpl, 1);
381        ret->name = "msn";
382        ret->login = msn_login;
383        ret->close = msn_close;
384        ret->send_im = msn_send_im;
385        ret->away_states = msn_away_states;
386        ret->get_status_string = msn_get_status_string;
387        ret->set_away = msn_set_away;
388        ret->set_info = msn_set_info;
389        ret->get_info = msn_get_info;
390        ret->add_buddy = msn_add_buddy;
391        ret->remove_buddy = msn_remove_buddy;
392        ret->chat_send = msn_chat_send;
393        ret->chat_invite = msn_chat_invite;
394        ret->chat_leave = msn_chat_leave;
395        ret->chat_open = msn_chat_open;
396        ret->keepalive = msn_keepalive;
397        ret->add_permit = msn_add_permit;
398        ret->rem_permit = msn_rem_permit;
399        ret->add_deny = msn_add_deny;
400        ret->rem_deny = msn_rem_deny;
401        ret->send_typing = msn_send_typing;
402        ret->cmp_buddynames = g_strcasecmp;
403
404        register_protocol(ret);
405}
Note: See TracBrowser for help on using the repository browser.