internet_addressing.c: minor cleanups
[citadel.git] / citadel / server / internet_addressing.c
index 05a0e900a236a04de7523ae8d2b128603d3f08bb..09d7fd1315a1ad7b380e84e5c3a644597872dd39 100644 (file)
@@ -1,10 +1,9 @@
 // This file contains functions which handle the mapping of Internet addresses
 // to users on the Citadel system.
 //
-// Copyright (c) 1987-2023 by the citadel.org team
+// Copyright (c) 1987-2024 by the citadel.org team
 //
-// This program is open source software.  Use, duplication, or disclosure
-// is subject to the terms of the GNU General Public License, version 3.
+// This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License version 3.
 
 #include "sysdep.h"
 #include <stdlib.h>
@@ -36,7 +35,6 @@
 #include "ctdl_module.h"
 
 
-
 char *inetcfg = NULL;
 
 // Return nonzero if the supplied name is an alias for this host.
@@ -89,7 +87,7 @@ int CtdlIsMe(char *addr, int addr_buf_len) {
        struct recptypes *recp;
        int i;
 
-       recp = validate_recipients(addr, NULL, 0);
+       recp = validate_recipients(addr, 0);
        if (recp == NULL) return(0);
 
        if (recp->num_local == 0) {
@@ -297,9 +295,8 @@ Array *split_recps(char *addresses, Array *append_to) {
 //
 // Caller needs to free the result using free_recipients()
 //
-struct recptypes *validate_recipients(char *supplied_recipients, const char *RemoteIdentifier, int Flags) {
+struct recptypes *validate_recipients(char *recipients_in, int Flags) {
        struct recptypes *ret;
-       char *recipients = NULL;
        char append[SIZ];
        long len;
        int mailtype;
@@ -315,14 +312,7 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
        if (ret == NULL) return(NULL);
        memset(ret, 0, sizeof(struct recptypes));                                       // set all values to null/zero
 
-       if (supplied_recipients == NULL) {
-               recipients = strdup("");
-       }
-       else {
-               recipients = strdup(supplied_recipients);
-       }
-
-       len = strlen(recipients) + 1024;                                                // allocate memory
+       len = strlen(recipients_in) + 1024;                                             // allocate memory
        ret->errormsg = malloc(len);
        ret->recp_local = malloc(len);
        ret->recp_internet = malloc(len);
@@ -338,7 +328,7 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
        ret->display_recp[0] = 0;
        ret->recptypes_magic = RECPTYPES_MAGIC;
 
-       Array *recp_array = split_recps(supplied_recipients, NULL);
+       Array *recp_array = split_recps(recipients_in, NULL);
 
        char *aliases = CtdlGetSysConfig(GLOBAL_ALIASES);                               // First hit the Global Alias Table
 
@@ -348,7 +338,7 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
                strncpy(this_recp, org_recp, sizeof this_recp);
 
                int i;
-               for (i=0; i<3; ++i) {                                           // pass three times through the aliaser
+               for (i=0; i<3; ++i) {                                                   // pass three times through the aliaser
                        mailtype = expand_aliases(this_recp, aliases);
        
                        // If an alias expanded to multiple recipients, strip off those recipients and append them
@@ -510,7 +500,6 @@ struct recptypes *validate_recipients(char *supplied_recipients, const char *Rem
                ret->num_local, ret->num_room, ret->num_internet, ret->num_error
        );
 
-       free(recipients);
        if (recp_array) {
                array_free(recp_array);
        }
@@ -677,12 +666,8 @@ void unfold_rfc822_field(char **field, char **FieldEnd)
 
        *FieldEnd = pFieldEnd;
        // convert non-space whitespace to spaces, and remove double blanks
-       for (sField = *field = pField; 
-            sField < pFieldEnd; 
-            pField++, sField++)
-       {
-               if ((*sField=='\r') || (*sField=='\n'))
-               {
+       for (sField = *field = pField; sField < pFieldEnd; pField++, sField++) {
+               if ((*sField=='\r') || (*sField=='\n')) {
                        int offset = 1;
                        while ( ( (*(sField + offset) == '\r') || (*(sField + offset) == '\n' )) && (sField + offset < pFieldEnd) ) {
                                offset ++;
@@ -698,8 +683,7 @@ void unfold_rfc822_field(char **field, char **FieldEnd)
                                        pField++;
                                        sField++;
                                        
-                                       while ((sField < pFieldEnd) && 
-                                              isspace(*sField))
+                                       while ((sField < pFieldEnd) && isspace(*sField))
                                                sField++;
                                        *pField = *sField;
                                }
@@ -834,7 +818,7 @@ int convert_field(struct CtdlMessage *msg, const char *beg, const char *end) {
                if (*pos == ':') colonpos = pos;
        }
 
-       if (colonpos == NULL) return(0);        /* no colon? not a valid header line */
+       if (colonpos == NULL) return(0);        // no colon? not a valid header line
 
        len = end - beg;
        key = malloc(len + 2);
@@ -954,8 +938,6 @@ int convert_field(struct CtdlMessage *msg, const char *beg, const char *end) {
                processed = 1;
        }
 
-
-
        // Clean up and move on.
        free(key);      // Don't free 'value', it's actually the same buffer
        return processed;
@@ -1029,9 +1011,8 @@ struct CtdlMessage *convert_internet_message_buf(StrBuf **rfc822) {
 
        while (!alldone) {
 
-               /* Locate beginning and end of field, keeping in mind that
-                * some fields might be multiline
-                */
+               // Locate beginning and end of field, keeping in mind that
+               // some fields might be multiline
                end = beg = pos;
 
                while ((end < totalend) && (end == beg) && (done == 0) ) {
@@ -1040,7 +1021,7 @@ struct CtdlMessage *convert_internet_message_buf(StrBuf **rfc822) {
                                end = pos;
                        }
 
-                       /* done with headers? */
+                       // done with headers?
                        if ((*pos=='\n') && ( (*(pos+1)=='\n') || (*(pos+1)=='\r')) ) {
                                alldone = 1;
                        }
@@ -1051,38 +1032,37 @@ struct CtdlMessage *convert_internet_message_buf(StrBuf **rfc822) {
                        }
 
                        ++pos;
-
                }
 
-               /* At this point we have a field.  Are we interested in it? */
+               // At this point we have a field.  Are we interested in it?
                converted = convert_field(msg, beg, end);
 
-               /* Strip the field out of the RFC822 header if we used it */
+               // Strip the field out of the RFC822 header if we used it
                if (!converted) {
                        StrBufAppendBufPlain(OtherHeaders, beg, end - beg, 0);
                        StrBufAppendBufPlain(OtherHeaders, HKEY("\n"), 0);
                }
 
-               /* If we've hit the end of the message, bail out */
+               // If we've hit the end of the message, bail out
                if (pos >= totalend)
                        alldone = 1;
        }
        StrBufAppendBufPlain(OtherHeaders, HKEY("\n"), 0);
-       if (pos < totalend)
+       if (pos < totalend) {
                StrBufAppendBufPlain(OtherHeaders, pos, totalend - pos, 0);
+       }
        FreeStrBuf(rfc822);
-       CM_SetAsFieldSB(msg, eMesageText, &OtherHeaders);
+       CM_SetAsFieldSB(msg, eMessageText, &OtherHeaders);
 
-       /* Follow-up sanity checks... */
+       // Follow-up sanity checks...
 
-       /* If there's no timestamp on this message, set it to now. */
+       // If there's no timestamp on this message, set it to now.
        if (CM_IsEmpty(msg, eTimestamp)) {
                CM_SetFieldLONG(msg, eTimestamp, time(NULL));
        }
 
-       /* If a W (references, or rather, Wefewences) field is present, we
-        * have to convert it from RFC822 format to Citadel format.
-        */
+       // If a W (references, or rather, Wefewences) field is present, we
+       // have to convert it from RFC822 format to Citadel format.
        if (!CM_IsEmpty(msg, eWeferences)) {
                /// todo: API!
                convert_references_to_wefewences(msg->cm_fields[eWeferences]);
@@ -1092,13 +1072,11 @@ struct CtdlMessage *convert_internet_message_buf(StrBuf **rfc822) {
 }
 
 
-/*
- * Look for a particular header field in an RFC822 message text.  If the
- * requested field is found, it is unfolded (if necessary) and returned to
- * the caller.  The field name is stripped out, leaving only its contents.
- * The caller is responsible for freeing the returned buffer.  If the requested
- * field is not present, or anything else goes wrong, it returns NULL.
- */
+// Look for a particular header field in an RFC822 message text.  If the
+// requested field is found, it is unfolded (if necessary) and returned to
+// the caller.  The field name is stripped out, leaving only its contents.
+// The caller is responsible for freeing the returned buffer.  If the requested
+// field is not present, or anything else goes wrong, it returns NULL.
 char *rfc822_fetch_field(const char *rfc822, const char *fieldname) {
        char *fieldbuf = NULL;
        const char *end_of_headers;
@@ -1107,13 +1085,13 @@ char *rfc822_fetch_field(const char *rfc822, const char *fieldname) {
        char *cont;
        char fieldhdr[SIZ];
 
-       /* Should never happen, but sometimes we get stupid */
+       // Should never happen, but sometimes we get stupid
        if (rfc822 == NULL) return(NULL);
        if (fieldname == NULL) return(NULL);
 
        snprintf(fieldhdr, sizeof fieldhdr, "%s:", fieldname);
 
-       /* Locate the end of the headers, so we don't run past that point */
+       // Locate the end of the headers, so we don't run past that point
        end_of_headers = cbmstrcasestr(rfc822, "\n\r\n");
        if (end_of_headers == NULL) {
                end_of_headers = cbmstrcasestr(rfc822, "\n\n");
@@ -1143,14 +1121,12 @@ char *rfc822_fetch_field(const char *rfc822, const char *fieldname) {
 }
 
 
-/*****************************************************************************
- *                      DIRECTORY MANAGEMENT FUNCTIONS                       *
- *****************************************************************************/
+//     *****************************************************************************
+//                          DIRECTORY MANAGEMENT FUNCTIONS                       
+//     *****************************************************************************
 
-/*
- * Generate the index key for an Internet e-mail address to be looked up
- * in the database.
- */
+// Generate the index key for an Internet e-mail address to be looked up
+// in the database.
 void directory_key(char *key, char *addr) {
        int i;
        int keylen = 0;
@@ -1161,14 +1137,11 @@ void directory_key(char *key, char *addr) {
                }
        }
        key[keylen++] = 0;
-
        syslog(LOG_DEBUG, "internet_addressing: directory key is <%s>", key);
 }
 
 
-/*
- * Return nonzero if the supplied address is in one of "our" domains
- */
+// Return nonzero if the supplied address is in one of "our" domains
 int IsDirectory(char *addr, int allow_masq_domains) {
        char domain[256];
        int h;
@@ -1190,9 +1163,7 @@ int IsDirectory(char *addr, int allow_masq_domains) {
 }
 
 
-/*
- * Add an Internet e-mail address to the directory for a user
- */
+// Add an Internet e-mail address to the directory for a user
 int CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
        char key[SIZ];
 
@@ -1206,12 +1177,10 @@ int CtdlDirectoryAddUser(char *internet_addr, char *citadel_addr) {
 }
 
 
-/*
- * Delete an Internet e-mail address from the directory.
- *
- * (NOTE: we don't actually use or need the citadel_addr variable; it's merely
- * here because the callback API expects to be able to send it.)
- */
+// Delete an Internet e-mail address from the directory.
+//
+// (NOTE: we don't actually use or need the citadel_addr variable; it's merely
+// here because the callback API expects to be able to send it.)
 int CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
        char key[SIZ];
        
@@ -1221,24 +1190,22 @@ int CtdlDirectoryDelUser(char *internet_addr, char *citadel_addr) {
 }
 
 
-/*
- * Look up an Internet e-mail address in the directory.
- * On success: returns 0, and Citadel address stored in 'target'
- * On failure: returns nonzero
- */
+// Look up an Internet e-mail address in the directory.
+// On success: returns 0, and Citadel address stored in 'target'
+// On failure: returns nonzero
 int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
        struct cdbdata cdbrec;
        char key[SIZ];
 
-       /* Dump it in there unchanged, just for kicks */
+       // Dump it in there unchanged, just for kicks
        if (target != NULL) {
                safestrncpy(target, internet_addr, targbuflen);
        }
 
-       /* Only do lookups for addresses with hostnames in them */
+       // Only do lookups for addresses with hostnames in them
        if (num_tokens(internet_addr, '@') != 2) return(-1);
 
-       /* Only do lookups for domains in the directory */
+       // Only do lookups for domains in the directory
        if (IsDirectory(internet_addr, 0) == 0) return(-1);
 
        directory_key(key, internet_addr);
@@ -1254,10 +1221,7 @@ int CtdlDirectoryLookup(char *target, char *internet_addr, size_t targbuflen) {
 }
 
 
-/*
- * Harvest any email addresses that someone might want to have in their
- * "collected addresses" book.
- */
+// Harvest any email addresses that someone might want to have in their "collected addresses" book.
 char *harvest_collected_addresses(struct CtdlMessage *msg) {
        char *coll = NULL;
        char addr[256];
@@ -1291,7 +1255,7 @@ char *harvest_collected_addresses(struct CtdlMessage *msg) {
 
        if (coll == NULL) return(NULL);
 
-       /* Scan both the R (To) and Y (CC) fields */
+       // Scan both the R (To) and Y (CC) fields
        for (i = 0; i < 2; ++i) {
                if (i == 0) field = eRecipient;
                if (i == 1) field = eCarbonCopY;
@@ -1325,9 +1289,7 @@ char *harvest_collected_addresses(struct CtdlMessage *msg) {
 }
 
 
-/*
- * Helper function for CtdlRebuildDirectoryIndex()
- */
+// Helper function for CtdlRebuildDirectoryIndex()
 void CtdlRebuildDirectoryIndex_backend(char *username, void *data) {
 
        int j = 0;
@@ -1347,9 +1309,7 @@ void CtdlRebuildDirectoryIndex_backend(char *username, void *data) {
 }
 
 
-/*
- * Initialize the directory database (erasing anything already there)
- */
+// Initialize the directory database (erasing anything already there)
 void CtdlRebuildDirectoryIndex(void) {
        syslog(LOG_INFO, "internet_addressing: rebuilding email address directory index");
        cdb_trunc(CDB_DIRECTORY);
@@ -1387,9 +1347,7 @@ void CtdlSetEmailAddressesForUser(char *requested_user, char *new_emailaddrs) {
 }
 
 
-/*
- * Auto-generate an Internet email address for a user account
- */
+// Auto-generate an Internet email address for a user account
 void AutoGenerateEmailAddressForUser(struct ctdluser *user) {
        char synthetic_email_addr[1024];
        int i, j;