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 |
|
|
#include <limits.h> |
8 |
lefcha |
1.9 |
#include <sys/types.h> |
9 |
|
|
#include <sys/stat.h> |
10 |
|
|
#include <fcntl.h> |
11 |
lefcha |
1.12 |
#include <time.h> |
12 |
lefcha |
1.1 |
|
13 |
|
|
#include "config.h" |
14 |
|
|
#include "imapfilter.h" |
15 |
|
|
|
16 |
|
|
|
17 |
lefcha |
1.5 |
extern char logfile[PATH_MAX]; |
18 |
lefcha |
1.8 |
extern unsigned int options; |
19 |
lefcha |
1.6 |
|
20 |
lefcha |
1.10 |
static FILE *logfp = NULL; /* Pointer to logfile. */ |
21 |
lefcha |
1.1 |
|
22 |
|
|
|
23 |
|
|
/* |
24 |
lefcha |
1.12 |
* Print message and/or write it into logfile. |
25 |
lefcha |
1.7 |
*/ |
26 |
|
|
void info(const char *info, ...) |
27 |
|
|
{ |
28 |
|
|
va_list args; |
29 |
lefcha |
1.12 |
|
30 |
|
|
va_start(args, info); |
31 |
lefcha |
1.8 |
|
32 |
lefcha |
1.12 |
if (!(options & OPTION_DETAILS_QUITE)) |
33 |
lefcha |
1.7 |
vprintf(info, args); |
34 |
lefcha |
1.12 |
|
35 |
|
|
va_end(args); |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
/* |
40 |
|
|
* |
41 |
|
|
*/ |
42 |
|
|
void log_info(int flag, void *ptr) |
43 |
|
|
{ |
44 |
|
|
static struct { |
45 |
|
|
char *server; |
46 |
|
|
char *mbox; |
47 |
|
|
unsigned int *action; |
48 |
|
|
char *destmbox; |
49 |
|
|
char *hdrs; |
50 |
|
|
} inf = { |
51 |
|
|
NULL, NULL, NULL, NULL, NULL |
52 |
|
|
}; |
53 |
|
|
|
54 |
|
|
if (!logfp) |
55 |
|
|
return; |
56 |
|
|
|
57 |
|
|
switch(flag) { |
58 |
|
|
case 0: |
59 |
|
|
fprintf(logfp, "%s %s %s %s%s\n", get_time(), inf.server, inf.mbox, |
60 |
|
|
(*inf.action == FILTER_ACTION_DELETE ? "delete" : |
61 |
|
|
*inf.action == FILTER_ACTION_COPY ? "copy " : |
62 |
|
|
*inf.action == FILTER_ACTION_MOVE ? "move " : "list"), |
63 |
|
|
(!inf.destmbox ? "" : inf.destmbox)); |
64 |
|
|
|
65 |
|
|
if (ptr) { |
66 |
|
|
inf.hdrs = (char *) ptr; |
67 |
|
|
fputc('\t', logfp); |
68 |
|
|
while (*inf.hdrs) { |
69 |
|
|
if (*inf.hdrs == '\n') { |
70 |
|
|
fputc('\n', logfp); |
71 |
|
|
if (*(inf.hdrs + 1)) |
72 |
|
|
fputc('\t', logfp); |
73 |
|
|
inf.hdrs++; |
74 |
|
|
} else |
75 |
|
|
fputc(*(inf.hdrs++), logfp); |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
break; |
79 |
|
|
case 1: |
80 |
|
|
inf.server = (char *) ptr; |
81 |
|
|
break; |
82 |
|
|
case 2: |
83 |
|
|
inf.mbox = (char *) ptr; |
84 |
|
|
break; |
85 |
|
|
case 3: |
86 |
|
|
inf.action = (unsigned int *) ptr; |
87 |
|
|
break; |
88 |
|
|
case 4: |
89 |
|
|
inf.destmbox = (char *) ptr; |
90 |
|
|
break; |
91 |
|
|
default: |
92 |
|
|
break; |
93 |
lefcha |
1.7 |
} |
94 |
|
|
} |
95 |
lefcha |
1.12 |
|
96 |
|
|
|
97 |
lefcha |
1.7 |
|
98 |
|
|
|
99 |
|
|
/* |
100 |
lefcha |
1.10 |
* Print message if in OPTION_DETAILS_VERBOSE mode. |
101 |
lefcha |
1.1 |
*/ |
102 |
|
|
void verbose(const char *info, ...) |
103 |
|
|
{ |
104 |
|
|
va_list args; |
105 |
|
|
|
106 |
lefcha |
1.10 |
if (options & OPTION_DETAILS_VERBOSE) { |
107 |
lefcha |
1.1 |
va_start(args, info); |
108 |
|
|
vprintf(info, args); |
109 |
|
|
va_end(args); |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/* |
114 |
lefcha |
1.10 |
* Print error message and write it into logfile. |
115 |
lefcha |
1.1 |
*/ |
116 |
|
|
void error(const char *errmsg, ...) |
117 |
|
|
{ |
118 |
|
|
va_list args; |
119 |
|
|
|
120 |
|
|
va_start(args, errmsg); |
121 |
|
|
|
122 |
lefcha |
1.8 |
if (logfp) { |
123 |
lefcha |
1.1 |
vfprintf(logfp, errmsg, args); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
vfprintf(stderr, errmsg, args); |
127 |
lefcha |
1.10 |
|
128 |
lefcha |
1.1 |
va_end(args); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
/* |
133 |
lefcha |
1.12 |
* Print error message and exit program. |
134 |
|
|
*/ |
135 |
|
|
void fatal(unsigned int errnum, const char *fatal, ...) |
136 |
|
|
{ |
137 |
|
|
va_list args; |
138 |
|
|
|
139 |
|
|
va_start(args, fatal); |
140 |
|
|
|
141 |
|
|
vfprintf(stderr, fatal, args); |
142 |
|
|
|
143 |
|
|
va_end(args); |
144 |
|
|
|
145 |
|
|
close_connection(); |
146 |
|
|
close_logfile(); |
147 |
|
|
|
148 |
|
|
exit(errnum); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
/* |
154 |
|
|
* Open the file for saving logging information. |
155 |
lefcha |
1.1 |
*/ |
156 |
|
|
int open_logfile(void) |
157 |
|
|
{ |
158 |
lefcha |
1.10 |
if (!*logfile) |
159 |
lefcha |
1.6 |
return 0; /* Logging not enabled. */ |
160 |
lefcha |
1.4 |
|
161 |
lefcha |
1.1 |
#ifdef DEBUG |
162 |
lefcha |
1.8 |
printf("debug: logfile: '%s'\n", logfile); |
163 |
lefcha |
1.1 |
#endif |
164 |
|
|
|
165 |
lefcha |
1.9 |
if (create_logfile()) |
166 |
lefcha |
1.12 |
return ERROR_TRIVIAL; |
167 |
lefcha |
1.9 |
|
168 |
lefcha |
1.5 |
logfp = fopen(logfile, "a"); |
169 |
lefcha |
1.1 |
|
170 |
|
|
if (!logfp) { |
171 |
lefcha |
1.12 |
error("imapfilter: opening logfile %s: %s\n", |
172 |
lefcha |
1.5 |
logfile, strerror(errno)); |
173 |
lefcha |
1.12 |
return ERROR_TRIVIAL; |
174 |
lefcha |
1.9 |
} |
175 |
|
|
|
176 |
|
|
return 0; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
|
180 |
|
|
/* |
181 |
lefcha |
1.12 |
* If logfile does not exist, create it with proper permissions. |
182 |
lefcha |
1.9 |
*/ |
183 |
|
|
int create_logfile(void) |
184 |
|
|
{ |
185 |
|
|
int fd; |
186 |
|
|
struct stat fs; |
187 |
|
|
|
188 |
|
|
if (stat(logfile, &fs) && errno == ENOENT) { |
189 |
|
|
fd = creat(logfile, S_IRUSR | S_IWUSR); |
190 |
|
|
close(fd); |
191 |
|
|
return 0; |
192 |
|
|
} else if (!S_ISREG(fs.st_mode)) { |
193 |
lefcha |
1.12 |
error("imapfilter: file %s already exists and not a regular file\n", |
194 |
lefcha |
1.9 |
logfile); |
195 |
lefcha |
1.12 |
return ERROR_TRIVIAL; |
196 |
lefcha |
1.1 |
} |
197 |
|
|
|
198 |
lefcha |
1.6 |
return 0; |
199 |
lefcha |
1.1 |
} |
200 |
|
|
|
201 |
|
|
|
202 |
|
|
/* |
203 |
lefcha |
1.10 |
* Close the logfile. |
204 |
lefcha |
1.1 |
*/ |
205 |
|
|
int close_logfile(void) |
206 |
|
|
{ |
207 |
lefcha |
1.8 |
if (!logfp) |
208 |
lefcha |
1.6 |
return 0; |
209 |
lefcha |
1.1 |
else |
210 |
|
|
return fclose(logfp); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
|
214 |
lefcha |
1.2 |
/* |
215 |
lefcha |
1.12 |
* Return current local time and date. |
216 |
lefcha |
1.2 |
*/ |
217 |
lefcha |
1.12 |
char *get_time(void) |
218 |
lefcha |
1.1 |
{ |
219 |
lefcha |
1.12 |
char *ct; |
220 |
|
|
time_t t; |
221 |
|
|
|
222 |
|
|
t = time(NULL); |
223 |
|
|
|
224 |
|
|
ct = ctime(&t); |
225 |
|
|
*(strchr(ct, '\n')) = 0; |
226 |
lefcha |
1.1 |
|
227 |
lefcha |
1.12 |
return ct; |
228 |
lefcha |
1.1 |
} |