ze-filter  (ze-filter-0.8.0-develop-180218)
ze-mbox.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : Mon Jun 19 11:38:04 CEST 2006
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 
25 #include <ze-sys.h>
26 #include <ze-filter.h>
27 #include <ze-mbox.h>
28 
29 /* ****************************************************************************
30  * *
31  * *
32  **************************************************************************** */
33 int
34 mbox_handle(fname, func, arg)
35  char *fname;
36  mbox_F func;
37  void *arg;
38 {
39  FILE *fin;
40  int nb = 0;
41 
42  ASSERT(fname != NULL);
43  ASSERT(func != NULL);
44 
45  if ((fin = fopen(fname, "r")) != NULL)
46  {
47  int nl = -1;
48  char line[2048];
49  FILE *fout;
50  char ofname[256];
51  int fd = -1;
52  int msgID = 0;
53 
54  memset(line, 0, sizeof (line));
55 
56  for (;;)
57  {
58  char *q = NULL;
59 
60  if (fd < 0)
61  {
62 #if 0
63  char *dir = "/tmp";
64  char *env = getenv("MBOXSPOOL");
65 
66  if (env != NULL) {
67  if (access(env, R_OK | W_OK | X_OK) == 0)
68  {
69  dir = env;
70  }
71  }
72  snprintf(ofname, sizeof(ofname), "%s/mbox-tmp.XXXXXX", dir);
73 #else
74  strlcpy(ofname, "/tmp/mbox-tmp.XXXXXX", sizeof (ofname));
75 #endif
76 
77  ZE_MessageInfo(20, "Creating %s", ofname);
78  if ((fd = mkstemp(ofname)) < 0)
79  {
80  ZE_LogSysError("Can't create temporary file");
81  continue;
82  }
83  }
84 
85  if (strlen(line) > 0)
86  (void) write(fd, line, strlen(line));
87 
88  while ((q = fgets(line, sizeof (line), fin)) != NULL)
89  {
90  nl++;
91  if (nl > 1 && strncmp(line, "From ", strlen("From ")) == 0)
92  break;
93 
94  (void) write(fd, line, strlen(line));
95  }
96  close(fd);
97  fd = -1;
98 
99  msgID++;
100  if (func(ofname, msgID, arg))
101  nb++;
102 
103  if (remove(ofname) != 0)
104  ZE_LogSysError("Error removing %s file", ofname);
105 
106  if (q == NULL)
107  break;
108  }
109  fclose(fin);
110  }
111  return nb;
112 }
113 
114 /* ****************************************************************************
115  * *
116  * *
117  **************************************************************************** */
118 #if HAVE_LSTAT
119 #define LSTAT(a,b) lstat((a),(b))
120 #else
121 #define LSTAT(a,b) stat((a),(b))
122 #endif
123 
124 int
125 maildir_handle(dirname, func, arg)
126  char *dirname;
127  mbox_F func;
128  void *arg;
129 {
130  DIR *dir;
131  struct dirent *p;
132  struct stat st;
133  int msgID = 0, nb = 0;
134 
135  ASSERT(dirname != NULL);
136  ASSERT(func != NULL);
137 
138  if ((dir = opendir(dirname)) != NULL)
139  {
140  while ((p = readdir(dir)) != NULL)
141  {
142  char fname[256];
143 
144  snprintf(fname, sizeof (fname), "%s/%s", dirname, p->d_name);
145 
146  if (LSTAT(fname, &st) == 0)
147  {
148  if (S_ISREG(st.st_mode))
149  {
150  msgID++;
151  if (func(fname, msgID, arg))
152  nb++;
153  }
154  } else
155  ZE_LogMsgWarning(0, "lstat(%s) error", STRNULL(fname, ""));
156  }
157  closedir(dir);
158  }
159  return nb;
160 }
#define ASSERT(a)
Definition: macros.h:27
#define LSTAT(a, b)
Definition: ze-mbox.c:121
#define STRNULL(x, r)
Definition: macros.h:81
#define strlcpy
Definition: zeString.h:32
int mbox_handle(char *fname, mbox_F func, void *arg)
Definition: ze-mbox.c:34
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
int nb
Definition: ze-connopen.c:61
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
#define ZE_LogMsgWarning(level,...)
Definition: zeSyslog.h:112
int maildir_handle(char *dirname, mbox_F func, void *arg)
Definition: ze-mbox.c:125
bool(* mbox_F)(char *fname, int id, void *arg)
Definition: ze-mbox.h:32