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