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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (show annotations)
Wed Jan 30 13:14:58 2002 UTC (22 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.22: +0 -1 lines
File MIME type: text/plain
Corefile restor settings not needed.

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 secmem_clear();
86 tty_restore();
87 close_logfile();
88
89 exit(errnum);
90 }
91
92
93 /*
94 * Catch signals that cause program's termination.
95 */
96 void catch_signals(void)
97 {
98 signal(SIGINT, signal_handler);
99 signal(SIGQUIT, signal_handler);
100 signal(SIGTERM, signal_handler);
101 }
102
103
104 /*
105 * Signal handler for signals that cause termination of program.
106 */
107 void signal_handler(int sig)
108 {
109 fatal(ERROR_SIGNAL, "imapfilter: killed by signal %d\n", sig);
110 }
111
112
113 /*
114 * Open the file for saving of logging information.
115 */
116 int open_logfile(void)
117 {
118 if (!*logfile)
119 return 0; /* Logging not enabled. */
120
121 #ifdef DEBUG
122 printf("debug: logfile: '%s'\n", logfile);
123 #endif
124
125 if (create_file(logfile, S_IRUSR | S_IWUSR))
126 return ERROR_TRIVIAL;
127
128 logfp = fopen(logfile, "a");
129
130 if (!logfp) {
131 error("imapfilter: opening logfile %s: %s\n",
132 logfile, strerror(errno));
133 return ERROR_TRIVIAL;
134 }
135 return 0;
136 }
137
138
139 /*
140 * Close the logfile.
141 */
142 int close_logfile(void)
143 {
144 if (!logfp)
145 return 0;
146 else
147 return fclose(logfp);
148 }
149
150
151 /*
152 * Prepares the log entry to be saved through continues calls, and writes it
153 * to logfile.
154 */
155 void log_info(int flag, void *ptr)
156 {
157 static struct {
158 char *server;
159 char *username;
160 char *mbox;
161 char *filter;
162 unsigned int *action;
163 char *destmbox;
164 char *hdrs;
165 } inf = {
166 NULL, NULL, NULL, NULL, NULL, NULL, NULL
167 };
168
169 if (!logfp)
170 return;
171
172 switch (flag) {
173 case LOG_WRITE:
174 fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(),
175 inf.server, inf.username, inf.mbox, inf.filter,
176 (*inf.action == FILTER_ACTION_DELETE ? "delete" :
177 *inf.action == FILTER_ACTION_COPY ? "copy " :
178 *inf.action == FILTER_ACTION_MOVE ? "move " : "list"),
179 (!*inf.destmbox ? "" : inf.destmbox));
180
181 if (ptr) {
182 inf.hdrs = (char *) ptr;
183 fputc('\t', logfp);
184 while (*inf.hdrs) {
185 if (*inf.hdrs == '\n') {
186 fputc('\n', logfp);
187 if (*(inf.hdrs + 1))
188 fputc('\t', logfp);
189 inf.hdrs++;
190 } else
191 fputc(*(inf.hdrs++), logfp);
192 }
193 }
194 break;
195 case LOG_SERVER:
196 inf.server = (char *) ptr;
197 break;
198 case LOG_USERNAME:
199 inf.username = (char *) ptr;
200 break;
201 case LOG_MAILBOX:
202 inf.mbox = (char *) ptr;
203 break;
204 case LOG_FILTER:
205 inf.filter = (char *) ptr;
206 break;
207 case LOG_ACTION:
208 inf.action = (unsigned int *) ptr;
209 break;
210 case LOG_DESTINATION_MAILBOX:
211 inf.destmbox = (char *) ptr;
212 break;
213 }
214 }
215
216
217 /*
218 * Return current local time and date.
219 */
220 char *get_time(void)
221 {
222 char *ct;
223 time_t t;
224
225 t = time(NULL);
226
227 ct = ctime(&t);
228 *(strchr(ct, '\n')) = 0;
229
230 return ct;
231 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26