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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (hide 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 lefcha 1.1 #include <stdio.h>
2     #include <stdlib.h>
3 lefcha 1.9 #include <unistd.h>
4 lefcha 1.1 #include <errno.h>
5     #include <string.h>
6     #include <stdarg.h>
7 lefcha 1.18 #include <signal.h>
8 lefcha 1.1 #include <limits.h>
9 lefcha 1.9 #include <sys/types.h>
10     #include <sys/stat.h>
11     #include <fcntl.h>
12 lefcha 1.12 #include <time.h>
13 lefcha 1.1
14     #include "config.h"
15     #include "imapfilter.h"
16    
17    
18 lefcha 1.5 extern char logfile[PATH_MAX];
19 lefcha 1.8 extern unsigned int options;
20 lefcha 1.6
21 lefcha 1.10 static FILE *logfp = NULL; /* Pointer to logfile. */
22 lefcha 1.1
23    
24     /*
25 lefcha 1.17 * Print message if not in OPTION_DETAILS_QUIET mode.
26 lefcha 1.7 */
27 lefcha 1.14 void info(const char *info,...)
28 lefcha 1.7 {
29     va_list args;
30 lefcha 1.14
31 lefcha 1.16 if (options & OPTION_DETAILS_QUIET)
32 lefcha 1.13 return;
33 lefcha 1.14
34 lefcha 1.12 va_start(args, info);
35 lefcha 1.13 vprintf(info, args);
36 lefcha 1.12 va_end(args);
37     }
38    
39    
40     /*
41 lefcha 1.10 * Print message if in OPTION_DETAILS_VERBOSE mode.
42 lefcha 1.1 */
43 lefcha 1.14 void verbose(const char *info,...)
44 lefcha 1.1 {
45     va_list args;
46    
47 lefcha 1.10 if (options & OPTION_DETAILS_VERBOSE) {
48 lefcha 1.1 va_start(args, info);
49     vprintf(info, args);
50     va_end(args);
51     }
52     }
53    
54 lefcha 1.17
55 lefcha 1.1 /*
56 lefcha 1.10 * Print error message and write it into logfile.
57 lefcha 1.1 */
58 lefcha 1.14 void error(const char *errmsg,...)
59 lefcha 1.1 {
60     va_list args;
61    
62     va_start(args, errmsg);
63    
64 lefcha 1.14 vfprintf(stderr, errmsg, args);
65    
66 lefcha 1.8 if (logfp) {
67 lefcha 1.1 vfprintf(logfp, errmsg, args);
68     }
69     va_end(args);
70     }
71    
72    
73     /*
74 lefcha 1.12 * Print error message and exit program.
75     */
76 lefcha 1.14 void fatal(unsigned int errnum, const char *fatal,...)
77 lefcha 1.12 {
78     va_list args;
79 lefcha 1.14
80 lefcha 1.12 va_start(args, fatal);
81     vfprintf(stderr, fatal, args);
82     va_end(args);
83 lefcha 1.14
84 lefcha 1.12 close_connection();
85 lefcha 1.22 secmem_clear();
86 lefcha 1.21 tty_restore();
87 lefcha 1.12 close_logfile();
88 lefcha 1.24 lockfile_remove();
89 lefcha 1.14
90 lefcha 1.12 exit(errnum);
91     }
92 lefcha 1.14
93 lefcha 1.18
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 lefcha 1.19 fatal(ERROR_SIGNAL, "imapfilter: killed by signal %d\n", sig);
111 lefcha 1.18 }
112 lefcha 1.12
113    
114     /*
115 lefcha 1.17 * Open the file for saving of logging information.
116 lefcha 1.1 */
117     int open_logfile(void)
118     {
119 lefcha 1.10 if (!*logfile)
120 lefcha 1.6 return 0; /* Logging not enabled. */
121 lefcha 1.4
122 lefcha 1.1 #ifdef DEBUG
123 lefcha 1.8 printf("debug: logfile: '%s'\n", logfile);
124 lefcha 1.1 #endif
125    
126 lefcha 1.21 if (create_file(logfile, S_IRUSR | S_IWUSR))
127 lefcha 1.12 return ERROR_TRIVIAL;
128 lefcha 1.9
129 lefcha 1.5 logfp = fopen(logfile, "a");
130 lefcha 1.1
131     if (!logfp) {
132 lefcha 1.12 error("imapfilter: opening logfile %s: %s\n",
133 lefcha 1.14 logfile, strerror(errno));
134 lefcha 1.12 return ERROR_TRIVIAL;
135 lefcha 1.1 }
136 lefcha 1.6 return 0;
137 lefcha 1.1 }
138    
139    
140     /*
141 lefcha 1.10 * Close the logfile.
142 lefcha 1.1 */
143     int close_logfile(void)
144     {
145 lefcha 1.8 if (!logfp)
146 lefcha 1.6 return 0;
147 lefcha 1.1 else
148     return fclose(logfp);
149     }
150    
151 lefcha 1.13
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 lefcha 1.14
170 lefcha 1.13 if (!logfp)
171     return;
172 lefcha 1.14
173     switch (flag) {
174 lefcha 1.13 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 lefcha 1.15 (!*inf.destmbox ? "" : inf.destmbox));
181 lefcha 1.14
182 lefcha 1.13 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 lefcha 1.14 break;
202 lefcha 1.13 case LOG_MAILBOX:
203     inf.mbox = (char *) ptr;
204     break;
205     case LOG_FILTER:
206     inf.filter = (char *) ptr;
207 lefcha 1.14 break;
208 lefcha 1.13 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 lefcha 1.14
217 lefcha 1.1
218 lefcha 1.2 /*
219 lefcha 1.12 * Return current local time and date.
220 lefcha 1.2 */
221 lefcha 1.12 char *get_time(void)
222 lefcha 1.1 {
223 lefcha 1.12 char *ct;
224     time_t t;
225 lefcha 1.14
226 lefcha 1.12 t = time(NULL);
227 lefcha 1.14
228 lefcha 1.12 ct = ctime(&t);
229     *(strchr(ct, '\n')) = 0;
230 lefcha 1.1
231 lefcha 1.12 return ct;
232 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26