nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

maildir.c (2350B)


      1 #include "maildir.h"
      2 
      3 #include <sys/types.h>
      4 #include <sys/stat.h>
      5 #include <unistd.h>
      6 #include "prioq.h"
      7 #include "env.h"
      8 #include "stralloc.h"
      9 #include "direntry.h"
     10 #include "datetime.h"
     11 #include "now.h"
     12 #include "str.h"
     13 
     14 struct strerr maildir_chdir_err;
     15 struct strerr maildir_scan_err;
     16 
     17 int maildir_chdir()
     18 {
     19  char *maildir;
     20  maildir = env_get("MAILDIR");
     21  if (!maildir)
     22    STRERR(-1,maildir_chdir_err,"MAILDIR not set")
     23  if (chdir(maildir) == -1)
     24    STRERR_SYS3(-1,maildir_chdir_err,"unable to chdir to ",maildir,": ")
     25  return 0;
     26 }
     27 
     28 void maildir_clean(tmpname)
     29 stralloc *tmpname;
     30 {
     31  DIR *dir;
     32  direntry *d;
     33  datetime_sec time;
     34  struct stat st;
     35 
     36  time = now();
     37 
     38  dir = opendir("tmp");
     39  if (!dir) return;
     40 
     41  while ((d = readdir(dir)))
     42   {
     43    if (d->d_name[0] == '.') continue;
     44    if (!stralloc_copys(tmpname,"tmp/")) break;
     45    if (!stralloc_cats(tmpname,d->d_name)) break;
     46    if (!stralloc_0(tmpname)) break;
     47    if (stat(tmpname->s,&st) == 0)
     48      if (time > st.st_atime + 129600)
     49        unlink(tmpname->s);
     50   }
     51  closedir(dir);
     52 }
     53 
     54 static int append(pq,filenames,subdir,time)
     55 prioq *pq;
     56 stralloc *filenames;
     57 char *subdir;
     58 datetime_sec time;
     59 {
     60  DIR *dir;
     61  direntry *d;
     62  struct prioq_elt pe;
     63  unsigned int pos;
     64  struct stat st;
     65 
     66  dir = opendir(subdir);
     67  if (!dir)
     68    STRERR_SYS3(-1,maildir_scan_err,"unable to scan $MAILDIR/",subdir,": ")
     69 
     70  while ((d = readdir(dir)))
     71   {
     72    if (d->d_name[0] == '.') continue;
     73    pos = filenames->len;
     74    if (!stralloc_cats(filenames,subdir)) break;
     75    if (!stralloc_cats(filenames,"/")) break;
     76    if (!stralloc_cats(filenames,d->d_name)) break;
     77    if (!stralloc_0(filenames)) break;
     78    if (stat(filenames->s + pos,&st) == 0)
     79      if (st.st_mtime < time) /* don't want to mix up the order */
     80       {
     81        pe.dt = st.st_mtime;
     82        pe.id = pos;
     83        if (!prioq_insert(pq,&pe)) break;
     84       }
     85   }
     86 
     87  closedir(dir);
     88  if (d) STRERR_SYS3(-1,maildir_scan_err,"unable to read $MAILDIR/",subdir,": ")
     89  return 0;
     90 }
     91 
     92 int maildir_scan(pq,filenames,flagnew,flagcur)
     93 prioq *pq;
     94 stralloc *filenames;
     95 int flagnew;
     96 int flagcur;
     97 {
     98  struct prioq_elt pe;
     99  datetime_sec time;
    100 
    101  if (!stralloc_copys(filenames,"")) return 0;
    102  while (prioq_min(pq,&pe)) prioq_delmin(pq);
    103 
    104  time = now();
    105 
    106  if (flagnew) if (append(pq,filenames,"new",time) == -1) return -1;
    107  if (flagcur) if (append(pq,filenames,"cur",time) == -1) return -1;
    108  return 0;
    109 }