/[imapfilter]/imapfilter/log.c
ViewVC logotype

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.31 - (show annotations)
Fri Mar 28 17:01:29 2003 UTC (21 years ago) by lefcha
Branch: MAIN
Changes since 1.30: +17 -15 lines
File MIME type: text/plain
Corrected bug with long headers and action list.

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_PREAMBLE:
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 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 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 break;
240 }
241 }
242
243
244 /*
245 * Return current local time and date.
246 */
247 char *
248 get_time(void)
249 {
250 char *ct;
251 time_t t;
252
253 t = time(NULL);
254
255 ct = ctime(&t);
256 *(strchr(ct, '\n')) = '\0';
257
258 return ct;
259 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26