Removed the 'Gateway Domain' function from the entire Citadel system. This function...
[citadel.git] / citadel / internet_addressing.c
1 /*
2  * $Id$
3  *
4  * This file contains functions which handle the mapping of Internet addresses
5  * to users on the Citadel system.
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <ctype.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <sys/wait.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <libcitadel.h>
34 #include "citadel.h"
35 #include "server.h"
36 #include "sysdep_decls.h"
37 #include "citserver.h"
38 #include "support.h"
39 #include "config.h"
40 #include "msgbase.h"
41 #include "internet_addressing.h"
42 #include "user_ops.h"
43 #include "room_ops.h"
44 #include "parsedate.h"
45 #include "database.h"
46
47
48 #ifndef HAVE_SNPRINTF
49 #include "snprintf.h"
50 #endif
51
52
53 struct trynamebuf {
54         char buffer1[SIZ];
55         char buffer2[SIZ];
56 };
57
58 char *inetcfg = NULL;
59 struct spamstrings_t *spamstrings = NULL;
60
61
62 /*
63  * Return nonzero if the supplied name is an alias for this host.
64  */
65 int CtdlHostAlias(char *fqdn) {
66         int config_lines;
67         int i;
68         char buf[256];
69         char host[256], type[256];
70
71         if (fqdn == NULL) return(hostalias_nomatch);
72         if (IsEmptyStr(fqdn)) return(hostalias_nomatch);
73         if (!strcasecmp(fqdn, "localhost")) return(hostalias_localhost);
74         if (!strcasecmp(fqdn, config.c_fqdn)) return(hostalias_localhost);
75         if (!strcasecmp(fqdn, config.c_nodename)) return(hostalias_localhost);
76         if (inetcfg == NULL) return(hostalias_nomatch);
77
78         config_lines = num_tokens(inetcfg, '\n');
79         for (i=0; i<config_lines; ++i) {
80                 extract_token(buf, inetcfg, i, '\n', sizeof buf);
81                 extract_token(host, buf, 0, '|', sizeof host);
82                 extract_token(type, buf, 1, '|', sizeof type);
83
84                 if ( (!strcasecmp(type, "localhost"))
85                    && (!strcasecmp(fqdn, host)))
86                         return(hostalias_localhost);
87
88                 if ( (!strcasecmp(type, "directory"))
89                    && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
90                         return(hostalias_directory);
91
92                 if ( (!strcasecmp(type, "masqdomain"))
93                    && (!strcasecmp(&fqdn[strlen(fqdn)-strlen(host)], host)))
94                         return(hostalias_masq);
95
96         }
97
98         return(hostalias_nomatch);
99 }
100
101
102
103
104
105
106
107 /*
108  * Return 0 if a given string fuzzy-matches a Citadel user account
109  *
110  * FIXME ... this needs to be updated to handle aliases.
111  */
112 int fuzzy_match(struct ctdluser *us, char *matchstring) {
113         int a;
114         long len;
115
116         if ( (!strncasecmp(matchstring, "cit", 3)) 
117            && (atol(&matchstring[3]) == us->usernum)) {
118                 return 0;
119         }
120
121         len = strlen(matchstring);
122         for (a=0; !IsEmptyStr(&us->fullname[a]); ++a) {
123                 if (!strncasecmp(&us->fullname[a],
124                    matchstring, len)) {
125                         return 0;
126                 }
127         }
128         return -1;
129 }
130
131
132 /*
133  * Unfold a multi-line field into a single line, removing multi-whitespaces
134  */
135 void unfold_rfc822_field(char *field) {
136         int i;
137         int quote = 0;
138
139         striplt(field);         /* remove leading/trailing whitespace */
140
141         /* convert non-space whitespace to spaces, and remove double blanks */
142         for (i=0; i<strlen(field); ++i) {
143                 if (field[i]=='\"') quote = 1 - quote;
144                 if (!quote) {
145                         if (isspace(field[i])) field[i] = ' ';
146                         while (isspace(field[i]) && isspace(field[i+1])) {
147                                 strcpy(&field[i+1], &field[i+2]);
148                         }
149                 }
150         }
151 }
152
153
154
155 /*
156  * Split an RFC822-style address into userid, host, and full name
157  *
158  */
159 void process_rfc822_addr(const char *rfc822, char *user, char *node, char *name)
160 {
161         int a;
162
163         strcpy(user, "");
164         strcpy(node, config.c_fqdn);
165         strcpy(name, "");
166
167         if (rfc822 == NULL) return;
168
169         /* extract full name - first, it's From minus <userid> */
170         strcpy(name, rfc822);
171         stripout(name, '<', '>');
172
173         /* strip anything to the left of a bang */
174         while ((!IsEmptyStr(name)) && (haschar(name, '!') > 0))
175                 strcpy(name, &name[1]);
176
177         /* and anything to the right of a @ or % */
178         for (a = 0; a < strlen(name); ++a) {
179                 if (name[a] == '@')
180                         name[a] = 0;
181                 if (name[a] == '%')
182                         name[a] = 0;
183         }
184
185         /* but if there are parentheses, that changes the rules... */
186         if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
187                 strcpy(name, rfc822);
188                 stripallbut(name, '(', ')');
189         }
190
191         /* but if there are a set of quotes, that supersedes everything */
192         if (haschar(rfc822, 34) == 2) {
193                 strcpy(name, rfc822);
194                 while ((!IsEmptyStr(name)) && (name[0] != 34)) {
195                         strcpy(&name[0], &name[1]);
196                 }
197                 strcpy(&name[0], &name[1]);
198                 for (a = 0; a < strlen(name); ++a)
199                         if (name[a] == 34)
200                                 name[a] = 0;
201         }
202         /* extract user id */
203         strcpy(user, rfc822);
204
205         /* first get rid of anything in parens */
206         stripout(user, '(', ')');
207
208         /* if there's a set of angle brackets, strip it down to that */
209         if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
210                 stripallbut(user, '<', '>');
211         }
212
213         /* strip anything to the left of a bang */
214         while ((!IsEmptyStr(user)) && (haschar(user, '!') > 0))
215                 strcpy(user, &user[1]);
216
217         /* and anything to the right of a @ or % */
218         for (a = 0; a < strlen(user); ++a) {
219                 if (user[a] == '@')
220                         user[a] = 0;
221                 if (user[a] == '%')
222                         user[a] = 0;
223         }
224
225
226         /* extract node name */
227         strcpy(node, rfc822);
228
229         /* first get rid of anything in parens */
230         stripout(node, '(', ')');
231
232         /* if there's a set of angle brackets, strip it down to that */
233         if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
234                 stripallbut(node, '<', '>');
235         }
236
237         /* If no node specified, tack ours on instead */
238         if (
239                 (haschar(node, '@')==0)
240                 && (haschar(node, '%')==0)
241                 && (haschar(node, '!')==0)
242         ) {
243                 strcpy(node, config.c_nodename);
244         }
245
246         else {
247
248                 /* strip anything to the left of a @ */
249                 while ((!IsEmptyStr(node)) && (haschar(node, '@') > 0))
250                         strcpy(node, &node[1]);
251         
252                 /* strip anything to the left of a % */
253                 while ((!IsEmptyStr(node)) && (haschar(node, '%') > 0))
254                         strcpy(node, &node[1]);
255         
256                 /* reduce multiple system bang paths to node!user */
257                 while ((!IsEmptyStr(node)) && (haschar(node, '!') > 1))
258                         strcpy(node, &node[1]);
259         
260                 /* now get rid of the user portion of a node!user string */
261                 for (a = 0; a < strlen(node); ++a)
262                         if (node[a] == '!')
263                                 node[a] = 0;
264         }
265
266         /* strip leading and trailing spaces in all strings */
267         striplt(user);
268         striplt(node);
269         striplt(name);
270
271         /* If we processed a string that had the address in angle brackets
272          * but no name outside the brackets, we now have an empty name.  In
273          * this case, use the user portion of the address as the name.
274          */
275         if ((IsEmptyStr(name)) && (!IsEmptyStr(user))) {
276                 strcpy(name, user);
277         }
278 }
279
280
281
282 /*
283  * convert_field() is a helper function for convert_internet_message().
284  * Given start/end positions for an rfc822 field, it converts it to a Citadel
285  * field if it wants to, and unfolds it if necessary.
286  *
287  * Returns 1 if the field was converted and inserted into the Citadel message
288  * structure, implying that the source field should be removed from the
289  * message text.
290  */
291 int convert_field(struct CtdlMessage *msg, int beg, int end) {
292         char *rfc822;
293         char *key, *value;
294         int i;
295         int colonpos = (-1);
296         int processed = 0;
297         char buf[SIZ];
298         char user[1024];
299         char node[1024];
300         char name[1024];
301         char addr[1024];
302         time_t parsed_date;
303
304         rfc822 = msg->cm_fields['M'];   /* M field contains rfc822 text */
305         for (i = end; i >= beg; --i) {
306                 if (rfc822[i] == ':') colonpos = i;
307         }
308
309         if (colonpos < 0) return(0);    /* no colon? not a valid header line */
310
311         key = malloc((end - beg) + 2);
312         safestrncpy(key, &rfc822[beg], (end-beg)+1);
313         key[colonpos - beg] = 0;
314         value = &key[(colonpos - beg) + 1];
315         unfold_rfc822_field(value);
316
317         /*
318          * Here's the big rfc822-to-citadel loop.
319          */
320
321         /* Date/time is converted into a unix timestamp.  If the conversion
322          * fails, we replace it with the time the message arrived locally.
323          */
324         if (!strcasecmp(key, "Date")) {
325                 parsed_date = parsedate(value);
326                 if (parsed_date < 0L) parsed_date = time(NULL);
327                 snprintf(buf, sizeof buf, "%ld", (long)parsed_date );
328                 if (msg->cm_fields['T'] == NULL)
329                         msg->cm_fields['T'] = strdup(buf);
330                 processed = 1;
331         }
332
333         else if (!strcasecmp(key, "From")) {
334                 process_rfc822_addr(value, user, node, name);
335                 CtdlLogPrintf(CTDL_DEBUG, "Converted to <%s@%s> (%s)\n", user, node, name);
336                 snprintf(addr, sizeof addr, "%s@%s", user, node);
337                 if (msg->cm_fields['A'] == NULL)
338                         msg->cm_fields['A'] = strdup(name);
339                 processed = 1;
340                 if (msg->cm_fields['F'] == NULL)
341                         msg->cm_fields['F'] = strdup(addr);
342                 processed = 1;
343         }
344
345         else if (!strcasecmp(key, "Subject")) {
346                 if (msg->cm_fields['U'] == NULL)
347                         msg->cm_fields['U'] = strdup(value);
348                 processed = 1;
349         }
350
351         else if (!strcasecmp(key, "To")) {
352                 if (msg->cm_fields['R'] == NULL)
353                         msg->cm_fields['R'] = strdup(value);
354                 processed = 1;
355         }
356
357         else if (!strcasecmp(key, "CC")) {
358                 if (msg->cm_fields['Y'] == NULL)
359                         msg->cm_fields['Y'] = strdup(value);
360                 processed = 1;
361         }
362
363         else if (!strcasecmp(key, "Message-ID")) {
364                 if (msg->cm_fields['I'] != NULL) {
365                         CtdlLogPrintf(CTDL_WARNING, "duplicate message id\n");
366                 }
367
368                 if (msg->cm_fields['I'] == NULL) {
369                         msg->cm_fields['I'] = strdup(value);
370
371                         /* Strip angle brackets */
372                         while (haschar(msg->cm_fields['I'], '<') > 0) {
373                                 strcpy(&msg->cm_fields['I'][0],
374                                         &msg->cm_fields['I'][1]);
375                         }
376                         for (i = 0; i<strlen(msg->cm_fields['I']); ++i)
377                                 if (msg->cm_fields['I'][i] == '>')
378                                         msg->cm_fields['I'][i] = 0;
379                 }
380
381                 processed = 1;
382         }
383
384         else if (!strcasecmp(key, "Return-Path")) {
385                 if (msg->cm_fields['P'] == NULL)
386                         msg->cm_fields['P'] = strdup(value);
387                 processed = 1;
388         }
389
390         else if (!strcasecmp(key, "Envelope-To")) {
391                 if (msg->cm_fields['V'] == NULL)
392                         msg->cm_fields['V'] = strdup(value);
393                 processed = 1;
394         }
395
396         else if (!strcasecmp(key, "References")) {
397                 if (msg->cm_fields['W'] != NULL) {
398                         free(msg->cm_fields['W']);
399                 }
400                 msg->cm_fields['W'] = strdup(value);
401                 processed = 1;
402         }
403
404         else if (!strcasecmp(key, "In-reply-to")) {
405                 if (msg->cm_fields['W'] == NULL) {              /* References: supersedes In-reply-to: */
406                         msg->cm_fields['W'] = strdup(value);
407                 }
408                 processed = 1;
409         }
410
411
412
413         /* Clean up and move on. */
414         free(key);      /* Don't free 'value', it's actually the same buffer */
415         return(processed);
416 }
417
418
419 /*
420  * Convert RFC822 references format (References) to Citadel references format (Weferences)
421  */
422 void convert_references_to_wefewences(char *str) {
423         int bracket_nesting = 0;
424         char *ptr = str;
425         char *moveptr = NULL;
426         char ch;
427
428         while(*ptr) {
429                 ch = *ptr;
430                 if (ch == '>') {
431                         --bracket_nesting;
432                         if (bracket_nesting < 0) bracket_nesting = 0;
433                 }
434                 if ((ch == '>') && (bracket_nesting == 0) && (*(ptr+1)) && (ptr>str) ) {
435                         *ptr = '|';
436                         ++ptr;
437                 }
438                 else if (bracket_nesting > 0) {
439                         ++ptr;
440                 }
441                 else {
442                         moveptr = ptr;
443                         while (*moveptr) {
444                                 *moveptr = *(moveptr+1);
445                                 ++moveptr;
446                         }
447                 }
448                 if (ch == '<') ++bracket_nesting;
449         }
450
451 }
452
453
454 /*
455  * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
456  * NOTE: the supplied buffer becomes part of the CtdlMessage structure, and
457  * will be deallocated when CtdlFreeMessage() is called.  Therefore, the
458  * supplied buffer should be DEREFERENCED.  It should not be freed or used
459  * again.
460  */
461 struct CtdlMessage *convert_internet_message(char *rfc822) {
462
463         struct CtdlMessage *msg;
464         int pos, beg, end, msglen;
465         int done;
466         char buf[SIZ];
467         int converted;
468
469         msg = malloc(sizeof(struct CtdlMessage));
470         if (msg == NULL) return msg;
471
472         memset(msg, 0, sizeof(struct CtdlMessage));
473         msg->cm_magic = CTDLMESSAGE_MAGIC;      /* self check */
474         msg->cm_anon_type = 0;                  /* never anonymous */
475         msg->cm_format_type = FMT_RFC822;       /* internet message */
476         msg->cm_fields['M'] = rfc822;
477
478         pos = 0;
479         done = 0;
480
481         while (!done) {
482
483                 /* Locate beginning and end of field, keeping in mind that
484                  * some fields might be multiline
485                  */
486                 beg = pos;
487                 end = (-1);
488
489                 msglen = strlen(rfc822);        
490                 while ( (end < 0) && (done == 0) ) {
491
492                         if ((rfc822[pos]=='\n')
493                            && (!isspace(rfc822[pos+1]))) {
494                                 end = pos;
495                         }
496
497                         /* done with headers? */
498                         if (   (rfc822[pos]=='\n')
499                            && ( (rfc822[pos+1]=='\n')
500                               ||(rfc822[pos+1]=='\r')) ) {
501                                 end = pos;
502                                 done = 1;
503                         }
504
505                         if (pos >= (msglen-1) ) {
506                                 end = pos;
507                                 done = 1;
508                         }
509
510                         ++pos;
511
512                 }
513
514                 /* At this point we have a field.  Are we interested in it? */
515                 converted = convert_field(msg, beg, end);
516
517                 /* Strip the field out of the RFC822 header if we used it */
518                 if (converted) {
519                         strcpy(&rfc822[beg], &rfc822[pos]);
520                         pos = beg;
521                 }
522
523                 /* If we've hit the end of the message, bail out */
524                 if (pos > strlen(rfc822)) done = 1;
525         }
526
527         /* Follow-up sanity checks... */
528
529         /* If there's no timestamp on this message, set it to now. */
530         if (msg->cm_fields['T'] == NULL) {
531                 snprintf(buf, sizeof buf, "%ld", (long)time(NULL));
532                 msg->cm_fields['T'] = strdup(buf);
533         }
534
535         /* If a W (references, or rather, Wefewences) field is present, we
536          * have to convert it from RFC822 format to Citadel format.
537          */
538         if (msg->cm_fields['W'] != NULL) {
539                 convert_references_to_wefewences(msg->cm_fields['W']);
540         }
541
542         return msg;
543 }
544
545
546
547 /*
548  * Look for a particular header field in an RFC822 message text.  If the
549  * requested field is found, it is unfolded (if necessary) and returned to
550  * the caller.  The field name is stripped out, leaving only its contents.
551  * The caller is responsible for freeing the returned buffer.  If the requested
552  * field is not present, or anything else goes wrong, it returns NULL.
553  */
554 char *rfc822_fetch_field(char *rfc822, char *fieldname) {
555         char *fieldbuf = NULL;
556         char *end_of_headers;
557         char *field_start;
558         char *ptr;
559         char *cont;
560         char fieldhdr[SIZ];
561
562         /* Should never happen, but sometimes we get stupid */
563         if (rfc822 == NULL) return(NULL);
564         if (fieldname == NULL) return(NULL);
565
566         snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
567
568         /* Locate the end of the headers, so we don't run past that point */
569         end_of_headers = bmstrcasestr(rfc822, "\n\r\n");
570         if (end_of_headers == NULL) {
571                 end_of_headers = bmstrcasestr(rfc822, "\n\n");
572         }
573         if (end_of_headers == NULL) return (NULL);
574
575         field_start = bmstrcasestr(rfc822, fieldhdr);
576         if (field_start == NULL) return(NULL);
577         if (field_start > end_of_headers) return(NULL);
578
579         fieldbuf = malloc(SIZ);
580         strcpy(fieldbuf, "");
581
582         ptr = field_start;
583         ptr = memreadline(ptr, fieldbuf, SIZ-strlen(fieldbuf) );
584         while ( (isspace(ptr[0])) && (ptr < end_of_headers) ) {
585                 strcat(fieldbuf, " ");
586                 cont = &fieldbuf[strlen(fieldbuf)];
587                 ptr = memreadline(ptr, cont, SIZ-strlen(fieldbuf) );
588                 striplt(cont);
589         }
590
591         strcpy(fieldbuf, &fieldbuf[strlen(fieldhdr)]);
592         striplt(fieldbuf);
593
594         return(fieldbuf);
595 }
596
597
598
599 /*****************************************************************************
600  *                      DIRECTORY MANAGEMENT FUNCTIONS                       *
601  *****************************************************************************/
602
603 /*
604  * Generate the index key for an Internet e-mail address to be looked up
605  * in the database.
606  */
607 void directory_key(char *key, char *addr) {
608         int i;
609         int keylen = 0;
610
611         for (i=0; !IsEmptyStr(&addr[i]); ++i) {
612                 if (!isspace(addr[i])) {
613                         key[keylen++] = tolower(addr[i]);
614                 }
615         }
616         key[keylen++] = 0;
617
618         CtdlLogPrintf(CTDL_DEBUG, "Directory key is <%s>\n", key);
619 }
620
621
622
623 /* Return nonzero if the supplied address is in a domain we keep in
624  * the directory
625  */
626 int IsDirectory(char *addr, int allow_masq_domains) {
627         char domain[256];
628         int h;
629
630         extract_token(domain, addr, 1, '@', sizeof domain);
631         striplt(domain);
632
633         h = CtdlHostAlias(domain);
634
635         if ( (h == hostalias_masq) && allow_masq_domains)
636                 return(1);
637         
638         if ( (h == hostalias_localhost) || (h == hostalias_directory) ) {
639                 return(1);
640         }
641         else {
642                 return(0);
643         }
644 }
645
646
647 /*
648  * Initialize the directory database (erasing anything already there)
649  */
650 void CtdlDirectoryInit(void) {
651         cdb_trunc(CDB_DIRECTORY);
652 }
653
654
655 /*
656  * Add an Internet e-mail address to the directory for a user
657  */
658 void CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
659         char key[SIZ];
660
661         CtdlLogPrintf(CTDL_DEBUG, "Dir: %s --> %s\n",
662                 internet_addr, citadel_addr);
663         if (IsDirectory(internet_addr, 0) == 0) return;
664
665         directory_key(key, internet_addr);
666
667         cdb_store(CDB_DIRECTORY, key, strlen(key),
668                 citadel_addr, strlen(citadel_addr)+1 );
669 }
670
671
672 /*
673  * Delete an Internet e-mail address from the directory.
674  *
675  * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
676  * here because the callback API expects to be able to send it.)
677  */
678 void CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
679         char key[SIZ];
680
681         directory_key(key, internet_addr);
682         cdb_delete(CDB_DIRECTORY, key, strlen(key) );
683 }
684
685
686 /*
687  * Look up an Internet e-mail address in the directory.
688  * On success: returns 0, and Citadel address stored in 'target'
689  * On failure: returns nonzero
690  */
691 int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
692         struct cdbdata *cdbrec;
693         char key[SIZ];
694
695         /* Dump it in there unchanged, just for kicks */
696         safestrncpy(target, internet_addr, targbuflen);
697
698         /* Only do lookups for addresses with hostnames in them */
699         if (num_tokens(internet_addr, '@') != 2) return(-1);
700
701         /* Only do lookups for domains in the directory */
702         if (IsDirectory(internet_addr, 0) == 0) return(-1);
703
704         directory_key(key, internet_addr);
705         cdbrec = cdb_fetch(CDB_DIRECTORY, key, strlen(key) );
706         if (cdbrec != NULL) {
707                 safestrncpy(target, cdbrec->ptr, targbuflen);
708                 cdb_free(cdbrec);
709                 return(0);
710         }
711
712         return(-1);
713 }
714
715
716 /*
717  * Harvest any email addresses that someone might want to have in their
718  * "collected addresses" book.
719  */
720 char *harvest_collected_addresses(struct CtdlMessage *msg) {
721         char *coll = NULL;
722         char addr[256];
723         char user[256], node[256], name[256];
724         int is_harvestable;
725         int i, j, h;
726         int field = 0;
727
728         if (msg == NULL) return(NULL);
729
730         is_harvestable = 1;
731         strcpy(addr, "");       
732         if (msg->cm_fields['A'] != NULL) {
733                 strcat(addr, msg->cm_fields['A']);
734         }
735         if (msg->cm_fields['F'] != NULL) {
736                 strcat(addr, " <");
737                 strcat(addr, msg->cm_fields['F']);
738                 strcat(addr, ">");
739                 if (IsDirectory(msg->cm_fields['F'], 0)) {
740                         is_harvestable = 0;
741                 }
742         }
743
744         if (is_harvestable) {
745                 coll = strdup(addr);
746         }
747         else {
748                 coll = strdup("");
749         }
750
751         if (coll == NULL) return(NULL);
752
753         /* Scan both the R (To) and Y (CC) fields */
754         for (i = 0; i < 2; ++i) {
755                 if (i == 0) field = 'R' ;
756                 if (i == 1) field = 'Y' ;
757
758                 if (msg->cm_fields[field] != NULL) {
759                         for (j=0; j<num_tokens(msg->cm_fields[field], ','); ++j) {
760                                 extract_token(addr, msg->cm_fields[field], j, ',', sizeof addr);
761                                 process_rfc822_addr(addr, user, node, name);
762                                 h = CtdlHostAlias(node);
763                                 if ( (h != hostalias_localhost) && (h != hostalias_directory) ) {
764                                         coll = realloc(coll, strlen(coll) + strlen(addr) + 4);
765                                         if (coll == NULL) return(NULL);
766                                         if (!IsEmptyStr(coll)) {
767                                                 strcat(coll, ",");
768                                         }
769                                         striplt(addr);
770                                         strcat(coll, addr);
771                                 }
772                         }
773                 }
774         }
775
776         if (IsEmptyStr(coll)) {
777                 free(coll);
778                 return(NULL);
779         }
780         return(coll);
781 }