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 <signal.h> |
8 |
#include <sys/types.h> |
9 |
#include <sys/stat.h> |
10 |
#include <fcntl.h> |
11 |
#include <time.h> |
12 |
|
13 |
#include "config.h" |
14 |
#include "imapfilter.h" |
15 |
|
16 |
|
17 |
extern conn_t connpri, connaux; |
18 |
extern char logfile[PATH_MAX]; |
19 |
extern unsigned int options; |
20 |
extern unsigned int flags; |
21 |
|
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 OPTION_DETAILS_QUIET mode. |
31 |
*/ |
32 |
void |
33 |
info(const char *info,...) |
34 |
{ |
35 |
va_list args; |
36 |
|
37 |
if (options & OPTION_DETAILS_QUIET) |
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 OPTION_DETAILS_VERBOSE mode. |
48 |
*/ |
49 |
void |
50 |
verbose(const char *info,...) |
51 |
{ |
52 |
va_list args; |
53 |
|
54 |
if (options & OPTION_DETAILS_VERBOSE) { |
55 |
va_start(args, info); |
56 |
vprintf(info, args); |
57 |
va_end(args); |
58 |
} |
59 |
} |
60 |
|
61 |
|
62 |
/* |
63 |
* Print error message and write it into logfile. |
64 |
*/ |
65 |
void |
66 |
error(const char *errmsg,...) |
67 |
{ |
68 |
va_list args; |
69 |
|
70 |
va_start(args, errmsg); |
71 |
|
72 |
fprintf(stderr, "imapfilter: "); |
73 |
vfprintf(stderr, errmsg, args); |
74 |
|
75 |
if ((options & OPTION_ERRORS) && logfp) { |
76 |
vfprintf(logfp, errmsg, args); |
77 |
fflush(logfp); |
78 |
} |
79 |
va_end(args); |
80 |
} |
81 |
|
82 |
|
83 |
/* |
84 |
* Print error message and exit program. |
85 |
*/ |
86 |
void |
87 |
fatal(unsigned int errnum, const char *fatal,...) |
88 |
{ |
89 |
va_list args; |
90 |
|
91 |
va_start(args, fatal); |
92 |
fprintf(stderr, "imapfilter: "); |
93 |
vfprintf(stderr, fatal, args); |
94 |
va_end(args); |
95 |
|
96 |
close_connection(&connpri); |
97 |
close_connection(&connaux); |
98 |
secmem_clear(); |
99 |
tty_restore(); |
100 |
close_logfile(); |
101 |
lockfile_remove(); |
102 |
|
103 |
exit(errnum); |
104 |
} |
105 |
|
106 |
|
107 |
/* |
108 |
* Catch signals that cause program's termination. |
109 |
*/ |
110 |
void |
111 |
catch_signals(void) |
112 |
{ |
113 |
signal(SIGUSR1, signal_handler); |
114 |
signal(SIGINT, signal_handler); |
115 |
signal(SIGQUIT, signal_handler); |
116 |
signal(SIGTERM, signal_handler); |
117 |
} |
118 |
|
119 |
|
120 |
/* |
121 |
* Signal handler for signals that cause termination of program. |
122 |
*/ |
123 |
void |
124 |
signal_handler(int sig) |
125 |
{ |
126 |
if (sig == SIGUSR1) { |
127 |
flags |= FLAG_SIGUSR1_RECEIVED; |
128 |
} else |
129 |
fatal(ERROR_SIGNAL, "killed by signal %d\n", sig); |
130 |
} |
131 |
|
132 |
|
133 |
/* |
134 |
* Open the file for saving of logging information. |
135 |
*/ |
136 |
int |
137 |
open_logfile(void) |
138 |
{ |
139 |
if (*logfile == '\0') |
140 |
return 0; /* Logging not enabled. */ |
141 |
|
142 |
#ifdef DEBUG |
143 |
fprintf(stderr, "debug: logfile: '%s'\n", logfile); |
144 |
#endif |
145 |
|
146 |
if (create_file(logfile, S_IRUSR | S_IWUSR)) |
147 |
return ERROR_TRIVIAL; |
148 |
|
149 |
logfp = fopen(logfile, "a"); |
150 |
if (logfp == NULL) { |
151 |
error("opening logfile %s: %s\n", logfile, strerror(errno)); |
152 |
return ERROR_TRIVIAL; |
153 |
} |
154 |
return 0; |
155 |
} |
156 |
|
157 |
|
158 |
/* |
159 |
* Close the logfile. |
160 |
*/ |
161 |
int |
162 |
close_logfile(void) |
163 |
{ |
164 |
if (logfp == NULL) |
165 |
return 0; |
166 |
else |
167 |
return fclose(logfp); |
168 |
} |
169 |
|
170 |
|
171 |
/* |
172 |
* Prepares the log entry to be saved through continues calls, and writes it |
173 |
* to logfile. |
174 |
*/ |
175 |
void |
176 |
log_info(int flag, void *ptr) |
177 |
{ |
178 |
static struct { |
179 |
char *account; |
180 |
char *mbox; |
181 |
char *filter; |
182 |
unsigned int *action; |
183 |
char *destaccount; |
184 |
char *destmbox; |
185 |
char *hdrs; |
186 |
} inf = { |
187 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL |
188 |
}; |
189 |
|
190 |
if (logfp == NULL) |
191 |
return; |
192 |
|
193 |
switch (flag) { |
194 |
case LOG_PREAMBLE: |
195 |
fprintf(logfp, "%s %s %s %s %s%s %s\n", get_time(), |
196 |
inf.account, inf.mbox, inf.filter, |
197 |
(*inf.action == FILTER_ACTION_DELETE ? "delete" : |
198 |
*inf.action == FILTER_ACTION_COPY ? "copy" : |
199 |
*inf.action == FILTER_ACTION_MOVE ? "move" : |
200 |
*inf.action == FILTER_ACTION_RCOPY ? "rcopy " : |
201 |
*inf.action == FILTER_ACTION_RMOVE ? "rmove " : |
202 |
*inf.action == FILTER_ACTION_FLAG_ADD || |
203 |
*inf.action == FILTER_ACTION_FLAG_REMOVE || |
204 |
*inf.action == FILTER_ACTION_FLAG_REPLACE ? "flag" : |
205 |
*inf.action == FILTER_ACTION_LIST ? "list" : |
206 |
"unknown "), |
207 |
(inf.destaccount == NULL ? "" : inf.destaccount), |
208 |
(*inf.destmbox == '\0' ? "" : inf.destmbox)); |
209 |
fflush(logfp); |
210 |
break; |
211 |
case LOG_ACCOUNT: |
212 |
inf.account = (char *)ptr; |
213 |
break; |
214 |
case LOG_MAILBOX: |
215 |
inf.mbox = (char *)ptr; |
216 |
break; |
217 |
case LOG_FILTER: |
218 |
inf.filter = (char *)ptr; |
219 |
break; |
220 |
case LOG_ACTION: |
221 |
inf.action = (unsigned int *)ptr; |
222 |
break; |
223 |
case LOG_DESTINATION_ACCOUNT: |
224 |
if (ptr == NULL) |
225 |
inf.destaccount = NULL; |
226 |
else |
227 |
inf.destaccount = ((account_t *) ptr)->key; |
228 |
break; |
229 |
case LOG_DESTINATION_MAILBOX: |
230 |
inf.destmbox = (char *)ptr; |
231 |
break; |
232 |
case LOG_HEADER: |
233 |
if (ptr) { |
234 |
inf.hdrs = (char *)ptr; |
235 |
fputc('\t', logfp); |
236 |
while (*inf.hdrs != '\0') { |
237 |
if (*inf.hdrs == '\r') { |
238 |
fputc('\n', logfp); |
239 |
if (*(inf.hdrs + 2) != '\0') |
240 |
fputc('\t', logfp); |
241 |
inf.hdrs += 2; |
242 |
} else |
243 |
fputc(*(inf.hdrs++), logfp); |
244 |
} |
245 |
} |
246 |
fflush(logfp); |
247 |
break; |
248 |
} |
249 |
} |
250 |
|
251 |
|
252 |
/* |
253 |
* Return current local time and date. |
254 |
*/ |
255 |
char * |
256 |
get_time(void) |
257 |
{ |
258 |
char *ct; |
259 |
time_t t; |
260 |
|
261 |
t = time(NULL); |
262 |
|
263 |
ct = ctime(&t); |
264 |
*(strchr(ct, '\n')) = '\0'; |
265 |
|
266 |
return ct; |
267 |
} |