source: protocols/msn/msn_util.c @ 5266354

Last change on this file since 5266354 was 6acc033, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-09T00:35:13Z

Moving MSN contacts between groups is now possible, but no support yet for
creating new groups.

  • Property mode set to 100644
File size: 9.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 - Miscellaneous utilities                                 */
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#include <ctype.h>
29
30int msn_write( struct im_connection *ic, char *s, int len )
31{
32        struct msn_data *md = ic->proto_data;
33        int st;
34       
35        st = write( md->fd, s, len );
36        if( st != len )
37        {
38                imcb_error( ic, "Short write() to main server" );
39                imc_logout( ic, TRUE );
40                return 0;
41        }
42       
43        return 1;
44}
45
46int msn_logged_in( struct im_connection *ic )
47{
48        imcb_connected( ic );
49       
50        return( 0 );
51}
52
53int msn_buddy_list_add( struct im_connection *ic, const char *list, const char *who, const char *realname_, const char *group )
54{
55        struct msn_data *md = ic->proto_data;
56        char buf[1024], *realname, groupid[8];
57       
58        realname = msn_http_encode( realname_ );
59       
60        *groupid = '\0';
61        if( group )
62        {
63                int i;
64                for( i = 0; i < md->groupcount; i ++ )
65                        if( g_strcasecmp( md->grouplist[i], group ) == 0 )
66                        {
67                                g_snprintf( groupid, sizeof( groupid ), " %d", i );
68                                break;
69                        }
70        }
71       
72        g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s%s\r\n", ++md->trId, list, who, realname, groupid );
73        if( msn_write( ic, buf, strlen( buf ) ) )
74        {
75                g_free( realname );
76               
77                return( 1 );
78        }
79       
80        g_free( realname );
81       
82        return( 0 );
83}
84
85int msn_buddy_list_remove( struct im_connection *ic, char *list, char *who )
86{
87        struct msn_data *md = ic->proto_data;
88        char buf[1024];
89       
90        g_snprintf( buf, sizeof( buf ), "REM %d %s %s\r\n", ++md->trId, list, who );
91        if( msn_write( ic, buf, strlen( buf ) ) )
92                return( 1 );
93       
94        return( 0 );
95}
96
97struct msn_buddy_ask_data
98{
99        struct im_connection *ic;
100        char *handle;
101        char *realname;
102};
103
104static void msn_buddy_ask_yes( void *data )
105{
106        struct msn_buddy_ask_data *bla = data;
107       
108        msn_buddy_list_add( bla->ic, "AL", bla->handle, bla->realname, NULL );
109       
110        imcb_ask_add( bla->ic, bla->handle, NULL );
111       
112        g_free( bla->handle );
113        g_free( bla->realname );
114        g_free( bla );
115}
116
117static void msn_buddy_ask_no( void *data )
118{
119        struct msn_buddy_ask_data *bla = data;
120       
121        msn_buddy_list_add( bla->ic, "BL", bla->handle, bla->realname, NULL );
122       
123        g_free( bla->handle );
124        g_free( bla->realname );
125        g_free( bla );
126}
127
128void msn_buddy_ask( struct im_connection *ic, char *handle, char *realname )
129{
130        struct msn_buddy_ask_data *bla = g_new0( struct msn_buddy_ask_data, 1 );
131        char buf[1024];
132       
133        bla->ic = ic;
134        bla->handle = g_strdup( handle );
135        bla->realname = g_strdup( realname );
136       
137        g_snprintf( buf, sizeof( buf ),
138                    "The user %s (%s) wants to add you to his/her buddy list.",
139                    handle, realname );
140        imcb_ask( ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no );
141}
142
143char *msn_findheader( char *text, char *header, int len )
144{
145        int hlen = strlen( header ), i;
146        char *ret;
147       
148        if( len == 0 )
149                len = strlen( text );
150       
151        i = 0;
152        while( ( i + hlen ) < len )
153        {
154                /* Maybe this is a bit over-commented, but I just hate this part... */
155                if( g_strncasecmp( text + i, header, hlen ) == 0 )
156                {
157                        /* Skip to the (probable) end of the header */
158                        i += hlen;
159                       
160                        /* Find the first non-[: \t] character */
161                        while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++;
162                       
163                        /* Make sure we're still inside the string */
164                        if( i >= len ) return( NULL );
165                       
166                        /* Save the position */
167                        ret = text + i;
168                       
169                        /* Search for the end of this line */
170                        while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++;
171                       
172                        /* Make sure we're still inside the string */
173                        if( i >= len ) return( NULL );
174                       
175                        /* Copy the found data */
176                        return( g_strndup( ret, text + i - ret ) );
177                }
178               
179                /* This wasn't the header we were looking for, skip to the next line. */
180                while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++;
181                while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++;
182               
183                /* End of headers? */
184                if( ( i >= 4 && strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 ) ||
185                    ( i >= 2 && ( strncmp( text + i - 2, "\n\n", 2 ) == 0 ||   
186                                  strncmp( text + i - 2, "\r\r", 2 ) == 0 ) ) )
187                {
188                        break;
189                }
190        }
191       
192        return( NULL );
193}
194
195/* *NOT* thread-safe, but that's not a problem for now... */
196char **msn_linesplit( char *line )
197{
198        static char **ret = NULL;
199        static int size = 3;
200        int i, n = 0;
201       
202        if( ret == NULL )
203                ret = g_new0( char*, size );
204       
205        for( i = 0; line[i] && line[i] == ' '; i ++ );
206        if( line[i] )
207        {
208                ret[n++] = line + i;
209                for( i ++; line[i]; i ++ )
210                {
211                        if( line[i] == ' ' )
212                                line[i] = 0;
213                        else if( line[i] != ' ' && !line[i-1] )
214                                ret[n++] = line + i;
215                       
216                        if( n >= size )
217                                ret = g_renew( char*, ret, size += 2 );
218                }
219        }
220        ret[n] = NULL;
221       
222        return( ret );
223}
224
225/* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give
226   commands, but sometimes they give additional data (payload). This function tries to handle
227   this all in a nice way and send all data to the right places. */
228
229/* Return values: -1: Read error, abort connection.
230                   0: Command reported error; Abort *immediately*. (The connection does not exist anymore)
231                   1: OK */
232
233int msn_handler( struct msn_handler_data *h )
234{
235        int st;
236       
237        h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 );
238        st = read( h->fd, h->rxq + h->rxlen, 1024 );
239        h->rxlen += st;
240       
241        if( st <= 0 )
242                return( -1 );
243       
244        while( st )
245        {
246                int i;
247               
248                if( h->msglen == 0 )
249                {
250                        for( i = 0; i < h->rxlen; i ++ )
251                        {
252                                if( h->rxq[i] == '\r' || h->rxq[i] == '\n' )
253                                {
254                                        char *cmd_text, **cmd;
255                                        int count;
256                                       
257                                        cmd_text = g_strndup( h->rxq, i );
258                                        cmd = msn_linesplit( cmd_text );
259                                        for( count = 0; cmd[count]; count ++ );
260                                        st = h->exec_command( h->data, cmd, count );
261                                        g_free( cmd_text );
262                                       
263                                        /* If the connection broke, don't continue. We don't even exist anymore. */
264                                        if( !st )
265                                                return( 0 );
266                                       
267                                        if( h->msglen )
268                                                h->cmd_text = g_strndup( h->rxq, i );
269                                       
270                                        /* Skip to the next non-emptyline */
271                                        while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++;
272                                       
273                                        break;
274                                }
275                        }
276                       
277                        /* If we reached the end of the buffer, there's still an incomplete command there.
278                           Return and wait for more data. */
279                        if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' )
280                                break;
281                }
282                else
283                {
284                        char *msg, **cmd;
285                        int count;
286                       
287                        /* Do we have the complete message already? */
288                        if( h->msglen > h->rxlen )
289                                break;
290                       
291                        msg = g_strndup( h->rxq, h->msglen );
292                        cmd = msn_linesplit( h->cmd_text );
293                        for( count = 0; cmd[count]; count ++ );
294                       
295                        st = h->exec_message( h->data, msg, h->msglen, cmd, count );
296                        g_free( msg );
297                        g_free( h->cmd_text );
298                        h->cmd_text = NULL;
299                       
300                        if( !st )
301                                return( 0 );
302                       
303                        i = h->msglen;
304                        h->msglen = 0;
305                }
306               
307                /* More data after this block? */
308                if( i < h->rxlen )
309                {
310                        char *tmp;
311                       
312                        tmp = g_memdup( h->rxq + i, h->rxlen - i );
313                        g_free( h->rxq );
314                        h->rxq = tmp;
315                        h->rxlen -= i;
316                        i = 0;
317                }
318                else
319                /* If not, reset the rx queue and get lost. */
320                {
321                        g_free( h->rxq );
322                        h->rxq = g_new0( char, 1 );
323                        h->rxlen = 0;
324                        return( 1 );
325                }
326        }
327       
328        return( 1 );
329}
330
331/* The difference between this function and the normal http_encode() function
332   is that this one escapes every 7-bit ASCII character because this is said
333   to avoid some lame server-side checks when setting a real-name. Also,
334   non-ASCII characters are not escaped because MSN servers don't seem to
335   appreciate that! */
336char *msn_http_encode( const char *input )
337{
338        char *ret, *s;
339        int i;
340       
341        ret = s = g_new0( char, strlen( input ) * 3 + 1 );
342        for( i = 0; input[i]; i ++ )
343                if( input[i] & 128 )
344                {
345                        *s = input[i];
346                        s ++;
347                }
348                else
349                {
350                        g_snprintf( s, 4, "%%%02X", input[i] );
351                        s += 3;
352                }
353       
354        return ret;
355}
356
357void msn_msgq_purge( struct im_connection *ic, GSList **list )
358{
359        struct msn_message *m;
360        GString *ret;
361        GSList *l;
362       
363        l = *list;
364        if( l == NULL )
365                return;
366       
367        m = l->data;
368        ret = g_string_sized_new( 1024 );
369        g_string_printf( ret, "Warning: Cleaning up MSN (switchboard) connection with unsent "
370                              "messages to %s:", m->who ? m->who : "unknown recipient" );
371       
372        while( l )
373        {
374                m = l->data;
375               
376                g_string_append_printf( ret, "\n%s", m->text );
377               
378                g_free( m->who );
379                g_free( m->text );
380                g_free( m );
381               
382                l = l->next;
383        }
384        g_slist_free( *list );
385        *list = NULL;
386       
387        imcb_log( ic, "%s", ret->str );
388        g_string_free( ret, TRUE );
389}
390
391gboolean msn_set_display_name( struct im_connection *ic, const char *rawname )
392{
393        char *fn = msn_http_encode( rawname );
394        struct msn_data *md = ic->proto_data;
395        char buf[1024];
396       
397        g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, ic->acc->user, fn );
398        g_free( fn );
399       
400        return msn_write( ic, buf, strlen( buf ) ) != 0;
401}
Note: See TracBrowser for help on using the repository browser.