From f2a45b472e7196bdced510fc512bac74d2561da1 Mon Sep 17 00:00:00 2001 From: "Steffen (Daode) Nurpmeso" Date: Mon, 7 Jul 2014 18:03:48 +0200 Subject: [PATCH] Add enum fedit_mode and use it for setfile()s --- cmd3.c | 4 ++-- imap.c | 5 +++-- lex.c | 61 +++++++++++++++++++++++++++++++++++++------------------------ maildir.c | 45 +++++++++++++++++++++++---------------------- main.c | 2 +- nail.h | 7 +++++++ nailfuns.h | 11 ++++++----- pop3.c | 8 ++++---- 8 files changed, 83 insertions(+), 60 deletions(-) diff --git a/cmd3.c b/cmd3.c index a5b9b240..e92a36e4 100644 --- a/cmd3.c +++ b/cmd3.c @@ -1018,7 +1018,7 @@ c_file(void *v) save_mbox_for_possible_quitstuff(); - i = setfile(*argv, 0); + i = setfile(*argv, FEDIT_NONE); if (i < 0) { i = 1; goto jleave; @@ -1301,7 +1301,7 @@ c_newmail(void *v) #ifdef HAVE_IMAP (mb.mb_type != MB_IMAP || imap_newmail(1)) && #endif - (val = setfile(mailname, 1)) == 0) { + (val = setfile(mailname, FEDIT_NEWMAIL)) == 0) { mdot = getmdot(1); setdot(message + mdot - 1); } diff --git a/imap.c b/imap.c index d1d2b269..2f760de4 100644 --- a/imap.c +++ b/imap.c @@ -1130,7 +1130,7 @@ imap_setptr(struct mailbox *mp, int nmail, int transparent, int *prevcount) } FL int -imap_setfile(const char *xserver, int nmail, int isedit) +imap_setfile(const char *xserver, enum fedit_mode fm) { struct url url; int rv; @@ -1144,7 +1144,8 @@ imap_setfile(const char *xserver, int nmail, int isedit) (!url.url_had_user || url.url_pass.s != NULL)) fprintf(stderr, "New-style URL used without *v15-compat* being set!\n"); - rv = _imap_setfile1(&url, nmail, isedit, 0); + rv = _imap_setfile1(&url, ((fm & FEDIT_NEWMAIL) != 0), + !(fm & FEDIT_SYSBOX), 0); jleave: NYD_LEAVE; return rv; diff --git a/lex.c b/lex.c index 51196edc..ee3a1a27 100644 --- a/lex.c +++ b/lex.c @@ -418,7 +418,7 @@ hangup(int s) } FL int -setfile(char const *name, int nmail) /* TODO oh my god */ +setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */ { static int shudclob; @@ -426,7 +426,6 @@ setfile(char const *name, int nmail) /* TODO oh my god */ struct flock flp; FILE *ibuf = NULL; int rv, i, compressed = 0, omsgCount = 0; - bool_t isedit; char const *who; size_t offset; struct shortcut *sh; @@ -436,8 +435,21 @@ setfile(char const *name, int nmail) /* TODO oh my god */ * necessary to make a mailbox accessible by a different user, and if that * has happened, let's just let the usual file perms decide */ who = (name[1] != '\0') ? name + 1 : myname; - isedit = (*name != '%' && ((sh = get_shortcut(name)) == NULL || - *sh->sh_long != '%')); + +#if 1 + if (name[0] == '%' || + ((sh = get_shortcut(name)) != NULL && *sh->sh_long == '%')) + fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */ +#else + if (name[0] == '%' && (name[1] == '\0' || name[1] == ':')) + fm |= FEDIT_SYSBOX; + else if ((sh = get_shortcut(name)) != NULL) { + char const *x = sh->sh_long; + + if (x[0] == '%' && (x[1] == '\0' || x[1] == ':')) + fm |= FEDIT_SYSBOX; + } +#endif if ((name = expand(name)) == NULL) goto jem1; @@ -445,21 +457,21 @@ setfile(char const *name, int nmail) /* TODO oh my god */ case PROTO_FILE: break; case PROTO_MAILDIR: - rv = maildir_setfile(name, nmail, isedit); + rv = maildir_setfile(name, fm); goto jleave; #ifdef HAVE_POP3 case PROTO_POP3: shudclob = 1; - rv = pop3_setfile(name, nmail, isedit); + rv = pop3_setfile(name, fm); goto jleave; #endif #ifdef HAVE_IMAP case PROTO_IMAP: shudclob = 1; - if (nmail && mb.mb_type == MB_CACHE) + if ((fm & FEDIT_NEWMAIL) && mb.mb_type == MB_CACHE) rv = 1; else - rv = imap_setfile(name, nmail, isedit); + rv = imap_setfile(name, fm); goto jleave; #endif default: @@ -473,8 +485,8 @@ setfile(char const *name, int nmail) /* TODO oh my god */ * FIXME the current box and (2) open the new box; yet, since (2) may fail * FIXME we terribly need our VOID box to make this logic order possible! */ if ((ibuf = Zopen(name, "r", &compressed)) == NULL) { - if ((!isedit && errno == ENOENT) || nmail) { - if (nmail) + if (((fm & FEDIT_SYSBOX) && errno == ENOENT) || (fm & FEDIT_NEWMAIL)) { + if (fm & FEDIT_NEWMAIL) goto jnonmail; goto jnomail; } @@ -483,7 +495,7 @@ setfile(char const *name, int nmail) /* TODO oh my god */ } if (fstat(fileno(ibuf), &stb) == -1) { - if (nmail) + if (fm & FEDIT_NEWMAIL) goto jnonmail; perror("fstat"); goto jem1; @@ -493,7 +505,7 @@ setfile(char const *name, int nmail) /* TODO oh my god */ ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"))) { /* EMPTY */ } else { - if (nmail) + if (fm & FEDIT_NEWMAIL) goto jnonmail; errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL; perror(name); @@ -505,10 +517,10 @@ setfile(char const *name, int nmail) /* TODO oh my god */ * file, else we will ruin the message[] data structure */ hold_sigs(); /* TODO note on this one in quit.c:quit() */ - if (shudclob && !nmail) + if (shudclob && !(fm & FEDIT_NEWMAIL)) quit(); #ifdef HAVE_SOCKETS - if (!nmail && mb.mb_sock.s_fd >= 0) + if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0) sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */ #endif @@ -516,9 +528,10 @@ setfile(char const *name, int nmail) /* TODO oh my god */ flp.l_type = F_RDLCK; flp.l_start = 0; flp.l_whence = SEEK_SET; - if (!nmail) { + if (!(fm & FEDIT_NEWMAIL)) { mb.mb_type = MB_FILE; - mb.mb_perm = (options & OPT_R_FLAG) ? 0 : MB_DELE | MB_EDIT; + mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY)) + ? 0 : MB_DELE | MB_EDIT); mb.mb_compressed = compressed; if (compressed) { if (compressed & 0200) @@ -540,7 +553,7 @@ setfile(char const *name, int nmail) /* TODO oh my god */ } } shudclob = 1; - edit = isedit; + edit = !(fm & FEDIT_SYSBOX); initbox(name); offset = 0; flp.l_len = 0; @@ -549,7 +562,7 @@ setfile(char const *name, int nmail) /* TODO oh my god */ rele_sigs(); goto jem1; } - } else /* nmail */{ + } else /* FEDIT_NEWMAIL */{ fseek(mb.mb_otf, 0L, SEEK_END); fseek(ibuf, mailsize, SEEK_SET); offset = mailsize; @@ -562,13 +575,13 @@ setfile(char const *name, int nmail) /* TODO oh my god */ } mailsize = fsize(ibuf); - if (nmail && UICMP(z, mailsize, <=, offset)) { + if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) { rele_sigs(); goto jnonmail; } setptr(ibuf, offset); setmsize(msgCount); - if (nmail && mb.mb_sorted) { + if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) { mb.mb_threaded = 0; c_sort((void*)-1); } @@ -576,12 +589,12 @@ setfile(char const *name, int nmail) /* TODO oh my god */ Fclose(ibuf); ibuf = NULL; rele_sigs(); - if (!nmail) + if (!(fm & FEDIT_NEWMAIL)) sawcom = FAL0; - if ((!edit || nmail) && msgCount == 0) { + if ((!edit || (fm & FEDIT_NEWMAIL)) && msgCount == 0) { jnonmail: - if (!nmail) { + if (!(fm & FEDIT_NEWMAIL)) { if (!ok_blook(emptystart)) jnomail: fprintf(stderr, _("No mail for %s\n"), who); @@ -589,7 +602,7 @@ jnomail: rv = 1; goto jleave; } - if (nmail) + if (fm & FEDIT_NEWMAIL) newmailinfo(omsgCount); rv = 0; diff --git a/maildir.c b/maildir.c index 8571f81f..f51e4de0 100644 --- a/maildir.c +++ b/maildir.c @@ -58,7 +58,7 @@ static void __maildircatch(int s); /* Do some cleanup in the tmp/ subdir */ static void _cleantmp(void); -static int maildir_setfile1(char const *name, int nmail, +static int _maildir_setfile1(char const *name, enum fedit_mode fm, int omsgCount); /* In combination with the names from mkname(), this comparison function @@ -67,7 +67,8 @@ static int maildir_setfile1(char const *name, int nmail, * a maildir folder by 'copy *', the message order wont' change */ static int mdcmp(void const *a, void const *b); -static int subdir(char const *name, char const *sub, int nmail); +static int _maildir_subdir(char const *name, char const *sub, + enum fedit_mode fm); static void _maildir_append(char const *name, char const *sub, char const *fn); @@ -129,23 +130,23 @@ jleave: } static int -maildir_setfile1(char const *name, int nmail, int omsgCount) +_maildir_setfile1(char const *name, enum fedit_mode fm, int omsgCount) { int i; NYD_ENTER; - if (!nmail) + if (!(fm & FEDIT_NEWMAIL)) _cleantmp(); - mb.mb_perm = (options & OPT_R_FLAG) ? 0 : MB_DELE; - if ((i = subdir(name, "cur", nmail)) != 0) + mb.mb_perm = ((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY)) ? 0 : MB_DELE; + if ((i = _maildir_subdir(name, "cur", fm)) != 0) goto jleave; - if ((i = subdir(name, "new", nmail)) != 0) + if ((i = _maildir_subdir(name, "new", fm)) != 0) goto jleave; _maildir_append(name, NULL, NULL); - for (i = (nmail ? omsgCount : 0); i < msgCount; ++i) + for (i = ((fm & FEDIT_NEWMAIL) ? omsgCount : 0); i < msgCount; ++i) readin(name, message + i); - if (nmail) { + if (fm & FEDIT_NEWMAIL) { if (msgCount > omsgCount) qsort(&message[omsgCount], msgCount - omsgCount, sizeof *message, &mdcmp); @@ -171,7 +172,7 @@ mdcmp(void const *a, void const *b) } static int -subdir(char const *name, char const *sub, int nmail) +_maildir_subdir(char const *name, char const *sub, enum fedit_mode fm) { DIR *dirp; struct dirent *dp; @@ -191,7 +192,7 @@ subdir(char const *name, char const *sub, int nmail) continue; if (dp->d_name[0] == '.') continue; - if (!nmail || mdlook(dp->d_name, NULL) == NULL) + if (!(fm & FEDIT_NEWMAIL) || mdlook(dp->d_name, NULL) == NULL) _maildir_append(name, sub, dp->d_name); } closedir(dirp); @@ -681,7 +682,7 @@ jleave: } FL int -maildir_setfile(char const * volatile name, int nmail, int isedit) +maildir_setfile(char const * volatile name, enum fedit_mode fm) { sighandler_type volatile saveint; struct cw cw; @@ -694,13 +695,13 @@ maildir_setfile(char const * volatile name, int nmail, int isedit) goto jleave; } - if (!nmail) + if (!(fm & FEDIT_NEWMAIL)) quit(); saveint = safe_signal(SIGINT, SIG_IGN); - if (!nmail) { - edit = (isedit != 0); + if (!(fm & FEDIT_NEWMAIL)) { + edit = !(fm & FEDIT_SYSBOX); if (mb.mb_itf) { fclose(mb.mb_itf); mb.mb_itf = NULL; @@ -725,13 +726,13 @@ maildir_setfile(char const * volatile name, int nmail, int isedit) _maildir_table = NULL; if (sigsetjmp(_maildir_jmp, 1) == 0) { - if (nmail) + if (fm & FEDIT_NEWMAIL) mktable(); if (saveint != SIG_IGN) safe_signal(SIGINT, &__maildircatch); - i = maildir_setfile1(name, nmail, omsgCount); + i = _maildir_setfile1(name, fm, omsgCount); } - if (nmail && _maildir_table != NULL) + if ((fm & FEDIT_NEWMAIL) && _maildir_table != NULL) free(_maildir_table); safe_signal(SIGINT, saveint); @@ -747,19 +748,19 @@ maildir_setfile(char const * volatile name, int nmail, int isedit) cwrelse(&cw); setmsize(msgCount); - if (nmail && mb.mb_sorted && msgCount > omsgCount) { + if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted && msgCount > omsgCount) { mb.mb_threaded = 0; c_sort((void*)-1); } - if (!nmail) + if (!(fm & FEDIT_NEWMAIL)) sawcom = FAL0; - if (!nmail && !edit && msgCount == 0) { + if (!(fm & FEDIT_NEWMAIL) && (fm & FEDIT_SYSBOX) && msgCount == 0) { if (mb.mb_type == MB_MAILDIR /* XXX ?? */ && !ok_blook(emptystart)) fprintf(stderr, _("No mail at %s\n"), name); i = 1; goto jleave; } - if (nmail && msgCount > omsgCount) + if ((fm & FEDIT_NEWMAIL) && msgCount > omsgCount) newmailinfo(omsgCount); i = 0; jleave: diff --git a/main.c b/main.c index 863518bd..1172564a 100644 --- a/main.c +++ b/main.c @@ -477,7 +477,7 @@ _rcv_mode(char const *folder, char const *Larg) n_strlcpy(mailname, cp, PATH_MAX); } - i = setfile(folder, 0); + i = setfile(folder, FEDIT_NONE); if (i < 0) { exit_status = EXIT_ERR; /* error already reported */ goto jleave; diff --git a/nail.h b/nail.h index b2f4dafb..c14a287d 100644 --- a/nail.h +++ b/nail.h @@ -539,6 +539,13 @@ enum exit_status { EXIT_SEND_ERROR = 1<<2 /* Unspecified send error occurred */ }; +enum fedit_mode { + FEDIT_NONE = 0, + FEDIT_SYSBOX = 1<<0, /* %: prefix */ + FEDIT_RDONLY = 1<<1, /* Readonly (per-box, OPT_R_FLAG is global) */ + FEDIT_NEWMAIL = 1<<2 /* `newmail' operation TODO OBSOLETE THIS! */ +}; + enum fexp_mode { FEXP_FULL, /* Full expansion */ FEXP_LOCAL = 1<<0, /* Result must be local file/maildir */ diff --git a/nailfuns.h b/nailfuns.h index 9af8c422..86cb3c4b 100644 --- a/nailfuns.h +++ b/nailfuns.h @@ -970,7 +970,7 @@ FL char const * imap_fileof(char const *xcp); FL enum okay imap_noop(void); FL enum okay imap_select(struct mailbox *mp, off_t *size, int *count, const char *mbx); -FL int imap_setfile(const char *xserver, int nmail, int isedit); +FL int imap_setfile(const char *xserver, enum fedit_mode fm); FL enum okay imap_header(struct message *m); FL enum okay imap_body(struct message *m); FL void imap_getheaders(int bot, int top); @@ -1047,8 +1047,9 @@ FL enum okay imap_search(char const *spec, int f); /* Set up editing on the given file name. * If the first character of name is %, we are considered to be editing the * file, otherwise we are reading our mail which has signficance for mbox and - * so forth. nmail: Check for new mail in the current folder only */ -FL int setfile(char const *name, int nmail); + * so forth. + nmail: Check for new mail in the current folder only */ +FL int setfile(char const *name, enum fedit_mode fm); FL int newmailinfo(int omsgCount); @@ -1125,7 +1126,7 @@ FL void * zalloc(FILE *fp); * maildir.c */ -FL int maildir_setfile(char const *name, int nmail, int isedit); +FL int maildir_setfile(char const *name, enum fedit_mode fm); FL void maildir_quit(void); @@ -1376,7 +1377,7 @@ FL enum okay smime_certsave(struct message *m, int n, FILE *op); FL enum okay pop3_noop(void); /* */ -FL int pop3_setfile(char const *server, int nmail, int isedit); +FL int pop3_setfile(char const *server, enum fedit_mode fm); /* */ FL enum okay pop3_header(struct message *m); diff --git a/pop3.c b/pop3.c index 6f084161..320dded8 100644 --- a/pop3.c +++ b/pop3.c @@ -816,7 +816,7 @@ pop3_noop(void) } FL int -pop3_setfile(char const *server, int nmail, int isedit) +pop3_setfile(char const *server, enum fedit_mode fm) { struct sockconn sc; sighandler_type saveint, savepipe; @@ -825,7 +825,7 @@ pop3_setfile(char const *server, int nmail, int isedit) NYD_ENTER; rv = 1; - if (nmail) + if (fm & FEDIT_NEWMAIL) goto jleave; rv = -1; @@ -847,7 +847,7 @@ pop3_setfile(char const *server, int nmail, int isedit) rv = 1; quit(); - edit = (isedit != 0); + edit = !(fm & FEDIT_SYSBOX); if (mb.mb_sock.s_fd >= 0) sclose(&mb.mb_sock); if (mb.mb_itf) { @@ -897,7 +897,7 @@ pop3_setfile(char const *server, int nmail, int isedit) goto jleave; } mb.mb_type = MB_POP3; - mb.mb_perm = (options & OPT_R_FLAG) ? 0 : MB_DELE; + mb.mb_perm = ((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY)) ? 0 : MB_DELE; pop3_setptr(&mb); setmsize(msgCount); sawcom = FAL0; -- 2.11.4.GIT