source: protocols/msn/msn.c @ c6ca3ee

Last change on this file since c6ca3ee was c6ca3ee, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-14T17:49:24Z

Some const/etc cleanups submitted by domen@… back in bug #431.

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