(scroll-bar-mode, toggle-scroll-bar): By default,
[bpt/emacs.git] / lib-src / movemail.c
1 /* movemail foo bar -- move file foo to file bar,
2 locking file foo the way /bin/mail respects.
3 Copyright (C) 1986, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
23 cause loss of mail* if you do it on a system that does not normally
24 use flock as its way of interlocking access to inbox files. The
25 setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
26 system's own conventions. It is not a choice that is up to you.
27
28 So, if your system uses lock files rather than flock, then the only way
29 you can get proper operation is to enable movemail to write lockfiles there.
30 This means you must either give that directory access modes
31 that permit everyone to write lockfiles in it, or you must make movemail
32 a setuid or setgid program. */
33
34 /*
35 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
36 *
37 * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
38 * movemail will accept input filename arguments of the form
39 * "po:username". This will cause movemail to open a connection to
40 * a pop server running on $MAILHOST (environment variable). Movemail
41 * must be setuid to root in order to work with POP.
42 *
43 * New module: popmail.c
44 * Modified routines:
45 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
46 * after POP code.
47 * New routines in movemail.c:
48 * get_errmsg - return pointer to system error message
49 *
50 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
51 *
52 * Move all of the POP code into a separate file, "pop.c".
53 * Use strerror instead of get_errmsg.
54 *
55 */
56
57 #define NO_SHORTNAMES /* Tell config not to load remap.h */
58 #include <../src/config.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <sys/file.h>
62 #include <stdio.h>
63 #include <errno.h>
64 #include <../src/syswait.h>
65 #include <getopt.h>
66 #ifdef MAIL_USE_POP
67 #include "pop.h"
68 #endif
69
70 #ifdef MSDOS
71 #undef access
72 #endif /* MSDOS */
73
74 #ifndef DIRECTORY_SEP
75 #define DIRECTORY_SEP '/'
76 #endif
77 #ifndef IS_DIRECTORY_SEP
78 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
79 #endif
80
81 #ifdef WINDOWSNT
82 #undef access
83 #undef unlink
84 #define fork() 0
85 #define sys_wait(var) (*(var) = 0)
86 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
87 though the locking call succeeds (and indeed blocks local access from
88 other NT programs). If you have direct file access using an NFS
89 client or something other than Samba, the locking call might work
90 properly - make sure it does before you enable this! */
91 #define DISABLE_DIRECT_ACCESS
92 #endif /* WINDOWSNT */
93
94 /* Cancel substitutions made by config.h for Emacs. */
95 #undef open
96 #undef read
97 #undef write
98 #undef close
99
100 #ifdef USG
101 #include <fcntl.h>
102 #include <unistd.h>
103 #ifndef F_OK
104 #define F_OK 0
105 #define X_OK 1
106 #define W_OK 2
107 #define R_OK 4
108 #endif
109 #endif /* USG */
110
111 #ifdef HAVE_UNISTD_H
112 #include <unistd.h>
113 #endif
114
115 #ifdef STDC_HEADERS
116 #include <stdlib.h>
117 #endif
118
119 #if defined (XENIX) || defined (WINDOWSNT)
120 #include <sys/locking.h>
121 #endif
122
123 #ifdef MAIL_USE_LOCKF
124 #define MAIL_USE_SYSTEM_LOCK
125 #endif
126
127 #ifdef MAIL_USE_FLOCK
128 #define MAIL_USE_SYSTEM_LOCK
129 #endif
130
131 #ifdef MAIL_USE_MMDF
132 extern int lk_open (), lk_close ();
133 #endif
134
135 #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
136 defined (HAVE_LIBMAIL) && defined (HAVE_MAILLOCK_H)
137 #include <maillock.h>
138 /* We can't use maillock unless we know what directory system mail
139 files appear in. */
140 #ifdef MAILDIR
141 #define MAIL_USE_MAILLOCK
142 static char *mail_spool_name ();
143 #endif
144 #endif
145
146 #ifndef errno
147 extern int errno;
148 #endif
149 char *strerror ();
150 extern char *rindex ();
151
152 void fatal ();
153 void error ();
154 void pfatal_with_name ();
155 void pfatal_and_delete ();
156 char *concat ();
157 long *xmalloc ();
158 int popmail ();
159 int pop_retr ();
160 int mbx_write ();
161 int mbx_delimit_begin ();
162 int mbx_delimit_end ();
163
164 /* Nonzero means this is name of a lock file to delete on fatal error. */
165 char *delete_lockname;
166
167 int
168 main (argc, argv)
169 int argc;
170 char **argv;
171 {
172 char *inname, *outname;
173 int indesc, outdesc;
174 int nread;
175 WAITTYPE status;
176 int c, preserve_mail = 0;
177
178 #ifndef MAIL_USE_SYSTEM_LOCK
179 struct stat st;
180 long now;
181 int tem;
182 char *lockname, *p;
183 char *tempname;
184 int desc;
185 #endif /* not MAIL_USE_SYSTEM_LOCK */
186
187 #ifdef MAIL_USE_MAILLOCK
188 char *spool_name;
189 #endif
190
191 #ifdef MAIL_USE_POP
192 int pop_flags = POP_NO_GETPASS | POP_NO_GSSAPI;
193 # define ARGSTR "gkp"
194 #else /* ! MAIL_USE_POP */
195 # define ARGSTR "p"
196 #endif /* MAIL_USE_POP */
197
198 delete_lockname = 0;
199
200 /*
201 'g' enables Kerberos and disables GSS-API.
202 'k' enables GSS-API and disables Kerberos.
203
204 By default, Kerberos is enabled (if it is compiled in) and
205 GSS-API is disabled, i.e., "-k" is the default. However, I'm
206 putting the flag in anyway, in case we decide to add new
207 authentication methods or change the default later.
208 */
209
210 while ((c = getopt (argc, argv, ARGSTR)) != EOF)
211 {
212 switch (c) {
213 #ifdef MAIL_USE_POP
214 case 'g':
215 pop_flags |= POP_NO_KERBEROS;
216 pop_flags &= ~POP_NO_GSSAPI;
217 break;
218 case 'k':
219 pop_flags |= POP_NO_GSSAPI;
220 pop_flags &= ~POP_NO_KERBEROS;
221 break;
222 #endif
223 case 'p':
224 preserve_mail++;
225 break;
226 default:
227 goto usage;
228 }
229 }
230
231 if (
232 #ifdef MAIL_USE_POP
233 (argc - optind < 2) || (argc - optind > 3)
234 #else
235 (argc - optind != 2)
236 #endif
237 )
238 {
239 usage:
240 fprintf (stderr, "Usage: movemail %s[-p] inbox destfile%s\n",
241 #ifdef MAIL_USE_POP
242 "[-g|-k] ", " [POP-password]"
243 #else
244 "", ""
245 #endif
246 );
247 exit (1);
248 }
249
250 inname = argv[optind];
251 outname = argv[optind+1];
252
253 #ifdef MAIL_USE_MMDF
254 mmdf_init (argv[0]);
255 #endif
256
257 if (*outname == 0)
258 fatal ("Destination file name is empty", 0);
259
260 /* Check access to output file. */
261 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
262 pfatal_with_name (outname);
263
264 /* Also check that outname's directory is writable to the real uid. */
265 {
266 char *buf = (char *) xmalloc (strlen (outname) + 1);
267 char *p;
268 strcpy (buf, outname);
269 p = buf + strlen (buf);
270 while (p > buf && !IS_DIRECTORY_SEP (p[-1]))
271 *--p = 0;
272 if (p == buf)
273 *p++ = '.';
274 if (access (buf, W_OK) != 0)
275 pfatal_with_name (buf);
276 free (buf);
277 }
278
279 #ifdef MAIL_USE_POP
280 if (!strncmp (inname, "po:", 3))
281 {
282 int status;
283
284 status = popmail (inname + 3, outname, preserve_mail,
285 (argc - optind == 3) ? argv[optind+2] : NULL, pop_flags);
286 exit (status);
287 }
288
289 setuid (getuid ());
290 #endif /* MAIL_USE_POP */
291
292 #ifndef DISABLE_DIRECT_ACCESS
293
294 /* Check access to input file. */
295 if (access (inname, R_OK | W_OK) != 0)
296 pfatal_with_name (inname);
297
298 #ifndef MAIL_USE_MMDF
299 #ifndef MAIL_USE_SYSTEM_LOCK
300 #ifdef MAIL_USE_MAILLOCK
301 spool_name = mail_spool_name (inname);
302 if (! spool_name)
303 #endif
304 {
305 /* Use a lock file named after our first argument with .lock appended:
306 If it exists, the mail file is locked. */
307 /* Note: this locking mechanism is *required* by the mailer
308 (on systems which use it) to prevent loss of mail.
309
310 On systems that use a lock file, extracting the mail without locking
311 WILL occasionally cause loss of mail due to timing errors!
312
313 So, if creation of the lock file fails
314 due to access permission on the mail spool directory,
315 you simply MUST change the permission
316 and/or make movemail a setgid program
317 so it can create lock files properly.
318
319 You might also wish to verify that your system is one
320 which uses lock files for this purpose. Some systems use other methods.
321
322 If your system uses the `flock' system call for mail locking,
323 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
324 and recompile movemail. If the s- file for your system
325 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
326 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
327
328 lockname = concat (inname, ".lock", "");
329 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
330 strcpy (tempname, inname);
331 p = tempname + strlen (tempname);
332 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
333 p--;
334 *p = 0;
335 strcpy (p, "EXXXXXX");
336 mktemp (tempname);
337 unlink (tempname);
338
339 while (1)
340 {
341 /* Create the lock file, but not under the lock file name. */
342 /* Give up if cannot do that. */
343 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
344 if (desc < 0)
345 {
346 char *message = (char *) xmalloc (strlen (tempname) + 50);
347 sprintf (message, "%s--see source file lib-src/movemail.c",
348 tempname);
349 pfatal_with_name (message);
350 }
351 close (desc);
352
353 tem = link (tempname, lockname);
354 unlink (tempname);
355 if (tem >= 0)
356 break;
357 sleep (1);
358
359 /* If lock file is five minutes old, unlock it.
360 Five minutes should be good enough to cope with crashes
361 and wedgitude, and long enough to avoid being fooled
362 by time differences between machines. */
363 if (stat (lockname, &st) >= 0)
364 {
365 now = time (0);
366 if (st.st_ctime < now - 300)
367 unlink (lockname);
368 }
369 }
370
371 delete_lockname = lockname;
372 }
373 #endif /* not MAIL_USE_SYSTEM_LOCK */
374 #endif /* not MAIL_USE_MMDF */
375
376 if (fork () == 0)
377 {
378 int lockcount = 0;
379 int status = 0;
380 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
381 long touched_lock, now;
382 #endif
383
384 setuid (getuid ());
385
386 #ifndef MAIL_USE_MMDF
387 #ifdef MAIL_USE_SYSTEM_LOCK
388 indesc = open (inname, O_RDWR);
389 #else /* if not MAIL_USE_SYSTEM_LOCK */
390 indesc = open (inname, O_RDONLY);
391 #endif /* not MAIL_USE_SYSTEM_LOCK */
392 #else /* MAIL_USE_MMDF */
393 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
394 #endif /* MAIL_USE_MMDF */
395
396 if (indesc < 0)
397 pfatal_with_name (inname);
398
399 #if defined (BSD_SYSTEM) || defined (XENIX)
400 /* In case movemail is setuid to root, make sure the user can
401 read the output file. */
402 /* This is desirable for all systems
403 but I don't want to assume all have the umask system call */
404 umask (umask (0) & 0333);
405 #endif /* BSD_SYSTEM || XENIX */
406 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
407 if (outdesc < 0)
408 pfatal_with_name (outname);
409
410 /* This label exists so we can retry locking
411 after a delay, if it got EAGAIN or EBUSY. */
412 retry_lock:
413
414 /* Try to lock it. */
415 #ifdef MAIL_USE_MAILLOCK
416 if (spool_name)
417 {
418 /* The "0 - " is to make it a negative number if maillock returns
419 non-zero. */
420 status = 0 - maillock (spool_name, 1);
421 #ifdef HAVE_TOUCHLOCK
422 touched_lock = time (0);
423 #endif
424 lockcount = 5;
425 }
426 else
427 #endif /* MAIL_USE_MAILLOCK */
428 {
429 #ifdef MAIL_USE_SYSTEM_LOCK
430 #ifdef MAIL_USE_LOCKF
431 status = lockf (indesc, F_LOCK, 0);
432 #else /* not MAIL_USE_LOCKF */
433 #ifdef XENIX
434 status = locking (indesc, LK_RLCK, 0L);
435 #else
436 #ifdef WINDOWSNT
437 status = locking (indesc, LK_RLCK, -1L);
438 #else
439 status = flock (indesc, LOCK_EX);
440 #endif
441 #endif
442 #endif /* not MAIL_USE_LOCKF */
443 #endif /* MAIL_USE_SYSTEM_LOCK */
444 }
445
446 /* If it fails, retry up to 5 times
447 for certain failure codes. */
448 if (status < 0)
449 {
450 if (++lockcount <= 5)
451 {
452 #ifdef EAGAIN
453 if (errno == EAGAIN)
454 {
455 sleep (1);
456 goto retry_lock;
457 }
458 #endif
459 #ifdef EBUSY
460 if (errno == EBUSY)
461 {
462 sleep (1);
463 goto retry_lock;
464 }
465 #endif
466 }
467
468 pfatal_with_name (inname);
469 }
470
471 {
472 char buf[1024];
473
474 while (1)
475 {
476 nread = read (indesc, buf, sizeof buf);
477 if (nread != write (outdesc, buf, nread))
478 {
479 int saved_errno = errno;
480 unlink (outname);
481 errno = saved_errno;
482 pfatal_with_name (outname);
483 }
484 if (nread < sizeof buf)
485 break;
486 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
487 if (spool_name)
488 {
489 now = time (0);
490 if (now - touched_lock > 60)
491 {
492 touchlock ();
493 touched_lock = now;
494 }
495 }
496 #endif /* MAIL_USE_MAILLOCK */
497 }
498 }
499
500 #ifdef BSD_SYSTEM
501 if (fsync (outdesc) < 0)
502 pfatal_and_delete (outname);
503 #endif
504
505 /* Check to make sure no errors before we zap the inbox. */
506 if (close (outdesc) != 0)
507 pfatal_and_delete (outname);
508
509 #ifdef MAIL_USE_SYSTEM_LOCK
510 if (! preserve_mail)
511 {
512 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
513 /* Stride, xenix have file locking, but no ftruncate.
514 This mess will do. */
515 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
516 #else
517 ftruncate (indesc, 0L);
518 #endif /* STRIDE or XENIX */
519 }
520 #endif /* MAIL_USE_SYSTEM_LOCK */
521
522 #ifdef MAIL_USE_MMDF
523 lk_close (indesc, 0, 0, 0);
524 #else
525 close (indesc);
526 #endif
527
528 #ifndef MAIL_USE_SYSTEM_LOCK
529 if (! preserve_mail)
530 {
531 /* Delete the input file; if we can't, at least get rid of its
532 contents. */
533 #ifdef MAIL_UNLINK_SPOOL
534 /* This is generally bad to do, because it destroys the permissions
535 that were set on the file. Better to just empty the file. */
536 if (unlink (inname) < 0 && errno != ENOENT)
537 #endif /* MAIL_UNLINK_SPOOL */
538 creat (inname, 0600);
539 }
540 #endif /* not MAIL_USE_SYSTEM_LOCK */
541
542 #ifdef MAIL_USE_MAILLOCK
543 /* This has to occur in the child, i.e., in the process that
544 acquired the lock! */
545 if (spool_name)
546 mailunlock ();
547 #endif
548 exit (0);
549 }
550
551 wait (&status);
552 if (!WIFEXITED (status))
553 exit (1);
554 else if (WRETCODE (status) != 0)
555 exit (WRETCODE (status));
556
557 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
558 #ifdef MAIL_USE_MAILLOCK
559 if (! spool_name)
560 #endif /* MAIL_USE_MAILLOCK */
561 unlink (lockname);
562 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
563
564 #endif /* ! DISABLE_DIRECT_ACCESS */
565
566 return 0;
567 }
568
569 #ifdef MAIL_USE_MAILLOCK
570 /* This function uses stat to confirm that the mail directory is
571 identical to the directory of the input file, rather than just
572 string-comparing the two paths, because one or both of them might
573 be symbolic links pointing to some other directory. */
574 static char *
575 mail_spool_name (inname)
576 char *inname;
577 {
578 struct stat stat1, stat2;
579 char *indir, *fname;
580 int status;
581
582 if (! (fname = rindex (inname, '/')))
583 return NULL;
584
585 fname++;
586
587 if (stat (MAILDIR, &stat1) < 0)
588 return NULL;
589
590 indir = (char *) xmalloc (fname - inname + 1);
591 strncpy (indir, inname, fname - inname);
592 indir[fname-inname] = '\0';
593
594
595 status = stat (indir, &stat2);
596
597 free (indir);
598
599 if (status < 0)
600 return NULL;
601
602 if (stat1.st_dev == stat2.st_dev
603 && stat1.st_ino == stat2.st_ino)
604 return fname;
605
606 return NULL;
607 }
608 #endif /* MAIL_USE_MAILLOCK */
609 \f
610 /* Print error message and exit. */
611
612 void
613 fatal (s1, s2)
614 char *s1, *s2;
615 {
616 if (delete_lockname)
617 unlink (delete_lockname);
618 error (s1, s2);
619 exit (1);
620 }
621
622 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
623
624 void
625 error (s1, s2, s3)
626 char *s1, *s2, *s3;
627 {
628 fprintf (stderr, "movemail: ");
629 fprintf (stderr, s1, s2, s3);
630 fprintf (stderr, "\n");
631 }
632
633 void
634 pfatal_with_name (name)
635 char *name;
636 {
637 char *s = concat ("", strerror (errno), " for %s");
638 fatal (s, name);
639 }
640
641 void
642 pfatal_and_delete (name)
643 char *name;
644 {
645 char *s = concat ("", strerror (errno), " for %s");
646 unlink (name);
647 fatal (s, name);
648 }
649
650 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
651
652 char *
653 concat (s1, s2, s3)
654 char *s1, *s2, *s3;
655 {
656 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
657 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
658
659 strcpy (result, s1);
660 strcpy (result + len1, s2);
661 strcpy (result + len1 + len2, s3);
662 *(result + len1 + len2 + len3) = 0;
663
664 return result;
665 }
666
667 /* Like malloc but get fatal error if memory is exhausted. */
668
669 long *
670 xmalloc (size)
671 unsigned size;
672 {
673 long *result = (long *) malloc (size);
674 if (!result)
675 fatal ("virtual memory exhausted", 0);
676 return result;
677 }
678 \f
679 /* This is the guts of the interface to the Post Office Protocol. */
680
681 #ifdef MAIL_USE_POP
682
683 #ifndef WINDOWSNT
684 #include <sys/socket.h>
685 #include <netinet/in.h>
686 #include <netdb.h>
687 #else
688 #undef _WINSOCKAPI_
689 #include <winsock.h>
690 #endif
691 #include <pwd.h>
692
693 #define NOTOK (-1)
694 #define OK 0
695 #define DONE 1
696
697 char *progname;
698 FILE *sfi;
699 FILE *sfo;
700 char ibuffer[BUFSIZ];
701 char obuffer[BUFSIZ];
702 char Errmsg[80];
703
704 popmail (user, outfile, preserve, password, flags)
705 char *user;
706 char *outfile;
707 int preserve;
708 char *password;
709 int flags;
710 {
711 int nmsgs, nbytes;
712 register int i;
713 int mbfi;
714 FILE *mbf;
715 char *getenv ();
716 popserver server;
717
718 server = pop_open (0, user, password, flags);
719 if (! server)
720 {
721 error (pop_error);
722 return (1);
723 }
724
725 if (pop_stat (server, &nmsgs, &nbytes))
726 {
727 error (pop_error);
728 return (1);
729 }
730
731 if (!nmsgs)
732 {
733 pop_close (server);
734 return (0);
735 }
736
737 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
738 if (mbfi < 0)
739 {
740 pop_close (server);
741 error ("Error in open: %s, %s", strerror (errno), outfile);
742 return (1);
743 }
744 fchown (mbfi, getuid (), -1);
745
746 if ((mbf = fdopen (mbfi, "wb")) == NULL)
747 {
748 pop_close (server);
749 error ("Error in fdopen: %s", strerror (errno));
750 close (mbfi);
751 unlink (outfile);
752 return (1);
753 }
754
755 for (i = 1; i <= nmsgs; i++)
756 {
757 mbx_delimit_begin (mbf);
758 if (pop_retr (server, i, mbf) != OK)
759 {
760 error (Errmsg);
761 close (mbfi);
762 return (1);
763 }
764 mbx_delimit_end (mbf);
765 fflush (mbf);
766 if (ferror (mbf))
767 {
768 error ("Error in fflush: %s", strerror (errno));
769 pop_close (server);
770 close (mbfi);
771 return (1);
772 }
773 }
774
775 /* On AFS, a call to write only modifies the file in the local
776 * workstation's AFS cache. The changes are not written to the server
777 * until a call to fsync or close is made. Users with AFS home
778 * directories have lost mail when over quota because these checks were
779 * not made in previous versions of movemail. */
780
781 #ifdef BSD_SYSTEM
782 if (fsync (mbfi) < 0)
783 {
784 error ("Error in fsync: %s", strerror (errno));
785 return (1);
786 }
787 #endif
788
789 if (close (mbfi) == -1)
790 {
791 error ("Error in close: %s", strerror (errno));
792 return (1);
793 }
794
795 if (! preserve)
796 for (i = 1; i <= nmsgs; i++)
797 {
798 if (pop_delete (server, i))
799 {
800 error (pop_error);
801 pop_close (server);
802 return (1);
803 }
804 }
805
806 if (pop_quit (server))
807 {
808 error (pop_error);
809 return (1);
810 }
811
812 return (0);
813 }
814
815 int
816 pop_retr (server, msgno, arg)
817 popserver server;
818 FILE *arg;
819 {
820 extern char *strerror ();
821 char *line;
822 int ret;
823
824 if (pop_retrieve_first (server, msgno, &line))
825 {
826 strncpy (Errmsg, pop_error, sizeof (Errmsg));
827 Errmsg[sizeof (Errmsg)-1] = '\0';
828 return (NOTOK);
829 }
830
831 while ((ret = pop_retrieve_next (server, &line)) >= 0)
832 {
833 if (! line)
834 break;
835
836 if (mbx_write (line, ret, arg) != OK)
837 {
838 strcpy (Errmsg, strerror (errno));
839 pop_close (server);
840 return (NOTOK);
841 }
842 }
843
844 if (ret)
845 {
846 strncpy (Errmsg, pop_error, sizeof (Errmsg));
847 Errmsg[sizeof (Errmsg)-1] = '\0';
848 return (NOTOK);
849 }
850
851 return (OK);
852 }
853
854 /* Do this as a macro instead of using strcmp to save on execution time. */
855 #define IS_FROM_LINE(a) ((a[0] == 'F') \
856 && (a[1] == 'r') \
857 && (a[2] == 'o') \
858 && (a[3] == 'm') \
859 && (a[4] == ' '))
860
861 int
862 mbx_write (line, len, mbf)
863 char *line;
864 int len;
865 FILE *mbf;
866 {
867 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
868 if (IS_FROM_LINE (line))
869 {
870 if (fputc ('>', mbf) == EOF)
871 return (NOTOK);
872 }
873 #endif
874 if (line[0] == '\037')
875 {
876 if (fputs ("^_", mbf) == EOF)
877 return (NOTOK);
878 line++;
879 len--;
880 }
881 if (fwrite (line, 1, len, mbf) != len)
882 return (NOTOK);
883 if (fputc (0x0a, mbf) == EOF)
884 return (NOTOK);
885 return (OK);
886 }
887
888 int
889 mbx_delimit_begin (mbf)
890 FILE *mbf;
891 {
892 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
893 return (NOTOK);
894 return (OK);
895 }
896
897 mbx_delimit_end (mbf)
898 FILE *mbf;
899 {
900 if (putc ('\037', mbf) == EOF)
901 return (NOTOK);
902 return (OK);
903 }
904
905 #endif /* MAIL_USE_POP */
906 \f
907 #ifndef HAVE_STRERROR
908 char *
909 strerror (errnum)
910 int errnum;
911 {
912 extern char *sys_errlist[];
913 extern int sys_nerr;
914
915 if (errnum >= 0 && errnum < sys_nerr)
916 return sys_errlist[errnum];
917 return (char *) "Unknown error";
918 }
919
920 #endif /* ! HAVE_STRERROR */