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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.37 - (hide annotations)
Fri Aug 8 00:18:45 2003 UTC (20 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.36: +1 -2 lines
File MIME type: text/plain
Corrected header includes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26