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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (hide annotations)
Sun Aug 3 17:40:10 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
CVS Tags: release-0_9
Branch point for: release-0_9-patches
Changes since 1.35: +3 -3 lines
File MIME type: text/plain
SIGHUP changed to SIGUSR1.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26