#pragma prototyped
#include "mailx.h"
void
settmp(const char* name, int dir)
{
int fd;
if (name) {
if (state.msg.op || state.folder == FMH)
quit();
state.readonly = 0;
if (dir) {
if (eaccess(name, W_OK))
state.readonly = 1;
}
else if ((fd = open(name, O_WRONLY|O_BINARY|O_cloexec)) < 0)
state.readonly = 1;
else
close(fd);
if (state.msg.ap) {
fileclose(state.msg.ap);
state.msg.ap = 0;
}
if (state.msg.ip) {
fileclose(state.msg.ip);
state.msg.ip = 0;
}
if (state.msg.op) {
fileclose(state.msg.op);
state.msg.op = 0;
}
strncopy(state.path.prev, state.path.mail, sizeof(state.path.prev));
if (name != state.path.mail)
strncopy(state.path.mail, name, sizeof(state.path.mail));
}
if (!dir) {
if (!(state.msg.op = fileopen(state.tmp.mesg, "EIw")))
exit(1);
if (!(state.msg.ip = fileopen(state.tmp.mesg, "EIr")))
exit(1);
rm(state.tmp.mesg);
}
}
int
setfolder(char* name)
{
FILE* ibuf;
int isedit = *name != '%';
char* who = name[1] ? name + 1 : state.var.user;
if (!(name = expand(name, 1)))
return -1;
state.msg.inbox = 0;
if (imap_name(name)) {
imap_setptr(name, isedit);
return 0;
}
if (state.folder == FIMAP)
imap_setptr((char*)0, 0);
if (!(ibuf = fileopen(name, "Rr"))) {
if (state.var.justcheck)
exit(1);
if (!isedit && errno == ENOENT)
goto nomail;
if (errno == EISDIR) {
mh_setptr(name, isedit);
return 0;
}
note(SYSTEM, "%s", name);
return -1;
}
if (!state.openstat.st_size) {
if (state.var.justcheck)
exit(1);
if (isedit)
note(0, "%s: empty file", name);
else {
fileclose(ibuf);
goto nomail;
}
}
if (S_ISDIR(state.openstat.st_mode)) {
mh_setptr(name, isedit);
return 0;
}
if (state.var.justcheck)
exit(0);
holdsigs();
settmp(name, 0);
state.edit = isedit;
state.mailsize = filesize(ibuf);
setptr(ibuf, 0);
state.mailsize = ftell(ibuf);
fileclose(ibuf);
relsesigs();
state.sawcom = 0;
if (!state.edit && !state.msg.count) {
nomail:
if (state.var.justcheck)
exit(1);
if (!state.var.justheaders) {
if (strchr(name, '/') || strchr(name, '\\'))
note(ERROR, "No mail for %s", who);
else
note(ERROR, "No mail", who);
}
return -1;
}
return 0;
}
int
incfile(void)
{
int newsize;
int msgcount = state.msg.count;
FILE* fp;
if (!(fp = fileopen(state.path.mail, "r")))
return -1;
holdsigs();
if (!(newsize = filesize(fp)))
return -1;
if (newsize < state.mailsize)
return -1;
if (newsize == state.mailsize)
return 0;
setptr(fp, state.mailsize);
state.mailsize = ftell(fp);
fileclose(fp);
relsesigs();
return state.msg.count - msgcount;
}
static void
intr(int sig)
{
state.noreset = 0;
if (state.startup)
state.startup = 0;
else
state.sawcom++;
while (state.sourcing)
unstack();
fileclear();
note(0, "Interrupt");
reset(sig);
}
static void
stop(int sig)
{
sig_t old_action = signal(sig, SIG_DFL);
kill(getpid(), sig);
signal(sig, old_action);
if (state.stopreset) {
state.stopreset = 0;
reset(sig);
}
}
static void
hangup(int sig)
{
exit(1);
}
void
commands(void)
{
register int n;
int eofloop = 0;
char linebuf[LINESIZE];
if (!state.sourcing) {
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, intr);
if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
signal(SIGHUP, hangup);
#if SIGTSTP
signal(SIGTSTP, stop);
#endif
#if SIGTTOU
signal(SIGTTOU, stop);
#endif
#if SIGTTIN
signal(SIGTTIN, stop);
#endif
}
setexit();
for (;;) {
fflush(stdout);
moretop();
if (!state.sourcing && state.var.interactive) {
if (state.var.autoinc && incfile() > 0)
note(0, "New mail has arrived");
state.stopreset = 1;
if (state.var.coprocess)
note(0, "");
else if (state.var.prompt)
note(PROMPT, "%s", state.var.prompt);
}
sreset();
n = 0;
for (;;) {
if (readline(state.input, &linebuf[n], LINESIZE - n) < 0) {
if (!n)
n = -1;
break;
}
if (!(n = strlen(linebuf)))
break;
n--;
if (linebuf[n] != '\\')
break;
linebuf[n++] = ' ';
}
state.stopreset = 0;
if (n < 0) {
if (state.loading)
break;
if (state.sourcing) {
unstack();
continue;
}
if (state.var.interactive &&
state.var.ignoreeof &&
++eofloop < 25) {
note(0, "Use \"quit\" to quit");
continue;
}
break;
}
eofloop = 0;
if (execute(linebuf, 0))
break;
}
}
int
execute(char* linebuf, int contxt)
{
struct cmd* com;
register char* s;
register int c;
register char* a;
char* next;
struct argvec vec;
int e = 1;
for (s = linebuf; isspace(*s); s++);
switch (*s) {
case '!':
a = "shell";
s++;
break;
case '#':
return 0;
case '=':
a = "dot";
s++;
break;
case '?':
a = "help";
s++;
break;
case '|':
a = "pipe";
s++;
break;
default:
if (isalpha(*s)) {
a = s;
s = 0;
}
else {
if (state.sourcing)
return 0;
a = "next";
}
break;
}
if (!(com = (struct cmd*)strpsearch(state.cmdtab, state.cmdnum, sizeof(struct cmd), a, &next))) {
note(0, "\"%s\": unknown command", a);
goto out;
}
state.cmd = com;
if (state.clobber = *next == '!')
next++;
if (!s)
s = next;
while (isspace(*s))
s++;
if (!(com->c_argtype & C) && state.cond && state.cond != state.mode)
return 0;
if (state.mode == SEND && !(com->c_argtype & M)) {
note(0, "Cannot execute \"%s\" while sending",
com->c_name);
goto out;
}
if (state.sourcing && (com->c_argtype & I)) {
note(0, "Cannot execute \"%s\" while sourcing",
com->c_name);
goto out;
}
if (state.readonly && (com->c_argtype & W)) {
note(0, "Cannot execute \"%s\" -- message file is read only",
com->c_name);
goto out;
}
if (contxt && (com->c_argtype & R)) {
note(0, "Cannot recursively invoke \"%s\"", com->c_name);
goto out;
}
switch (com->c_argtype & LISTMASK) {
case MSGLIST:
if (!state.msg.list) {
note(0, "Invalid use of \"message list\"");
break;
}
if ((c = getmsglist(s, com->c_msgflag)) < 0)
break;
if (!c) {
state.msg.list->m_index = first(com->c_msgflag, com->c_msgmask);
(state.msg.list + 1)->m_index = 0;
}
if (!state.msg.list->m_index) {
note(0, "No applicable messages");
break;
}
e = (*com->c_func)(state.msg.list);
break;
case NDMLIST:
if (!state.msg.list) {
note(0, "Invalid use of \"message list\"");
break;
}
if (getmsglist(s, com->c_msgflag) < 0)
break;
e = (*com->c_func)(state.msg.list);
break;
case STRLIST:
e = (*com->c_func)(s);
break;
case RAWLIST:
initargs(&vec);
getargs(&vec, s);
if ((c = endargs(&vec)) < 0)
break;
if (c < com->c_minargs) {
note(0, "%s requires %s %d arg%s",
com->c_name,
com->c_minargs == com->c_maxargs ? "exactly" : "at least",
com->c_minargs,
com->c_minargs == 1 ? "" : "s");
break;
}
if (c > com->c_maxargs) {
note(0, "%s takes no more than %d arg%s",
com->c_name,
com->c_maxargs,
com->c_maxargs == 1 ? "" : "s");
break;
}
e = (*com->c_func)(vec.argv);
break;
case NOLIST:
e = (*com->c_func)(0);
break;
default:
note(PANIC, "0x%08x: unknown argtype", com->c_argtype & LISTMASK);
}
out:
if (e) {
if (e < 0 || state.loading)
return 1;
if (state.sourcing)
unstack();
return 0;
}
if (state.var.autoprint && (com->c_argtype & P))
if (!(state.msg.dot->m_flag & (MDELETE|MNONE|MSPAM))) {
state.msg.list->m_index = state.msg.dot - state.msg.list + 1;
(state.msg.list + 1)->m_index = 0;
type(state.msg.list);
}
if (!state.sourcing && !(com->c_argtype & Z))
state.sawcom = 1;
return 0;
}
struct msg*
folderinfo(int msgcount)
{
register struct msg* mp;
register int d;
register int m;
register int n;
register int s;
register int u;
int i;
struct msg* dot;
char* name;
char buf[LINESIZE];
if (state.var.justheaders && state.var.header)
return state.msg.list;
if (dot = state.msg.context)
state.msg.context = 0;
else if (msgcount > 0 && state.msg.dot < state.msg.list + msgcount - 1)
dot = state.msg.dot;
else {
for (mp = state.msg.list + msgcount; mp < state.msg.list + state.msg.count; mp++)
if (mp->m_flag & MNEW)
break;
if (mp >= state.msg.list + state.msg.count)
for (mp = state.msg.list + msgcount; mp < state.msg.list + state.msg.count; mp++)
if (!(mp->m_flag & MREAD))
break;
if (mp < state.msg.list + state.msg.count)
dot = mp;
else if (msgcount <= 0 && state.var.recent)
dot = state.msg.list + state.msg.count - 1;
else {
dot = state.msg.list;
if (!state.msg.count)
dot += msgcount - 1;
}
}
d = m = n = s = u = 0;
for (mp = state.msg.list; mp < state.msg.list + state.msg.count; mp++) {
if (!(mp->m_flag & MNONE)) {
m++;
if (mp->m_flag & MDELETE)
d++;
else if (mp->m_flag & MSAVE)
s++;
else if (!(mp->m_flag & (MREAD|MNEW)))
u++;
else if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
n++;
}
}
name = state.path.mail;
if (getfolder(buf, sizeof(buf)) >= 0) {
i = strlen(buf);
buf[i++] = '/';
if (!strncmp(state.path.mail, buf, i))
sfsprintf(name = buf, sizeof(buf), "+%s", state.path.mail + i);
}
printf("\"%s\": %d message%s", name, m, m == 1 ? "" : "s");
if (msgcount > 0 && state.msg.count > msgcount)
printf(" %d incorporated", state.msg.count - msgcount);
if (n > 0)
printf(" %d new", n);
if (u > 0)
printf(" %d unread", u);
if (d > 0)
printf(" %d deleted", d);
if (s > 0)
printf(" %d saved", s);
switch (state.folder) {
case FIMAP:
printf(" [imap]");
break;
case FMH:
printf(" [mh]");
break;
}
if (state.readonly)
printf(" [Read only]");
printf("\n");
return dot;
}
void
announce()
{
state.msg.dot = folderinfo(0);
if (state.msg.list) {
state.msg.list->m_index = state.msg.dot - state.msg.list + 1;
(state.msg.list + 1)->m_index = 0;
if (state.msg.count > 0 && state.var.header) {
state.startup = 1;
headers(state.msg.list);
state.startup = 0;
}
}
}
int
version(void* a)
{
note(0, "Version %s", state.version);
return 0;
}
int
license(void* a)
{
version(a);
note(0, "\n%s", state.license);
return 0;
}
void
load(char* name)
{
register FILE* fp;
register FILE* input;
if (!(fp = fileopen(name, "r")))
return;
input = state.input;
state.input = fp;
state.loading = 1;
state.sourcing = 1;
commands();
state.loading = 0;
state.sourcing = 0;
state.input = input;
fileclose(fp);
}