diff -Naur torsmo-0.17/torsmo.c torsmo-0.17_patched/torsmo.c --- torsmo-0.17/torsmo.c 2004-05-25 12:58:51.000000000 +0200 +++ torsmo-0.17_patched/torsmo.c 2004-12-12 18:23:19.000000000 +0100 @@ -8,7 +8,6 @@ #include "torsmo.h" #include #include -#include #include #include #include @@ -19,10 +18,17 @@ #include #include #include +#include #include #include #include +/* some constants for maildir patch */ +#define NEWMAIL_DIR "new" +#define CURMAIL_DIR "cur" + +#define BUFFER_SIZE 1024 + /* alignments */ enum { TOP_LEFT = 1, @@ -290,78 +296,21 @@ static double last_mail_update; static void update_mail_count() { - struct stat buf; - + time_t updated_time; + if (current_mail_spool == NULL) return; - + /* don't check mail so often (9.5s is minimum interval) */ if (current_update_time - last_mail_update < 9.5) return; else last_mail_update = current_update_time; - - if (stat(current_mail_spool, &buf)) { - static int rep; - if (!rep) { - ERR("can't stat %s: %s", current_mail_spool, strerror(errno)); - rep = 1; - } - return; - } - - if (buf.st_mtime != last_mail_mtime) { - /* 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 */ - - new_mail_count = 0; - mail_count = 0; - - fp = open_file(current_mail_spool, &rep); - if (!fp) return; - - /* NOTE: adds mail as new if there isn't Status-field at all */ - - while (!feof(fp)) { - char buf[128]; - if (fgets(buf, 128, fp) == NULL) break; - - if (strncmp(buf, "From ", 5) == 0) { - /* ignore MAILER-DAEMON */ - if (strncmp(buf+5, "MAILER-DAEMON ", 14) != 0) { - mail_count++; - - if (reading_status) - new_mail_count++; - else - reading_status = 1; - } - } - else { - if (reading_status && strncmp(buf, "Status:", 7) == 0) { - /* check that mail isn't already read */ - if (strchr(buf+7, 'R') == NULL) - new_mail_count++; - - reading_status = 0; - continue; - } - } - - /* skip until \n */ - while (strchr(buf, '\n') == NULL && !feof(fp)) - fgets(buf, 128, fp); - } - - fclose(fp); - - if (reading_status) new_mail_count++; - - last_mail_mtime = buf.st_mtime; + + updated_time=check_update_time(current_mail_spool); + if (updated_time > last_mail_mtime) { + if (check_maildir(current_mail_spool,&new_mail_count,&mail_count)==-1) + return; + last_mail_mtime = updated_time; } } @@ -2387,3 +2336,90 @@ return 0; } + +int +count_mail_indir(const char *path){ + + int mails_nbr; + DIR *cur_maildir; + struct dirent *dir_entry; + + mails_nbr=0; + + if (path==NULL) + return -1; + + if ((cur_maildir=opendir(path))==NULL){ + perror("directory open error:"); + return -1; + } + + /* count number of entries in folder */ + while ((dir_entry=readdir(cur_maildir))){ + /* skip .. and . entries */ + if (strcmp(dir_entry->d_name,"..")!=0 && strcmp(dir_entry->d_name,".")!=0) + mails_nbr++; + } + /* free(dir_entry); */ + closedir (cur_maildir); + + return mails_nbr; +} + +int +check_maildir (const char *path, int *new, int *cur){ + + int new_mails, cur_mails, retvalue; + char buffer_newmail [BUFFER_SIZE], buffer_curmail [BUFFER_SIZE]; + + /* I'm paranoic! */ + + memset((void *)buffer_newmail,0,BUFFER_SIZE); + memset((void *)buffer_curmail,0,BUFFER_SIZE); + + strncpy(buffer_newmail,path,BUFFER_SIZE-1); + strncpy(buffer_curmail,path,BUFFER_SIZE-1); + + strncat(buffer_newmail,NEWMAIL_DIR,BUFFER_SIZE-1); + strncat(buffer_curmail,CURMAIL_DIR,BUFFER_SIZE-1); + + new_mails=count_mail_indir(buffer_newmail); + cur_mails=count_mail_indir(buffer_curmail); + + if (new_mails==-1 || cur_mails==-1) + retvalue=-1; + else{ + *new=new_mails; + *cur=cur_mails; + retvalue=0; + } + return retvalue; +} + +time_t +check_update_time(const char *path){ + char buffer_newmail [BUFFER_SIZE], buffer_curmail [BUFFER_SIZE]; + struct stat buf; + time_t cur_updated, new_updated; + + cur_updated=new_updated=0; + + /* Somebody can factorize this code portion? */ + + memset((void *)buffer_newmail,0,BUFFER_SIZE); + memset((void *)buffer_curmail,0,BUFFER_SIZE); + + strncpy(buffer_newmail,path,BUFFER_SIZE-1); + strncpy(buffer_curmail,path,BUFFER_SIZE-1); + + strncat(buffer_newmail,NEWMAIL_DIR,BUFFER_SIZE-1); + strncat(buffer_curmail,CURMAIL_DIR,BUFFER_SIZE-1); + + if (stat(buffer_curmail, &buf)!=-1) + cur_updated=buf.st_mtime; + + if (stat(buffer_newmail, &buf)!=-1) + new_updated=buf.st_mtime; + + return (cur_updated>new_updated)?cur_updated:new_updated; +} diff -Naur torsmo-0.17/torsmo.h torsmo-0.17_patched/torsmo.h --- torsmo-0.17/torsmo.h 2004-05-25 10:57:36.000000000 +0200 +++ torsmo-0.17_patched/torsmo.h 2004-12-12 00:27:11.000000000 +0100 @@ -3,6 +3,7 @@ #include "config.h" #include +#include #define ERR(s, varargs...) \ fprintf(stderr, "torsmo: " s "\n", ##varargs) @@ -70,4 +71,18 @@ struct net_stat *get_net_stat(const char *dev); +/* my hack for maildir format */ + +/* check mail in directory (path argument) */ +/* This function return number of mail (or files) in passed folder */ +/* if the returned value is differend -1 */ + +int count_mail_indir(const char *path); + +/* check mail in Maildir (new mails in new and old in cur */ +int check_maildir (const char *path, int *new, int *old); + +/* check last modification time */ +time_t check_update_time(const char *path); + #endif