#include <sys/time.h>
#include <fcntl.h>
#include "rcv.h"
#include "extern.h"
static char *save2str(char *, char *);
char *
savestr(char *str)
{
char *new;
int size = strlen(str) + 1;
if ((new = salloc(size)) != NULL)
bcopy(str, new, size);
return (new);
}
static char *
save2str(char *str, char *old)
{
char *new;
int newsize = strlen(str) + 1;
int oldsize = old ? strlen(old) + 1 : 0;
if ((new = salloc(newsize + oldsize)) != NULL) {
if (oldsize) {
bcopy(old, new, oldsize);
new[oldsize - 1] = ' ';
}
bcopy(str, new + oldsize, newsize);
}
return (new);
}
void
touch(struct message *mp)
{
mp->m_flag |= MTOUCH;
if ((mp->m_flag & MREAD) == 0)
mp->m_flag |= MREAD|MSTATUS;
}
int
isdir(char name[])
{
struct stat sbuf;
if (stat(name, &sbuf) < 0)
return (0);
return (S_ISDIR(sbuf.st_mode));
}
int
argcount(char **argv)
{
char **ap;
for (ap = argv; *ap++ != NULL;)
;
return (ap - argv - 1);
}
char *
hfield(const char *field, struct message *mp)
{
FILE *ibuf;
char linebuf[LINESIZE];
int lc;
char *hfield;
char *colon, *oldhfield = NULL;
ibuf = setinput(mp);
if ((lc = mp->m_lines - 1) < 0)
return (NULL);
if (readline(ibuf, linebuf, sizeof(linebuf)) < 0)
return (NULL);
while (lc > 0) {
if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
return (oldhfield);
if ((hfield = ishfield(linebuf, colon, field)) != NULL)
oldhfield = save2str(hfield, oldhfield);
}
return (oldhfield);
}
int
gethfield(FILE *f, char linebuf[], int rem, char **colon)
{
char line2[LINESIZE];
char *cp, *cp2;
int c;
for (;;) {
if (--rem < 0)
return (-1);
if ((c = readline(f, linebuf, LINESIZE)) <= 0)
return (-1);
for (cp = linebuf; isprint((unsigned char)*cp) && *cp != ' ' && *cp != ':';
cp++)
;
if (*cp != ':' || cp == linebuf)
continue;
*colon = cp;
cp = linebuf + c;
for (;;) {
while (--cp >= linebuf && (*cp == ' ' || *cp == '\t'))
;
cp++;
if (rem <= 0)
break;
ungetc(c = getc(f), f);
if (c != ' ' && c != '\t')
break;
if ((c = readline(f, line2, sizeof(line2))) < 0)
break;
rem--;
for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
;
c -= cp2 - line2;
if (cp + c >= linebuf + LINESIZE - 2)
break;
*cp++ = ' ';
bcopy(cp2, cp, c);
cp += c;
}
*cp = 0;
return (rem);
}
}
char*
ishfield(char *linebuf, char *colon, const char *field)
{
char *cp = colon;
*cp = 0;
if (strcasecmp(linebuf, field) != 0) {
*cp = ':';
return (0);
}
*cp = ':';
for (cp++; *cp == ' ' || *cp == '\t'; cp++)
;
return (cp);
}
void
istrncpy(char *dest, const char *src, size_t dsize)
{
strlcpy(dest, src, dsize);
for (; *dest; dest++)
*dest = tolower((unsigned char)*dest);
}
static int ssp;
struct sstack {
FILE *s_file;
int s_cond;
int s_loading;
};
#define SSTACK_SIZE 64
static struct sstack sstack[SSTACK_SIZE];
int
source(void *arg)
{
char **arglist = arg;
FILE *fi;
char *cp;
if ((cp = expand(*arglist)) == NULL)
return (1);
if ((fi = Fopen(cp, "r")) == NULL) {
warn("%s", cp);
return (1);
}
if (ssp >= SSTACK_SIZE - 1) {
printf("Too much \"sourcing\" going on.\n");
(void)Fclose(fi);
return (1);
}
sstack[ssp].s_file = input;
sstack[ssp].s_cond = cond;
sstack[ssp].s_loading = loading;
ssp++;
loading = 0;
cond = CANY;
input = fi;
sourcing++;
return (0);
}
int
unstack(void)
{
if (ssp <= 0) {
printf("\"Source\" stack over-pop.\n");
sourcing = 0;
return (1);
}
(void)Fclose(input);
if (cond != CANY)
printf("Unmatched \"if\"\n");
ssp--;
cond = sstack[ssp].s_cond;
loading = sstack[ssp].s_loading;
input = sstack[ssp].s_file;
if (ssp == 0)
sourcing = loading;
return (0);
}
void
alter(char *name)
{
struct timespec ts[2];
(void)clock_gettime(CLOCK_REALTIME, &ts[0]);
ts[0].tv_sec++;
ts[1].tv_sec = 0;
ts[1].tv_nsec = UTIME_OMIT;
(void)utimensat(AT_FDCWD, name, ts, 0);
}
char *
nameof(struct message *mp, int reptype)
{
char *cp, *cp2;
cp = skin(name1(mp, reptype));
if (reptype != 0 || charcount(cp, '!') < 2)
return (cp);
cp2 = strrchr(cp, '!');
cp2--;
while (cp2 > cp && *cp2 != '!')
cp2--;
if (*cp2 == '!')
return (cp2 + 1);
return (cp);
}
char *
skip_comment(char *cp)
{
int nesting = 1;
for (; nesting > 0 && *cp; cp++) {
switch (*cp) {
case '\\':
if (cp[1])
cp++;
break;
case '(':
nesting++;
break;
case ')':
nesting--;
break;
}
}
return (cp);
}
char *
skin(char *name)
{
char *nbuf, *bufend, *cp, *cp2;
int c, gotlt, lastsp;
if (name == NULL)
return (NULL);
if (strchr(name, '(') == NULL && strchr(name, '<') == NULL
&& strchr(name, ' ') == NULL)
return (name);
if ((nbuf = malloc(strlen(name) + 1)) == NULL)
err(1, "Out of memory");
gotlt = 0;
lastsp = 0;
bufend = nbuf;
for (cp = name, cp2 = bufend; (c = *cp++) != '\0'; ) {
switch (c) {
case '(':
cp = skip_comment(cp);
lastsp = 0;
break;
case '"':
while ((c = *cp) != '\0') {
cp++;
if (c == '"')
break;
if (c != '\\')
*cp2++ = c;
else if ((c = *cp) != '\0') {
*cp2++ = c;
cp++;
}
}
lastsp = 0;
break;
case ' ':
if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
cp += 3, *cp2++ = '@';
else
if (cp[0] == '@' && cp[1] == ' ')
cp += 2, *cp2++ = '@';
else
lastsp = 1;
break;
case '<':
cp2 = bufend;
gotlt++;
lastsp = 0;
break;
case '>':
if (gotlt) {
gotlt = 0;
while ((c = *cp) != '\0' && c != ',') {
cp++;
if (c == '(')
cp = skip_comment(cp);
else if (c == '"')
while ((c = *cp) != '\0') {
cp++;
if (c == '"')
break;
if (c == '\\' && *cp != '\0')
cp++;
}
}
lastsp = 0;
break;
}
default:
if (lastsp) {
lastsp = 0;
*cp2++ = ' ';
}
*cp2++ = c;
if (c == ',' && !gotlt &&
(*cp == ' ' || *cp == '"' || *cp == '<')) {
*cp2++ = ' ';
while (*cp == ' ')
cp++;
lastsp = 0;
bufend = cp2;
}
}
}
*cp2 = '\0';
if ((cp = realloc(nbuf, strlen(nbuf) + 1)) != NULL)
nbuf = cp;
return (nbuf);
}
char *
name1(struct message *mp, int reptype)
{
char namebuf[LINESIZE];
char linebuf[LINESIZE];
char *cp, *cp2;
FILE *ibuf;
int first = 1;
if ((cp = hfield("from", mp)) != NULL)
return (cp);
if (reptype == 0 && (cp = hfield("sender", mp)) != NULL)
return (cp);
ibuf = setinput(mp);
namebuf[0] = '\0';
if (readline(ibuf, linebuf, sizeof(linebuf)) < 0)
return (savestr(namebuf));
newname:
for (cp = linebuf; *cp != '\0' && *cp != ' '; cp++)
;
for (; *cp == ' ' || *cp == '\t'; cp++)
;
for (cp2 = &namebuf[strlen(namebuf)];
*cp != '\0' && *cp != ' ' && *cp != '\t' &&
cp2 < namebuf + sizeof(namebuf) - 1;)
*cp2++ = *cp++;
*cp2 = '\0';
if (readline(ibuf, linebuf, sizeof(linebuf)) < 0)
return (savestr(namebuf));
if ((cp = strchr(linebuf, 'F')) == NULL)
return (savestr(namebuf));
if (strncmp(cp, "From", 4) != 0)
return (savestr(namebuf));
while ((cp = strchr(cp, 'r')) != NULL) {
if (strncmp(cp, "remote", 6) == 0) {
if ((cp = strchr(cp, 'f')) == NULL)
break;
if (strncmp(cp, "from", 4) != 0)
break;
if ((cp = strchr(cp, ' ')) == NULL)
break;
cp++;
if (first) {
cp2 = namebuf;
first = 0;
} else
cp2 = strrchr(namebuf, '!') + 1;
strlcpy(cp2, cp, sizeof(namebuf) - (cp2 - namebuf) - 1);
strcat(namebuf, "!");
goto newname;
}
cp++;
}
return (savestr(namebuf));
}
int
charcount(char *str, int c)
{
char *cp;
int i;
for (i = 0, cp = str; *cp != '\0'; cp++)
if (*cp == c)
i++;
return (i);
}
int
isign(const char *field, struct ignoretab ignore[2])
{
char realfld[LINESIZE];
if (ignore == ignoreall)
return (1);
istrncpy(realfld, field, sizeof(realfld));
if (ignore[1].i_count > 0)
return (!member(realfld, ignore + 1));
else
return (member(realfld, ignore));
}
int
member(char *realfield, struct ignoretab *table)
{
struct ignore *igp;
for (igp = table->i_head[hash(realfield)]; igp != NULL; igp = igp->i_link)
if (*igp->i_field == *realfield &&
equal(igp->i_field, realfield))
return (1);
return (0);
}