4 * This file contains functions which handle the mapping of Internet addresses
5 * to users on the Citadel system.
16 #include <sys/types.h>
24 #include "sysdep_decls.h"
25 #include "citserver.h"
29 #include "internet_addressing.h"
34 * Return 0 if a given string fuzzy-matches a Citadel user account
36 * FIX ... this needs to be updated to match any and all address syntaxes.
38 int fuzzy_match(struct usersupp *us, char *matchstring) {
41 for (a=0; a<strlen(us->fullname); ++a) {
42 if (!strncasecmp(&us->fullname[a],
43 matchstring, strlen(matchstring))) {
54 * Split an RFC822-style address into userid, host, and full name
55 * (Originally from citmail.c, and unchanged so far)
58 void process_rfc822_addr(char *rfc822, char *user, char *node, char *name)
63 strcpy(node, config.c_fqdn);
66 /* extract full name - first, it's From minus <userid> */
68 for (a = 0; a < strlen(name); ++a) {
71 strcpy(&name[a], &name[a + 1]);
72 } while ((strlen(name) > 0) && (name[a] != '>'));
73 strcpy(&name[a], &name[a + 1]);
76 /* strip anything to the left of a bang */
77 while ((strlen(name) > 0) && (haschar(name, '!') > 0))
78 strcpy(name, &name[1]);
80 /* and anything to the right of a @ or % */
81 for (a = 0; a < strlen(name); ++a) {
88 /* but if there are parentheses, that changes the rules... */
89 if ((haschar(rfc822, '(') == 1) && (haschar(rfc822, ')') == 1)) {
91 while ((strlen(name) > 0) && (name[0] != '(')) {
92 strcpy(&name[0], &name[1]);
94 strcpy(&name[0], &name[1]);
95 for (a = 0; a < strlen(name); ++a) {
101 /* but if there are a set of quotes, that supersedes everything */
102 if (haschar(rfc822, 34) == 2) {
103 strcpy(name, rfc822);
104 while ((strlen(name) > 0) && (name[0] != 34)) {
105 strcpy(&name[0], &name[1]);
107 strcpy(&name[0], &name[1]);
108 for (a = 0; a < strlen(name); ++a)
112 /* extract user id */
113 strcpy(user, rfc822);
115 /* first get rid of anything in parens */
116 for (a = 0; a < strlen(user); ++a)
117 if (user[a] == '(') {
119 strcpy(&user[a], &user[a + 1]);
120 } while ((strlen(user) > 0) && (user[a] != ')'));
121 strcpy(&user[a], &user[a + 1]);
123 /* if there's a set of angle brackets, strip it down to that */
124 if ((haschar(user, '<') == 1) && (haschar(user, '>') == 1)) {
125 while ((strlen(user) > 0) && (user[0] != '<')) {
126 strcpy(&user[0], &user[1]);
128 strcpy(&user[0], &user[1]);
129 for (a = 0; a < strlen(user); ++a)
133 /* strip anything to the left of a bang */
134 while ((strlen(user) > 0) && (haschar(user, '!') > 0))
135 strcpy(user, &user[1]);
137 /* and anything to the right of a @ or % */
138 for (a = 0; a < strlen(user); ++a) {
146 /* extract node name */
147 strcpy(node, rfc822);
149 /* first get rid of anything in parens */
150 for (a = 0; a < strlen(node); ++a)
151 if (node[a] == '(') {
153 strcpy(&node[a], &node[a + 1]);
154 } while ((strlen(node) > 0) && (node[a] != ')'));
155 strcpy(&node[a], &node[a + 1]);
157 /* if there's a set of angle brackets, strip it down to that */
158 if ((haschar(node, '<') == 1) && (haschar(node, '>') == 1)) {
159 while ((strlen(node) > 0) && (node[0] != '<')) {
160 strcpy(&node[0], &node[1]);
162 strcpy(&node[0], &node[1]);
163 for (a = 0; a < strlen(node); ++a)
167 /* strip anything to the left of a @ */
168 while ((strlen(node) > 0) && (haschar(node, '@') > 0))
169 strcpy(node, &node[1]);
171 /* strip anything to the left of a % */
172 while ((strlen(node) > 0) && (haschar(node, '%') > 0))
173 strcpy(node, &node[1]);
175 /* reduce multiple system bang paths to node!user */
176 while ((strlen(node) > 0) && (haschar(node, '!') > 1))
177 strcpy(node, &node[1]);
179 /* now get rid of the user portion of a node!user string */
180 for (a = 0; a < strlen(node); ++a)
184 /* strip leading and trailing spaces in all strings */
193 * Back end for convert_internet_address()
194 * (Compares an internet name [buffer1] and stores in [buffer2] if found)
196 void try_name(struct usersupp *us) {
198 if (!strncasecmp(CC->buffer1, "cit", 3))
199 if (atol(&CC->buffer1[3]) == us->usernum)
200 strcpy(CC->buffer2, us->fullname);
202 if (!collapsed_strcmp(CC->buffer1, us->fullname))
203 strcpy(CC->buffer2, us->fullname);
205 if (us->uid != BBSUID)
206 if (!strcasecmp(CC->buffer1, getpwuid(us->uid)->pw_name))
207 strcpy(CC->buffer2, us->fullname);
213 * Convert an Internet email address to a Citadel user/host combination
215 int convert_internet_address(char *destuser, char *desthost, char *source)
222 process_rfc822_addr(source, user, node, name);
224 /* Map the FQDN to a Citadel node name
225 * FIX ... we have to check for all known aliases for the local
226 * system, and also handle gateway domains, etc. etc.
228 if (!strcasecmp(node, config.c_fqdn)) {
229 strcpy(node, config.c_nodename);
232 /* Return an error condition if the node is not known.
233 * FIX ... make this work for non-local systems
235 if (strcasecmp(node, config.c_nodename)) {
236 return(rfc822_address_invalid);
239 /* Now try to resolve the name
240 * FIX ... do the multiple-addresses thing
242 if (!strcasecmp(node, config.c_nodename)) {
243 strcpy(destuser, user);
244 strcpy(desthost, config.c_nodename);
245 strcpy(CC->buffer1, user);
246 strcpy(CC->buffer2, "");
247 ForEachUser(try_name);
248 if (strlen(CC->buffer2) == 0) return(rfc822_no_such_user);
249 strcpy(destuser, CC->buffer2);
250 return(rfc822_address_locally_validated);
253 return(rfc822_address_invalid); /* unknown error */
261 * Convert an RFC822 message (headers + body) to a CtdlMessage structure.
263 struct CtdlMessage *convert_internet_message(char *rfc822) {
265 struct CtdlMessage *msg;
268 msg = mallok(sizeof(struct CtdlMessage));
269 if (msg == NULL) return msg;
271 memset(msg, 0, sizeof(struct CtdlMessage));
272 msg->cm_magic = CTDLMESSAGE_MAGIC; /* self check */
273 msg->cm_anon_type = 0; /* never anonymous */
274 msg->cm_format_type = 4; /* always MIME */
275 msg->cm_fields['M'] = rfc822;
277 /* FIX there's plenty to do here. */
280 /* Follow-up sanity check. */
282 /* If there's no timestamp on this message, set it to now. */
283 if (msg->cm_fields['T'] == NULL) {
284 sprintf(buf, "%ld", time(NULL));
285 msg->cm_fields['T'] = strdoop(buf);