File: parse_utils.c

package info (click to toggle)
everybuddy 0.2.1beta6-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,936 kB
  • ctags: 5,627
  • sloc: ansic: 50,783; sh: 8,559; yacc: 191; makefile: 182; perl: 97; lex: 50
file content (371 lines) | stat: -rw-r--r-- 8,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#define _REENTRANT
/*
 * libmsn
 *
 * Copyright (C) 1999, Shane P. Brady <shane@jxie.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include "libmsn.h"
#include "parse_utils.h"
#include "msn_commands.h"

extern char CommandString[NUM_OF_COMMANDS][COMMAND_LENGTH];

/* DecodeMime auxiliary funcs */
static void base64_decode(char **dest, char **src);
static void quoted_printable_decode(char **dest, char **src);
#ifndef HAVE_STRSEP
char *strsep (char **stringp, const char *delim);
#endif

/*
** Name:    ParseHostPort
** Purpose: This function parses out the host and port for a switchboard
**          server
** Input:   address - host and port
**          host    - host
**          port    - port number
** Output:  0 on success, -1 on failure
*/

int ParseHostPort(char *address, char **host, int *port)
{
    *host = strsep(&address, ":");
    if (*host == NULL)
        return -1;
    if (address == NULL)
        *port = 1863;
    else
        *port = atoi(address);

    return 0;
}

/*
** Name:    ParseMimeHeaders
** Purpose: This function returns the mime parts out of the message
** Input:   message - the whole message
**          mimeInfo    - mime info
**          im          - instant message type
** Output:  0 on success, -1 on failure
*/

int ParseMimeHeaders(char *message, char **mimeInfo, char **im)
{
    char *place;
    
    *mimeInfo = strstr(message, "MIME-Version: ");
    place = strstr(message, "\r\n\r\n");
    if (place == NULL)
        return -1;

    *place = '\0';
    *im = place + 4;
    if (strcasecmp(*im, "\r\n") == 0)
        *im += 2;
    return 0;
}

/*
** Name:    DecodeMime
** Purpose: decode a mime encoded message. the original string
**          IS MODIFIED
** Input:   message
** Output:  decoded message
*/
void DecodeMime(char *s)
{
	char *buf = NULL;	
	
    if(!s) return;
	
	buf = s;	
	
	while(*buf) {		
		if ((*buf == '=') && (*++buf == '?')) {
			char tmp;
			while(*++buf != '?');
			buf++;
			tmp = *buf; buf += 2;
			if ((tmp == 'Q') || (tmp == 'q')) quoted_printable_decode(&s, &buf);
			else if ((tmp == 'B') || (tmp == 'b')) base64_decode(&s, &buf);
		}
		else {*s++ = *buf++;}
    }
    *s=0;
}

/*
** Name:    ParseArguments
** Purpose: This function parses a line of input, stores the arguments in an
**          array, and returns the count
** Input:   string    -  string to be parsed
**          delimiter - string delimiter
**          args      - argument array
**          numOfArgs - number of arguments
** Ouput:   0 if arguments are parsed, -1 if not
*/

int ParseArguments(char *string, char *delimiter, char ***args, int *numOfArgs)
{

    char *place,*temp;

    if ((string == NULL) || (delimiter == NULL))
        return -1;

    *args = NULL;
    *numOfArgs = 0;

#ifdef HAVE_STRTOK_R
    temp = strtok_r(string, delimiter, &place);
#else
#warning You do not have a re-entrant strtok_r - MSN MAY CRASH if run multithreaded
    temp = strtok(string, delimiter);
#endif

    while (temp) {
        *args = (char **)realloc(*args, sizeof(char *) * (*numOfArgs+1));
        (*args)[*numOfArgs] = strdup(temp);
        *numOfArgs += 1;
#ifdef HAVE_STRTOK_R
        temp = strtok_r(place, delimiter, &place);  
#else
        temp = strtok(NULL, delimiter);  
#endif
    } 
    return 0;

} 

/*
** Name:    DestroyArguments
** Purpose: This function destroys a set of arguments
** Input:   args      - arguments to be destroyed
**          numOfArgs - number of arguments
** Output:  0 on success, -1 on failure
*/

int DestroyArguments(char ***args, int numOfArgs)
{
    int count = 0;

    if ((args == NULL) || (!numOfArgs))
        return -1;

    while (count < numOfArgs) {
        free((*args)[count++]);
    }
    free(*args);
    (*args) = NULL;
    return 0;
}

/*
** Name:    AddHotmail
** Purpose: This function adds @hotmail.com to a username that needs it
**          If @hotmail.com is already present, string is not modified
** Input:   src - string to look at
**          dst - destination string
** Output:  0 on success, -1 on failure
*/

int AddHotmail(const char *src, char **dst)
{
    *dst = NULL;
    if (src == NULL)
        return -1;

	if(strchr(src, '@') == NULL) {
        *dst = (char *)malloc(strlen(src) + strlen("@hotmail.com") + 1);  
        sprintf(*dst, "%s@hotmail.com", src);
    }
    else
        *dst = strdup(src); 

    return 0;
}

/*
** Name:    RemoveHotmail
** Purpose: This function removes @hotmail.com from any string
**          If @hotmail.com is already missing, string is not modifed
** Input:   src - string to look at
**          dst - destination string
** Output:  0 on success, -1 on failure
*/

int RemoveHotmail(const char *src, char **dst)
{
    char *temp;
    char *ptr;
    *dst = NULL;

    if (src == NULL)
        return -1;

    temp = strdup(src);
    ptr = strstr(temp, "@hotmail.com");
    if (ptr == NULL) 
        *dst = strdup(src);
    else {
        *ptr = '\0';
        *dst = strdup(temp);
    }

    free(temp);
    return 0;
}

static void
base64_decode(char **dest, char **src)
{
	static unsigned char tab[256];
	static unsigned char initialized = 0;
	char *buf = *src, *s = *dest;
	unsigned int pr[6];
	register int n;
	register int doit=1;

	if (!initialized) {
		int i, n = 0;
		initialized++;
		memset(tab, 100, 256);
		tab[0] = tab[255] = 255;
		for (i = 'A'; i <= 'Z'; i++) tab[i] = n++;
		for (i = 'a'; i <= 'z'; i++) tab[i] = n++;
		for (i = '0'; i <= '9'; i++) tab[i] = n++;
		tab['+'] = n++;
		tab['/'] = n++;
		tab['='] = 255;
	}
		
	while(doit) {
		n=0;
		while(n < 4) {
			register unsigned char ch;
			ch=tab[*(unsigned char *)buf];
			if(ch < 100) { pr[n]=ch; n++; buf++; continue; }
			if(ch == 100) { buf++; continue; }
			while (*buf == '=') buf++;
			doit = 0; break;
		}
		if(!doit && n < 4) {pr[n+2] = pr[n+1] = pr[n] = 0;}

		*(char *)s = (pr[0] << 2) | (pr[1] >> 4);
		*(char *)(s+1) = (pr[1] << 4) | (pr[2] >> 2);
		*(char *)(s+2) = (pr[2] << 6) | pr[3];

		s+=(n * 3) >> 2 ;
	}
	if ((*buf == '?') && *(buf+1) == '=') buf++;
	*src = buf;
	*dest = s;
}


static void
quoted_printable_decode(char **dest, char **src)
{
	char *buf = *src;
	char *s = *dest;
	register int n = 0;

	buf--;
	while(*++buf) {
		if(*buf == '_') {
			*s++ = ' ';
			continue;
		};

		if (*buf == '?') {
			buf++;
			if (!*buf) {*src = --buf; break;}
			else if(*buf == '=') {*src = ++buf; break;}
		}
		
        if(*buf == '=') {						
            n = *++buf;
            if(!n) {buf--; break; }
			if(!*(buf+1)) break;

			if((n >= '0') && (n <= '9')) *s = (n - '0');
			else if(((n >= 'A') && (n <= 'F')) || ((n >= 'a') && (n <= 'f')) )
				*s = 10 + n - ((n>='a')?'a':'A');
			else {
				if(n == '\n' || n == '\r') continue;
				*s++ = '='; *s++ = n; continue;
			};

			*s <<= 4;
			n = *++buf;

			if((n >= '0') && (n <= '9')) *s += (n - '0');
			else if(((n >= 'A') && (n <= 'F')) || ((n >= 'a') && (n <= 'f')))
				*s += 10 + n - ((n>='a')?'a':'A');
			else {
				if(n == '\n' || n == '\r') { *s++ = n; continue; };
				*s = ' '; continue;
			};

			s++;
			continue;
        };		
		*s++ = *buf;
    }
    *dest = s;
}


/**
 * We don't have strsep() on DEC OSF or SunOS (maybe not on any
 * non-Linux system -- ? ).  So we'll take it from glibc-2.0.6.
 * 
 * David Sweet <dsweet@chaos.umd.edu>
 **/

#ifndef HAVE_STRSEP

char *strsep (char **stringp, const char *delim) {

  char *begin, *end;

  begin = *stringp;
  if (! begin || *begin == '\0')
    return NULL;

  /* Find the end of the token.  */
  end = strpbrk (begin, delim);
  if (end)
    {
      /* Terminate the token and set *STRINGP past NUL character.  */
      *end++ = '\0';
      *stringp = end;
    }
  else
    /* No more delimiters; this is the last token.  */
    *stringp = 0;

  return begin;
}                      
#endif