File: mbox.c

package info (click to toggle)
axmail 2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 572 kB
  • sloc: ansic: 2,417; sh: 263; makefile: 49
file content (358 lines) | stat: -rw-r--r-- 8,118 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

/*
 *	mbox.c - Read, parse and write the system mailbox
 *
 *	Much taken from mailx-5.3b, which is 
 *	Copyright (c) 1980 Regents of the University of California.
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#include "mbox.h"
#include "utils.h"
#include "defines.h"
#include "config.h"
#include "head.h"

FILE *mbox;			/* Temporary mailbox in /tmp */
struct message *message;	/* Message list */
struct message *dot;		/* Current message pointer */
int messages = 0;		/* Amount of messages in list */
int current = 0;		/* Current message index */
int newm = 0;			/* New messages */

long sysboxlen = 0;		/* Length of the system mailbox at reading */
				/* It will increase if new arrives */

/* Check if we have this message in the message list yet */

int havemessage(char *id)
{
	int i;
	
	for (i = 0; i < messages; i++) {
		if (!strcmp(message[i].id, id))
			return 1;
	}
	
	return 0;
}

/* Open a temp file by creating and unlinking.
   Return the open file descriptor. */

int opentemp(char file[])
{
	int f;
	
	if ((f = open(file, O_CREAT|O_EXCL|O_RDWR, 0600)) < 0)
		panic("opentemp: could not open file");
	remove(file);
	return f;
}

/* Take the data out of the passed ghost file and toss it into
   a dynamically allocated message structure. */

void makemessage(FILE *f)
{
	int size = (messages + 1) * sizeof (struct message);

	if (message != 0)
		free((char *) message);
	if ((message = (struct message *) malloc((unsigned) size)) == 0)
		panic("Insufficient memory for %d messages", messages);
	dot = message;
	size -= sizeof (struct message);
	fflush(f);
	lseek(fileno(f), (long) sizeof *message, 0);
	if (read(fileno(f), (char *) message, size) != size)
		panic("Message temporary file corrupted");
	message[messages].m_size = 0;
	message[messages].m_lines = 0;
	message[messages].receipt = NULL;
	message[messages].from = NULL;
	message[messages].date = NULL;
	message[messages].subj = NULL;
	message[messages].id = NULL;
	fclose(f);
}

/* Append the passed message descriptor onto the temp file.
   If the write fails, return 1, else 0 */

void append(struct message *mp, FILE *f)
{
	if (fwrite((char *) mp, sizeof *mp, 1, f) != 1)
		panic("append: Could not append message");
}

/* Copy mailbox from ~/mail/mbox and /var/spool/mail to /tmp
   (yo2loj - 13 april 2003) */

int getmail(void)
{
	struct stat stb;

	FILE *ibuf;
	FILE *mestmp;

	char buf[LINESIZE];
	int count;

	long offset = 0;
	int inheader = 0;
	int maybe = 1;
	char *cp = NULL;
	char c;


	struct message this = { MUSED|MNEW, 0, 0, 0 };
	
	
	if ((mbox = fopen(tempMail, "w")) == NULL)
		panic("getmail: Could not create temporary file");

	
	if ((ibuf = fopen(userbox, "r")) == NULL) goto sysbox;
	
	if (fstat(fileno(ibuf), &stb) < 0) {
		printf("Ouch, could not fstat() mailbox.\n");
		syslog(LOG_NOTICE, "getmail: Could not fstat()");
		fclose(ibuf);
		goto sysbox;;
	}
	
	switch (stb.st_mode & S_IFMT) {
		case S_IFREG:		/* Good, a regular file */
			break;
		default:		/* Yuck, i won't eat THAT! */
			fclose(ibuf);
			goto sysbox;
	}
	
	if (stb.st_size == 0) {		/* Zero-sized mailbox? */
		fclose(ibuf);
		goto sysbox;;
	}
	
	/* copy old read mail */
	
	while (fgets(buf, LINESIZE, ibuf) != NULL) {
	    count = strlen(buf);
	    fwrite(buf, sizeof *buf, count, mbox);
	    if (ferror(mbox))
			panic("getmail: Could not write temporary file");	
	}

	fclose(ibuf);
	
sysbox:
	if ((ibuf = fopen(mailbox, "r")) == NULL) goto rdexit;
	
	if (fstat(fileno(ibuf), &stb) < 0) {
		printf("Ouch, could not fstat() mailbox.\n");
		syslog(LOG_NOTICE, "getmail: Could not fstat()");
		fclose(ibuf);
		goto rdexit;
	}
	
	switch (stb.st_mode & S_IFMT) {
		case S_IFREG:		/* Good, a regular file */
			break;
		default:		/* Yuck, i won't eat THAT! */
			fclose(ibuf);
			goto rdexit;
	}
	
	if (stb.st_size == 0) {		/* Zero-sized mailbox? */
		fclose(ibuf);
		goto rdexit;
	}
	
	/* copy unread and new mail */
	
	while (fgets(buf, LINESIZE, ibuf) != NULL) {
	    count = strlen(buf);
	    fwrite(buf, sizeof *buf, count, mbox);
	    sysboxlen += count;
	    if (ferror(mbox))
			panic("getmail: Could not write temporary file");
	}
	
	fclose(ibuf);

rdexit:
	fclose(mbox);	/* all mail is now in temp file - digest it! */

	if ((c = opentemp(tempMesg)) < 0)
		exit(1);
		
	if ((mestmp = fdopen(c, "r+")) == NULL)
		panic("getmail: Could not open temporary file");

	if ((mbox = fopen(tempMail, "r")) == NULL)
		panic("getmail: Could not open temporary file");


	for (;;) {
		if (fgets(buf, LINESIZE, mbox) == NULL) {
		    /* End of file */
		    append(&this, mestmp);
		    makemessage(mestmp);
		    if (fclose(mbox))
		    	panic("getmail: Could not close temporary mailbox");
		    return 0;
		}
		count = strlen(buf);
		buf[count - 1] = '\0';
		if (maybe && buf[0] == 'F' && ishead(buf)) {
			messages++;
			newm++;
			append(&this, mestmp);
			this.m_flag = MUSED|MNEW;
			this.m_size = 0;
			this.m_lines = 0;
			this.m_offset = offset;
			this.receipt = NULL;
			this.subj = NULL;
			this.from = NULL;
			this.date = NULL;
			this.id = NULL;
			inheader = 1;
		} else if (buf[0] == 0) {
			inheader = 0;
		} else if (inheader) {
			if (!strncasecmp(buf, "Status: ", 8)) {
				cp = &buf[8];
				if (strchr(cp, 'R') != NULL)
					this.m_flag |= MREAD;
				if (strchr(cp, 'O') != NULL) {
					this.m_flag &= ~MNEW;
					newm--;
				}
			}
			
                        if ((!strncasecmp(buf, "Disposition-Notification-To:", 28)) && (this.receipt == NULL)) {
                                cp = &buf[28];
                                while ((*cp) && (isspace(*cp)))
                                        cp++;
                                this.receipt = strdup(cp);
                        }

			if ((!strncasecmp(buf, "Subject:", 8)) && (this.subj == NULL)) {
				cp = &buf[8];
				while ((*cp) && (isspace(*cp)))
					cp++;
				this.subj = strdup(cp);
			}
			
			if ((!strncasecmp(buf, "From:", 5)) && (this.from == NULL)) {
				cp = &buf[5];
				while ((*cp) && (isspace(*cp)))
					cp++;
				this.from = strdup(cp);
			}
			
			if ((!strncasecmp(buf, "Date:", 5)) && (this.date == NULL)) {
				cp = &buf[5];
				while ((*cp) && (isspace(*cp)))
					cp++;
				this.date = strdup(cp);
			}

			if ((!strncasecmp(buf, "Message-ID:", 11)) && (this.id == NULL)) {
				cp = &buf[11];
				while ((*cp) && (isspace(*cp)))
					cp++;
				this.id = strdup(cp);
			}
		}
		offset += count;
		this.m_size += count;
		this.m_lines++;
		maybe = buf[0] == 0;

	}
	
	
	return 0;
}

/* Read a message */
 
int readmesg(int msg, int verbose)
{
	int i, line = 0;
	int inhdr = 1;
	char buf[LINESIZE];
	if ((msg < 1) || (msg > messages)) {
		printf("There's no message %i.\n", msg);
		return -1;
	}

	if ((mbox = fopen(tempMail, "r")) == NULL) {
		printf("Could not open temporary mailbox.\n");
		return -1;
	}

	i = msg - 1;
	dot = &message[i];

	if (fseek(mbox, dot->m_offset, SEEK_SET)) {
		printf("readmesg: Could not fseek to message %i.\n", msg);
		return -1;
	}
	
	current = msg;
	
	printf("Message %i:%s%s%s\n", msg, 
		((dot->m_flag & (MREAD|MNEW)) == MNEW) ? " (New)" : "",
		((dot->m_flag & (MREAD|MNEW)) == 0) ? " (Unread)": "",
		((dot->m_flag & (MDELETED)) == MDELETED) ? " (Deleted)" : ""
		);
		
	dot->m_flag |= MREAD;
	dot->m_flag ^= (dot->m_flag & MNEW);
	
	for (;;) {
		if (fgets(buf, LINESIZE, mbox) == NULL) {
			/* End of file */
			fclose(mbox);
			return 0;
		}
		line++;
		if (inhdr) {
			/* Headers */
			if (((verbose) && (line != 1) && (strncasecmp(buf, "Status: ", 8)) && (strncasecmp(buf, "X-Status: ", 10)) ) ||
			    (!strncasecmp(buf, "Date:", 5)) ||
			    (!strncasecmp(buf, "From:", 5)) ||
			    (!strncasecmp(buf, "To:", 3)) ||
			    (!strncasecmp(buf, "Cc:", 3)) ||
//			    (!strncasecmp(buf, "Disposition-Notification-To:", 3)) ||
			    (!strncasecmp(buf, "Subject:", 8)))
				printf("%s", buf);

			if (!strcmp("\n", buf)) {
				inhdr = 0;
			}
		} else {
			/* Data */
			if (line == dot->m_lines) {
				fclose(mbox);
				return 0;
			}
			printf("%s", buf);
		}

	}
	return 0;

}