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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (show annotations)
Wed Jan 30 19:19:00 2002 UTC (22 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.23: +1 -0 lines
File MIME type: text/plain
Added lockfile to prevent many imapfilters from running simultaneously.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26