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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (show annotations)
Sat Jul 13 21:19:23 2002 UTC (21 years, 8 months ago) by lefcha
Branch: MAIN
CVS Tags: release-0_8
Branch point for: release-0_8-patches
Changes since 1.25: +11 -11 lines
File MIME type: text/plain
Replace fputc()/fputs() with printf().

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 (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 *server;
162 char *username;
163 char *mbox;
164 char *filter;
165 unsigned int *action;
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_WRITE:
177 fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(),
178 inf.server, inf.username, 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 " : "list"),
182 (!*inf.destmbox ? "" : inf.destmbox));
183
184 if (ptr) {
185 inf.hdrs = (char *)ptr;
186 fprintf(logfp, "%c", '\t');
187 while (*inf.hdrs) {
188 if (*inf.hdrs == '\n') {
189 fprintf(logfp, "%c", '\n');
190 if (*(inf.hdrs + 1))
191 fprintf(logfp, "%c", '\t');
192 inf.hdrs++;
193 } else
194 fprintf(logfp, "%c", *(inf.hdrs++));
195 }
196 }
197 break;
198 case LOG_SERVER:
199 inf.server = (char *)ptr;
200 break;
201 case LOG_USERNAME:
202 inf.username = (char *)ptr;
203 break;
204 case LOG_MAILBOX:
205 inf.mbox = (char *)ptr;
206 break;
207 case LOG_FILTER:
208 inf.filter = (char *)ptr;
209 break;
210 case LOG_ACTION:
211 inf.action = (unsigned int *)ptr;
212 break;
213 case LOG_DESTINATION_MAILBOX:
214 inf.destmbox = (char *)ptr;
215 break;
216 }
217 }
218
219
220 /*
221 * Return current local time and date.
222 */
223 char *get_time(void)
224 {
225 char *ct;
226 time_t t;
227
228 t = time(NULL);
229
230 ct = ctime(&t);
231 *(strchr(ct, '\n')) = 0;
232
233 return ct;
234 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26