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"
37 * Return 0 if a given string fuzzy-matches a Citadel user account
39 * FIX ... this needs to be updated to match any and all address syntaxes.
41 int fuzzy_match(struct usersupp *us, char *matchstring) {
44 for (a=0; a<strlen(us->fullname); ++a) {
45 if (!strncasecmp(&us->fullname[a],
46 matchstring, strlen(matchstring))) {
55 * Unfold a multi-line field into a single line, removing multi-whitespaces
57 void unfold_rfc822_field(char *field) {
61 striplt(field); /* remove leading/trailing whitespace */
63 /* convert non-space whitespace to spaces, and remove double blanks */
64 for (i=0; i<strlen(field); ++i) {
65 if (field[i]=='\"') quote = 1 - quote;
67 if (isspace(field[i])) field[i] = ' ';
68 while (isspace(field[i]) && isspace(field[i+1])) {
69 strcpy(&field[i+1], &field[i+2]);
78 * Split an RFC822-style address into userid, host, and full name
79 * (Originally from citmail.c, and unchanged so far)
82 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
87 strcpy(node, config.c_fqdn);
90 /* extract full name - first, it's From minus <userid> */
92 for (a = 0; a < strlen(name); ++a) {
95 strcpy(&name[a], &name[a + 1]);
96 } while ((strlen(name) > 0) && (name[a] != '>'));
97 strcpy(&name[a], &name[a + 1]);
100 /* strip anything to the left of a bang */
101 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
102 strcpy(name, &name[1]);
104 /* and anything to the right of a @ or % */
105 for (a = 0; a < strlen(name); ++a) {
112 /* but if there are parentheses, that changes the rules... */
113 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
114 strcpy(name, rfc822);
115 while ((strlen(name) > 0) && (name[0] != '(')) {
116 strcpy(&name[0], &name[1]);
118 strcpy(&name[0], &name[1]);
119 for (a = 0; a < strlen(name); ++a) {
120 if (name[a] == ')') {
125 /* but if there are a set of quotes, that supersedes everything */
126 if (haschar(rfc822, 34) == 2) {
127 strcpy(name, rfc822);
128 while ((strlen(name) > 0) && (name[0] != 34)) {
129 strcpy(&name[0], &name[1]);
131 strcpy(&name[0], &name[1]);
132 for (a = 0; a < strlen(name); ++a)
136 /* extract user id */
137 strcpy(user, rfc822);
139 /* first get rid of anything in parens */
140 for (a = 0; a < strlen(user); ++a)
141 if (user[a] == '(') {
143 strcpy(&user[a], &user[a + 1]);
144 } while ((strlen(user) > 0) && (user[a] != ')'));
145 strcpy(&user[a], &user[a + 1]);
147 /* if there's a set of angle brackets, strip it down to that */
148 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
149 while ((strlen(user) > 0) && (user[0] != '<')) {
150 strcpy(&user[0], &user[1]);
152 strcpy(&user[0], &user[1]);
153 for (a = 0; a < strlen(user); ++a)
157 /* strip anything to the left of a bang */
158 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
159 strcpy(user, &user[1]);
161 /* and anything to the right of a @ or % */
162 for (a = 0; a < strlen(user); ++a) {
170 /* extract node name */
171 strcpy(node, rfc822);
173 /* first get rid of anything in parens */
174 for (a = 0; a < strlen(node); ++a)
175 if (node[a] == '(') {
177 strcpy(&node[a], &node[a + 1]);
178 } while ((strlen(node) > 0) && (node[a] != ')'));
179 strcpy(&node[a], &node[a + 1]);
181 /* if there's a set of angle brackets, strip it down to that */
182 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
183 while ((strlen(node) > 0) && (node[0] != '<')) {
184 strcpy(&node[0], &node[1]);
186 strcpy(&node[0], &node[1]);
187 for (a = 0; a < strlen(node); ++a)
191 /* strip anything to the left of a @ */
192 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
193 strcpy(node, &node[1]);
195 /* strip anything to the left of a % */
196 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
197 strcpy(node, &node[1]);
199 /* reduce multiple system bang paths to node!user */
200 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
201 strcpy(node, &node[1]);
203 /* now get rid of the user portion of a node!user string */
204 for (a = 0; a < strlen(node); ++a)
208 /* strip leading and trailing spaces in all strings */
217 * Back end for convert_internet_address()
218 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
220 void try_name(struct usersupp *us) {
222 if (!strncasecmp(CC->buffer1, "cit", 3))
223 if (atol(&CC->buffer1[3]) == us->usernum)
224 strcpy(CC->buffer2, us->fullname);
226 if (!collapsed_strcmp(CC->buffer1, us->fullname))
227 strcpy(CC->buffer2, us->fullname);
229 if (us->uid != BBSUID)
230 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
231 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)
243 struct quickroom qrbuf;
246 process_rfc822_addr(source, user, node, name);
248 /* Map the FQDN to a Citadel node name
249 * FIX ... we have to check for all known aliases for the local
250 * system, and also handle gateway domains, etc. etc.
252 if (!strcasecmp(node, config.c_fqdn)) {
253 strcpy(node, config.c_nodename);
256 /* Now try to resolve the name
257 * FIX ... do the multiple-addresses thing
259 if (!strcasecmp(node, config.c_nodename)) {
260 /* Try all local rooms */
261 if (!strncasecmp(destuser, "room_", 5)) {
262 /* FIX do this here */
265 /* Try all local users */
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 strcpy(destuser, user);
277 strcpy(desthost, node);
278 return(rfc822_address_invalid); /* unknown error */
285 * convert_field() is a helper function for convert_internet_message().
286 * Given start/end positions for an rfc822 field, it converts it to a Citadel
287 * field if it wants to, and unfolds it if necessary.
289 * Returns 1 if the field was converted and inserted into the Citadel message
290 * structure, implying that the source field should be removed from the
293 int convert_field(struct CtdlMessage *msg, int beg, int end) {
300 char *user, *node, *name;
302 rfc822 = msg->cm_fields['M']; /* M field contains rfc822 text */
303 for (i = end; i >= beg; --i) {
304 if (rfc822[i] == ':') colonpos = i;
307 if (colonpos < 0) return(0); /* no colon? not a valid header line */
309 key = mallok((end - beg) + 2);
310 safestrncpy(key, &rfc822[beg], (end-beg)+1);
311 key[colonpos - beg] = 0;
312 value = &key[(colonpos - beg) + 1];
313 unfold_rfc822_field(value);
315 lprintf(9, "Key=<%s> Value=<%s>\n", key, value);
317 /* Here's the big rfc822-to-citadel loop. */
319 if (!strcasecmp(key, "Date")) {
320 sprintf(buf, "%ld", parsedate(value) );
321 if (msg->cm_fields['T'] == NULL)
322 msg->cm_fields['T'] = strdoop(buf);
326 else if (!strcasecmp(key, "From")) {
327 user = mallok(strlen(value));
328 node = mallok(strlen(value));
329 name = mallok(strlen(value));
330 process_rfc822_addr(value, user, node, name);
331 lprintf(9, "Converted to <%s@%s> (%s)\n", user, node, name);
332 if (msg->cm_fields['A'] == NULL)
333 msg->cm_fields['A'] = user;
336 if (msg->cm_fields['N'] == NULL)
337 msg->cm_fields['N'] = node;
340 if (msg->cm_fields['H'] == NULL)
341 msg->cm_fields['H'] = name;
347 else if (!strcasecmp(key, "Subject")) {
348 if (msg->cm_fields['U'] == NULL)
349 msg->cm_fields['U'] = strdoop(value);
353 /* Clean up and move on. */
354 phree(key); /* Don't free 'value', it's actually the same buffer */
360 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
362 struct CtdlMessage *convert_internet_message(char *rfc822) {
364 struct CtdlMessage *msg;
370 msg = mallok(sizeof(struct CtdlMessage));
371 if (msg == NULL) return msg;
373 memset(msg, 0, sizeof(struct CtdlMessage));
374 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
375 msg->cm_anon_type = 0; /* never anonymous */
376 msg->cm_format_type = FMT_RFC822; /* internet message */
377 msg->cm_fields['M'] = rfc822;
379 lprintf(9, "Unconverted RFC822 message length = %d\n", strlen(rfc822));
385 /* Locate beginning and end of field, keeping in mind that
386 * some fields might be multiline
390 for (pos=beg; ((pos<=strlen(rfc822))&&(end<0)); ++pos) {
391 if ((rfc822[pos]=='\n')
392 && (!isspace(rfc822[pos+1]))) {
395 if ( (rfc822[pos]=='\n') /* done w. headers? */
396 && ( (rfc822[pos+1]=='\n')
397 ||(rfc822[pos+1]=='\r'))) {
404 /* At this point we have a field. Are we interested in it? */
405 converted = convert_field(msg, beg, end);
409 strcpy(&rfc822[beg], &rfc822[pos]);
414 /* If we've hit the end of the message, bail out */
415 if (pos > strlen(rfc822)) done = 1;
418 /* Follow-up sanity checks... */
420 /* If there's no timestamp on this message, set it to now. */
421 if (msg->cm_fields['T'] == NULL) {
422 sprintf(buf, "%ld", time(NULL));
423 msg->cm_fields['T'] = strdoop(buf);
426 lprintf(9, "RFC822 length remaining after conversion = %d\n",