]> code.citadel.org Git - citadel.git/blob - citadel/tools.c
* Allow incoming SMTP to relay to other Citadel nodes for whom we are
[citadel.git] / citadel / tools.c
1 /*
2  * $Id$
3  *
4  * Utility functions that are used by both the client and server.
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <string.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include "tools.h"
30 #include "citadel.h"
31
32 #define TRUE  1
33 #define FALSE 0
34
35 typedef unsigned char byte;           /* Byte type */
36 static byte dtable[256];              /* base64 encode / decode table */
37
38
39 char *safestrncpy(char *dest, const char *src, size_t n)
40 {
41         if (dest == NULL || src == NULL) {
42                 fprintf(stderr, "safestrncpy: NULL argument\n");
43                 abort();
44         }
45         strncpy(dest, src, n);
46         dest[n - 1] = 0;
47         return dest;
48 }
49
50
51
52 #ifndef HAVE_STRNCASECMP
53 int strncasecmp(char *lstr, char *rstr, int len)
54 {
55         int pos = 0;
56         char lc,rc;
57         while (pos<len) {
58                 lc=tolower(lstr[pos]);
59                 rc=tolower(rstr[pos]);
60                 if ((lc==0)&&(rc==0)) return(0);
61                 if (lc<rc) return(-1);
62                 if (lc>rc) return(1);
63                 pos=pos+1;
64         }
65         return(0);
66 }
67 #endif
68
69
70
71 /*
72  * num_tokens()  -  discover number of parameters/tokens in a string
73  */
74 int num_tokens(char *source, char tok) {
75         int a;
76         int count = 1;
77
78         if (source == NULL) return(0);
79         for (a=0; a<strlen(source); ++a) {
80                 if (source[a]==tok) ++count;
81         }
82         return(count);
83 }
84
85 /*
86  * extract_token()  -  a smarter string tokenizer
87  */
88 void extract_token(char *dest, char *source, int parmnum, char separator) 
89 {
90         int i;
91         int len;
92         int curr_parm;
93
94         strcpy(dest,"");
95         len = 0;
96         curr_parm = 0;
97
98         if (strlen(source)==0) {
99                 return;
100                 }
101
102         for (i=0; i<strlen(source); ++i) {
103                 if (source[i]==separator) {
104                         ++curr_parm;
105                 }
106                 else if (curr_parm == parmnum) {
107                         dest[len+1] = 0;
108                         dest[len++] = source[i];
109                 }
110         }
111 }
112
113
114
115 /*
116  * remove_token()  -  a tokenizer that kills, maims, and destroys
117  */
118 void remove_token(char *source, int parmnum, char separator)
119 {
120         int i;
121         int len;
122         int curr_parm;
123         int start, end;
124
125         len = 0;
126         curr_parm = 0;
127         start = (-1);
128         end = (-1);
129
130         if (strlen(source)==0) {
131                 return;
132                 }
133
134         for (i=0; i<strlen(source); ++i) {
135                 if ( (start < 0) && (curr_parm == parmnum) ) {
136                         start = i;
137                 }
138
139                 if ( (end < 0) && (curr_parm == (parmnum+1)) ) {
140                         end = i;
141                 }
142
143                 if (source[i]==separator) {
144                         ++curr_parm;
145                 }
146         }
147
148         if (end < 0) end = strlen(source);
149         strcpy(&source[start], &source[end]);
150 }
151
152
153
154
155 /*
156  * extract_int()  -  extract an int parm w/o supplying a buffer
157  */
158 int extract_int(char *source, int parmnum)
159 {
160         char buf[SIZ];
161         
162         extract_token(buf, source, parmnum, '|');
163         return(atoi(buf));
164 }
165
166 /*
167  * extract_long()  -  extract an long parm w/o supplying a buffer
168  */
169 long extract_long(char *source, long int parmnum)
170 {
171         char buf[SIZ];
172         
173         extract_token(buf, source, parmnum, '|');
174         return(atol(buf));
175 }
176
177
178
179 /*
180  * decode_base64() and encode_base64() are adaptations of code by
181  * John Walker, found in full in the file "base64.c" included with this
182  * distribution.  The difference between those functions and these is that
183  * these are intended to encode/decode small string buffers, and those are
184  * intended to encode/decode entire MIME parts.
185  */
186
187 void encode_base64(char *dest, char *source)
188 {
189     int i, hiteof = FALSE;
190     int spos = 0;
191     int dpos = 0;
192
193     /*  Fill dtable with character encodings.  */
194
195     for (i = 0; i < 26; i++) {
196         dtable[i] = 'A' + i;
197         dtable[26 + i] = 'a' + i;
198     }
199     for (i = 0; i < 10; i++) {
200         dtable[52 + i] = '0' + i;
201     }
202     dtable[62] = '+';
203     dtable[63] = '/';
204
205     while (!hiteof) {
206         byte igroup[3], ogroup[4];
207         int c, n;
208
209         igroup[0] = igroup[1] = igroup[2] = 0;
210         for (n = 0; n < 3; n++) {
211             c = source[spos++];
212             if (c == 0) {
213                 hiteof = TRUE;
214                 break;
215             }
216             igroup[n] = (byte) c;
217         }
218         if (n > 0) {
219             ogroup[0] = dtable[igroup[0] >> 2];
220             ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
221             ogroup[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
222             ogroup[3] = dtable[igroup[2] & 0x3F];
223
224             /* Replace characters in output stream with "=" pad
225                characters if fewer than three characters were
226                read from the end of the input stream. */
227
228             if (n < 3) {
229                 ogroup[3] = '=';
230                 if (n < 2) {
231                     ogroup[2] = '=';
232                 }
233             }
234             for (i = 0; i < 4; i++) {
235                 dest[dpos++] = ogroup[i];
236                 dest[dpos] = 0;
237             }
238         }
239     }
240 }
241
242
243 /* 
244  * Convert base64-encoded to binary.  Returns the length of the decoded data.
245  * It will stop after reading 'length' bytes.
246  */
247 int decode_base64(char *dest, char *source, size_t length)
248 {
249     int i, c;
250     int dpos = 0;
251     int spos = 0;
252
253     for (i = 0; i < 255; i++) {
254         dtable[i] = 0x80;
255     }
256     for (i = 'A'; i <= 'Z'; i++) {
257         dtable[i] = 0 + (i - 'A');
258     }
259     for (i = 'a'; i <= 'z'; i++) {
260         dtable[i] = 26 + (i - 'a');
261     }
262     for (i = '0'; i <= '9'; i++) {
263         dtable[i] = 52 + (i - '0');
264     }
265     dtable['+'] = 62;
266     dtable['/'] = 63;
267     dtable['='] = 0;
268
269     /*CONSTANTCONDITION*/
270     while (TRUE) {
271         byte a[4], b[4], o[3];
272
273         for (i = 0; i < 4; i++) {
274             if (spos >= length) {
275                 return(dpos);
276             }
277             c = source[spos++];
278
279             if (c == 0) {
280                 if (i > 0) {
281                     return(dpos);
282                 }
283                 return(dpos);
284             }
285             if (dtable[c] & 0x80) {
286                 /* Ignoring errors: discard invalid character. */
287                 i--;
288                 continue;
289             }
290             a[i] = (byte) c;
291             b[i] = (byte) dtable[c];
292         }
293         o[0] = (b[0] << 2) | (b[1] >> 4);
294         o[1] = (b[1] << 4) | (b[2] >> 2);
295         o[2] = (b[2] << 6) | b[3];
296         i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
297         if (i>=1) dest[dpos++] = o[0];
298         if (i>=2) dest[dpos++] = o[1];
299         if (i>=3) dest[dpos++] = o[2];
300         dest[dpos] = 0;
301         if (i < 3) {
302             return(dpos);
303         }
304     }
305 }
306
307
308
309 /*
310  * Strip leading and trailing spaces from a string
311  */
312 void striplt(char *buf)
313 {
314         while ((strlen(buf) > 0) && (isspace(buf[0])))
315                 strcpy(buf, &buf[1]);
316         while (isspace(buf[strlen(buf) - 1]))
317                 buf[strlen(buf) - 1] = 0;
318 }
319
320
321
322
323
324 /* 
325  * Return the number of occurances of character ch in string st
326  */ 
327 int haschar(char *st, int ch)
328 {
329         int a, b;
330         b = 0;
331         for (a = 0; a < strlen(st); ++a)
332                 if (st[a] == ch)
333                         ++b;
334         return (b);
335 }
336
337
338
339
340 /*
341  * Compare two strings, insensitive to case, punctuation, and non-alnum chars
342  */
343 int collapsed_strcmp(char *s1, char *s2) {
344         char *c1, *c2;
345         int i, ret, pos;
346
347         c1 = malloc(strlen(s1)+1);
348         c2 = malloc(strlen(s2)+1);
349         c1[0] = 0;
350         c2[0] = 0;
351
352         pos = 0;
353         for (i=0; i<strlen(s1); ++i) {
354                 if (isalnum(s1[i])) {
355                         c1[pos] = tolower(s1[i]);
356                         c1[++pos] = 0;
357                 }
358         }
359
360         pos = 0;
361         for (i=0; i<strlen(s2); ++i) {
362                 if (isalnum(s2[i])) {
363                         c2[pos] = tolower(s2[i]);
364                         c2[++pos] = 0;
365                 }
366         }
367
368         ret = strcmp(c1, c2);
369         free(c1);
370         free(c2);
371         return(ret);
372 }
373
374
375
376 /*
377  * Format a date/time stamp for output 
378  * seconds is whether to print the seconds
379  */
380 void fmt_date(char *buf, time_t thetime, int seconds) {
381         struct tm *tm;
382         int hour;
383
384         char *ascmonths[] = {
385                 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
386                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
387         };
388
389         strcpy(buf, "");
390         tm = localtime(&thetime);
391
392         hour = tm->tm_hour;
393         if (hour == 0)  hour = 12;
394         else if (hour > 12) hour = hour - 12;
395
396         if (seconds) {
397                 sprintf(buf, "%s %d %4d %d:%02d:%02d%s",
398                         ascmonths[tm->tm_mon],
399                         tm->tm_mday,
400                         tm->tm_year + 1900,
401                         hour,
402                         tm->tm_min,
403                         tm->tm_sec,
404                         ( (tm->tm_hour >= 12) ? "pm" : "am" )
405                 );
406         } else {
407                 sprintf(buf, "%s %d %4d %d:%02d%s",
408                         ascmonths[tm->tm_mon],
409                         tm->tm_mday,
410                         tm->tm_year + 1900,
411                         hour,
412                         tm->tm_min,
413                         ( (tm->tm_hour >= 12) ? "pm" : "am" )
414                 );
415         }
416 }
417
418
419
420 /*
421  * Determine whether the specified message number is contained within the
422  * specified set.
423  */
424 int is_msg_in_mset(char *mset, long msgnum) {
425         int num_sets;
426         int s;
427         char setstr[SIZ], lostr[SIZ], histr[SIZ];       /* was 1024 */
428         long lo, hi;
429
430         /*
431          * Now set it for all specified messages.
432          */
433         num_sets = num_tokens(mset, ',');
434         for (s=0; s<num_sets; ++s) {
435                 extract_token(setstr, mset, s, ',');
436
437                 extract_token(lostr, setstr, 0, ':');
438                 if (num_tokens(setstr, ':') >= 2) {
439                         extract_token(histr, setstr, 1, ':');
440                         if (!strcmp(histr, "*")) {
441                                 sprintf(histr, "%ld", LONG_MAX);
442                         }
443                 } 
444                 else {
445                         strcpy(histr, lostr);
446                 }
447                 lo = atol(lostr);
448                 hi = atol(histr);
449
450                 if ((msgnum >= lo) && (msgnum <= hi)) return(1);
451         }
452
453         return(0);
454 }
455
456
457 /*
458  * Utility function to "readline" from memory
459  * (returns new pointer)
460  */
461 char *memreadline(char *start, char *buf, int maxlen)
462 {
463         char ch;
464         char *ptr;
465         int len = 0;    /* tally our own length to avoid strlen() delays */
466
467         ptr = start;
468         memset(buf, 0, maxlen);
469
470         while (1) {
471                 ch = *ptr++;
472                 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
473                         buf[strlen(buf) + 1] = 0;
474                         buf[strlen(buf)] = ch;
475                         ++len;
476                 }
477                 if ((ch == 10) || (ch == 0)) {
478                         return ptr;
479                 }
480         }
481 }
482
483
484 /*
485  * Strip a boundarized substring out of a string (for example, remove
486  * parentheses and anything inside them).
487  */
488 void stripout(char *str, char leftboundary, char rightboundary) {
489         int a;
490         int lb = (-1);
491         int rb = (-1);
492
493         for (a = 0; a < strlen(str); ++a) {
494                 if (str[a] == leftboundary) lb = a;
495                 if (str[a] == rightboundary) rb = a;
496         }
497
498         if ( (lb > 0) && (rb > lb) ) {
499                 strcpy(&str[lb - 1], &str[rb + 1]);
500         }
501
502 }
503
504
505 /*
506  * Reduce a string down to a boundarized substring (for example, remove
507  * parentheses and anything outside them).
508  */
509 void stripallbut(char *str, char leftboundary, char rightboundary) {
510         int a;
511
512         for (a = 0; a < strlen(str); ++ a) {
513                 if (str[a] == leftboundary) strcpy(str, &str[a+1]);
514         }
515
516         for (a = 0; a < strlen(str); ++ a) {
517                 if (str[a] == rightboundary) str[a] = 0;
518         }
519
520 }