Move string dope from strings.c to the new memory.c..
[s-mailx.git] / folder.c
blob117b57d750cfcf778ac2ddd0d36bda56275769c1
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Folder (mailbox) initialization, newmail announcement and related.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE folder
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #include <pwd.h>
44 /* Update mailname (if name != NULL) and displayname, return wether displayname
45 * was large enough to swallow mailname */
46 static bool_t _update_mailname(char const *name);
47 #ifdef HAVE_C90AMEND1 /* TODO unite __narrow_suffix() into one fun! */
48 SINLINE size_t __narrow_suffix(char const *cp, size_t cpl, size_t maxl);
49 #endif
51 #ifdef HAVE_C90AMEND1
52 SINLINE size_t
53 __narrow_suffix(char const *cp, size_t cpl, size_t maxl)
55 int err;
56 size_t i, ok;
57 NYD_ENTER;
59 for (err = ok = i = 0; cpl > maxl || err;) {
60 int ml = mblen(cp, cpl);
61 if (ml < 0) { /* XXX _narrow_suffix(): mblen() error; action? */
62 (void)mblen(NULL, 0);
63 err = 1;
64 ml = 1;
65 } else {
66 if (!err)
67 ok = i;
68 err = 0;
69 if (ml == 0)
70 break;
72 cp += ml;
73 i += ml;
74 cpl -= ml;
76 NYD_LEAVE;
77 return ok;
79 #endif /* HAVE_C90AMEND1 */
81 static bool_t
82 _update_mailname(char const *name)
84 char tbuf[PATH_MAX], *mailp, *dispp;
85 size_t i, j;
86 bool_t rv = TRU1;
87 NYD_ENTER;
89 /* Don't realpath(3) if it's only an update request */
90 if (name != NULL) {
91 #ifdef HAVE_REALPATH
92 enum protocol p = which_protocol(name);
93 if (p == PROTO_FILE || p == PROTO_MAILDIR) {
94 if (realpath(name, mailname) == NULL) {
95 n_err(_("Can't canonicalize \"%s\"\n"), name);
96 rv = FAL0;
97 goto jdocopy;
99 } else
100 jdocopy:
101 #endif
102 n_strscpy(mailname, name, sizeof(mailname));
105 mailp = mailname;
106 dispp = displayname;
108 /* Don't display an absolute path but "+FOLDER" if under *folder* */
109 if (getfold(tbuf, sizeof tbuf)) {
110 i = strlen(tbuf);
111 if (i < sizeof(tbuf) -1)
112 tbuf[i++] = '/';
113 if (!strncmp(tbuf, mailp, i)) {
114 mailp += i;
115 *dispp++ = '+';
119 /* We want to see the name of the folder .. on the screen */
120 i = strlen(mailp);
121 if (i < sizeof(displayname) -1)
122 memcpy(dispp, mailp, i +1);
123 else {
124 rv = FAL0;
125 /* Avoid disrupting multibyte sequences (if possible) */
126 #ifndef HAVE_C90AMEND1
127 j = sizeof(displayname) / 3 - 1;
128 i -= sizeof(displayname) - (1/* + */ + 3) - j;
129 #else
130 j = field_detect_clip(sizeof(displayname) / 3, mailp, i);
131 i = j + __narrow_suffix(mailp + j, i - j,
132 sizeof(displayname) - (1/* + */ + 3 + 1) - j);
133 #endif
134 snprintf(dispp, sizeof(displayname), "%.*s...%s",
135 (int)j, mailp, mailp + i);
137 NYD_LEAVE;
138 return rv;
141 FL int
142 setfile(char const *name, enum fedit_mode fm) /* TODO oh my god */
144 /* Note we don't 'userid(myname) != getuid()', preliminary steps are usually
145 * necessary to make a mailbox accessible by a different user, and if that
146 * has happened, let's just let the usual file perms decide */
147 static int shudclob;
149 struct stat stb;
150 size_t offset;
151 char const *who;
152 int rv, omsgCount = 0;
153 FILE *ibuf = NULL, *lckfp = NULL;
154 bool_t isdevnull = FAL0;
155 NYD_ENTER;
157 pstate &= ~PS_SETFILE_OPENED;
159 if (name[0] == '%' || ((who = shortcut_expand(name)) != NULL && *who == '%'))
160 fm |= FEDIT_SYSBOX; /* TODO fexpand() needs to tell is-valid-user! */
162 who = (name[1] != '\0') ? name + 1 : myname;
164 if ((name = expand(name)) == NULL)
165 goto jem1;
167 switch (which_protocol(name)) {
168 case PROTO_FILE:
169 if (temporary_protocol_ext != NULL)
170 name = savecat(name, temporary_protocol_ext);
171 isdevnull = ((options & OPT_BATCH_FLAG) && !strcmp(name, "/dev/null"));
172 #ifdef HAVE_REALPATH
173 do { /* TODO we need objects, goes away then */
174 char ebuf[PATH_MAX];
175 if (realpath(name, ebuf) != NULL)
176 name = savestr(ebuf);
177 } while (0);
178 #endif
179 rv = 1;
180 break;
181 case PROTO_MAILDIR:
182 shudclob = 1;
183 rv = maildir_setfile(name, fm);
184 goto jleave;
185 #ifdef HAVE_POP3
186 case PROTO_POP3:
187 shudclob = 1;
188 rv = pop3_setfile(name, fm);
189 goto jleave;
190 #endif
191 default:
192 n_err(_("Cannot handle protocol: %s\n"), name);
193 goto jem1;
196 if ((ibuf = Zopen(name, "r")) == NULL) {
197 if ((fm & FEDIT_SYSBOX) && errno == ENOENT) {
198 if (strcmp(who, myname) && getpwnam(who) == NULL) {
199 n_err(_("\"%s\" is not a user of this system\n"), who);
200 goto jem2;
202 if (!(options & OPT_QUICKRUN_MASK) && ok_blook(bsdcompat))
203 n_err(_("No mail for %s\n"), who);
204 if (fm & FEDIT_NEWMAIL)
205 goto jleave;
206 if (ok_blook(emptystart)) {
207 if (!(options & OPT_QUICKRUN_MASK) && !ok_blook(bsdcompat))
208 n_perr(name, 0);
209 /* We must avoid returning -1 and causing program exit */
210 mb.mb_type = MB_VOID;
211 rv = 1;
212 goto jleave;
214 goto jem2;
215 } else if (fm & FEDIT_NEWMAIL)
216 goto jleave;
217 n_perr(name, 0);
218 goto jem1;
221 if (fstat(fileno(ibuf), &stb) == -1) {
222 if (fm & FEDIT_NEWMAIL)
223 goto jleave;
224 n_perr(_("fstat"), 0);
225 goto jem1;
228 if (S_ISREG(stb.st_mode) || isdevnull) {
229 /* EMPTY */
230 } else {
231 if (fm & FEDIT_NEWMAIL)
232 goto jleave;
233 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
234 n_perr(name, 0);
235 goto jem1;
238 /* Looks like all will be well. We must now relinquish our hold on the
239 * current set of stuff. Must hold signals while we are reading the new
240 * file, else we will ruin the message[] data structure */
242 hold_sigs(); /* TODO note on this one in quit.c:quit() */
243 if (shudclob && !(fm & FEDIT_NEWMAIL))
244 quit();
245 #ifdef HAVE_SOCKETS
246 if (!(fm & FEDIT_NEWMAIL) && mb.mb_sock.s_fd >= 0)
247 sclose(&mb.mb_sock); /* TODO sorry? VMAILFS->close(), thank you */
248 #endif
250 /* TODO There is no intermediate VOID box we've switched to: name may
251 * TODO point to the same box that we just have written, so any updates
252 * TODO we won't see! Reopen again in this case. RACY! Goes with VOID! */
253 /* TODO In addition: in case of compressed/hook boxes we lock a temporary! */
254 /* TODO We may uselessly open twice but quit() doesn't say wether we were
255 * TODO modified so we can't tell: Mailbox::is_modified() :-(( */
256 if (/*shudclob && !(fm & FEDIT_NEWMAIL) &&*/ !strcmp(name, mailname)) {
257 name = mailname;
258 Fclose(ibuf);
260 if ((ibuf = Zopen(name, "r")) == NULL ||
261 fstat(fileno(ibuf), &stb) == -1 ||
262 (!S_ISREG(stb.st_mode) && !isdevnull)) {
263 n_perr(name, 0);
264 rele_sigs();
265 goto jem2;
269 /* Copy the messages into /tmp and set pointers */
270 if (!(fm & FEDIT_NEWMAIL)) {
271 mb.mb_type = MB_FILE;
272 mb.mb_perm = (((options & OPT_R_FLAG) || (fm & FEDIT_RDONLY) ||
273 access(name, W_OK) < 0) ? 0 : MB_DELE | MB_EDIT);
274 if (shudclob) {
275 if (mb.mb_itf) {
276 fclose(mb.mb_itf);
277 mb.mb_itf = NULL;
279 if (mb.mb_otf) {
280 fclose(mb.mb_otf);
281 mb.mb_otf = NULL;
284 shudclob = 1;
285 if (fm & FEDIT_SYSBOX)
286 pstate &= ~PS_EDIT;
287 else
288 pstate |= PS_EDIT;
289 initbox(name);
290 offset = 0;
291 } else {
292 fseek(mb.mb_otf, 0L, SEEK_END);
293 /* TODO Doing this without holding a lock is.. And not err checking.. */
294 fseek(ibuf, mailsize, SEEK_SET);
295 offset = mailsize;
296 omsgCount = msgCount;
299 if (isdevnull)
300 lckfp = (FILE*)-1;
301 else if (!(pstate & PS_EDIT))
302 lckfp = dot_lock(name, fileno(ibuf), FLT_READ, offset,0,
303 (fm & FEDIT_NEWMAIL ? 0 : 1));
304 else if (file_lock(fileno(ibuf), FLT_READ, offset,0,
305 (fm & FEDIT_NEWMAIL ? 0 : 1)))
306 lckfp = (FILE*)-1;
308 if (lckfp == NULL) {
309 if (!(fm & FEDIT_NEWMAIL)) {
310 char const *emsg = (pstate & PS_EDIT)
311 ? N_("Unable to lock mailbox, aborting operation")
312 : N_("Unable to (dot) lock mailbox, aborting operation");
313 n_perr(V_(emsg), 0);
315 rele_sigs();
316 if (!(fm & FEDIT_NEWMAIL))
317 goto jem1;
318 goto jleave;
321 mailsize = fsize(ibuf);
323 /* TODO This is too simple minded? We should regenerate an index file
324 * TODO to be able to truly tell wether *anything* has changed! */
325 if ((fm & FEDIT_NEWMAIL) && UICMP(z, mailsize, <=, offset)) {
326 rele_sigs();
327 goto jleave;
329 setptr(ibuf, offset);
330 setmsize(msgCount);
331 if ((fm & FEDIT_NEWMAIL) && mb.mb_sorted) {
332 mb.mb_threaded = 0;
333 c_sort((void*)-1);
336 Fclose(ibuf);
337 ibuf = NULL;
338 if (lckfp != NULL && lckfp != (FILE*)-1) {
339 Pclose(lckfp, FAL0);
340 /*lckfp = NULL;*/
342 rele_sigs();
344 if (!(fm & FEDIT_NEWMAIL)) {
345 pstate &= ~PS_SAW_COMMAND;
346 pstate |= PS_SETFILE_OPENED;
349 if ((options & OPT_EXISTONLY) && !(options & OPT_HEADERLIST)) {
350 rv = (msgCount == 0);
351 goto jleave;
354 if ((!(pstate & PS_EDIT) || (fm & FEDIT_NEWMAIL)) && msgCount == 0) {
355 if (!(fm & FEDIT_NEWMAIL)) {
356 if (!ok_blook(emptystart))
357 n_err(_("No mail for %s\n"), who);
359 goto jleave;
362 if (fm & FEDIT_NEWMAIL)
363 newmailinfo(omsgCount);
365 rv = 0;
366 jleave:
367 if (ibuf != NULL) {
368 Fclose(ibuf);
369 if (lckfp != NULL && lckfp != (FILE*)-1)
370 Pclose(lckfp, FAL0);
372 NYD_LEAVE;
373 return rv;
374 jem2:
375 mb.mb_type = MB_VOID;
376 jem1:
377 rv = -1;
378 goto jleave;
381 FL int
382 newmailinfo(int omsgCount)
384 int mdot, i;
385 NYD_ENTER;
387 for (i = 0; i < omsgCount; ++i)
388 message[i].m_flag &= ~MNEWEST;
390 if (msgCount > omsgCount) {
391 for (i = omsgCount; i < msgCount; ++i)
392 message[i].m_flag |= MNEWEST;
393 printf(_("New mail has arrived.\n"));
394 if ((i = msgCount - omsgCount) == 1)
395 printf(_("Loaded 1 new message.\n"));
396 else
397 printf(_("Loaded %d new messages.\n"), i);
398 } else
399 printf(_("Loaded %d messages.\n"), msgCount);
401 check_folder_hook(TRU1);
403 mdot = getmdot(1);
405 if (ok_blook(header))
406 print_headers(omsgCount + 1, msgCount, FAL0);
407 NYD_LEAVE;
408 return mdot;
411 FL void
412 setmsize(int sz)
414 NYD_ENTER;
415 if (n_msgvec != NULL)
416 free(n_msgvec);
417 n_msgvec = scalloc(sz + 1, sizeof *n_msgvec);
418 NYD_LEAVE;
421 FL void
422 print_header_summary(char const *Larg)
424 size_t bot, top, i, j;
425 NYD_ENTER;
427 if (Larg != NULL) {
428 /* Avoid any messages XXX add a make_mua_silent() and use it? */
429 if ((options & (OPT_VERB | OPT_EXISTONLY)) == OPT_EXISTONLY) {
430 freopen("/dev/null", "w", stdout);
431 freopen("/dev/null", "w", stderr);
433 assert(n_msgvec != NULL);
434 i = (getmsglist(/*TODO make arg const */UNCONST(Larg), n_msgvec, 0) <= 0);
435 if (options & OPT_EXISTONLY) {
436 exit_status = (int)i;
437 goto jleave;
439 if (i)
440 goto jleave;
441 for (bot = msgCount, top = 0, i = 0; (j = n_msgvec[i]) != 0; ++i) {
442 if (bot > j)
443 bot = j;
444 if (top < j)
445 top = j;
447 } else
448 bot = 1, top = msgCount;
449 print_headers(bot, top, (Larg != NULL)); /* TODO should take iterator!! */
450 jleave:
451 NYD_LEAVE;
454 FL void
455 announce(int printheaders)
457 int vec[2], mdot;
458 NYD_ENTER;
460 mdot = newfileinfo();
461 vec[0] = mdot;
462 vec[1] = 0;
463 dot = message + mdot - 1;
464 if (printheaders && msgCount > 0 && ok_blook(header)) {
465 print_header_group(vec); /* XXX errors? */
467 NYD_LEAVE;
470 FL int
471 newfileinfo(void)
473 struct message *mp;
474 int u, n, mdot, d, s, hidden, moved;
475 NYD_ENTER;
477 if (mb.mb_type == MB_VOID) {
478 mdot = 1;
479 goto jleave;
482 mdot = getmdot(0);
483 s = d = hidden = moved =0;
484 for (mp = message, n = 0, u = 0; PTRCMP(mp, <, message + msgCount); ++mp) {
485 if (mp->m_flag & MNEW)
486 ++n;
487 if ((mp->m_flag & MREAD) == 0)
488 ++u;
489 if ((mp->m_flag & (MDELETED | MSAVED)) == (MDELETED | MSAVED))
490 ++moved;
491 if ((mp->m_flag & (MDELETED | MSAVED)) == MDELETED)
492 ++d;
493 if ((mp->m_flag & (MDELETED | MSAVED)) == MSAVED)
494 ++s;
495 if (mp->m_flag & MHIDDEN)
496 ++hidden;
499 /* If displayname gets truncated the user effectively has no option to see
500 * the full pathname of the mailbox, so print it at least for '? fi' */
501 printf(_("\"%s\": "),
502 (_update_mailname(NULL) ? displayname : mailname));
503 if (msgCount == 1)
504 printf(_("1 message"));
505 else
506 printf(_("%d messages"), msgCount);
507 if (n > 0)
508 printf(_(" %d new"), n);
509 if (u-n > 0)
510 printf(_(" %d unread"), u);
511 if (d > 0)
512 printf(_(" %d deleted"), d);
513 if (s > 0)
514 printf(_(" %d saved"), s);
515 if (moved > 0)
516 printf(_(" %d moved"), moved);
517 if (hidden > 0)
518 printf(_(" %d hidden"), hidden);
519 else if (mb.mb_perm == 0)
520 printf(_(" [Read only]"));
521 printf("\n");
522 jleave:
523 NYD_LEAVE;
524 return mdot;
527 FL int
528 getmdot(int nmail)
530 struct message *mp;
531 char *cp;
532 int mdot;
533 enum mflag avoid = MHIDDEN | MDELETED;
534 NYD_ENTER;
536 if (!nmail) {
537 if (ok_blook(autothread)) {
538 OBSOLETE(_("please use *autosort=thread* instead of *autothread*"));
539 c_thread(NULL);
540 } else if ((cp = ok_vlook(autosort)) != NULL) {
541 if (mb.mb_sorted != NULL)
542 free(mb.mb_sorted);
543 mb.mb_sorted = sstrdup(cp);
544 c_sort(NULL);
547 if (mb.mb_type == MB_VOID) {
548 mdot = 1;
549 goto jleave;
552 if (nmail)
553 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
554 if ((mp->m_flag & (MNEWEST | avoid)) == MNEWEST)
555 break;
557 if (!nmail || PTRCMP(mp, >=, message + msgCount)) {
558 if (mb.mb_threaded) {
559 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
560 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
561 break;
562 } else {
563 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
564 if ((mp->m_flag & (MNEW | avoid)) == MNEW)
565 break;
569 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
570 if (mb.mb_threaded) {
571 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
572 if (mp->m_flag & MFLAGGED)
573 break;
574 } else {
575 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
576 if (mp->m_flag & MFLAGGED)
577 break;
581 if ((mb.mb_threaded ? (mp == NULL) : PTRCMP(mp, >=, message + msgCount))) {
582 if (mb.mb_threaded) {
583 for (mp = threadroot; mp != NULL; mp = next_in_thread(mp))
584 if (!(mp->m_flag & (MREAD | avoid)))
585 break;
586 } else {
587 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
588 if (!(mp->m_flag & (MREAD | avoid)))
589 break;
593 if (nmail &&
594 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
595 mdot = (int)PTR2SIZE(mp - message + 1);
596 else if (ok_blook(showlast)) {
597 if (mb.mb_threaded) {
598 for (mp = this_in_thread(threadroot, -1); mp;
599 mp = prev_in_thread(mp))
600 if (!(mp->m_flag & avoid))
601 break;
602 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
603 } else {
604 for (mp = message + msgCount - 1; mp >= message; --mp)
605 if (!(mp->m_flag & avoid))
606 break;
607 mdot = (mp >= message) ? (int)PTR2SIZE(mp - message + 1) : msgCount;
609 } else if (!nmail &&
610 (mb.mb_threaded ? (mp != NULL) : PTRCMP(mp, <, message + msgCount)))
611 mdot = (int)PTR2SIZE(mp - message + 1);
612 else if (mb.mb_threaded) {
613 for (mp = threadroot; mp; mp = next_in_thread(mp))
614 if (!(mp->m_flag & avoid))
615 break;
616 mdot = (mp != NULL) ? (int)PTR2SIZE(mp - message + 1) : 1;
617 } else {
618 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
619 if (!(mp->m_flag & avoid))
620 break;
621 mdot = PTRCMP(mp, <, message + msgCount)
622 ? (int)PTR2SIZE(mp - message + 1) : 1;
624 jleave:
625 NYD_LEAVE;
626 return mdot;
629 FL void
630 initbox(char const *name)
632 char *tempMesg;
633 NYD_ENTER;
635 if (mb.mb_type != MB_VOID)
636 n_strscpy(prevfile, mailname, PATH_MAX);
638 /* TODO name always NE mailname (but goes away for objects anyway)
639 * TODO Well, not true no more except that in parens */
640 _update_mailname((name != mailname) ? name : NULL);
642 if ((mb.mb_otf = Ftmp(&tempMesg, "tmpmbox", OF_WRONLY | OF_HOLDSIGS)) ==
643 NULL) {
644 n_perr(_("temporary mail message file"), 0);
645 exit(EXIT_ERR);
647 if ((mb.mb_itf = safe_fopen(tempMesg, "r", NULL)) == NULL) {
648 n_perr(_("temporary mail message file"), 0);
649 exit(EXIT_ERR);
651 Ftmp_release(&tempMesg);
653 message_reset();
654 mb.mb_threaded = 0;
655 if (mb.mb_sorted != NULL) {
656 free(mb.mb_sorted);
657 mb.mb_sorted = NULL;
659 dot = prevdot = threadroot = NULL;
660 pstate &= ~PS_DID_PRINT_DOT;
661 NYD_LEAVE;
664 FL bool_t
665 getfold(char *name, size_t size)
667 char const *folder;
668 NYD_ENTER;
670 if ((folder = ok_vlook(folder)) != NULL)
671 n_strscpy(name, folder, size);
672 NYD_LEAVE;
673 return (folder != NULL);
676 /* s-it-mode */