fd7b55e2e21a326dcd81e1411e52989ea522eaa4
[mmh] / uip / inc.c
1
2 /*
3  * inc.c -- incorporate messages from a maildrop into a folder
4  *
5  * $Id$
6  */
7
8 #ifdef MAILGROUP
9 /* Revised: Sat Apr 14 17:08:17 PDT 1990 (marvit@hplabs)
10  *    Added hpux hacks to set and reset gid to be "mail" as needed. The reset
11  *    is necessary so inc'ed mail is the group of the inc'er, rather than
12  *    "mail". We setgid to egid only when [un]locking the mail file. This
13  *    is also a major security precaution which will not be explained here.
14  *
15  * Fri Feb  7 16:04:57 PST 1992         John Romine <bug-mh@ics.uci.edu>
16  *   NB: I'm not 100% sure that this setgid stuff is secure even now.
17  */
18 #endif
19
20 #include <h/mh.h>
21 #include <fcntl.h>
22
23 #ifdef POP
24 # include <h/dropsbr.h>
25 # include <h/popsbr.h>
26 #endif
27
28 #ifdef HESIOD
29 # include <hesiod.h>
30 #endif
31
32 #include <h/fmt_scan.h>
33 #include <h/scansbr.h>
34 #include <h/signals.h>
35 #include <zotnet/tws/tws.h>
36 #include <zotnet/mts/mts.h>
37 #include <errno.h>
38 #include <signal.h>
39
40 #ifndef POP
41 # define POPminc(a) (a)
42 #else
43 # define POPminc(a)  0
44 #endif
45
46 #ifndef RPOP
47 # define RPOPminc(a) (a)
48 #else
49 # define RPOPminc(a)  0
50 #endif
51
52 #ifndef APOP
53 # define APOPminc(a) (a)
54 #else
55 # define APOPminc(a)  0
56 #endif
57
58 #ifndef KPOP
59 # define KPOPminc(a) (a)
60 #else
61 # define KPOPminc(a)  0
62 #endif
63
64 static struct swit switches[] = {
65 #define AUDSW                      0
66     { "audit audit-file", 0 },
67 #define NAUDSW                     1
68     { "noaudit", 0 },
69 #define CHGSW                      2
70     { "changecur", 0 },
71 #define NCHGSW                     3
72     { "nochangecur", 0 },
73 #define FILESW                     4
74     { "file name", 0 },
75 #define FORMSW                     5
76     { "form formatfile", 0 },
77 #define FMTSW                      6
78     { "format string", 5 },
79 #define HOSTSW                     7
80     { "host hostname", POPminc (-4) },
81 #define USERSW                     8
82     { "user username", POPminc (-4) },
83 #define PACKSW                     9
84     { "pack file", POPminc (-4) },
85 #define NPACKSW                   10
86     { "nopack", POPminc (-6) },
87 #define APOPSW                    11
88     { "apop", APOPminc (-4) },
89 #define NAPOPSW                   12
90     { "noapop", APOPminc (-6) },
91 #define RPOPSW                    13
92     { "rpop", RPOPminc (-4) },
93 #define NRPOPSW                   14
94     { "norpop", RPOPminc (-6) },
95 #define SILSW                     15
96     { "silent", 0 },
97 #define NSILSW                    16
98     { "nosilent", 0 },
99 #define TRNCSW                    17
100     { "truncate", 0 },
101 #define NTRNCSW                   18
102     { "notruncate", 0 },
103 #define WIDTHSW                   19
104     { "width columns", 0 },
105 #define VERSIONSW                 20
106     { "version", 0 },
107 #define HELPSW                    21
108     { "help", 4 },
109 #define SNOOPSW                   22
110     { "snoop", -5 },
111 #define KPOPSW                    23
112     { "kpop", KPOPminc (-4) },
113     { NULL, 0 }
114 };
115
116 extern int errno;
117
118 /*
119  * flags for the mail source
120  */
121 #define INC_FILE  0
122 #define INC_POP   1
123
124 static int inc_type;
125 static int snoop = 0;
126
127 #ifdef POP
128 extern char response[];
129
130 static char *packfile = NULL;
131 static int size;
132 static long pos;
133 static long start;
134 static long stop;
135
136 static int mbx_style = MMDF_FORMAT;
137 static int pd = NOTOK;
138 static FILE *pf = NULL;
139 #endif /* POP */
140
141
142 /*
143  * For setting and returning to "mail" gid
144  */
145 #ifdef MAILGROUP
146 static int return_gid;
147 #endif
148
149 /*
150  * prototypes
151  */
152 char *map_name(char *);
153
154 #ifdef POP
155 int done(int);
156 static int pop_action(char *);
157 static int pop_pack(char *);
158 static int map_count(void);
159 #endif
160
161
162 int
163 main (int argc, char **argv)
164 {
165     int chgflag = 1, trnflag = 1;
166     int noisy = 1, width = 0, locked = 0;
167     int rpop, i, hghnum, msgnum;
168     int kpop = 0;
169     char *cp, *maildir, *folder = NULL;
170     char *format = NULL, *form = NULL;
171     char *newmail, *host = NULL, *user = NULL;
172     char *audfile = NULL, *from = NULL;
173     char buf[BUFSIZ], **argp, *nfs, **arguments;
174     struct msgs *mp;
175     struct stat st, s1;
176     FILE *in, *aud = NULL;
177
178 #ifdef POP
179     int nmsgs, nbytes, p = 0;
180     char *pass = NULL;
181 #endif
182
183 #ifdef MHE
184     FILE *mhe = NULL;
185 #endif
186
187 #ifdef HESIOD
188     struct hes_postoffice *po;
189     char *tmphost;
190 #endif
191
192 #ifdef LOCALE
193     setlocale(LC_ALL, "");
194 #endif
195     invo_name = r1bindex (argv[0], '/');
196
197     /* read user profile/context */
198     context_read();
199
200     mts_init (invo_name);
201     arguments = getarguments (invo_name, argc, argv, 1);
202     argp = arguments;
203
204 #ifdef POP
205 # ifdef HESIOD
206     /*
207      * Scheme is:
208      *        use MAILHOST environment variable if present,
209      *  else try Hesiod.
210      *  If that fails, use the default (if any)
211      *  provided by mts.conf in mts_init()
212      */
213     if ((tmphost = getenv("MAILHOST")) != NULL)
214         pophost = tmphost;
215     else if ((po = hes_getmailhost(getusername())) != NULL &&
216              strcmp(po->po_type, "POP") == 0)
217         pophost = po->po_host;
218 # endif /* HESIOD */
219     /*
220      * If there is a valid "pophost" entry in mts.conf,
221      * then use it as the default host.
222      */
223     if (pophost && *pophost)
224         host = pophost;
225
226     if ((cp = getenv ("MHPOPDEBUG")) && *cp)
227         snoop++;
228 #endif /* POP */
229
230     rpop = 0;
231
232     while ((cp = *argp++)) {
233         if (*cp == '-') {
234             switch (smatch (++cp, switches)) {
235             case AMBIGSW: 
236                 ambigsw (cp, switches);
237                 done (1);
238             case UNKWNSW: 
239                 adios (NULL, "-%s unknown", cp);
240
241             case HELPSW: 
242                 snprintf (buf, sizeof(buf), "%s [+folder] [switches]", invo_name);
243                 print_help (buf, switches, 1);
244                 done (1);
245             case VERSIONSW:
246                 print_version(invo_name);
247                 done (1);
248
249             case AUDSW: 
250                 if (!(cp = *argp++) || *cp == '-')
251                     adios (NULL, "missing argument to %s", argp[-2]);
252                 audfile = getcpy (m_maildir (cp));
253                 continue;
254             case NAUDSW: 
255                 audfile = NULL;
256                 continue;
257
258             case CHGSW: 
259                 chgflag++;
260                 continue;
261             case NCHGSW: 
262                 chgflag = 0;
263                 continue;
264
265             /*
266              * The flag `trnflag' has the value:
267              *
268              * 2 if -truncate is given
269              * 1 by default (truncating is default)
270              * 0 if -notruncate is given
271              */
272             case TRNCSW: 
273                 trnflag = 2;
274                 continue;
275             case NTRNCSW: 
276                 trnflag = 0;
277                 continue;
278
279             case FILESW: 
280                 if (!(cp = *argp++) || *cp == '-')
281                     adios (NULL, "missing argument to %s", argp[-2]);
282                 from = path (cp, TFILE);
283
284                 /*
285                  * If the truncate file is in default state,
286                  * change to not truncate.
287                  */
288                 if (trnflag == 1)
289                     trnflag = 0;
290                 continue;
291
292             case SILSW: 
293                 noisy = 0;
294                 continue;
295             case NSILSW: 
296                 noisy++;
297                 continue;
298
299             case FORMSW: 
300                 if (!(form = *argp++) || *form == '-')
301                     adios (NULL, "missing argument to %s", argp[-2]);
302                 format = NULL;
303                 continue;
304             case FMTSW: 
305                 if (!(format = *argp++) || *format == '-')
306                     adios (NULL, "missing argument to %s", argp[-2]);
307                 form = NULL;
308                 continue;
309
310             case WIDTHSW: 
311                 if (!(cp = *argp++) || *cp == '-')
312                     adios (NULL, "missing argument to %s", argp[-2]);
313                 width = atoi (cp);
314                 continue;
315
316             case HOSTSW:
317                 if (!(host = *argp++) || *host == '-')
318                     adios (NULL, "missing argument to %s", argp[-2]);
319                 continue;
320             case USERSW:
321                 if (!(user = *argp++) || *user == '-')
322                     adios (NULL, "missing argument to %s", argp[-2]);
323                 continue;
324
325             case PACKSW:
326 #ifndef POP
327                 if (!(cp = *argp++) || *cp == '-')
328                     adios (NULL, "missing argument to %s", argp[-2]);
329 #else /* POP */
330                 if (!(packfile = *argp++) || *packfile == '-')
331                     adios (NULL, "missing argument to %s", argp[-2]);
332 #endif /* POP */
333                 continue;
334             case NPACKSW:
335 #ifdef POP
336                 packfile = NULL;
337 #endif /* POP */
338                 continue;
339
340             case APOPSW:
341                 rpop = -1;
342                 continue;
343             case NAPOPSW:
344                 rpop = 0;
345                 continue;
346
347             case RPOPSW:
348                 rpop = 1;
349                 continue;
350             case NRPOPSW:
351                 rpop = 0;
352                 continue;
353
354             case KPOPSW:
355                 kpop = 1;
356                 continue;
357
358             case SNOOPSW:
359                 snoop++;
360                 continue;
361             }
362         }
363         if (*cp == '+' || *cp == '@') {
364             if (folder)
365                 adios (NULL, "only one folder at a time!");
366             else
367                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
368         } else {
369             adios (NULL, "usage: %s [+folder] [switches]", invo_name);
370         }
371     }
372
373 #ifdef MAILGROUP
374     return_gid = getegid();  /* Save effective gid, assuming we'll use it */
375     setgid(getgid());        /* Turn off extraordinary privileges         */
376 #endif  /* MAILGROUP */
377
378 #ifdef POP
379     if (host && !*host)
380         host = NULL;
381     if (from || !host || rpop <= 0)
382         setuid (getuid ());
383 #endif /* POP */
384
385     /*
386      * Where are we getting the new mail?
387      */
388     if (from)
389         inc_type = INC_FILE;
390 #ifdef POP
391     else if (host)
392         inc_type = INC_POP;
393 #endif
394     else
395         inc_type = INC_FILE;
396
397 #ifdef POP
398     /*
399      * Are we getting the mail from
400      * a POP server?
401      */
402     if (inc_type == INC_POP) {
403         if (user == NULL)
404             user = getusername ();
405         if (kpop || ( rpop > 0))
406             pass = getusername ();
407         else
408             ruserpass (host, &user, &pass);
409
410         /*
411          * initialize POP connection
412          */
413         if (pop_init (host, user, pass, snoop, kpop ? 1 : rpop, kpop) == NOTOK)
414             adios (NULL, "%s", response);
415
416         /* Check if there are any messages */
417         if (pop_stat (&nmsgs, &nbytes) == NOTOK)
418             adios (NULL, "%s", response);
419
420         if (rpop > 0)
421             setuid (getuid ());
422         if (nmsgs == 0) {
423             pop_quit();
424             adios (NULL, "no mail to incorporate");
425         }
426     }
427 #endif /* POP */
428
429     /*
430      * We will get the mail from a file
431      * (typically the standard maildrop)
432      */
433
434     if (inc_type == INC_FILE) {
435         if (from)
436             newmail = from;
437         else if ((newmail = getenv ("MAILDROP")) && *newmail)
438             newmail = m_mailpath (newmail);
439         else if ((newmail = context_find ("maildrop")) && *newmail)
440             newmail = m_mailpath (newmail);
441         else {
442             newmail = concat (MAILDIR, "/", MAILFIL, NULL);
443         }
444         if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
445             adios (NULL, "no mail to incorporate");
446     }
447
448 #ifdef POP
449     /* skip the folder setup */
450     if ((inc_type == INC_POP) && packfile)
451         goto go_to_it;
452 #endif /* POP */
453
454     if (!context_find ("path"))
455         free (path ("./", TFOLDER));
456     if (!folder)
457         folder = getfolder (0);
458     maildir = m_maildir (folder);
459
460     if (stat (maildir, &st) == NOTOK) {
461         if (errno != ENOENT)
462             adios (maildir, "error on folder");
463         cp = concat ("Create folder \"", maildir, "\"? ", NULL);
464         if (noisy && !getanswer (cp))
465             done (1);
466         free (cp);
467         if (!makedir (maildir))
468             adios (NULL, "unable to create folder %s", maildir);
469     }
470
471     if (chdir (maildir) == NOTOK)
472         adios (maildir, "unable to change directory to");
473
474     /* read folder and create message structure */
475     if (!(mp = folder_read (folder)))
476         adios (NULL, "unable to read folder %s", folder);
477
478 #ifdef POP
479 go_to_it:
480 #endif /* POP */
481
482     if (inc_type == INC_FILE) {
483         if (access (newmail, W_OK) != NOTOK) {
484             locked++;
485             if (trnflag) {
486                 SIGNAL (SIGHUP, SIG_IGN);
487                 SIGNAL (SIGINT, SIG_IGN);
488                 SIGNAL (SIGQUIT, SIG_IGN);
489                 SIGNAL (SIGTERM, SIG_IGN);
490             }
491
492 #ifdef MAILGROUP
493             setgid(return_gid); /* Reset gid to lock mail file */
494 #endif /* MAILGROUP */
495
496             /* lock and fopen the mail spool */
497             if ((in = lkfopen (newmail, "r")) == NULL)
498                 adios (NULL, "unable to lock and fopen %s", newmail);
499
500 #ifdef MAILGROUP
501             setgid(getgid());   /* Return us to normal privileges */
502 #endif /* MAILGROUP */
503             fstat (fileno(in), &s1);
504         } else {
505             trnflag = 0;
506             if ((in = fopen (newmail, "r")) == NULL)
507                 adios (newmail, "unable to read");
508         }
509     }
510
511 #ifdef MAILGROUP
512     setgid(getgid());   /* Return us to normal privileges */
513 #endif /* MAILGROUP */
514
515     if (audfile) {
516         if ((i = stat (audfile, &st)) == NOTOK)
517             advise (NULL, "Creating Receive-Audit: %s", audfile);
518         if ((aud = fopen (audfile, "a")) == NULL)
519             adios (audfile, "unable to append to");
520         else if (i == NOTOK)
521             chmod (audfile, m_gmprot ());
522
523 #ifdef POP
524         fprintf (aud, from ? "<<inc>> %s -ms %s\n"
525                  : host ? "<<inc>> %s -host %s -user %s%s\n"
526                  : "<<inc>> %s\n",
527                  dtimenow (0), from ? from : host, user,
528                  rpop < 0 ? " -apop" : rpop > 0 ? " -rpop" : "");
529 #else /* POP */
530         fprintf (aud, from ? "<<inc>> %s  -ms %s\n" : "<<inc>> %s\n",
531                  dtimenow (0), from);
532 #endif /* POP */
533     }
534
535 #ifdef MHE
536     if (context_find ("mhe")) {
537         cp = concat (maildir, "/++", NULL);
538         i = stat (cp, &st);
539         if ((mhe = fopen (cp, "a")) == NULL)
540             admonish (cp, "unable to append to");
541         else
542             if (i == NOTOK)
543                 chmod (cp, m_gmprot ());
544         free (cp);
545     }
546 #endif /* MHE */
547
548     /* Get new format string */
549     nfs = new_fs (form, format, FORMAT);
550
551     if (noisy) {
552         printf ("Incorporating new mail into %s...\n\n", folder);
553         fflush (stdout);
554     }
555
556 #ifdef POP
557     /*
558      * Get the mail from a POP server
559      */
560     if (inc_type == INC_POP) {
561         if (packfile) {
562             packfile = path (packfile, TFILE);
563             if (stat (packfile, &st) == NOTOK) {
564                 if (errno != ENOENT)
565                     adios (packfile, "error on file");
566                 cp = concat ("Create file \"", packfile, "\"? ", NULL);
567                 if (noisy && !getanswer (cp))
568                     done (1);
569                 free (cp);
570             }
571             msgnum = map_count ();
572             if ((pd = mbx_open (packfile, mbx_style, getuid(), getgid(), m_gmprot()))
573                 == NOTOK)
574                 adios (packfile, "unable to open");
575             if ((pf = fdopen (pd, "w+")) == NULL)
576                 adios (NULL, "unable to fdopen %s", packfile);
577         } else {
578             hghnum = msgnum = mp->hghmsg;
579             /*
580              * Check if we have enough message space for all the new
581              * messages.  If not, then realloc the folder and add enough
582              * space for all new messages plus 10 additional slots.
583              */
584             if (mp->hghmsg + nmsgs >= mp->hghoff
585                 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + nmsgs + 10)))
586                 adios (NULL, "unable to allocate folder storage");
587         }
588
589         for (i = 1; i <= nmsgs; i++) {
590             msgnum++;
591             if (packfile) {
592                 fseek (pf, 0L, SEEK_CUR);
593                 pos = ftell (pf);
594                 size = 0;
595                 fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
596                 start = ftell (pf);
597
598                 if (pop_retr (i, pop_pack) == NOTOK)
599                     adios (NULL, "%s", response);
600
601                 fseek (pf, 0L, SEEK_CUR);
602                 stop = ftell (pf);
603                 if (fflush (pf))
604                     adios (packfile, "write error on");
605                 fseek (pf, start, SEEK_SET);
606             } else {
607                 cp = getcpy (m_name (msgnum));
608                 if ((pf = fopen (cp, "w+")) == NULL)
609                     adios (cp, "unable to write");
610                 chmod (cp, m_gmprot ());
611                 start = stop = 0L;
612
613                 if (pop_retr (i, pop_action) == NOTOK)
614                     adios (NULL, "%s", response);
615
616                 if (fflush (pf))
617                     adios (cp, "write error on");
618                 fseek (pf, 0L, SEEK_SET);
619             }
620             switch (p = scan (pf, msgnum, 0, nfs, width,
621                               packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
622                               1, NULL, stop - start, noisy)) {
623             case SCNEOF: 
624                 printf ("%*d  empty\n", DMAXFOLDER, msgnum);
625                 break;
626
627             case SCNFAT:
628                 trnflag = 0;
629                 noisy++;
630                 /* advise (cp, "unable to read"); already advised */
631                 /* fall thru */
632
633             case SCNERR:
634             case SCNNUM: 
635                 break;
636
637             case SCNMSG: 
638             case SCNENC:
639             default: 
640                 if (aud)
641                     fputs (scanl, aud);
642 # ifdef MHE
643                 if (mhe)
644                     fputs (scanl, mhe);
645 # endif /* MHE */
646                 if (noisy)
647                     fflush (stdout);
648                 if (!packfile) {
649                     clear_msg_flags (mp, msgnum);
650                     set_exists (mp, msgnum);
651                     set_unseen (mp, msgnum);
652                     mp->msgflags |= SEQMOD;
653                 }
654                 break;
655             }
656             if (packfile) {
657                 fseek (pf, stop, SEEK_SET);
658                 fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
659                 if (fflush (pf) || ferror (pf)) {
660                     int e = errno;
661                     pop_quit ();
662                     errno = e;
663                     adios (packfile, "write error on");
664                 }
665                 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
666             } else {
667                 if (ferror(pf) || fclose (pf)) {
668                     int e = errno;
669                     unlink (cp);
670                     pop_quit ();
671                     errno = e;
672                     adios (cp, "write error on");
673                 }
674                 free (cp);
675             }
676
677             if (trnflag && pop_dele (i) == NOTOK)
678                 adios (NULL, "%s", response);
679         }
680
681         if (pop_quit () == NOTOK)
682             adios (NULL, "%s", response);
683         if (packfile) {
684             mbx_close (packfile, pd);
685             pd = NOTOK;
686         }
687     }
688 #endif /* POP */
689
690     /*
691      * Get the mail from file (usually mail spool)
692      */
693     if (inc_type == INC_FILE) {
694         m_unknown (in);         /* the MAGIC invocation... */
695         hghnum = msgnum = mp->hghmsg;
696         for (i = 0;;) {
697             /*
698              * Check if we need to allocate more space for message status.
699              * If so, then add space for an additional 100 messages.
700              */
701             if (msgnum >= mp->hghoff
702                 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
703                 advise (NULL, "unable to allocate folder storage");
704                 i = NOTOK;
705                 break;
706             }
707
708 #if 0
709             /* copy file from spool to tmp file */
710             tmpfilenam = m_scratch ("", invo_name);
711             if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
712                 adios (tmpfilenam, "unable to create");
713             chmod (tmpfilenam, m_gmprot ());
714             if (!(in2 = fdopen (fd, "r+")))
715                 adios (tmpfilenam, "unable to access");
716             cpymsg (in, in2);
717
718             /* link message into folder */
719             newmsg = folder_addmsg(mp, tmpfilenam);
720 #endif
721
722             /* create scanline for new message */
723             switch (i = scan (in, msgnum + 1, msgnum + 1, nfs, width,
724                               msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
725             case SCNFAT:
726             case SCNEOF: 
727                 break;
728
729             case SCNERR:
730                 if (aud)
731                     fputs ("inc aborted!\n", aud);
732                 advise (NULL, "aborted!");      /* doesn't clean up locks! */
733                 break;
734
735             case SCNNUM: 
736                 advise (NULL, "BUG in %s, number out of range", invo_name);
737                 break;
738
739             default: 
740                 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, i);
741                 break;
742
743             case SCNMSG:
744             case SCNENC:
745                 if (aud)
746                     fputs (scanl, aud);
747 #ifdef MHE
748                 if (mhe)
749                     fputs (scanl, mhe);
750 #endif /* MHE */
751                 if (noisy)
752                     fflush (stdout);
753
754                 msgnum++;
755                 mp->hghmsg++;
756                 clear_msg_flags (mp, msgnum);
757                 set_exists (mp, msgnum);
758                 set_unseen (mp, msgnum);
759                 mp->msgflags |= SEQMOD;
760                 continue;
761             }
762             break;
763         }
764     }
765
766 #ifdef POP
767     if (p < 0) {                /* error */
768 #else
769     if (i < 0) {                /* error */
770 #endif
771         if (locked) {
772 #ifdef MAILGROUP
773             /* Be sure we can unlock mail file */
774             setgid(return_gid);
775 #endif /* MAILGROUP */
776
777             lkfclose (in, newmail);
778
779 #ifdef MAILGROUP
780             /* And then return us to normal privileges */
781             setgid(getgid());
782 #endif /* MAILGROUP */
783         } else {
784             fclose (in);
785         }
786         adios (NULL, "failed");
787     }
788
789     if (aud)
790         fclose (aud);
791
792 #ifdef MHE
793     if (mhe)
794         fclose (mhe);
795 #endif /* MHE */
796
797     if (noisy)
798         fflush (stdout);
799
800 #ifdef POP
801     if ((inc_type == INC_POP) && packfile)
802         done (0);
803 #endif /* POP */
804
805     /*
806      * truncate file we are incorporating from
807      */
808     if (inc_type == INC_FILE) {
809         if (trnflag) {
810             if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
811                 advise (NULL, "new messages have arrived!\007");
812             else {
813                 if ((i = creat (newmail, 0600)) != NOTOK)
814                     close (i);
815                 else
816                     admonish (newmail, "error zero'ing");
817                 unlink(map_name(newmail));
818             }
819         } else {
820             if (noisy)
821                 printf ("%s not zero'd\n", newmail);
822         }
823     }
824
825     if (msgnum == hghnum) {
826         admonish (NULL, "no messages incorporated");
827     } else {
828         context_replace (pfolder, folder);      /* update current folder */
829         if (chgflag)
830             mp->curmsg = hghnum + 1;
831         mp->hghmsg = msgnum;
832         if (mp->lowmsg == 0)
833             mp->lowmsg = 1;
834         if (chgflag)            /* sigh... */
835             seq_setcur (mp, mp->curmsg);
836     }
837
838     /*
839      * unlock the mail spool
840      */
841     if (inc_type == INC_FILE) {
842         if (locked) {
843 #ifdef MAILGROUP
844             setgid(return_gid); /* Be sure we can unlock mail file */
845 #endif /* MAILGROUP */
846
847             lkfclose (in, newmail);
848
849 #ifdef MAILGROUP
850             setgid(getgid());   /* And then return us to normal privileges */
851 #endif /* MAILGROUP */
852         } else {
853             fclose (in);
854         }
855     }
856
857     seq_setunseen (mp, 0);      /* set the Unseen-Sequence */
858     seq_save (mp);              /* synchronize sequences   */
859     context_save ();            /* save the context file   */
860     return done (0);
861 }
862
863
864 #if 0
865
866 /*
867  * Copy message message from spool into
868  * temporary file.  Massage the "From " line
869  * while copying.
870  */
871
872 cpymsg (FILE *in, FILE *out)
873 {
874     int state;
875     char *tmpbuf, name[NAMESZ];
876
877     for (;;) {
878         state = m_getfld (state, name, tmpbuf, rlwidth, in);
879         switch (state) {
880         case FLD:
881         case FLDPLUS:
882             break;
883         case BODY:
884             break;
885         case LENERR:
886         case FMTERR:
887             break;
888         case FILEEOF:
889             break;
890         default:
891         }
892     }
893 }
894 #endif /* if 0 */
895
896
897 #ifdef POP
898 int
899 done (int status)
900 {
901     if (packfile && pd != NOTOK)
902         mbx_close (packfile, pd);
903
904     exit (status);
905     return 1;  /* dead code to satisfy the compiler */
906 }
907
908 static int
909 pop_action (char *s)
910 {
911     fprintf (pf, "%s\n", s);
912     stop += strlen (s) + 1;
913     return 0;  /* Is return value used?  This was missing before 1999-07-15. */
914 }
915
916 static int
917 pop_pack (char *s)
918 {
919     int j;
920     char buffer[BUFSIZ];
921
922     snprintf (buffer, sizeof(buffer), "%s\n", s);
923     for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
924         continue;
925     for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
926         continue;
927     fputs (buffer, pf);
928     size += strlen (buffer) + 1;
929     return 0;  /* Is return value used?  This was missing before 1999-07-15. */
930 }
931
932 static int
933 map_count (void)
934 {
935     int md;
936     char *cp;
937     struct drop d;
938     struct stat st;
939
940     if (stat (packfile, &st) == NOTOK)
941         return 0;
942     if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
943             || map_chk (cp, md, &d, (long) st.st_size, 1)) {
944         if (md != NOTOK)
945             close (md);
946         return 0;
947     }
948     close (md);
949     return (d.d_id);
950 }
951 #endif /* POP */