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