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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations)
Mon Oct 1 11:48:51 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.12: +70 -64 lines
File MIME type: text/plain
Added some log records.

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 <limits.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <time.h>
12
13 #include "config.h"
14 #include "imapfilter.h"
15
16
17 extern char logfile[PATH_MAX];
18 extern unsigned int options;
19
20 static FILE *logfp = NULL; /* Pointer to logfile. */
21
22
23 /*
24 * Print message and/or write it into logfile.
25 */
26 void info(const char *info, ...)
27 {
28 va_list args;
29
30 if (options & OPTION_DETAILS_QUITE)
31 return;
32
33 va_start(args, info);
34 vprintf(info, args);
35 va_end(args);
36 }
37
38
39 /*
40 * Print message if in OPTION_DETAILS_VERBOSE mode.
41 */
42 void verbose(const char *info, ...)
43 {
44 va_list args;
45
46 if (options & OPTION_DETAILS_VERBOSE) {
47 va_start(args, info);
48 vprintf(info, args);
49 va_end(args);
50 }
51 }
52
53 /*
54 * Print error message and write it into logfile.
55 */
56 void error(const char *errmsg, ...)
57 {
58 va_list args;
59
60 va_start(args, errmsg);
61
62 if (logfp) {
63 vfprintf(logfp, errmsg, args);
64 }
65
66 vfprintf(stderr, errmsg, args);
67
68 va_end(args);
69 }
70
71
72 /*
73 * Print error message and exit program.
74 */
75 void fatal(unsigned int errnum, const char *fatal, ...)
76 {
77 va_list args;
78
79 va_start(args, fatal);
80
81 vfprintf(stderr, fatal, args);
82
83 va_end(args);
84
85 close_connection();
86 close_logfile();
87
88 exit(errnum);
89 }
90
91
92
93 /*
94 * Open the file for saving logging information.
95 */
96 int open_logfile(void)
97 {
98 if (!*logfile)
99 return 0; /* Logging not enabled. */
100
101 #ifdef DEBUG
102 printf("debug: logfile: '%s'\n", logfile);
103 #endif
104
105 if (create_logfile())
106 return ERROR_TRIVIAL;
107
108 logfp = fopen(logfile, "a");
109
110 if (!logfp) {
111 error("imapfilter: opening logfile %s: %s\n",
112 logfile, strerror(errno));
113 return ERROR_TRIVIAL;
114 }
115
116 return 0;
117 }
118
119
120 /*
121 * If logfile does not exist, create it with proper permissions.
122 */
123 int create_logfile(void)
124 {
125 int fd;
126 struct stat fs;
127
128 if (stat(logfile, &fs) && errno == ENOENT) {
129 fd = creat(logfile, S_IRUSR | S_IWUSR);
130 close(fd);
131 return 0;
132 } else if (!S_ISREG(fs.st_mode)) {
133 error("imapfilter: file %s already exists and not a regular file\n",
134 logfile);
135 return ERROR_TRIVIAL;
136 }
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 fputc('\t', logfp);
187 while (*inf.hdrs) {
188 if (*inf.hdrs == '\n') {
189 fputc('\n', logfp);
190 if (*(inf.hdrs + 1))
191 fputc('\t', logfp);
192 inf.hdrs++;
193 } else
194 fputc(*(inf.hdrs++), logfp);
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 case LOG_MAILBOX:
204 inf.mbox = (char *) ptr;
205 break;
206 case LOG_FILTER:
207 inf.filter = (char *) ptr;
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 default:
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