source: protocols/msn/msn.c @ 64768d4

Last change on this file since 64768d4 was 64768d4, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-09-02T09:15:44Z

Replace msn*write functions with saner versions that accept format strings.
Also preparing for additional temporary NS connections (auth token renewal).

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