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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (show annotations)
Thu Jul 31 15:53:19 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.33: +6 -5 lines
File MIME type: text/plain
Broke up program files and created some new header files.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26