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