[go: up one dir, main page]

Menu

[r55]: / trunk / libmail / mbox.c  Maximize  Restore  History

Download this file

75 lines (60 with data), 1.9 kB

 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
/* This file is part of libmail.
(c) 2009 - Dimitris Mandalidis <mandas@users.sourceforge.net>
libmail 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 3 of the License, or
(at your option) any later version.
libmail 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 libmail. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <libmail/libmail.h>
#include <libmail/error.h>
int mbox_check(libmail_local_mailbox_t *mbox, int *n_new_messages, int *n_messages) {
char buffer[999];
FILE *fp;
char *ptr;
struct stat buf;
// Make sure we 're not trying to open a directory
if (stat(mbox->location, &buf) != 0)
return LIBMAIL_SYSERROR;
if (S_ISDIR(buf.st_mode))
return LIBMAIL_MBOX_ISDIR;
//
if ((fp = fopen(mbox->location, "r")) == NULL)
return LIBMAIL_SYSERROR;
*n_messages = 0;
*n_new_messages = 0;
int in_header = 0;
while (fgets(buffer, 998, fp) != NULL) {
if (!in_header) {
if (strncmp(buffer, "From ", 5) == 0) {
(*n_messages)++;
(*n_new_messages)++;
in_header = 1;
}
}
else {
if (strncmp(buffer, "Status: ", 8) == 0) {
ptr = &buffer[8];
if (strchr(ptr, 'R') || strchr(ptr, 'O'))
(*n_new_messages)--;
}
else if (strcmp(buffer, "\r\n") == 0 || strcmp(buffer, "\n") == 0)
in_header = 0;
}
}
assert(*n_new_messages < 0);
if (fclose(fp) != 0)
return LIBMAIL_SYSERROR;
return LIBMAIL_SUCCESS;
}