source: protocols/msn/msn.c @ 1febf5c

Last change on this file since 1febf5c was 1febf5c, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-01-05T21:15:32Z

Added "mail_notifications" setting. Who needs those notifications anyway?
Closes: #338.

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