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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations)
Fri Nov 9 18:01:29 2001 UTC (22 years, 4 months ago) by lefcha
Branch: MAIN
Changes since 1.17: +20 -0 lines
File MIME type: text/plain
Added signal handling.

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 char logfile[PATH_MAX];
19 extern unsigned int options;
20
21 static FILE *logfp = NULL; /* Pointer to logfile. */
22
23
24 /*
25 * Print message if not in OPTION_DETAILS_QUIET mode.
26 */
27 void info(const char *info,...)
28 {
29 va_list args;
30
31 if (options & OPTION_DETAILS_QUIET)
32 return;
33
34 va_start(args, info);
35 vprintf(info, args);
36 va_end(args);
37 }
38
39
40 /*
41 * Print message if in OPTION_DETAILS_VERBOSE mode.
42 */
43 void verbose(const char *info,...)
44 {
45 va_list args;
46
47 if (options & OPTION_DETAILS_VERBOSE) {
48 va_start(args, info);
49 vprintf(info, args);
50 va_end(args);
51 }
52 }
53
54
55 /*
56 * Print error message and write it into logfile.
57 */
58 void error(const char *errmsg,...)
59 {
60 va_list args;
61
62 va_start(args, errmsg);
63
64 vfprintf(stderr, errmsg, args);
65
66 if (logfp) {
67 vfprintf(logfp, errmsg, args);
68 }
69 va_end(args);
70 }
71
72
73 /*
74 * Print error message and exit program.
75 */
76 void fatal(unsigned int errnum, const char *fatal,...)
77 {
78 va_list args;
79
80 va_start(args, fatal);
81 vfprintf(stderr, fatal, args);
82 va_end(args);
83
84 close_connection();
85 close_logfile();
86
87 exit(errnum);
88 }
89
90
91 /*
92 * Catch signals that cause program's termination.
93 */
94 void catch_signals(void)
95 {
96 signal(SIGINT, signal_handler);
97 signal(SIGQUIT, signal_handler);
98 signal(SIGTERM, signal_handler);
99 }
100
101
102 /*
103 * Signal handler for signals that cause termination of program.
104 */
105 void signal_handler(int sig)
106 {
107 fatal(ERROR_SIGNAL, "imapfilter: killed by signal %d\n", sig);
108 }
109
110
111 /*
112 * Open the file for saving of logging information.
113 */
114 int open_logfile(void)
115 {
116 if (!*logfile)
117 return 0; /* Logging not enabled. */
118
119 #ifdef DEBUG
120 printf("debug: logfile: '%s'\n", logfile);
121 #endif
122
123 if (create_logfile())
124 return ERROR_TRIVIAL;
125
126 logfp = fopen(logfile, "a");
127
128 if (!logfp) {
129 error("imapfilter: opening logfile %s: %s\n",
130 logfile, strerror(errno));
131 return ERROR_TRIVIAL;
132 }
133 return 0;
134 }
135
136
137 /*
138 * If logfile does not exist, create it with proper permissions.
139 */
140 int create_logfile(void)
141 {
142 int fd;
143 struct stat fs;
144
145 if (stat(logfile, &fs) && errno == ENOENT) {
146 fd = creat(logfile, S_IRUSR | S_IWUSR);
147 close(fd);
148 return 0;
149 } else if (!S_ISREG(fs.st_mode)) {
150 error("imapfilter: file %s already exists and not a regular file\n",
151 logfile);
152 return ERROR_TRIVIAL;
153 }
154 return 0;
155 }
156
157
158 /*
159 * Close the logfile.
160 */
161 int close_logfile(void)
162 {
163 if (!logfp)
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 log_info(int flag, void *ptr)
175 {
176 static struct {
177 char *server;
178 char *username;
179 char *mbox;
180 char *filter;
181 unsigned int *action;
182 char *destmbox;
183 char *hdrs;
184 } inf = {
185 NULL, NULL, NULL, NULL, NULL, NULL, NULL
186 };
187
188 if (!logfp)
189 return;
190
191 switch (flag) {
192 case LOG_WRITE:
193 fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(),
194 inf.server, inf.username, 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 " : "list"),
198 (!*inf.destmbox ? "" : inf.destmbox));
199
200 if (ptr) {
201 inf.hdrs = (char *) ptr;
202 fputc('\t', logfp);
203 while (*inf.hdrs) {
204 if (*inf.hdrs == '\n') {
205 fputc('\n', logfp);
206 if (*(inf.hdrs + 1))
207 fputc('\t', logfp);
208 inf.hdrs++;
209 } else
210 fputc(*(inf.hdrs++), logfp);
211 }
212 }
213 break;
214 case LOG_SERVER:
215 inf.server = (char *) ptr;
216 break;
217 case LOG_USERNAME:
218 inf.username = (char *) ptr;
219 break;
220 case LOG_MAILBOX:
221 inf.mbox = (char *) ptr;
222 break;
223 case LOG_FILTER:
224 inf.filter = (char *) ptr;
225 break;
226 case LOG_ACTION:
227 inf.action = (unsigned int *) ptr;
228 break;
229 case LOG_DESTINATION_MAILBOX:
230 inf.destmbox = (char *) ptr;
231 break;
232 }
233 }
234
235
236 /*
237 * Return current local time and date.
238 */
239 char *get_time(void)
240 {
241 char *ct;
242 time_t t;
243
244 t = time(NULL);
245
246 ct = ctime(&t);
247 *(strchr(ct, '\n')) = 0;
248
249 return ct;
250 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26