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