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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.37 - (show annotations)
Fri Aug 8 00:18:45 2003 UTC (20 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.36: +1 -2 lines
File MIME type: text/plain
Corrected header includes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26