]> code.citadel.org Git - citadel.git/blobdiff - citadel/internet_addressing.c
Initial version of global alias table is in place. But it seems to have a Heisenbug.
[citadel.git] / citadel / internet_addressing.c
index 132eba9b76bd0227a2c0df539312de4131902aa1..1da58751440e0842b8176cff7f69f02e088d60e5 100644 (file)
@@ -390,33 +390,50 @@ enum {
        EA_LOCAL,               // Local message, do no network processing
        EA_INTERNET             // Convert msg and send as Internet mail
 };
+char *killo[] = {              // FIXME remove this when diags are complete
+       "error",
+       "multiple",
+       "local",
+       "internet"
+};
 
 
-/*
- * Aliasing for network mail.
- */
-int expand_aliases(char *name) {                               /* process alias and routing info for mail */
+// Process alias and routing info for email addresses
+int expand_aliases(char *name) {
        int a;
        char aaa[SIZ];
        int at = 0;
        char node[64];
+       char *t;
 
+       char *aliases = CtdlGetSysConfig(GLOBAL_ALIASES);       // First hit the Global Alias Table
+       if (aliases) {
+               char *aptr = aliases;
+               while ((t = strtok_r(aptr, "\n", &aptr))) {
+                       char *bar = strchr(t, '|');
+                       if (bar) {
+                               bar[0] = 0;
+                               ++bar;
+                               if (!strcasecmp(name, t)) {
+                                       syslog(LOG_DEBUG, "\033[36mAliasing <%s> to <%s>\033[0m", name, bar);
+                                       strcpy(name, bar);
+                               }
+                       }
+               }
 
-
-
-       // temporary test of expansion
-       if (!strcasecmp(name, "root")) {
-               strcpy(name, "foo,bar,root,baz");
-               return(EA_MULTIPLE);
+               free(aliases);
+               if (strchr(name, ',')) {
+                       return(EA_MULTIPLE);
+               }
        }
 
 
 
 
-
        char original_name[256];
        safestrncpy(original_name, name, sizeof original_name);
 
+       // should these checks still be here, or maybe move them to split_recps() ?
        striplt(name);
        remove_any_whitespace_to_the_left_or_right_of_at_symbol(name);
        stripallbut(name, '<', '>');
@@ -521,8 +538,7 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
        int mailtype;
        int invalid;
        struct ctdluser tempUS;
-       struct ctdlroom tempQR;
-       struct ctdlroom tempQR2;
+       struct ctdlroom original_room;
        int err = 0;
        char errmsg[SIZ];
        char *org_recp;
@@ -530,7 +546,6 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
 
        ret = (struct recptypes *) malloc(sizeof(struct recptypes));                    // Initialize
        if (ret == NULL) return(NULL);
-
        memset(ret, 0, sizeof(struct recptypes));                                       // set all values to null/zero
 
        if (supplied_recipients == NULL) {
@@ -554,43 +569,44 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
        ret->recp_room[0] = 0;
        ret->recp_orgroom[0] = 0;
        ret->display_recp[0] = 0;
-
        ret->recptypes_magic = RECPTYPES_MAGIC;
 
-
        Array *recp_array = split_recps(supplied_recipients);
-
-
-
+       int original_array_len = array_len(recp_array);
        for (int r=0; r<array_len(recp_array); ++r) {
                org_recp = (char *)array_get_element_at(recp_array, r);
                strncpy(this_recp, org_recp, sizeof this_recp);
 
+               mailtype = expand_aliases(this_recp);
 
-
-               // To prevent endless aliasing loops, we will only do this five times.
-               for (int alias_loop=0; alias_loop<5; ++alias_loop) {
-                       mailtype = expand_aliases(this_recp);
-                       if (mailtype == EA_MULTIPLE) {
-                               // If an alias expanded to multiple recipients, strip off those recipients and append them
-                               // to the end of the array.  This loop will hit those again when it gets there.
+               // If an alias expanded to multiple recipients, strip off those recipients and append them
+               // to the end of the array.  This loop will hit those again when it gets there.
+               // Note that we don't do this after we get past the *original* array length, to avoid aliasing loops.
+               if (mailtype == EA_MULTIPLE) {
+                       if (r < original_array_len) {
                                char *comma;
-                               while (comma = strrchr(this_recp, ',')) {
+                               while ((comma = strrchr(this_recp, ','))) {
                                        comma[0] = 0;
                                        array_append(recp_array, &comma[1]);
                                        strcpy(org_recp, this_recp);
                                }
                        }
+                       else {
+                               mailtype = EA_ERROR;
+                       }
                }
 
-
-               syslog(LOG_DEBUG, "this_recp: \033[31m%s\033[0m   org_recp: \033[32m%s\033[0m", this_recp, org_recp);
-
+               mailtype = expand_aliases(this_recp);           // do it ONCE again to handle alias expansions
+               if (mailtype == EA_MULTIPLE) {
+                       mailtype = EA_ERROR;                    // and fail if it wants to expand a second time
+               }
 
                invalid = 0;
                errmsg[0] = 0;
                switch(mailtype) {
-               case EA_LOCAL:
+               case EA_LOCAL:                                  // There are several types of "local" recipients.
+
+                       // Old BBS conventions require mail to "sysop" to go somewhere.  Send it to the admin room.
                        if (!strcasecmp(this_recp, "sysop")) {
                                ++ret->num_room;
                                strcpy(this_recp, CtdlGetConfigStr("c_aideroom"));
@@ -599,45 +615,55 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
                                }
                                strcat(ret->recp_room, this_recp);
                        }
-                       else if ( (!strncasecmp(this_recp, "room_", 5)) && (!CtdlGetRoom(&tempQR, &this_recp[5])) ) {
-
 
-                               // FIXME -- handle the underscores
+                       // This handles rooms which can receive posts via email.
+                       else if (!strncasecmp(this_recp, "room_", 5)) {
+                               original_room = CC->room;                               // Remember where we parked
 
-
-                                                                                       // Save room so we can restore it later
-                               tempQR2 = CC->room;
-                               CC->room = tempQR;
-                                       
-                               err = CtdlDoIHavePermissionToPostInThisRoom(            // check for write permissions to room
-                                       errmsg, 
-                                       sizeof errmsg, 
-                                       RemoteIdentifier,
-                                       Flags,
-                                       0                                               // 0 means "not a reply"
-                               );
-                               if (err) {
+                               char mail_to_room[ROOMNAMELEN];
+                               char *m;
+                               strncpy(mail_to_room, &this_recp[5], sizeof mail_to_room);
+                               for (m = mail_to_room; *m; ++m) {
+                                       if (m[0] == '_') m[0]=' ';
+                               }
+                               if (!CtdlGetRoom(&CC->room, mail_to_room)) {            // Find the room they asked for
+
+                                       err = CtdlDoIHavePermissionToPostInThisRoom(    // check for write permissions to room
+                                               errmsg, 
+                                               sizeof errmsg, 
+                                               RemoteIdentifier,
+                                               Flags,
+                                               0                                       // 0 means "this is not a reply"
+                                       );
+                                       if (err) {
+                                               ++ret->num_error;
+                                               invalid = 1;
+                                       } 
+                                       else {
+                                               ++ret->num_room;
+                                               if (!IsEmptyStr(ret->recp_room)) {
+                                                       strcat(ret->recp_room, "|");
+                                               }
+                                               strcat(ret->recp_room, &this_recp[5]);
+       
+                                               if (!IsEmptyStr(ret->recp_orgroom)) {
+                                                       strcat(ret->recp_orgroom, "|");
+                                               }
+                                               strcat(ret->recp_orgroom, org_recp);
+       
+                                       }
+                               }
+                               else {                                                  // no such room exists
                                        ++ret->num_error;
                                        invalid = 1;
-                               } 
-                               else {
-                                       ++ret->num_room;
-                                       if (!IsEmptyStr(ret->recp_room)) {
-                                               strcat(ret->recp_room, "|");
-                                       }
-                                       strcat(ret->recp_room, &this_recp[5]);
-
-                                       if (!IsEmptyStr(ret->recp_orgroom)) {
-                                               strcat(ret->recp_orgroom, "|");
-                                       }
-                                       strcat(ret->recp_orgroom, org_recp);
-
                                }
-                                       
-                               /* Restore room in case something needs it */
-                               CC->room = tempQR2;
+                                               
+                               // Restore this session's original room location.
+                               CC->room = original_room;
 
                        }
+
+                       // This handles the most common case, which is mail to a user's inbox.
                        else if (CtdlGetUser(&tempUS, this_recp) == 0) {
                                ++ret->num_local;
                                strcpy(this_recp, tempUS.fullname);
@@ -646,18 +672,17 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
                                }
                                strcat(ret->recp_local, this_recp);
                        }
+
+                       // No match for this recipient
                        else {
                                ++ret->num_error;
                                invalid = 1;
                        }
                        break;
                case EA_INTERNET:
-                       /* Yes, you're reading this correctly: if the target
-                        * domain points back to the local system,
-                        * the address is invalid.  That's
-                        * because if the address were valid, we would have
-                        * already translated it to a local address by now.
-                        */
+                       // Yes, you're reading this correctly: if the target domain points back to the local system,
+                       // the address is invalid.  That's because if the address were valid, we would have
+                       // already translated it to a local address by now.
                        if (IsDirectory(this_recp, 0)) {
                                ++ret->num_error;
                                invalid = 1;
@@ -700,6 +725,7 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
                                strcat(ret->display_recp, append);
                        }
                }
+               syslog(LOG_DEBUG, "org_recp: \033[31m%-30s\033[0m   this_recp: \033[32m%-30s\033[0m   mailtype: %s", org_recp, this_recp, killo[mailtype]);
        }
 
        if ( (ret->num_local + ret->num_internet + ret->num_room + ret->num_error) == 0) {