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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations)
Tue Oct 2 07:33:21 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.13: +23 -27 lines
File MIME type: text/plain
A forgotten break.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26