summaryrefslogtreecommitdiff
path: root/guimb/collect.c
blob: 15f95a5601e4e7aa5a99332936e2509267dd2bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "guimb.h"
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif

#ifndef _PATH_MAILDIR
# define _PATH_MAILDIR "/var/spool/mail"
#endif

char *temp_filename;
FILE *temp_file;
mailbox_t mbox;

void
collect_open_default ()
{
  size_t nmesg;
  
  if (mailbox_create (&mbox, default_mailbox) != 0
      || mailbox_open (mbox, MU_STREAM_RDWR) != 0)
    {
      util_error ("can't open default mailbox %s: %s",
		  default_mailbox, strerror (errno));
      exit (1);
    }

  /* Suck in the messages */
  mailbox_messages_count (mbox, &nmesg);
}

/* Open temporary file for collecting incoming messages */
void
collect_open_mailbox_file ()
{
  int fd;

  /* Create input mailbox */
  fd = mu_tempfile (NULL, &temp_filename);
  if (fd == -1)
    exit (1);

  temp_file = fdopen (fd, "w");
  if (!temp_file)
    {
      util_error ("fdopen: %s", strerror (errno));
      close (fd);
      exit (1);
    }
}

/* Append contents of file `name' to the temporary file */
int
collect_append_file (char *name)
{
  char *buf = NULL;
  size_t n = 0;
  FILE *fp;

  if (strcmp (name, "-") == 0)
    fp = stdin;
  else
    {
      fp = fopen (name, "r");
      if (!fp)
	{
	  util_error ("can't open input file %s: %s", name, strerror (errno));
	  return -1;
	}
    }

  /* Copy the contents of the file */
  while (getline (&buf, &n, fp) > 0)
    fprintf (temp_file, "%s", buf);

  free (buf);
  fclose (fp);
  return 0;
}

/* Close the temporary, and reopen it as a mailbox. */
void
collect_create_mailbox ()
{
  size_t nmesg;

  if (!temp_file)
    return;
  
  fclose (temp_file);

  if (mailbox_create (&mbox, temp_filename) != 0
      || mailbox_open (mbox, MU_STREAM_READ) != 0)
    {
      util_error ("can't create temp mailbox %s: %s",
		  temp_filename, strerror (errno));
      unlink (temp_filename);
      exit (1);
    }

  /* Suck in the messages */
  mailbox_messages_count (mbox, &nmesg);

  if (nmesg == 0)
    {
      util_error ("input format not recognized");
      exit (1);
    }
}

int
collect_output ()
{
  size_t i, count = 0;
  mailbox_t outbox = NULL;
  int saved_umask;

  if (!temp_filename)
    {
      mailbox_expunge (mbox);
      return 0;
    }

  if (!default_mailbox)
    {
      if (!user_name)
	return 0;
      asprintf (&default_mailbox, "%s/%s", _PATH_MAILDIR, user_name);
      if (!default_mailbox)
	{
	  fprintf (stderr, "guimb: not enough memory\n");
	  return 1;
	}
    }

  if (user_name)
    saved_umask = umask (077);
  
  if (mailbox_create_default (&outbox, default_mailbox) != 0
      || mailbox_open (outbox, MU_STREAM_RDWR|MU_STREAM_CREAT) != 0)
    {
      mailbox_destroy (&outbox);
      fprintf (stderr, "guimb: can't open output mailbox %s: %s\n",
	       default_mailbox, strerror (errno));
      return 1;
    }

  mailbox_messages_count (mbox, &count);
  for (i = 1; i <= count; i++)
    {
      message_t msg = NULL;
      attribute_t attr = NULL;

      mailbox_get_message (mbox, i, &msg);
      message_get_attribute (msg, &attr);
      if (!attribute_is_deleted (attr))
	{
	  attribute_set_recent (attr);
	  mailbox_append_message (outbox, msg);
	}
    }

  mailbox_close (outbox);
  mailbox_destroy (&outbox);

  if (user_name)
    umask (saved_umask);
  return 0;
}

  
/* Close the temporary mailbox and unlink the file associated with it */
void
collect_drop_mailbox ()
{
  mailbox_close (mbox);
  mailbox_destroy (&mbox);
  if (temp_filename)
    {
      unlink (temp_filename);
      free (temp_filename);
    }
}

Return to:

Send suggestions and report system problems to the System administrator.