moved stuff
[monky] / src / mail.c
1 /*
2  * Conky, a system monitor, based on torsmo
3  *
4  * This program is licensed under BSD license, read COPYING
5  *
6  *  $Id$
7  */
8
9 #include "conky.h"
10 #include <stdio.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <sys/time.h>
14 #include <sys/stat.h>
15 #include <dirent.h>
16
17 char *current_mail_spool;
18
19 static time_t last_mail_mtime;
20 static double last_mail_update;
21
22 void update_mail_count()
23 {
24         struct stat buf;
25
26         if (current_mail_spool == NULL)
27                 return;
28
29         /* TODO: use that fine file modification notify on Linux 2.4 */
30
31         /* don't check mail so often (9.5s is minimum interval) */
32         if (current_update_time - last_mail_update < 9.5)
33                 return;
34         else
35                 last_mail_update = current_update_time;
36
37         if (stat(current_mail_spool, &buf)) {
38                 static int rep;
39                 if (!rep) {
40                         ERR("can't stat %s: %s", current_mail_spool,
41                             strerror(errno));
42                         rep = 1;
43                 }
44                 return;
45         }
46 #if HAVE_DIRENT_H
47         /* maildir format */
48         if (S_ISDIR(buf.st_mode)) {
49                 DIR *dir;
50                 char *dirname;
51                 struct dirent *dirent;
52                 info.mail_count = 0;
53                 info.new_mail_count = 0;
54
55                 dirname =
56                     (char *) malloc(sizeof(char) *
57                                     (strlen(current_mail_spool) + 5));
58                 if (!dirname) {
59                         ERR("malloc");
60                         return;
61                 }
62                 strcpy(dirname, current_mail_spool);
63                 strcat(dirname, "/");
64                 /* checking the cur subdirectory */
65                 strcat(dirname, "cur");
66
67                 dir = opendir(dirname);
68                 if (!dir) {
69                         ERR("cannot open directory");
70                         free(dirname);
71                         return;
72                 }
73                 dirent = readdir(dir);
74                 while (dirent) {
75                         /* . and .. are skipped */
76                         if (dirent->d_name[0] != '.') {
77                                 info.mail_count++;
78                         }
79                         dirent = readdir(dir);
80                 }
81                 closedir(dir);
82
83                 dirname[strlen(dirname) - 3] = '\0';
84                 strcat(dirname, "new");
85
86                 dir = opendir(dirname);
87                 if (!dir) {
88                         ERR("cannot open directory");
89                         free(dirname);
90                         return;
91                 }
92                 dirent = readdir(dir);
93                 while (dirent) {
94                         /* . and .. are skipped */
95                         if (dirent->d_name[0] != '.') {
96                                 info.new_mail_count++;
97                                 info.mail_count++;
98                         }
99                         dirent = readdir(dir);
100                 }
101                 closedir(dir);
102
103                 free(dirname);
104                 return;
105         }
106 #endif
107         /* mbox format */
108
109
110         if (buf.st_mtime != last_mail_mtime) {
111                 /* yippee, modification time has changed, let's read mail count! */
112                 static int rep;
113                 FILE *fp;
114                 int reading_status = 0;
115
116                 /* could lock here but I don't think it's really worth it because
117                  * this isn't going to write mail spool */
118
119                 info.new_mail_count = 0;
120                 info.mail_count = 0;
121
122                 fp = open_file(current_mail_spool, &rep);
123                 if (!fp)
124                         return;
125
126                 /* NOTE: adds mail as new if there isn't Status-field at all */
127
128                 while (!feof(fp)) {
129                         char buf[128];
130                         if (fgets(buf, 128, fp) == NULL)
131                                 break;
132
133                         if (strncmp(buf, "From ", 5) == 0) {
134                                 /* ignore MAILER-DAEMON */
135                                 if (strncmp(buf + 5, "MAILER-DAEMON ", 14)
136                                     != 0) {
137                                         info.mail_count++;
138
139                                         if (reading_status)
140                                                 info.new_mail_count++;
141                                         else
142                                                 reading_status = 1;
143                                 }
144                         } else {
145                                 if (reading_status
146                                     && strncmp(buf, "X-Mozilla-Status:",
147                                                17) == 0) {
148                                         /* check that mail isn't already read */
149                                         if (strchr(buf + 21, '0'))
150                                                 info.new_mail_count++;
151
152                                         reading_status = 0;
153                                         continue;
154                                 }
155                                 if (reading_status
156                                     && strncmp(buf, "Status:", 7) == 0) {
157                                         /* check that mail isn't already read */
158                                         if (strchr(buf + 7, 'R') == NULL)
159                                                 info.new_mail_count++;
160
161                                         reading_status = 0;
162                                         continue;
163                                 }
164                         }
165
166                         /* skip until \n */
167                         while (strchr(buf, '\n') == NULL && !feof(fp))
168                                 fgets(buf, 128, fp);
169                 }
170
171                 fclose(fp);
172
173                 if (reading_status)
174                         info.new_mail_count++;
175
176                 last_mail_mtime = buf.st_mtime;
177         }
178 }