source: protocols/msn/msn.c @ 193dc74

Last change on this file since 193dc74 was 193dc74, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-14T16:16:52Z

Responses to add requests work now.

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