2008-02-20 20:30:45 +00:00
|
|
|
/* Conky, a system monitor, based on torsmo
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2007-08-10 19:53:44 +00:00
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2008-03-31 04:56:39 +00:00
|
|
|
* Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2008-02-20 20:30:45 +00:00
|
|
|
* $Id$ */
|
2005-08-05 01:06:17 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <sys/stat.h>
|
2006-03-16 18:07:31 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <dirent.h>
|
2006-03-16 18:07:31 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "conky.h"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
char *current_mail_spool;
|
|
|
|
|
2007-10-02 23:57:41 +00:00
|
|
|
void update_mail_count(struct local_mail_s *mail)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-03-29 09:58:09 +00:00
|
|
|
struct stat st;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (mail == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
/* TODO: use that fine file modification notify on Linux 2.4 */
|
|
|
|
|
|
|
|
/* don't check mail so often (9.5s is minimum interval) */
|
2008-02-20 20:30:45 +00:00
|
|
|
if (current_update_time - mail->last_update < 9.5) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->last_update = current_update_time;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
if (stat(mail->box, &st)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
static int rep;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
if (!rep) {
|
2008-02-20 20:30:45 +00:00
|
|
|
ERR("can't stat %s: %s", mail->box, strerror(errno));
|
2005-07-20 00:30:40 +00:00
|
|
|
rep = 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if HAVE_DIRENT_H
|
|
|
|
/* maildir format */
|
2008-03-29 09:58:09 +00:00
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
DIR *dir;
|
|
|
|
char *dirname;
|
|
|
|
struct dirent *dirent;
|
|
|
|
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->mail_count = mail->new_mail_count = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
dirname = (char *) malloc(sizeof(char) * (strlen(mail->box) + 5));
|
2005-07-20 00:30:40 +00:00
|
|
|
if (!dirname) {
|
|
|
|
ERR("malloc");
|
|
|
|
return;
|
|
|
|
}
|
2007-10-02 23:57:41 +00:00
|
|
|
strcpy(dirname, mail->box);
|
2005-07-20 00:30:40 +00:00
|
|
|
strcat(dirname, "/");
|
|
|
|
/* checking the cur subdirectory */
|
|
|
|
strcat(dirname, "cur");
|
|
|
|
|
|
|
|
dir = opendir(dirname);
|
|
|
|
if (!dir) {
|
|
|
|
ERR("cannot open directory");
|
|
|
|
free(dirname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dirent = readdir(dir);
|
|
|
|
while (dirent) {
|
|
|
|
/* . and .. are skipped */
|
|
|
|
if (dirent->d_name[0] != '.') {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->mail_count++;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
dirent = readdir(dir);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
dirname[strlen(dirname) - 3] = '\0';
|
|
|
|
strcat(dirname, "new");
|
|
|
|
|
|
|
|
dir = opendir(dirname);
|
|
|
|
if (!dir) {
|
|
|
|
ERR("cannot open directory");
|
|
|
|
free(dirname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dirent = readdir(dir);
|
|
|
|
while (dirent) {
|
|
|
|
/* . and .. are skipped */
|
|
|
|
if (dirent->d_name[0] != '.') {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->new_mail_count++;
|
|
|
|
mail->mail_count++;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
dirent = readdir(dir);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
free(dirname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* mbox format */
|
2008-03-29 09:58:09 +00:00
|
|
|
if (st.st_mtime != mail->last_mtime) {
|
2005-07-20 00:30:40 +00:00
|
|
|
/* yippee, modification time has changed, let's read mail count! */
|
|
|
|
static int rep;
|
|
|
|
FILE *fp;
|
|
|
|
int reading_status = 0;
|
|
|
|
|
|
|
|
/* could lock here but I don't think it's really worth it because
|
|
|
|
* this isn't going to write mail spool */
|
|
|
|
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->new_mail_count = mail->mail_count = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2007-10-02 23:57:41 +00:00
|
|
|
fp = open_file(mail->box, &rep);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!fp) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
/* NOTE: adds mail as new if there isn't Status-field at all */
|
|
|
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
char buf[128];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (fgets(buf, 128, fp) == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
if (strncmp(buf, "From ", 5) == 0) {
|
|
|
|
/* ignore MAILER-DAEMON */
|
2008-02-20 20:30:45 +00:00
|
|
|
if (strncmp(buf + 5, "MAILER-DAEMON ", 14) != 0) {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->mail_count++;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (reading_status) {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->new_mail_count++;
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
reading_status = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (reading_status
|
2008-02-20 20:30:45 +00:00
|
|
|
&& strncmp(buf, "X-Mozilla-Status:", 17) == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
/* check that mail isn't already read */
|
2008-02-20 20:30:45 +00:00
|
|
|
if (strchr(buf + 21, '0')) {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->new_mail_count++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
reading_status = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
if (reading_status && strncmp(buf, "Status:", 7) == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
/* check that mail isn't already read */
|
2008-02-20 20:30:45 +00:00
|
|
|
if (strchr(buf + 7, 'R') == NULL) {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->new_mail_count++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
reading_status = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip until \n */
|
2008-02-20 20:30:45 +00:00
|
|
|
while (strchr(buf, '\n') == NULL && !feof(fp)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
fgets(buf, 128, fp);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (reading_status) {
|
2007-10-02 23:57:41 +00:00
|
|
|
mail->new_mail_count++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
mail->last_mtime = st.st_mtime;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
}
|