source: protocols/msn/msn.c @ 4e4af1b

Last change on this file since 4e4af1b was 4e4af1b, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-14T08:48:46Z

Remove some old Passport stuff, this is all in soap.[ch] now.

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