source: protocols/msn/msn_util.c @ 70ac477

Last change on this file since 70ac477 was 70ac477, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-11T11:34:39Z

Create new MSN groups when necessary.

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