4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
17 #include <sys/types.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
30 #include "internet_addressing.h"
32 #include "parsedate.h"
36 * Return 0 if a given string fuzzy-matches a Citadel user account
38 * FIX ... this needs to be updated to match any and all address syntaxes.
40 int fuzzy_match(struct usersupp *us, char *matchstring) {
43 for (a=0; a<strlen(us->fullname); ++a) {
44 if (!strncasecmp(&us->fullname[a],
45 matchstring, strlen(matchstring))) {
54 * Unfold a multi-line field into a single line, removing multi-whitespaces
56 void unfold_rfc822_field(char *field) {
60 striplt(field); /* remove leading/trailing whitespace */
62 /* convert non-space whitespace to spaces, and remove double blanks */
63 for (i=0; i<strlen(field); ++i) {
64 if (field[i]=='\"') quote = 1 - quote;
66 if (isspace(field[i])) field[i] = ' ';
67 while (isspace(field[i]) && isspace(field[i+1])) {
68 strcpy(&field[i+1], &field[i+2]);
77 * Split an RFC822-style address into userid, host, and full name
78 * (Originally from citmail.c, and unchanged so far)
81 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
86 strcpy(node, config.c_fqdn);
89 /* extract full name - first, it's From minus <userid> */
91 for (a = 0; a < strlen(name); ++a) {
94 strcpy(&name[a], &name[a + 1]);
95 } while ((strlen(name) > 0) && (name[a] != '>'));
96 strcpy(&name[a], &name[a + 1]);
99 /* strip anything to the left of a bang */
100 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
101 strcpy(name, &name[1]);
103 /* and anything to the right of a @ or % */
104 for (a = 0; a < strlen(name); ++a) {
111 /* but if there are parentheses, that changes the rules... */
112 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
113 strcpy(name, rfc822);
114 while ((strlen(name) > 0) && (name[0] != '(')) {
115 strcpy(&name[0], &name[1]);
117 strcpy(&name[0], &name[1]);
118 for (a = 0; a < strlen(name); ++a) {
119 if (name[a] == ')') {
124 /* but if there are a set of quotes, that supersedes everything */
125 if (haschar(rfc822, 34) == 2) {
126 strcpy(name, rfc822);
127 while ((strlen(name) > 0) && (name[0] != 34)) {
128 strcpy(&name[0], &name[1]);
130 strcpy(&name[0], &name[1]);
131 for (a = 0; a < strlen(name); ++a)
135 /* extract user id */
136 strcpy(user, rfc822);
138 /* first get rid of anything in parens */
139 for (a = 0; a < strlen(user); ++a)
140 if (user[a] == '(') {
142 strcpy(&user[a], &user[a + 1]);
143 } while ((strlen(user) > 0) && (user[a] != ')'));
144 strcpy(&user[a], &user[a + 1]);
146 /* if there's a set of angle brackets, strip it down to that */
147 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
148 while ((strlen(user) > 0) && (user[0] != '<')) {
149 strcpy(&user[0], &user[1]);
151 strcpy(&user[0], &user[1]);
152 for (a = 0; a < strlen(user); ++a)
156 /* strip anything to the left of a bang */
157 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
158 strcpy(user, &user[1]);
160 /* and anything to the right of a @ or % */
161 for (a = 0; a < strlen(user); ++a) {
169 /* extract node name */
170 strcpy(node, rfc822);
172 /* first get rid of anything in parens */
173 for (a = 0; a < strlen(node); ++a)
174 if (node[a] == '(') {
176 strcpy(&node[a], &node[a + 1]);
177 } while ((strlen(node) > 0) && (node[a] != ')'));
178 strcpy(&node[a], &node[a + 1]);
180 /* if there's a set of angle brackets, strip it down to that */
181 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
182 while ((strlen(node) > 0) && (node[0] != '<')) {
183 strcpy(&node[0], &node[1]);
185 strcpy(&node[0], &node[1]);
186 for (a = 0; a < strlen(node); ++a)
190 /* strip anything to the left of a @ */
191 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
192 strcpy(node, &node[1]);
194 /* strip anything to the left of a % */
195 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
196 strcpy(node, &node[1]);
198 /* reduce multiple system bang paths to node!user */
199 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
200 strcpy(node, &node[1]);
202 /* now get rid of the user portion of a node!user string */
203 for (a = 0; a < strlen(node); ++a)
207 /* strip leading and trailing spaces in all strings */
216 * Back end for convert_internet_address()
217 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
219 void try_name(struct usersupp *us) {
221 if (!strncasecmp(CC->buffer1, "cit", 3))
222 if (atol(&CC->buffer1[3]) == us->usernum)
223 strcpy(CC->buffer2, us->fullname);
225 if (!collapsed_strcmp(CC->buffer1, us->fullname))
226 strcpy(CC->buffer2, us->fullname);
228 if (us->uid != BBSUID)
229 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
230 strcpy(CC->buffer2, us->fullname);
236 * Convert an Internet email address to a Citadel user/host combination
238 int convert_internet_address(char *destuser, char *desthost, char *source)
245 process_rfc822_addr(source, user, node, name);
247 /* Map the FQDN to a Citadel node name
248 * FIX ... we have to check for all known aliases for the local
249 * system, and also handle gateway domains, etc. etc.
251 if (!strcasecmp(node, config.c_fqdn)) {
252 strcpy(node, config.c_nodename);
255 /* Return an error condition if the node is not known.
256 * FIX ... make this work for non-local systems
258 if (strcasecmp(node, config.c_nodename)) {
259 return(rfc822_address_invalid);
262 /* Now try to resolve the name
263 * FIX ... do the multiple-addresses thing
265 if (!strcasecmp(node, config.c_nodename)) {
266 strcpy(destuser, user);
267 strcpy(desthost, config.c_nodename);
268 strcpy(CC->buffer1, user);
269 strcpy(CC->buffer2, "");
270 ForEachUser(try_name);
271 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
272 strcpy(destuser, CC->buffer2);
273 return(rfc822_address_locally_validated);
276 return(rfc822_address_invalid); /* unknown error */
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.
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
291 int convert_field(struct CtdlMessage *msg, int beg, int end) {
298 char *user, *node, *name;
300 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
301 for (i = end; i >= beg; --i) {
302 if (rfc822[i] == ':') colonpos = i;
305 if (colonpos < 0) return(0); /* no colon? not a valid header line */
307 value = mallok((end - beg) + 2);
308 safestrncpy(value, &rfc822[beg], (end-beg)+1);
310 key[colonpos - beg] = 0;
311 value = &key[(colonpos - beg) + 1];
312 unfold_rfc822_field(value);
314 /* Here's the big rfc822-to-citadel loop. */
316 if (!strcasecmp(key, "Date")) {
317 lprintf(9, "converting date <%s>\n", value);
318 sprintf(buf, "%ld", parsedate(value) );
319 lprintf(9, "parsed value is <%s>\n", buf);
320 if (msg->cm_fields['T'] != NULL)
321 msg->cm_fields['T'] = strdoop(buf);
325 else if (!strcasecmp(key, "From")) {
326 user = mallok(strlen(value));
327 node = mallok(strlen(value));
328 name = mallok(strlen(value));
329 process_rfc822_addr(value, user, node, name);
330 if (msg->cm_fields['A'] != NULL)
331 msg->cm_fields['A'] = strdoop(user);
332 if (msg->cm_fields['N'] != NULL)
333 msg->cm_fields['N'] = strdoop(node);
334 if (msg->cm_fields['H'] != NULL)
335 msg->cm_fields['H'] = strdoop(name);
342 else if (!strcasecmp(key, "Subject")) {
343 if (msg->cm_fields['U'] != NULL)
344 msg->cm_fields['U'] = strdoop(key);
348 /* Clean up and move on. */
349 lprintf(9, "Field: key=<%s> value=<%s> processed=%d\n",
350 key, value, processed);
351 phree(key); /* Don't free 'value', it's actually the same buffer */
357 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
359 struct CtdlMessage *convert_internet_message(char *rfc822) {
361 struct CtdlMessage *msg;
368 msg = mallok(sizeof(struct CtdlMessage));
369 if (msg == NULL) return msg;
371 memset(msg, 0, sizeof(struct CtdlMessage));
372 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
373 msg->cm_anon_type = 0; /* never anonymous */
374 msg->cm_format_type = 4; /* always MIME */
375 msg->cm_fields['M'] = rfc822;
377 /* FIX there's plenty to do here. */
378 msglen = strlen(rfc822);
384 /* Locate beginning and end of field, keeping in mind that
385 * some fields might be multiline
389 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
390 if ((rfc822[pos]=='\n')
391 && (!isspace(rfc822[pos+1]))) {
394 if ( (rfc822[pos]=='\n') /* done w. headers? */
395 && ( (rfc822[pos+1]=='\n')
396 ||(rfc822[pos+1]=='\r'))) {
403 /* At this point we have a field. Are we interested in it? */
404 converted = convert_field(msg, beg, end);
406 strcpy(&rfc822[beg], &rfc822[pos]);
410 /* If we've hit the end of the message, bail out */
411 if (pos > strlen(rfc822)) done = 1;
415 /* Follow-up sanity checks... */
417 /* If there's no timestamp on this message, set it to now. */
418 if (msg->cm_fields['T'] == NULL) {
419 sprintf(buf, "%ld", time(NULL));
420 msg->cm_fields['T'] = strdoop(buf);