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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26.2.3 - (show annotations)
Fri Mar 28 16:57:52 2003 UTC (21 years ago) by lefcha
Branch: release-0_8-patches
Changes since 1.26.2.2: +40 -26 lines
File MIME type: text/plain
Correct bug with long headers along with action list and logger.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26