1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*$Id: copy.c,v 1.8 1998/06/21 22:49:37 lindberg Exp $*/
/*$Name: ezmlm-idx-0313 $*/
/* Copies a file relative the current directory and substitutes */
/* !A at the beginning of a line for the target, */
/* !R at the beginning of a line for the confirm reply address, */
/* the first <#A#> for target, the first <#R#> for confirm, and */
/* the first <#L#> on a line for the list local name. Subsequent */
/* tags on any line are ignored. A missing file is a permanent */
/* error so owner finds out ASAP. May not have access to maillog. */
#include "stralloc.h"
#include "substdio.h"
#include "strerr.h"
#include "str.h"
#include "getln.h"
#include "case.h"
#include "readwrite.h"
#include "qmail.h"
#include "errtxt.h"
#include "error.h"
#include "copy.h"
#include "mime.h"
/* for public setup functions only */
#define FATAL "copy: fatal: "
static stralloc line = {0};
static stralloc outline = {0};
static stralloc qline = {0};
static stralloc outlocal = {0};
static stralloc outhost = {0};
static substdio sstext;
static char textbuf[256];
static char *target = "?????";
static char *confirm = "?????";
void set_cpoutlocal(ln)
stralloc *ln;
{
if (!stralloc_copy(&outlocal,ln))
strerr_die2x(111,FATAL,ERR_NOMEM);
}
void set_cpouthost(ln)
stralloc *ln;
{
if (!stralloc_copy(&outhost,ln))
strerr_die2x(111,FATAL,ERR_NOMEM);
}
void set_cptarget(tg)
char *tg;
{
target = tg;
}
void set_cpconfirm(cf)
char *cf;
{
confirm = cf;
}
static struct qmail *qq;
static void codeput(l,n,code,fatal)
char *l;
unsigned int n;
char code;
char *fatal;
{
if (!code)
qmail_put(qq,l,n);
else {
if (code == 'Q')
encodeQ(l,n,&qline,fatal);
else
encodeB(l,n,&qline,0,fatal);
qmail_put(qq,qline.s,qline.len);
}
}
static void codeputs(l,code,fatal)
char *l;
char code;
char *fatal;
{
codeput(l,str_len(l),code,fatal);
}
void copy(qqp,fn,q,fatal)
struct qmail *qqp;
char *fn; /* text file name */
char q; /* = '\0' for regular output, 'B' for base64, */
/* 'Q' for quoted printable */
char *fatal; /* FATAL error string */
{
int fd;
int match, done;
unsigned int pos,nextpos;
qq = qqp;
if ((fd = open_read(fn)) == -1)
if (errno != error_noent)
strerr_die4sys(111,fatal,ERR_OPEN,fn,": ");
else
strerr_die4sys(100,fatal,ERR_OPEN,fn,": ");
substdio_fdbuf(&sstext,read,fd,textbuf,sizeof(textbuf));
for (;;) {
if (getln(&sstext,&line,&match,'\n') == -1)
strerr_die4sys(111,fatal,ERR_READ,fn,": ");
if (match) {
if (line.s[0] == '!') {
if (line.s[1] == 'R') {
codeput(" ",3,q,fatal);
codeputs(confirm,q,fatal);
codeput("\n",1,q,fatal);
continue;
}
if (line.s[1] == 'A') {
codeput(" ",3,q,fatal);
codeputs(target,q,fatal);
codeput("\n",1,q,fatal);
continue;
}
}
/* Find tags <#x#>. Replace with for x=R confirm, for x=A */
/* target, x=l outlocal, x=h outhost. For others, just */
/* skip tag. If outlocal/outhost are not set, the tags are*/
/* skipped. If confirm/taget are not set, the tags are */
/* replaced by "???????" */
pos = 0;
nextpos = 0;
done = 0;
outline.len = 0; /* zap outline */
while ((pos += byte_chr(line.s+pos,line.len-pos,'<')) != line.len) {
if (pos + 4 < line.len &&
line.s[pos+1] == '#' &&
line.s[pos+3] == '#' &&
line.s[pos+4] == '>') { /* tag. Copy first part of line */
done = 1; /* did something */
if (!stralloc_catb(&outline,line.s+nextpos,pos-nextpos))
die_nomem(fatal);
switch(line.s[pos+2]) {
case 'A':
if (!stralloc_cats(&outline,target)) die_nomem(fatal);
break;
case 'R':
if (!stralloc_cats(&outline,confirm)) die_nomem(fatal);
break;
case 'l':
if (!stralloc_cat(&outline,&outlocal)) die_nomem(fatal);
break;
case 'h':
if (!stralloc_cat(&outline,&outhost)) die_nomem(fatal);
break;
default:
break; /* unknown tags killed */
}
pos += 5;
nextpos = pos;
} else
++pos; /* try next position */
}
if (!done)
codeput(line.s,line.len,q,fatal);
else {
if (!stralloc_catb(&outline,line.s+nextpos,line.len-nextpos))
die_nomem(fatal); /* remainder */
codeput(outline.s,outline.len,q,fatal);
}
} else
break;
}
close(fd);
}
|