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"
33 #include "parsedate.h"
38 * Return 0 if a given string fuzzy-matches a Citadel user account
40 * FIX ... this needs to be updated to match any and all address syntaxes.
42 int fuzzy_match(struct usersupp *us, char *matchstring) {
45 for (a=0; a<strlen(us->fullname); ++a) {
46 if (!strncasecmp(&us->fullname[a],
47 matchstring, strlen(matchstring))) {
56 * Unfold a multi-line field into a single line, removing multi-whitespaces
58 void unfold_rfc822_field(char *field) {
62 striplt(field); /* remove leading/trailing whitespace */
64 /* convert non-space whitespace to spaces, and remove double blanks */
65 for (i=0; i<strlen(field); ++i) {
66 if (field[i]=='\"') quote = 1 - quote;
68 if (isspace(field[i])) field[i] = ' ';
69 while (isspace(field[i]) && isspace(field[i+1])) {
70 strcpy(&field[i+1], &field[i+2]);
79 * Split an RFC822-style address into userid, host, and full name
80 * (Originally from citmail.c, and unchanged so far)
83 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
88 strcpy(node, config.c_fqdn);
91 /* extract full name - first, it's From minus <userid> */
93 for (a = 0; a < strlen(name); ++a) {
96 strcpy(&name[a], &name[a + 1]);
97 } while ((strlen(name) > 0) && (name[a] != '>'));
98 strcpy(&name[a], &name[a + 1]);
101 /* strip anything to the left of a bang */
102 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
103 strcpy(name, &name[1]);
105 /* and anything to the right of a @ or % */
106 for (a = 0; a < strlen(name); ++a) {
113 /* but if there are parentheses, that changes the rules... */
114 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
115 strcpy(name, rfc822);
116 while ((strlen(name) > 0) && (name[0] != '(')) {
117 strcpy(&name[0], &name[1]);
119 strcpy(&name[0], &name[1]);
120 for (a = 0; a < strlen(name); ++a) {
121 if (name[a] == ')') {
126 /* but if there are a set of quotes, that supersedes everything */
127 if (haschar(rfc822, 34) == 2) {
128 strcpy(name, rfc822);
129 while ((strlen(name) > 0) && (name[0] != 34)) {
130 strcpy(&name[0], &name[1]);
132 strcpy(&name[0], &name[1]);
133 for (a = 0; a < strlen(name); ++a)
137 /* extract user id */
138 strcpy(user, rfc822);
140 /* first get rid of anything in parens */
141 for (a = 0; a < strlen(user); ++a)
142 if (user[a] == '(') {
144 strcpy(&user[a], &user[a + 1]);
145 } while ((strlen(user) > 0) && (user[a] != ')'));
146 strcpy(&user[a], &user[a + 1]);
148 /* if there's a set of angle brackets, strip it down to that */
149 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
150 while ((strlen(user) > 0) && (user[0] != '<')) {
151 strcpy(&user[0], &user[1]);
153 strcpy(&user[0], &user[1]);
154 for (a = 0; a < strlen(user); ++a)
158 /* strip anything to the left of a bang */
159 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
160 strcpy(user, &user[1]);
162 /* and anything to the right of a @ or % */
163 for (a = 0; a < strlen(user); ++a) {
171 /* extract node name */
172 strcpy(node, rfc822);
174 /* first get rid of anything in parens */
175 for (a = 0; a < strlen(node); ++a)
176 if (node[a] == '(') {
178 strcpy(&node[a], &node[a + 1]);
179 } while ((strlen(node) > 0) && (node[a] != ')'));
180 strcpy(&node[a], &node[a + 1]);
182 /* if there's a set of angle brackets, strip it down to that */
183 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
184 while ((strlen(node) > 0) && (node[0] != '<')) {
185 strcpy(&node[0], &node[1]);
187 strcpy(&node[0], &node[1]);
188 for (a = 0; a < strlen(node); ++a)
192 /* strip anything to the left of a @ */
193 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
194 strcpy(node, &node[1]);
196 /* strip anything to the left of a % */
197 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
198 strcpy(node, &node[1]);
200 /* reduce multiple system bang paths to node!user */
201 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
202 strcpy(node, &node[1]);
204 /* now get rid of the user portion of a node!user string */
205 for (a = 0; a < strlen(node); ++a)
209 /* strip leading and trailing spaces in all strings */
218 * Back end for convert_internet_address()
219 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
221 void try_name(struct usersupp *us) {
223 if (!strncasecmp(CC->buffer1, "cit", 3))
224 if (atol(&CC->buffer1[3]) == us->usernum)
225 strcpy(CC->buffer2, us->fullname);
227 if (!collapsed_strcmp(CC->buffer1, us->fullname))
228 strcpy(CC->buffer2, us->fullname);
230 if (us->uid != BBSUID)
231 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
232 strcpy(CC->buffer2, us->fullname);
237 * Convert an Internet email address to a Citadel user/host combination
239 int convert_internet_address(char *destuser, char *desthost, char *source)
244 struct quickroom qrbuf;
248 process_rfc822_addr(source, user, node, name);
250 /* Map the FQDN to a Citadel node name
251 * FIX ... we have to check for all known aliases for the local
252 * system, and also handle gateway domains, etc. etc.
254 if (!strcasecmp(node, config.c_fqdn)) {
255 strcpy(node, config.c_nodename);
258 /* Now try to resolve the name
259 * FIX ... do the multiple-addresses thing
261 if (!strcasecmp(node, config.c_nodename)) {
262 /* Try all local rooms */
263 if (!strncasecmp(user, "room_", 5)) {
264 strcpy(name, &user[5]);
265 for (i=0; i<strlen(name); ++i)
266 if (name[i]=='_') name[i]=' ';
267 if (getroom(&qrbuf, name) == 0) {
268 strcpy(destuser, qrbuf.QRname);
269 strcpy(desthost, config.c_nodename);
270 return rfc822_room_delivery;
274 /* Try all local users */
275 strcpy(destuser, user);
276 strcpy(desthost, config.c_nodename);
277 strcpy(CC->buffer1, user);
278 strcpy(CC->buffer2, "");
279 ForEachUser(try_name);
280 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
281 strcpy(destuser, CC->buffer2);
282 return(rfc822_address_locally_validated);
285 strcpy(destuser, user);
286 strcpy(desthost, node);
287 return(rfc822_address_invalid); /* unknown error */
294 * convert_field() is a helper function for convert_internet_message().
295 * Given start/end positions for an rfc822 field, it converts it to a Citadel
296 * field if it wants to, and unfolds it if necessary.
298 * Returns 1 if the field was converted and inserted into the Citadel message
299 * structure, implying that the source field should be removed from the
302 int convert_field(struct CtdlMessage *msg, int beg, int end) {
309 char *user, *node, *name;
311 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
312 for (i = end; i >= beg; --i) {
313 if (rfc822[i] == ':') colonpos = i;
316 if (colonpos < 0) return(0); /* no colon? not a valid header line */
318 key = mallok((end - beg) + 2);
319 safestrncpy(key, &rfc822[beg], (end-beg)+1);
320 key[colonpos - beg] = 0;
321 value = &key[(colonpos - beg) + 1];
322 unfold_rfc822_field(value);
324 lprintf(9, "Key=<%s> Value=<%s>\n", key, value);
326 /* Here's the big rfc822-to-citadel loop. */
328 if (!strcasecmp(key, "Date")) {
329 sprintf(buf, "%ld", parsedate(value) );
330 if (msg->cm_fields['T'] == NULL)
331 msg->cm_fields['T'] = strdoop(buf);
335 else if (!strcasecmp(key, "From")) {
336 user = mallok(strlen(value));
337 node = mallok(strlen(value));
338 name = mallok(strlen(value));
339 process_rfc822_addr(value, user, node, name);
340 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
341 if (msg->cm_fields['A'] == NULL)
342 msg->cm_fields['A'] = user;
345 if (msg->cm_fields['N'] == NULL)
346 msg->cm_fields['N'] = node;
349 if (msg->cm_fields['H'] == NULL)
350 msg->cm_fields['H'] = name;
356 else if (!strcasecmp(key, "Subject")) {
357 if (msg->cm_fields['U'] == NULL)
358 msg->cm_fields['U'] = strdoop(value);
362 /* Clean up and move on. */
363 phree(key); /* Don't free 'value', it's actually the same buffer */
369 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
371 struct CtdlMessage *convert_internet_message(char *rfc822) {
373 struct CtdlMessage *msg;
379 msg = mallok(sizeof(struct CtdlMessage));
380 if (msg == NULL) return msg;
382 memset(msg, 0, sizeof(struct CtdlMessage));
383 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
384 msg->cm_anon_type = 0; /* never anonymous */
385 msg->cm_format_type = FMT_RFC822; /* internet message */
386 msg->cm_fields['M'] = rfc822;
388 lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
394 /* Locate beginning and end of field, keeping in mind that
395 * some fields might be multiline
399 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
400 if ((rfc822[pos]=='\n')
401 && (!isspace(rfc822[pos+1]))) {
404 if ( (rfc822[pos]=='\n') /* done w. headers? */
405 && ( (rfc822[pos+1]=='\n')
406 ||(rfc822[pos+1]=='\r'))) {
413 /* At this point we have a field. Are we interested in it? */
414 converted = convert_field(msg, beg, end);
418 strcpy(&rfc822[beg], &rfc822[pos]);
423 /* If we've hit the end of the message, bail out */
424 if (pos > strlen(rfc822)) done = 1;
427 /* Follow-up sanity checks... */
429 /* If there's no timestamp on this message, set it to now. */
430 if (msg->cm_fields['T'] == NULL) {
431 sprintf(buf, "%ld", time(NULL));
432 msg->cm_fields['T'] = strdoop(buf);
435 lprintf(9, "RFC822 length remaining after conversion = %d\n",