2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 char *current_mail_spool;
41 static time_t last_mail_mtime;
42 static double last_mail_update;
44 void update_mail_count()
48 if (current_mail_spool == NULL)
51 /* TODO: use that fine file modification notify on Linux 2.4 */
53 /* don't check mail so often (9.5s is minimum interval) */
54 if (current_update_time - last_mail_update < 9.5)
57 last_mail_update = current_update_time;
59 if (stat(current_mail_spool, &buf)) {
62 ERR("can't stat %s: %s", current_mail_spool,
70 if (S_ISDIR(buf.st_mode)) {
73 struct dirent *dirent;
75 info.new_mail_count = 0;
78 (char *) malloc(sizeof(char) *
79 (strlen(current_mail_spool) + 5));
84 strcpy(dirname, current_mail_spool);
86 /* checking the cur subdirectory */
87 strcat(dirname, "cur");
89 dir = opendir(dirname);
91 ERR("cannot open directory");
95 dirent = readdir(dir);
97 /* . and .. are skipped */
98 if (dirent->d_name[0] != '.') {
101 dirent = readdir(dir);
105 dirname[strlen(dirname) - 3] = '\0';
106 strcat(dirname, "new");
108 dir = opendir(dirname);
110 ERR("cannot open directory");
114 dirent = readdir(dir);
116 /* . and .. are skipped */
117 if (dirent->d_name[0] != '.') {
118 info.new_mail_count++;
121 dirent = readdir(dir);
130 if (buf.st_mtime != last_mail_mtime) {
131 /* yippee, modification time has changed, let's read mail count! */
134 int reading_status = 0;
136 /* could lock here but I don't think it's really worth it because
137 * this isn't going to write mail spool */
139 info.new_mail_count = 0;
142 fp = open_file(current_mail_spool, &rep);
146 /* NOTE: adds mail as new if there isn't Status-field at all */
150 if (fgets(buf, 128, fp) == NULL)
153 if (strncmp(buf, "From ", 5) == 0) {
154 /* ignore MAILER-DAEMON */
155 if (strncmp(buf + 5, "MAILER-DAEMON ", 14)
160 info.new_mail_count++;
166 && strncmp(buf, "X-Mozilla-Status:",
168 /* check that mail isn't already read */
169 if (strchr(buf + 21, '0'))
170 info.new_mail_count++;
176 && strncmp(buf, "Status:", 7) == 0) {
177 /* check that mail isn't already read */
178 if (strchr(buf + 7, 'R') == NULL)
179 info.new_mail_count++;
187 while (strchr(buf, '\n') == NULL && !feof(fp))
194 info.new_mail_count++;
196 last_mail_mtime = buf.st_mtime;