8 |
#include <sys/types.h> |
#include <sys/types.h> |
9 |
#include <sys/stat.h> |
#include <sys/stat.h> |
10 |
#include <fcntl.h> |
#include <fcntl.h> |
11 |
|
#include <time.h> |
12 |
|
|
13 |
#include "config.h" |
#include "config.h" |
14 |
#include "imapfilter.h" |
#include "imapfilter.h" |
21 |
|
|
22 |
|
|
23 |
/* |
/* |
24 |
* Print message if not in OPTION_DETAILS_QUITE mode. |
* Print message and/or write it into logfile. |
25 |
*/ |
*/ |
26 |
void info(const char *info, ...) |
void info(const char *info, ...) |
27 |
{ |
{ |
28 |
va_list args; |
va_list args; |
29 |
|
|
30 |
|
va_start(args, info); |
31 |
|
|
32 |
if (!(options & OPTION_DETAILS_QUITE)) { |
if (!(options & OPTION_DETAILS_QUITE)) |
|
va_start(args, info); |
|
33 |
vprintf(info, args); |
vprintf(info, args); |
34 |
va_end(args); |
|
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 |
} |
} |
94 |
} |
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
/* |
/* |
130 |
|
|
131 |
|
|
132 |
/* |
/* |
133 |
* Open the file to save logging information. |
* 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 |
*/ |
*/ |
156 |
int open_logfile(void) |
int open_logfile(void) |
157 |
{ |
{ |
163 |
#endif |
#endif |
164 |
|
|
165 |
if (create_logfile()) |
if (create_logfile()) |
166 |
return 1; |
return ERROR_TRIVIAL; |
167 |
|
|
168 |
logfp = fopen(logfile, "a"); |
logfp = fopen(logfile, "a"); |
169 |
|
|
170 |
if (!logfp) { |
if (!logfp) { |
171 |
fprintf(stderr, "imapfilter: opening logfile %s: %s\n", |
error("imapfilter: opening logfile %s: %s\n", |
172 |
logfile, strerror(errno)); |
logfile, strerror(errno)); |
173 |
return 1; |
return ERROR_TRIVIAL; |
174 |
} |
} |
175 |
|
|
176 |
return 0; |
return 0; |
178 |
|
|
179 |
|
|
180 |
/* |
/* |
181 |
* If file does not exist, create it with proper permissions. |
* If logfile does not exist, create it with proper permissions. |
182 |
*/ |
*/ |
183 |
int create_logfile(void) |
int create_logfile(void) |
184 |
{ |
{ |
190 |
close(fd); |
close(fd); |
191 |
return 0; |
return 0; |
192 |
} else if (!S_ISREG(fs.st_mode)) { |
} else if (!S_ISREG(fs.st_mode)) { |
193 |
printf |
error("imapfilter: file %s already exists and not a regular file\n", |
|
("imapfilter: file %s already exists and not a regular file\n", |
|
194 |
logfile); |
logfile); |
195 |
return 1; |
return ERROR_TRIVIAL; |
196 |
} |
} |
197 |
|
|
198 |
return 0; |
return 0; |
212 |
|
|
213 |
|
|
214 |
/* |
/* |
215 |
* Write information to logfile. |
* Return current local time and date. |
216 |
*/ |
*/ |
217 |
void log_info(const char *info, ...) |
char *get_time(void) |
218 |
{ |
{ |
219 |
va_list args; |
char *ct; |
220 |
|
time_t t; |
221 |
|
|
222 |
|
t = time(NULL); |
223 |
|
|
224 |
|
ct = ctime(&t); |
225 |
|
*(strchr(ct, '\n')) = 0; |
226 |
|
|
227 |
va_start(args, info); |
return ct; |
|
|
|
|
if (logfp) { |
|
|
vfprintf(logfp, info, args); |
|
|
} |
|
|
|
|
|
vfprintf(stdout, info, args); |
|
|
|
|
|
va_end(args); |
|
228 |
} |
} |