Back to index

courier  0.68.2
comrwheader.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2000 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "courier.h"
00007 #include      "rw.h"
00008 #include      "rfc822.h"
00009 #include      <stdio.h>
00010 #include      <stdlib.h>
00011 #include      <string.h>
00012 
00013 
00014 static char *rw_rewrite_header_common(struct rw_transport *rw,
00015        void (*rwfunc)(struct rw_info *, void (*)(struct rw_info *), void *),
00016        const char *header, int mode, struct rfc822token *sender,
00017        char **errmsgptr, void *funcarg)
00018 {
00019 struct rfc822t *rfcp;
00020 struct rfc822a *rfca;
00021 char   **bufptrs;
00022 int    i;
00023 char   *new_header=0;
00024 
00025        rfcp=rfc822t_alloc_new(header, NULL, NULL);
00026        rfca=rfc822a_alloc(rfcp);
00027        if (!rfca)    clog_msg_errno();
00028 
00029        bufptrs=0;
00030        if (rfca->naddrs && (bufptrs=malloc(sizeof(char *) * rfca->naddrs))
00031               == 0)
00032        {
00033               rfc822t_free(rfcp);
00034               rfc822a_free(rfca);
00035               clog_msg_errno();
00036        }
00037 
00038        *errmsgptr=0;
00039        for (i=0; i<rfca->naddrs; i++)
00040        {
00041        struct rw_info_rewrite rwr;
00042        struct rw_info rwi;
00043        struct rfc822token *tokenp;
00044 
00045               if (rfca->addrs[i].tokens == NULL)
00046               {
00047                      bufptrs[i]=0;
00048                      continue;
00049               }
00050 
00051               rwr.buf=0;
00052               rwr.errmsg=0;
00053 
00054               rw_info_init(&rwi, rfca->addrs[i].tokens, rw_err_func);
00055               rwi.mode=mode;
00056               rwi.sender=sender;
00057               rwi.udata=(void *)&rwr;
00058 
00059               /*
00060               ** If address is of the form "@", this is a delivery
00061               ** status notification, so leave it untouched.
00062               */
00063 
00064               if (rwi.ptr && rwi.ptr->token == '@' &&
00065                      rwi.ptr->next == 0)
00066                      rw_rewrite_print(&rwi);
00067 
00068               /* --- */
00069 
00070               else if (rw)
00071                      rw_rewrite_module(rw, &rwi,
00072                             rw_rewrite_print
00073                      );
00074               else
00075                      (*rwfunc)(&rwi,
00076                             rw_rewrite_print
00077                             , funcarg
00078                             );
00079 
00080               if ( (*errmsgptr=((struct rw_info_rewrite *)rwi.udata)->errmsg)
00081                      != 0)  break;
00082 
00083               if ( (bufptrs[i]=((struct rw_info_rewrite *)rwi.udata)->buf)
00084                      == 0)  continue;
00085 
00086               tokenp=rfca->addrs[i].tokens;
00087 
00088               tokenp->next=0;
00089               tokenp->token=0;
00090               tokenp->ptr=bufptrs[i];
00091               tokenp->len=strlen(tokenp->ptr);
00092        }
00093 
00094        new_header=0;
00095 
00096        if ( !*errmsgptr)
00097        {
00098        unsigned i, l;
00099        char   *p;
00100 
00101               new_header=rfc822_getaddrs_wrap(rfca, 70);
00102               if (!new_header)     clog_msg_errno();
00103 
00104               for (i=l=0; new_header[i]; i++)
00105                      if (new_header[i] == '\n' && new_header[i+1])
00106                             l += 2;
00107               p=courier_malloc(strlen(new_header)+1+l);
00108               for (i=l=0; new_header[i]; i++)
00109               {
00110                      p[l++]=new_header[i];
00111                      if (new_header[i] == '\n' && new_header[i+1])
00112                      {
00113                             p[l++]=' ';
00114                             p[l++]=' ';
00115                      }
00116               }
00117               p[l]=0;
00118               free(new_header);
00119               new_header=p;
00120        }
00121 
00122        for (i=0; i<rfca->naddrs; i++)
00123               if (bufptrs[i])      free(bufptrs[i]);
00124 
00125        rfc822a_free(rfca);
00126        rfc822t_free(rfcp);
00127        if (bufptrs)  free(bufptrs);
00128 
00129        return (new_header);
00130 }
00131 
00132 char   *rw_rewrite_header(struct rw_transport *rw,
00133        const char *header, int mode, struct rfc822token *sender,
00134        char **errmsgptr)
00135 {
00136        return (rw_rewrite_header_common(rw, 0,
00137               header, mode, sender, errmsgptr, 0));
00138 }
00139 
00140 char   *rw_rewrite_header_func(void (*rwfunc)(
00141                      struct rw_info *, void (*)(struct rw_info *), void *),
00142        const char *header, int mode, struct rfc822token *sender,
00143        char **errmsgptr, void *funcarg)
00144 {
00145        return (rw_rewrite_header_common(0, rwfunc,
00146               header, mode, sender, errmsgptr, funcarg));
00147 }