Back to index

citadel  8.12
journaling.c
Go to the documentation of this file.
00001 /*
00002  * Message journaling functions.
00003  */
00004 
00005 #include "sysdep.h"
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <stdio.h>
00009 #include <fcntl.h>
00010 
00011 #if TIME_WITH_SYS_TIME
00012 # include <sys/time.h>
00013 # include <time.h>
00014 #else
00015 # if HAVE_SYS_TIME_H
00016 #  include <sys/time.h>
00017 # else
00018 #  include <time.h>
00019 # endif
00020 #endif
00021 
00022 
00023 #include <ctype.h>
00024 #include <string.h>
00025 #include <limits.h>
00026 #include <errno.h>
00027 #include <stdarg.h>
00028 #include <sys/stat.h>
00029 #include <libcitadel.h>
00030 #include "citadel.h"
00031 #include "server.h"
00032 #include "database.h"
00033 #include "msgbase.h"
00034 #include "support.h"
00035 #include "sysdep_decls.h"
00036 #include "citserver.h"
00037 #include "room_ops.h"
00038 #include "user_ops.h"
00039 #include "file_ops.h"
00040 #include "config.h"
00041 #include "control.h"
00042 #include "genstamp.h"
00043 #include "internet_addressing.h"
00044 #include "serv_vcard.h"                   /* Needed for vcard_getuser and extract_inet_email_addrs */
00045 #include "journaling.h"
00046 
00047 #include "ctdl_module.h"
00048 #include "threads.h"
00049 
00050 struct jnlq *jnlq = NULL;   /* journal queue */
00051 
00052 /*
00053  * Hand off a copy of a message to be journalized.
00054  */
00055 void JournalBackgroundSubmit(struct CtdlMessage *msg,
00056                      StrBuf *saved_rfc822_version,
00057                      struct recptypes *recps) {
00058 
00059        struct jnlq *jptr = NULL;
00060 
00061        /* Avoid double journaling! */
00062        if (msg->cm_fields['J'] != NULL) {
00063               FreeStrBuf(&saved_rfc822_version);
00064               return;
00065        }
00066 
00067        jptr = (struct jnlq *)malloc(sizeof(struct jnlq));
00068        if (jptr == NULL) {
00069               FreeStrBuf(&saved_rfc822_version);
00070               return;
00071        }
00072        memset(jptr, 0, sizeof(struct jnlq));
00073        if (recps != NULL) memcpy(&jptr->recps, recps, sizeof(struct recptypes));
00074        if (msg->cm_fields['A'] != NULL) jptr->from = strdup(msg->cm_fields['A']);
00075        if (msg->cm_fields['N'] != NULL) jptr->node = strdup(msg->cm_fields['N']);
00076        if (msg->cm_fields['F'] != NULL) jptr->rfca = strdup(msg->cm_fields['F']);
00077        if (msg->cm_fields['U'] != NULL) jptr->subj = strdup(msg->cm_fields['U']);
00078        if (msg->cm_fields['I'] != NULL) jptr->msgn = strdup(msg->cm_fields['I']);
00079        jptr->rfc822 = SmashStrBuf(&saved_rfc822_version);
00080 
00081        /* Add to the queue */
00082        begin_critical_section(S_JOURNAL_QUEUE);
00083        jptr->next = jnlq;
00084        jnlq = jptr;
00085        end_critical_section(S_JOURNAL_QUEUE);
00086 }
00087 
00088 
00089 /*
00090  * Convert a local user name to an internet email address for the journal
00091  */
00092  
00093 /*
00094  * TODODRW: change this into a CtdlModuleDo type function that returns alternative address info
00095  * for this local user. Something like CtdlModuleGoGetAddr(char *localuser, int type, char *alt_addr, size_t alt_addr_len)
00096  * where type can be ADDR_EMAIL, ADDR_FIDO, ADDR_UUCP, ADDR_WEB, ADDR_POSTAL etc etc.
00097  * This then begs the question of what should be returned. Is it wise to return a single char* using a comma as a
00098  * delimiter? Or would it be better to return a linked list of some kind?
00099  */
00100 void local_to_inetemail(char *inetemail, char *localuser, size_t inetemail_len) {
00101        struct ctdluser us;
00102        struct vCard *v;
00103 
00104        strcpy(inetemail, "");
00105        if (CtdlGetUser(&us, localuser) != 0) {
00106               return;
00107        }
00108 
00109        v = vcard_get_user(&us);
00110        if (v == NULL) {
00111               return;
00112        }
00113 
00114        extract_inet_email_addrs(inetemail, inetemail_len, NULL, 0, v, 1);
00115        vcard_free(v);
00116 }
00117 
00118 
00119 /*
00120  * Called by JournalRunQueue() to send an individual message.
00121  */
00122 void JournalRunQueueMsg(struct jnlq *jmsg) {
00123 
00124        struct CtdlMessage *journal_msg = NULL;
00125        struct recptypes *journal_recps = NULL;
00126        char *message_text = NULL;
00127        char mime_boundary[256];
00128        char recipient[256];
00129        char inetemail[256];
00130        static int seq = 0;
00131        int i;
00132 
00133        if (jmsg == NULL)
00134               return;
00135        journal_recps = validate_recipients(config.c_journal_dest, NULL, 0);
00136        if (journal_recps != NULL) {
00137 
00138               if (  (journal_recps->num_local > 0)
00139                  || (journal_recps->num_internet > 0)
00140                  || (journal_recps->num_ignet > 0)
00141                  || (journal_recps->num_room > 0)
00142               ) {
00143 
00144                      /*
00145                       * Construct journal message.
00146                       * Note that we are transferring ownership of some of the memory here.
00147                       */
00148                      journal_msg = malloc(sizeof(struct CtdlMessage));
00149                      memset(journal_msg, 0, sizeof(struct CtdlMessage));
00150                      journal_msg->cm_magic = CTDLMESSAGE_MAGIC;
00151                      journal_msg->cm_anon_type = MES_NORMAL;
00152                      journal_msg->cm_format_type = FMT_RFC822;
00153                      journal_msg->cm_fields['J'] = strdup("is journal");
00154                      journal_msg->cm_fields['A'] = jmsg->from;
00155                      journal_msg->cm_fields['N'] = jmsg->node;
00156                      journal_msg->cm_fields['F'] = jmsg->rfca;
00157                      journal_msg->cm_fields['U'] = jmsg->subj;
00158 
00159                      sprintf(mime_boundary, "--Citadel-Journal-%08lx-%04x--", time(NULL), ++seq);
00160                      message_text = malloc(strlen(jmsg->rfc822) + sizeof(struct recptypes) + 1024);
00161 
00162                      /*
00163                       * Here is where we begin to compose the journalized message.
00164                       * NOTE: the superfluous "Content-Identifer: ExJournalReport" header was
00165                       *       requested by a paying customer, and yes, it is intentionally
00166                       *       spelled wrong.  Do NOT remove or change it.
00167                       */
00168                      sprintf(message_text,
00169                             "Content-type: multipart/mixed; boundary=\"%s\"\r\n"
00170                             "Content-Identifer: ExJournalReport\r\n"
00171                             "MIME-Version: 1.0\r\n"
00172                             "\n"
00173                             "--%s\r\n"
00174                             "Content-type: text/plain\r\n"
00175                             "\r\n"
00176                             "Sender: %s "
00177                      ,
00178                             mime_boundary,
00179                             mime_boundary,
00180                             ( journal_msg->cm_fields['A'] ? journal_msg->cm_fields['A'] : "(null)" )
00181                      );
00182 
00183                      if (journal_msg->cm_fields['F']) {
00184                             sprintf(&message_text[strlen(message_text)], "<%s>",
00185                                    journal_msg->cm_fields['F']);
00186                      }
00187                      else if (journal_msg->cm_fields['N']) {
00188                             sprintf(&message_text[strlen(message_text)], "@ %s",
00189                                    journal_msg->cm_fields['N']);
00190                      }
00191 
00192                      sprintf(&message_text[strlen(message_text)],
00193                             "\r\n"
00194                             "Message-ID: <%s>\r\n"
00195                             "Recipients:\r\n"
00196                      ,
00197                             jmsg->msgn
00198                      );
00199 
00200                      if (jmsg->recps.num_local > 0) {
00201                             for (i=0; i<jmsg->recps.num_local; ++i) {
00202                                    extract_token(recipient, jmsg->recps.recp_local,
00203                                                  i, '|', sizeof recipient);
00204                                    local_to_inetemail(inetemail, recipient, sizeof inetemail);
00205                                    sprintf(&message_text[strlen(message_text)],
00206                                           "      %s <%s>\r\n", recipient, inetemail);
00207                             }
00208                      }
00209 
00210                      if (jmsg->recps.num_ignet > 0) {
00211                             for (i=0; i<jmsg->recps.num_ignet; ++i) {
00212                                    extract_token(recipient, jmsg->recps.recp_ignet,
00213                                                  i, '|', sizeof recipient);
00214                                    sprintf(&message_text[strlen(message_text)],
00215                                           "      %s\r\n", recipient);
00216                             }
00217                      }
00218 
00219                      if (jmsg->recps.num_internet > 0) {
00220                             for (i=0; i<jmsg->recps.num_internet; ++i) {
00221                                    extract_token(recipient, jmsg->recps.recp_internet,
00222                                                  i, '|', sizeof recipient);
00223                                    sprintf(&message_text[strlen(message_text)],
00224                                           "      %s\r\n", recipient);
00225                             }
00226                      }
00227 
00228                      sprintf(&message_text[strlen(message_text)],
00229                             "\r\n"
00230                             "--%s\r\n"
00231                             "Content-type: message/rfc822\r\n"
00232                             "\r\n"
00233                             "%s"
00234                             "--%s--\r\n"
00235                      ,
00236                             mime_boundary,
00237                             jmsg->rfc822,
00238                             mime_boundary
00239                      );
00240 
00241                      journal_msg->cm_fields['M'] = message_text;
00242                      free(jmsg->rfc822);
00243                      free(jmsg->msgn);
00244                      
00245                      /* Submit journal message */
00246                      CtdlSubmitMsg(journal_msg, journal_recps, "", 0);
00247                      CtdlFreeMessage(journal_msg);
00248               }
00249 
00250               free_recipients(journal_recps);
00251        }
00252 
00253        /* We are responsible for freeing this memory. */
00254        free(jmsg);
00255 }
00256 
00257 
00258 /*
00259  * Run the queue.
00260  */
00261 void JournalRunQueue(void) {
00262        struct jnlq *jptr = NULL;
00263 
00264        while (jnlq != NULL) {
00265               begin_critical_section(S_JOURNAL_QUEUE);
00266               if (jnlq != NULL) {
00267                      jptr = jnlq;
00268                      jnlq = jnlq->next;
00269               }
00270               end_critical_section(S_JOURNAL_QUEUE);
00271               JournalRunQueueMsg(jptr);
00272        }
00273 }
00274 
00275