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