Updating user programs to use <h/tws> instead of <zotnet/tws/tws.h>
[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 <h/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", 0 },
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 ( strcmp( POPSERVICE, "kpop" ) == 0 ) {
406             kpop = 1;
407         }
408         if (kpop || ( rpop > 0))
409             pass = getusername ();
410         else
411             ruserpass (host, &user, &pass);
412
413         /*
414          * initialize POP connection
415          */
416         if (pop_init (host, user, pass, snoop, kpop ? 1 : rpop, kpop) == NOTOK)
417             adios (NULL, "%s", response);
418
419         /* Check if there are any messages */
420         if (pop_stat (&nmsgs, &nbytes) == NOTOK)
421             adios (NULL, "%s", response);
422
423         if (rpop > 0)
424             setuid (getuid ());
425         if (nmsgs == 0) {
426             pop_quit();
427             adios (NULL, "no mail to incorporate");
428         }
429     }
430 #endif /* POP */
431
432     /*
433      * We will get the mail from a file
434      * (typically the standard maildrop)
435      */
436
437     if (inc_type == INC_FILE) {
438         if (from)
439             newmail = from;
440         else if ((newmail = getenv ("MAILDROP")) && *newmail)
441             newmail = m_mailpath (newmail);
442         else if ((newmail = context_find ("maildrop")) && *newmail)
443             newmail = m_mailpath (newmail);
444         else {
445             newmail = concat (MAILDIR, "/", MAILFIL, NULL);
446         }
447         if (stat (newmail, &s1) == NOTOK || s1.st_size == 0)
448             adios (NULL, "no mail to incorporate");
449     }
450
451 #ifdef POP
452     /* skip the folder setup */
453     if ((inc_type == INC_POP) && packfile)
454         goto go_to_it;
455 #endif /* POP */
456
457     if (!context_find ("path"))
458         free (path ("./", TFOLDER));
459     if (!folder)
460         folder = getfolder (0);
461     maildir = m_maildir (folder);
462
463     if (stat (maildir, &st) == NOTOK) {
464         if (errno != ENOENT)
465             adios (maildir, "error on folder");
466         cp = concat ("Create folder \"", maildir, "\"? ", NULL);
467         if (noisy && !getanswer (cp))
468             done (1);
469         free (cp);
470         if (!makedir (maildir))
471             adios (NULL, "unable to create folder %s", maildir);
472     }
473
474     if (chdir (maildir) == NOTOK)
475         adios (maildir, "unable to change directory to");
476
477     /* read folder and create message structure */
478     if (!(mp = folder_read (folder)))
479         adios (NULL, "unable to read folder %s", folder);
480
481 #ifdef POP
482 go_to_it:
483 #endif /* POP */
484
485     if (inc_type == INC_FILE) {
486         if (access (newmail, W_OK) != NOTOK) {
487             locked++;
488             if (trnflag) {
489                 SIGNAL (SIGHUP, SIG_IGN);
490                 SIGNAL (SIGINT, SIG_IGN);
491                 SIGNAL (SIGQUIT, SIG_IGN);
492                 SIGNAL (SIGTERM, SIG_IGN);
493             }
494
495 #ifdef MAILGROUP
496             setgid(return_gid); /* Reset gid to lock mail file */
497 #endif /* MAILGROUP */
498
499             /* lock and fopen the mail spool */
500             if ((in = lkfopen (newmail, "r")) == NULL)
501                 adios (NULL, "unable to lock and fopen %s", newmail);
502
503 #ifdef MAILGROUP
504             setgid(getgid());   /* Return us to normal privileges */
505 #endif /* MAILGROUP */
506             fstat (fileno(in), &s1);
507         } else {
508             trnflag = 0;
509             if ((in = fopen (newmail, "r")) == NULL)
510                 adios (newmail, "unable to read");
511         }
512     }
513
514 #ifdef MAILGROUP
515     setgid(getgid());   /* Return us to normal privileges */
516 #endif /* MAILGROUP */
517
518     if (audfile) {
519         if ((i = stat (audfile, &st)) == NOTOK)
520             advise (NULL, "Creating Receive-Audit: %s", audfile);
521         if ((aud = fopen (audfile, "a")) == NULL)
522             adios (audfile, "unable to append to");
523         else if (i == NOTOK)
524             chmod (audfile, m_gmprot ());
525
526 #ifdef POP
527         fprintf (aud, from ? "<<inc>> %s -ms %s\n"
528                  : host ? "<<inc>> %s -host %s -user %s%s\n"
529                  : "<<inc>> %s\n",
530                  dtimenow (0), from ? from : host, user,
531                  rpop < 0 ? " -apop" : rpop > 0 ? " -rpop" : "");
532 #else /* POP */
533         fprintf (aud, from ? "<<inc>> %s  -ms %s\n" : "<<inc>> %s\n",
534                  dtimenow (0), from);
535 #endif /* POP */
536     }
537
538 #ifdef MHE
539     if (context_find ("mhe")) {
540         cp = concat (maildir, "/++", NULL);
541         i = stat (cp, &st);
542         if ((mhe = fopen (cp, "a")) == NULL)
543             admonish (cp, "unable to append to");
544         else
545             if (i == NOTOK)
546                 chmod (cp, m_gmprot ());
547         free (cp);
548     }
549 #endif /* MHE */
550
551     /* Get new format string */
552     nfs = new_fs (form, format, FORMAT);
553
554     if (noisy) {
555         printf ("Incorporating new mail into %s...\n\n", folder);
556         fflush (stdout);
557     }
558
559 #ifdef POP
560     /*
561      * Get the mail from a POP server
562      */
563     if (inc_type == INC_POP) {
564         if (packfile) {
565             packfile = path (packfile, TFILE);
566             if (stat (packfile, &st) == NOTOK) {
567                 if (errno != ENOENT)
568                     adios (packfile, "error on file");
569                 cp = concat ("Create file \"", packfile, "\"? ", NULL);
570                 if (noisy && !getanswer (cp))
571                     done (1);
572                 free (cp);
573             }
574             msgnum = map_count ();
575             if ((pd = mbx_open (packfile, mbx_style, getuid(), getgid(), m_gmprot()))
576                 == NOTOK)
577                 adios (packfile, "unable to open");
578             if ((pf = fdopen (pd, "w+")) == NULL)
579                 adios (NULL, "unable to fdopen %s", packfile);
580         } else {
581             hghnum = msgnum = mp->hghmsg;
582             /*
583              * Check if we have enough message space for all the new
584              * messages.  If not, then realloc the folder and add enough
585              * space for all new messages plus 10 additional slots.
586              */
587             if (mp->hghmsg + nmsgs >= mp->hghoff
588                 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghmsg + nmsgs + 10)))
589                 adios (NULL, "unable to allocate folder storage");
590         }
591
592         for (i = 1; i <= nmsgs; i++) {
593             msgnum++;
594             if (packfile) {
595                 fseek (pf, 0L, SEEK_CUR);
596                 pos = ftell (pf);
597                 size = 0;
598                 fwrite (mmdlm1, 1, strlen (mmdlm1), pf);
599                 start = ftell (pf);
600
601                 if (pop_retr (i, pop_pack) == NOTOK)
602                     adios (NULL, "%s", response);
603
604                 fseek (pf, 0L, SEEK_CUR);
605                 stop = ftell (pf);
606                 if (fflush (pf))
607                     adios (packfile, "write error on");
608                 fseek (pf, start, SEEK_SET);
609             } else {
610                 cp = getcpy (m_name (msgnum));
611                 if ((pf = fopen (cp, "w+")) == NULL)
612                     adios (cp, "unable to write");
613                 chmod (cp, m_gmprot ());
614                 start = stop = 0L;
615
616                 if (pop_retr (i, pop_action) == NOTOK)
617                     adios (NULL, "%s", response);
618
619                 if (fflush (pf))
620                     adios (cp, "write error on");
621                 fseek (pf, 0L, SEEK_SET);
622             }
623             switch (p = scan (pf, msgnum, 0, nfs, width,
624                               packfile ? 0 : msgnum == mp->hghmsg + 1 && chgflag,
625                               1, NULL, stop - start, noisy)) {
626             case SCNEOF: 
627                 printf ("%*d  empty\n", DMAXFOLDER, msgnum);
628                 break;
629
630             case SCNFAT:
631                 trnflag = 0;
632                 noisy++;
633                 /* advise (cp, "unable to read"); already advised */
634                 /* fall thru */
635
636             case SCNERR:
637             case SCNNUM: 
638                 break;
639
640             case SCNMSG: 
641             case SCNENC:
642             default: 
643                 if (aud)
644                     fputs (scanl, aud);
645 # ifdef MHE
646                 if (mhe)
647                     fputs (scanl, mhe);
648 # endif /* MHE */
649                 if (noisy)
650                     fflush (stdout);
651                 if (!packfile) {
652                     clear_msg_flags (mp, msgnum);
653                     set_exists (mp, msgnum);
654                     set_unseen (mp, msgnum);
655                     mp->msgflags |= SEQMOD;
656                 }
657                 break;
658             }
659             if (packfile) {
660                 fseek (pf, stop, SEEK_SET);
661                 fwrite (mmdlm2, 1, strlen (mmdlm2), pf);
662                 if (fflush (pf) || ferror (pf)) {
663                     int e = errno;
664                     pop_quit ();
665                     errno = e;
666                     adios (packfile, "write error on");
667                 }
668                 map_write (packfile, pd, 0, 0L, start, stop, pos, size, noisy);
669             } else {
670                 if (ferror(pf) || fclose (pf)) {
671                     int e = errno;
672                     unlink (cp);
673                     pop_quit ();
674                     errno = e;
675                     adios (cp, "write error on");
676                 }
677                 free (cp);
678             }
679
680             if (trnflag && pop_dele (i) == NOTOK)
681                 adios (NULL, "%s", response);
682         }
683
684         if (pop_quit () == NOTOK)
685             adios (NULL, "%s", response);
686         if (packfile) {
687             mbx_close (packfile, pd);
688             pd = NOTOK;
689         }
690     }
691 #endif /* POP */
692
693     /*
694      * Get the mail from file (usually mail spool)
695      */
696     if (inc_type == INC_FILE) {
697         m_unknown (in);         /* the MAGIC invocation... */
698         hghnum = msgnum = mp->hghmsg;
699         for (i = 0;;) {
700             /*
701              * Check if we need to allocate more space for message status.
702              * If so, then add space for an additional 100 messages.
703              */
704             if (msgnum >= mp->hghoff
705                 && !(mp = folder_realloc (mp, mp->lowoff, mp->hghoff + 100))) {
706                 advise (NULL, "unable to allocate folder storage");
707                 i = NOTOK;
708                 break;
709             }
710
711 #if 0
712             /* copy file from spool to tmp file */
713             tmpfilenam = m_scratch ("", invo_name);
714             if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
715                 adios (tmpfilenam, "unable to create");
716             chmod (tmpfilenam, m_gmprot ());
717             if (!(in2 = fdopen (fd, "r+")))
718                 adios (tmpfilenam, "unable to access");
719             cpymsg (in, in2);
720
721             /* link message into folder */
722             newmsg = folder_addmsg(mp, tmpfilenam);
723 #endif
724
725             /* create scanline for new message */
726             switch (i = scan (in, msgnum + 1, msgnum + 1, nfs, width,
727                               msgnum == hghnum && chgflag, 1, NULL, 0L, noisy)) {
728             case SCNFAT:
729             case SCNEOF: 
730                 break;
731
732             case SCNERR:
733                 if (aud)
734                     fputs ("inc aborted!\n", aud);
735                 advise (NULL, "aborted!");      /* doesn't clean up locks! */
736                 break;
737
738             case SCNNUM: 
739                 advise (NULL, "BUG in %s, number out of range", invo_name);
740                 break;
741
742             default: 
743                 advise (NULL, "BUG in %s, scan() botch (%d)", invo_name, i);
744                 break;
745
746             case SCNMSG:
747             case SCNENC:
748                 if (aud)
749                     fputs (scanl, aud);
750 #ifdef MHE
751                 if (mhe)
752                     fputs (scanl, mhe);
753 #endif /* MHE */
754                 if (noisy)
755                     fflush (stdout);
756
757                 msgnum++;
758                 mp->hghmsg++;
759                 clear_msg_flags (mp, msgnum);
760                 set_exists (mp, msgnum);
761                 set_unseen (mp, msgnum);
762                 mp->msgflags |= SEQMOD;
763                 continue;
764             }
765             break;
766         }
767     }
768
769 #ifdef POP
770     if (p < 0) {                /* error */
771 #else
772     if (i < 0) {                /* error */
773 #endif
774         if (locked) {
775 #ifdef MAILGROUP
776             /* Be sure we can unlock mail file */
777             setgid(return_gid);
778 #endif /* MAILGROUP */
779
780             lkfclose (in, newmail);
781
782 #ifdef MAILGROUP
783             /* And then return us to normal privileges */
784             setgid(getgid());
785 #endif /* MAILGROUP */
786         } else {
787             fclose (in);
788         }
789         adios (NULL, "failed");
790     }
791
792     if (aud)
793         fclose (aud);
794
795 #ifdef MHE
796     if (mhe)
797         fclose (mhe);
798 #endif /* MHE */
799
800     if (noisy)
801         fflush (stdout);
802
803 #ifdef POP
804     if ((inc_type == INC_POP) && packfile)
805         done (0);
806 #endif /* POP */
807
808     /*
809      * truncate file we are incorporating from
810      */
811     if (inc_type == INC_FILE) {
812         if (trnflag) {
813             if (stat (newmail, &st) != NOTOK && s1.st_mtime != st.st_mtime)
814                 advise (NULL, "new messages have arrived!\007");
815             else {
816                 if ((i = creat (newmail, 0600)) != NOTOK)
817                     close (i);
818                 else
819                     admonish (newmail, "error zero'ing");
820                 unlink(map_name(newmail));
821             }
822         } else {
823             if (noisy)
824                 printf ("%s not zero'd\n", newmail);
825         }
826     }
827
828     if (msgnum == hghnum) {
829         admonish (NULL, "no messages incorporated");
830     } else {
831         context_replace (pfolder, folder);      /* update current folder */
832         if (chgflag)
833             mp->curmsg = hghnum + 1;
834         mp->hghmsg = msgnum;
835         if (mp->lowmsg == 0)
836             mp->lowmsg = 1;
837         if (chgflag)            /* sigh... */
838             seq_setcur (mp, mp->curmsg);
839     }
840
841     /*
842      * unlock the mail spool
843      */
844     if (inc_type == INC_FILE) {
845         if (locked) {
846 #ifdef MAILGROUP
847             setgid(return_gid); /* Be sure we can unlock mail file */
848 #endif /* MAILGROUP */
849
850             lkfclose (in, newmail);
851
852 #ifdef MAILGROUP
853             setgid(getgid());   /* And then return us to normal privileges */
854 #endif /* MAILGROUP */
855         } else {
856             fclose (in);
857         }
858     }
859
860     seq_setunseen (mp, 0);      /* set the Unseen-Sequence */
861     seq_save (mp);              /* synchronize sequences   */
862     context_save ();            /* save the context file   */
863     return done (0);
864 }
865
866
867 #if 0
868
869 /*
870  * Copy message message from spool into
871  * temporary file.  Massage the "From " line
872  * while copying.
873  */
874
875 cpymsg (FILE *in, FILE *out)
876 {
877     int state;
878     char *tmpbuf, name[NAMESZ];
879
880     for (;;) {
881         state = m_getfld (state, name, tmpbuf, rlwidth, in);
882         switch (state) {
883         case FLD:
884         case FLDPLUS:
885             break;
886         case BODY:
887             break;
888         case LENERR:
889         case FMTERR:
890             break;
891         case FILEEOF:
892             break;
893         default:
894         }
895     }
896 }
897 #endif /* if 0 */
898
899
900 #ifdef POP
901 int
902 done (int status)
903 {
904     if (packfile && pd != NOTOK)
905         mbx_close (packfile, pd);
906
907     exit (status);
908     return 1;  /* dead code to satisfy the compiler */
909 }
910
911 static int
912 pop_action (char *s)
913 {
914     fprintf (pf, "%s\n", s);
915     stop += strlen (s) + 1;
916     return 0;  /* Is return value used?  This was missing before 1999-07-15. */
917 }
918
919 static int
920 pop_pack (char *s)
921 {
922     int j;
923     char buffer[BUFSIZ];
924
925     snprintf (buffer, sizeof(buffer), "%s\n", s);
926     for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
927         continue;
928     for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
929         continue;
930     fputs (buffer, pf);
931     size += strlen (buffer) + 1;
932     return 0;  /* Is return value used?  This was missing before 1999-07-15. */
933 }
934
935 static int
936 map_count (void)
937 {
938     int md;
939     char *cp;
940     struct drop d;
941     struct stat st;
942
943     if (stat (packfile, &st) == NOTOK)
944         return 0;
945     if ((md = open (cp = map_name (packfile), O_RDONLY)) == NOTOK
946             || map_chk (cp, md, &d, (long) st.st_size, 1)) {
947         if (md != NOTOK)
948             close (md);
949         return 0;
950     }
951     close (md);
952     return (d.d_id);
953 }
954 #endif /* POP */