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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations)
Thu Oct 4 15:49:52 2001 UTC (22 years, 5 months ago) by lefcha
Branch: MAIN
Changes since 1.14: +1 -2 lines
File MIME type: text/plain
Fixed a bug about logging of destmbox.

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 vfprintf(stderr, errmsg, args);
63
64 if (logfp) {
65 vfprintf(logfp, errmsg, args);
66 }
67 va_end(args);
68 }
69
70
71 /*
72 * Print error message and exit program.
73 */
74 void fatal(unsigned int errnum, const char *fatal,...)
75 {
76 va_list args;
77
78 va_start(args, fatal);
79 vfprintf(stderr, fatal, args);
80 va_end(args);
81
82 close_connection();
83 close_logfile();
84
85 exit(errnum);
86 }
87
88
89
90 /*
91 * Open the file for saving logging information.
92 */
93 int open_logfile(void)
94 {
95 if (!*logfile)
96 return 0; /* Logging not enabled. */
97
98 #ifdef DEBUG
99 printf("debug: logfile: '%s'\n", logfile);
100 #endif
101
102 if (create_logfile())
103 return ERROR_TRIVIAL;
104
105 logfp = fopen(logfile, "a");
106
107 if (!logfp) {
108 error("imapfilter: opening logfile %s: %s\n",
109 logfile, strerror(errno));
110 return ERROR_TRIVIAL;
111 }
112 return 0;
113 }
114
115
116 /*
117 * If logfile does not exist, create it with proper permissions.
118 */
119 int create_logfile(void)
120 {
121 int fd;
122 struct stat fs;
123
124 if (stat(logfile, &fs) && errno == ENOENT) {
125 fd = creat(logfile, S_IRUSR | S_IWUSR);
126 close(fd);
127 return 0;
128 } else if (!S_ISREG(fs.st_mode)) {
129 error("imapfilter: file %s already exists and not a regular file\n",
130 logfile);
131 return ERROR_TRIVIAL;
132 }
133 return 0;
134 }
135
136
137 /*
138 * Close the logfile.
139 */
140 int close_logfile(void)
141 {
142 if (!logfp)
143 return 0;
144 else
145 return fclose(logfp);
146 }
147
148
149 /*
150 * Prepares the log entry to be saved through continues calls, and writes it
151 * to logfile.
152 */
153 void log_info(int flag, void *ptr)
154 {
155 static struct {
156 char *server;
157 char *username;
158 char *mbox;
159 char *filter;
160 unsigned int *action;
161 char *destmbox;
162 char *hdrs;
163 } inf = {
164 NULL, NULL, NULL, NULL, NULL, NULL, NULL
165 };
166
167 if (!logfp)
168 return;
169
170 switch (flag) {
171 case LOG_WRITE:
172 fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(),
173 inf.server, inf.username, inf.mbox, inf.filter,
174 (*inf.action == FILTER_ACTION_DELETE ? "delete" :
175 *inf.action == FILTER_ACTION_COPY ? "copy " :
176 *inf.action == FILTER_ACTION_MOVE ? "move " : "list"),
177 (!*inf.destmbox ? "" : inf.destmbox));
178
179 if (ptr) {
180 inf.hdrs = (char *) ptr;
181 fputc('\t', logfp);
182 while (*inf.hdrs) {
183 if (*inf.hdrs == '\n') {
184 fputc('\n', logfp);
185 if (*(inf.hdrs + 1))
186 fputc('\t', logfp);
187 inf.hdrs++;
188 } else
189 fputc(*(inf.hdrs++), logfp);
190 }
191 }
192 break;
193 case LOG_SERVER:
194 inf.server = (char *) ptr;
195 break;
196 case LOG_USERNAME:
197 inf.username = (char *) ptr;
198 break;
199 case LOG_MAILBOX:
200 inf.mbox = (char *) ptr;
201 break;
202 case LOG_FILTER:
203 inf.filter = (char *) ptr;
204 break;
205 case LOG_ACTION:
206 inf.action = (unsigned int *) ptr;
207 break;
208 case LOG_DESTINATION_MAILBOX:
209 inf.destmbox = (char *) ptr;
210 break;
211 }
212 }
213
214
215 /*
216 * Return current local time and date.
217 */
218 char *get_time(void)
219 {
220 char *ct;
221 time_t t;
222
223 t = time(NULL);
224
225 ct = ctime(&t);
226 *(strchr(ct, '\n')) = 0;
227
228 return ct;
229 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26