Commit 0211af8b 0211af8b96efd3050d8fcce51e44f8fd02f05612 by Sergey Poznyakoff

Collect input.

1 parent 6567d383
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "guimb.h"
19
20 char *temp_filename;
21 FILE *temp_file;
22 mailbox_t mbox;
23 size_t nmesg;
24 size_t current_mesg_no;
25 message_t current_message;
26
27 /* Open temporary file for collecting incoming message */
28 void
29 collect_open_mailbox_file ()
30 {
31 int fd;
32
33 /* Create input mailbox */
34 fd = util_tempfile (&temp_filename);
35 if (fd == -1)
36 exit (1);
37
38 temp_file = fdopen (fd, "w");
39 if (!temp_file)
40 {
41 util_error ("fdopen: %s", strerror (errno));
42 close (fd);
43 exit (1);
44 }
45 }
46
47 /* Append contents of file `name' to the temporary file */
48 int
49 collect_append_file (char *name)
50 {
51 char *buf = NULL;
52 size_t n = 0;
53 FILE *fp;
54
55 if (strcmp (name, "-") == 0)
56 fp = stdin;
57 else
58 {
59 fp = fopen (name, "r");
60 if (!fp)
61 {
62 util_error ("can't open input file %s: %s", name, strerror (errno));
63 return -1;
64 }
65 }
66
67 /* Copy the contents of the file */
68 while (getline (&buf, &n, fp) > 0)
69 fprintf (temp_file, "%s", buf);
70
71 free (buf);
72 fclose (fp);
73 return 0;
74 }
75
76 /* Close the temporary, and reopen it as a mailbox. */
77 void
78 collect_create_mailbox ()
79 {
80 size_t count;
81
82 fclose (temp_file);
83
84 if (mailbox_create (&mbox, temp_filename) != 0
85 || mailbox_open (mbox, MU_STREAM_READ) != 0)
86 {
87 util_error ("can't create temp mailbox %s: %s",
88 temp_filename, strerror (errno));
89 unlink (temp_filename);
90 exit (1);
91 }
92
93 /* Suck in the messages */
94 mailbox_messages_count (mbox, &nmesg);
95
96 if (nmesg == 0)
97 {
98 util_error ("input format not recognized");
99 exit (1);
100 }
101 }
102
103 /* Close the temporary mailbox and unlink the file associated with it */
104 void
105 collect_drop_mailbox ()
106 {
107 mailbox_close (mbox);
108 mailbox_destroy (&mbox);
109 unlink (temp_filename);
110 free (temp_filename);
111 }