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