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