Back to index

courier  0.68.2
comtrack.c
Go to the documentation of this file.
00001 #include "comtrack.h"
00002 #include "courier.h"
00003 #include "localstatedir.h"
00004 #include "numlib/numlib.h"
00005 #include <sys/types.h>
00006 #include <stdio.h>
00007 #include <dirent.h>
00008 #include <string.h>
00009 #include <stdlib.h>
00010 #include <unistd.h>
00011 
00012 void trackpurge()
00013 {
00014        DIR *dirp;
00015        struct dirent *de;
00016        time_t curTime=time(NULL) / 3600;
00017        char namebuf[sizeof(TRACKDIR) + NUMBUFSIZE+2];
00018 
00019        dirp=opendir(TRACKDIR);
00020 
00021        if (!dirp)
00022        {
00023               clog_msg_start_err();
00024               clog_msg_str("Cannot open ");
00025               clog_msg_str(TRACKDIR);
00026               clog_msg_send();
00027               clog_msg_prerrno();
00028               return;
00029        }
00030 
00031        while ((de=readdir(dirp)) != NULL)
00032        {
00033               if (de->d_name[0] == '.')
00034                      continue;
00035 
00036               if (atoi(de->d_name) >= curTime - (TRACK_NHOURS+2))
00037                      continue;
00038 
00039 
00040               strcat(strcpy(namebuf, TRACKDIR "/"), de->d_name);
00041               unlink(namebuf);
00042        }
00043        closedir(dirp);
00044 }
00045 
00046 int track_find(const char *address, time_t *timestamp)
00047 {
00048        char namebuf[sizeof(TRACKDIR) + NUMBUFSIZE+2];
00049        char buf2[NUMBUFSIZE];
00050        FILE *fp;
00051        time_t curTime=time(NULL) / 3600;
00052        int i;
00053        char linebuf[BUFSIZ];
00054        char *p;
00055        char *addrbuf=strdup(address);
00056        int results=TRACK_NOTFOUND;
00057 
00058        if (!addrbuf)
00059               return TRACK_NOTFOUND;
00060 
00061        domainlower(addrbuf);
00062 
00063        for (i=0; i <= TRACK_NHOURS; ++i)
00064        {
00065               strcat(strcpy(namebuf, TRACKDIR "/"),
00066                      libmail_str_time_t(curTime - i, buf2));
00067 
00068               if ((fp=fopen(namebuf, "r")) == NULL)
00069                      continue;
00070 
00071               while (fgets(linebuf, sizeof(linebuf), fp))
00072               {
00073                      p=strchr(linebuf, '\n');
00074                      if (p) *p=0;
00075 
00076                      p=strchr(linebuf, ' ');
00077                      if (!p)
00078                             continue;
00079                      ++p;
00080 
00081                      if (*p && strcmp(p+1, addrbuf) == 0)
00082                      {
00083                             if ((*timestamp=libmail_atotime_t(linebuf))
00084                                 != 0)
00085                                    results=*p;
00086                      }
00087               }
00088               fclose(fp);
00089               if (results != TRACK_NOTFOUND)
00090                      break;
00091        }
00092        free(addrbuf);
00093        return results;
00094 }
00095 
00096 void track_save(const char *address, int status)
00097 {
00098        char namebuf[sizeof(TRACKDIR) + NUMBUFSIZE+2];
00099        char buf2[NUMBUFSIZE];
00100        FILE *fp;
00101        time_t curTime=time(NULL);
00102        time_t t=curTime / 3600;
00103        char *addrbuf=strdup(address);
00104 
00105        if (!addrbuf)
00106               return;
00107 
00108        domainlower(addrbuf);
00109 
00110        strcat(strcpy(namebuf, TRACKDIR "/"), libmail_str_time_t(t, buf2));
00111        libmail_str_time_t(curTime, buf2);
00112 
00113        fp=fopen(namebuf, "a");
00114        if (fp)
00115        {
00116               fprintf(fp, "%s %c%s\n", buf2, (char)status, addrbuf);
00117               fclose(fp);
00118        }
00119        free(addrbuf);
00120 }
00121 
00122 int track_read(int (*cb_func)(time_t timestamp, int status,
00123                            const char *address, void *voidarg),
00124               void *voidarg)
00125 {
00126        char namebuf[sizeof(TRACKDIR) + NUMBUFSIZE+2];
00127        char buf2[NUMBUFSIZE];
00128        FILE *fp;
00129        time_t curTime=time(NULL) / 3600;
00130        int i;
00131        char linebuf[BUFSIZ];
00132        char *p;
00133 
00134        for (i=0; i <= TRACK_NHOURS; ++i)
00135        {
00136               strcat(strcpy(namebuf, TRACKDIR "/"),
00137                      libmail_str_time_t(curTime - i, buf2));
00138 
00139               if ((fp=fopen(namebuf, "r")) == NULL)
00140                      continue;
00141 
00142               while (fgets(linebuf, sizeof(linebuf), fp))
00143               {
00144                      p=strchr(linebuf, '\n');
00145                      if (p) *p=0;
00146 
00147                      p=strchr(linebuf, ' ');
00148                      if (!p)
00149                             continue;
00150                      ++p;
00151 
00152                      if (*p)
00153                      {
00154                             int rc=(*cb_func)(libmail_atotime_t(linebuf),
00155                                             *p, p+1, voidarg);
00156 
00157                             if (rc)
00158                             {
00159                                    fclose(fp);
00160                                    return rc;
00161                             }
00162                      }
00163               }
00164               fclose(fp);
00165        }
00166 
00167        return (0);
00168 }