1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <string.h> |
4 |
#include <errno.h> |
5 |
#include <stdarg.h> |
6 |
#include <limits.h> |
7 |
#include <sys/types.h> |
8 |
#include <sys/stat.h> |
9 |
#include <time.h> |
10 |
#include <signal.h> |
11 |
|
12 |
#include "config.h" |
13 |
#include "imapfilter.h" |
14 |
#include "pathnames.h" |
15 |
|
16 |
|
17 |
extern connection_t connpri, connaux; |
18 |
extern options_t opts; |
19 |
extern unsigned int flags; |
20 |
|
21 |
static FILE *debugfp = NULL; /* Pointer to debug file. */ |
22 |
static FILE *logfp = NULL; /* Pointer to logfile. */ |
23 |
|
24 |
|
25 |
void signal_handler(int sig); |
26 |
char *get_time(void); |
27 |
|
28 |
|
29 |
/* |
30 |
* Print message if not in the lowest verbosity level. |
31 |
*/ |
32 |
void |
33 |
info(const char *info,...) |
34 |
{ |
35 |
va_list args; |
36 |
|
37 |
if (opts.verbosity == -2) |
38 |
return; |
39 |
|
40 |
va_start(args, info); |
41 |
vprintf(info, args); |
42 |
va_end(args); |
43 |
} |
44 |
|
45 |
|
46 |
/* |
47 |
* Print message if in the highest verbosity level. |
48 |
*/ |
49 |
void |
50 |
verbose(const char *info,...) |
51 |
{ |
52 |
va_list args; |
53 |
|
54 |
if (opts.verbosity == 2) { |
55 |
va_start(args, info); |
56 |
vprintf(info, args); |
57 |
va_end(args); |
58 |
} |
59 |
} |
60 |
|
61 |
|
62 |
/* |
63 |
* Write message to debug file. |
64 |
*/ |
65 |
void |
66 |
debug(const char *debug,...) |
67 |
{ |
68 |
va_list args; |
69 |
|
70 |
if (opts.debug && debugfp) { |
71 |
va_start(args, debug); |
72 |
vfprintf(debugfp, debug, args); |
73 |
fflush(debugfp); |
74 |
va_end(args); |
75 |
} |
76 |
} |
77 |
|
78 |
|
79 |
/* |
80 |
* Print error message and write it into logfile. |
81 |
*/ |
82 |
void |
83 |
error(const char *errmsg,...) |
84 |
{ |
85 |
va_list args; |
86 |
|
87 |
va_start(args, errmsg); |
88 |
|
89 |
fprintf(stderr, "imapfilter: "); |
90 |
vfprintf(stderr, errmsg, args); |
91 |
|
92 |
if (opts.errors && logfp) { |
93 |
vfprintf(logfp, errmsg, args); |
94 |
fflush(logfp); |
95 |
} |
96 |
va_end(args); |
97 |
} |
98 |
|
99 |
|
100 |
/* |
101 |
* Print error message and exit program. |
102 |
*/ |
103 |
void |
104 |
fatal(unsigned int errnum, const char *fatal,...) |
105 |
{ |
106 |
va_list args; |
107 |
|
108 |
va_start(args, fatal); |
109 |
fprintf(stderr, "imapfilter: "); |
110 |
vfprintf(stderr, fatal, args); |
111 |
va_end(args); |
112 |
|
113 |
close_connection(&connpri); |
114 |
close_connection(&connaux); |
115 |
secmem_clear(); |
116 |
tty_restore(); |
117 |
log_stop(); |
118 |
lockfile_remove(); |
119 |
debug_stop(); |
120 |
|
121 |
exit(errnum); |
122 |
} |
123 |
|
124 |
|
125 |
/* |
126 |
* Catch signals that cause rereading of the configuration file or program's |
127 |
* termination. |
128 |
*/ |
129 |
void |
130 |
catch_signals(void) |
131 |
{ |
132 |
signal(SIGUSR1, signal_handler); |
133 |
signal(SIGINT, signal_handler); |
134 |
signal(SIGQUIT, signal_handler); |
135 |
signal(SIGTERM, signal_handler); |
136 |
} |
137 |
|
138 |
|
139 |
/* |
140 |
* Signal handler for signals that cause rereading of the configuration file or |
141 |
* termination of program. |
142 |
*/ |
143 |
void |
144 |
signal_handler(int sig) |
145 |
{ |
146 |
if (sig == SIGUSR1) |
147 |
flags |= FLAG_SIGUSR1; |
148 |
else |
149 |
fatal(ERROR_SIGNAL, "killed by signal %d\n", sig); |
150 |
} |
151 |
|
152 |
|
153 |
/* |
154 |
* Open temporary debug file and associate a stream with the returned file |
155 |
* descriptor. |
156 |
*/ |
157 |
int |
158 |
debug_start(void) |
159 |
{ |
160 |
static char dt[] = PATHNAME_DEBUG; |
161 |
int fd; |
162 |
|
163 |
if (!opts.debug) |
164 |
return 0; |
165 |
|
166 |
fd = mkstemp(dt); |
167 |
|
168 |
if (fd != -1) { |
169 |
debugfp = fdopen(fd, "w"); |
170 |
if (debugfp == NULL) { |
171 |
error("opening debug file %s: %s\n", opts.logfile, |
172 |
strerror(errno)); |
173 |
return ERROR_TRIVIAL; |
174 |
} |
175 |
} |
176 |
return 0; |
177 |
} |
178 |
|
179 |
|
180 |
/* |
181 |
* Close temporary debug file. |
182 |
*/ |
183 |
int |
184 |
debug_stop(void) |
185 |
{ |
186 |
if (debugfp == NULL) |
187 |
return 0; |
188 |
else |
189 |
return fclose(debugfp); |
190 |
} |
191 |
|
192 |
|
193 |
/* |
194 |
* Open the file for saving of logging information. |
195 |
*/ |
196 |
int |
197 |
log_start(void) |
198 |
{ |
199 |
if (*opts.logfile == '\0') |
200 |
return 0; /* Logging not enabled. */ |
201 |
|
202 |
debug("log file: '%s'\n", opts.logfile); |
203 |
|
204 |
if (create_file(opts.logfile, S_IRUSR | S_IWUSR)) |
205 |
return ERROR_TRIVIAL; |
206 |
|
207 |
logfp = fopen(opts.logfile, "a"); |
208 |
if (logfp == NULL) { |
209 |
error("opening log file %s: %s\n", opts.logfile, |
210 |
strerror(errno)); |
211 |
return ERROR_TRIVIAL; |
212 |
} |
213 |
return 0; |
214 |
} |
215 |
|
216 |
|
217 |
/* |
218 |
* Close the log file. |
219 |
*/ |
220 |
int |
221 |
log_stop(void) |
222 |
{ |
223 |
if (logfp == NULL) |
224 |
return 0; |
225 |
else |
226 |
return fclose(logfp); |
227 |
} |
228 |
|
229 |
|
230 |
/* |
231 |
* Prepares the log entry to be saved through continues calls, and writes it |
232 |
* to logfile. |
233 |
*/ |
234 |
void |
235 |
log_info(int flag, void *ptr) |
236 |
{ |
237 |
static struct { |
238 |
char *account; |
239 |
char *mbox; |
240 |
char *filter; |
241 |
unsigned int *action; |
242 |
char *destaccount; |
243 |
char *destmbox; |
244 |
char *hdrs; |
245 |
} inf = { |
246 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL |
247 |
}; |
248 |
|
249 |
if (logfp == NULL) |
250 |
return; |
251 |
|
252 |
switch (flag) { |
253 |
case LOG_PREAMBLE: |
254 |
fprintf(logfp, "%s %s %s %s %s%s %s\n", get_time(), |
255 |
inf.account, inf.mbox, inf.filter, |
256 |
(*inf.action == ACTION_DELETE ? "delete" : |
257 |
*inf.action == ACTION_COPY ? "copy" : |
258 |
*inf.action == ACTION_MOVE ? "move" : |
259 |
*inf.action == ACTION_RCOPY ? "rcopy " : |
260 |
*inf.action == ACTION_RMOVE ? "rmove " : |
261 |
*inf.action == ACTION_FLAG_ADD || |
262 |
*inf.action == ACTION_FLAG_REMOVE || |
263 |
*inf.action == ACTION_FLAG_REPLACE ? "flag" : |
264 |
*inf.action == ACTION_LIST ? "list" : |
265 |
"unknown "), |
266 |
(inf.destaccount == NULL ? "" : inf.destaccount), |
267 |
(*inf.destmbox == '\0' ? "" : inf.destmbox)); |
268 |
fflush(logfp); |
269 |
break; |
270 |
case LOG_ACCOUNT: |
271 |
inf.account = (char *)ptr; |
272 |
break; |
273 |
case LOG_MBOX: |
274 |
inf.mbox = (char *)ptr; |
275 |
break; |
276 |
case LOG_FILTER: |
277 |
inf.filter = (char *)ptr; |
278 |
break; |
279 |
case LOG_ACTION: |
280 |
inf.action = (unsigned int *)ptr; |
281 |
break; |
282 |
case LOG_DEST_ACCOUNT: |
283 |
if (ptr == NULL) |
284 |
inf.destaccount = NULL; |
285 |
else |
286 |
inf.destaccount = ((account_t *) ptr)->key; |
287 |
break; |
288 |
case LOG_DEST_MBOX: |
289 |
inf.destmbox = (char *)ptr; |
290 |
break; |
291 |
case LOG_HEADER: |
292 |
if (ptr) { |
293 |
inf.hdrs = (char *)ptr; |
294 |
fputc('\t', logfp); |
295 |
while (*inf.hdrs != '\0') { |
296 |
if (*inf.hdrs == '\r') { |
297 |
fputc('\n', logfp); |
298 |
if (*(inf.hdrs + 2) != '\0') |
299 |
fputc('\t', logfp); |
300 |
inf.hdrs += 2; |
301 |
} else |
302 |
fputc(*(inf.hdrs++), logfp); |
303 |
} |
304 |
} |
305 |
fflush(logfp); |
306 |
break; |
307 |
} |
308 |
} |
309 |
|
310 |
|
311 |
/* |
312 |
* Return current local time and date. |
313 |
*/ |
314 |
char * |
315 |
get_time(void) |
316 |
{ |
317 |
char *ct; |
318 |
time_t t; |
319 |
|
320 |
t = time(NULL); |
321 |
|
322 |
ct = ctime(&t); |
323 |
*(strchr(ct, '\n')) = '\0'; |
324 |
|
325 |
return ct; |
326 |
} |