File: maildir-filter.c

package info (click to toggle)
maildir-filter 1.20-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 48 kB
  • ctags: 30
  • sloc: ansic: 217; makefile: 62
file content (245 lines) | stat: -rw-r--r-- 5,431 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pcre.h>
#include <errno.h>
#include <string.h>
#include <sys/utsname.h>
#include <time.h>
#include <libintl.h>

#define _(X) gettext(X)

#define BOUNCE		100
#define DEFERAL		111
#define STOP_PROCESSING	99
#define DONE_OK		EXIT_SUCCESS

#ifdef DEBUG
#define D(X) (X)
#else
#define D(X)
#endif

#define VERSION "$Id: maildir-filter.c,v 1.2 2004/07/14 04:25:07 mosca Exp $"

char **message=NULL;
char *directory=NULL;
int lines=0;
int kill=0;
int echo=0;
int debug=0;
int bounce=0;
int reg_opt=0;
int negated=0;
int headers_only=0;
pcre *regexp;
pcre_extra *reg_info;

void usage(int argc,char **argv);
void do_bounce();
void do_kill();
void do_deliver(const char *directory, unsigned long msg_len);
void version(int argc,char **argv);

int main(int argc,char **argv)
{
	int c;
	char *pattern=NULL;
	const char *err_msg=NULL;
	int err_offset;
	int matched=0;
	unsigned long msg_len=0;
	int in_headers=1;

	textdomain("maildir-filter");

	while((c=getopt(argc,argv,"r:kd:pDibVnH"))>0) {
		switch(c) {
			case 'r':
				pattern=optarg;
				break;
			case 'd':
				directory=optarg;
				break;
			case 'p':
				echo=1;
				break;
			case 'V':
				version(argc,argv);
				break;
			case 'D':
				debug=1;
				break;
			case 'i':
				reg_opt|=PCRE_CASELESS;
				break;
			case 'b':
				bounce=1;
				break;
			case 'n':
				negated=1;
				break;
			case 'H':
				headers_only=1;
				break;
			case 'k':
				kill = 1;
				break;
			default:
			case ':':
			case '?':
				if(c != ':' && c != '?')
					fprintf(stderr,_("Unkown option: %c\n"),c);
				usage(argc,argv);
				break;
		}
	}
	D(fprintf(stderr,"We are running..."));
	if(!pattern || (!directory && !bounce)) {
		D(fprintf(stderr,"pattern: %s\ndirectory: %s\nbounce: %i\n",pattern,directory,bounce));
		usage(argc,argv);
	}
		
    char line[BUFSIZ];
	for(lines=0;fgets(line,BUFSIZ,stdin);lines++) {
		msg_len += strlen(line);
        message = realloc(message,sizeof(void*) * lines+1);
        if (!message) {
          fprintf(stderr,_("%s: OOM: %s\n"),argv[0],strerror(errno));
          exit(DEFERAL);
        } else {
          message[lines] = strdup(line);
          if (!message[lines]) {
            fprintf(stderr,_("%s: OOM: %s\n"),argv[0],strerror(errno));
            exit(DEFERAL);
          }
        }
	}

	if(!message) {
		fprintf(stderr,_("%s: OOM: %s\n"),argv[0],strerror(errno));
		exit(DEFERAL);
	}

	regexp=pcre_compile((const char *)pattern,reg_opt,&err_msg,&err_offset,NULL);
	if(!regexp) {
		fprintf(stderr,_("Error compiling regexp: '%s' at character %i\n"),pattern,err_offset);
		fprintf(stderr,"%s\n",err_msg);
		exit(DEFERAL);
	} else {
		reg_info=pcre_study(regexp,0,&err_msg);
	}

	for(c=0;c<lines;c++) {
		int ret;

		if(echo) {
			fwrite(message[c],sizeof(char),strlen(message[c]),stdout);
		}

		if(in_headers && (message[c][0]=='\r' || message[c][0]=='\n')) {
			in_headers=0;
			if(debug) {
				fprintf(stderr,"headers end: %i\n",c+1);
			}
		}

		if(!matched) {
			if(!headers_only || (headers_only && in_headers)) {
				ret=pcre_exec(regexp,(const pcre_extra *)reg_info,message[c],strlen(message[c]),0,0 /* opt */,NULL,0);
				if(ret>=0) {
					fprintf(stderr, "%s : %s %i,", VERSION, _("we got a match at line"),c+1);
					if(bounce)
						fprintf(stderr,_(" bouncing\n"));
					else 
						fprintf(stderr, _(" delivering to %s. %lu bytes\n"), directory, msg_len);

					matched=1;
					if(!echo)
						break;
				} else if(ret<-1) {
					fprintf(stderr,_("Unexpect error matching string: %i\n"),ret);
					exit(DEFERAL);
				}
			}
		}
	}

	if(negated)
		matched = matched?0:1;

	if(matched) {
		if(bounce)
			do_bounce();
		else if(kill)
			do_kill();
		else
			do_deliver((const char *)directory, msg_len);
	}
	exit(DONE_OK);
}

void version(int argc,char **argv)
{
	fprintf(stderr,"%s\n", VERSION);
	fprintf(stderr, " by Marcelo Bezerra <mosca@mosca.yi.org>\n");
	exit(EXIT_SUCCESS);
}

void usage(int argc,char **argv)
{
	fprintf(stderr, _("Incorrect usage\n"));
	fprintf(stderr, _("%s -r <regexp> [-d ./Maildir/.folder | -b] [-D] [-p] [-i] [-V] [-n] [-H]\n"),argv[0]);
	fprintf(stderr, _("\t-b\tbounce if matches\n"));
	fprintf(stderr, _("\t-D\tturn on a few debug messages\n"));
	fprintf(stderr, _("\t-p\tprint message to stdout\n"));
	fprintf(stderr, _("\t-i\tcase insensitive match (m//i)\n"));
	fprintf(stderr, _("\t-V\tprint version and exit\n"));
	fprintf(stderr, _("\t-n\tnegate regexp. (!~ m//)\n"));
	fprintf(stderr, _("\t-k\tkill message (delete)\n"));
	fprintf(stderr, _("\t-H\tmatch headers only\n"));
	exit(DEFERAL);
}


void do_kill()
{
	exit(STOP_PROCESSING);
}

void do_bounce()
{
	exit(BOUNCE);
}

void do_deliver(const char *directory, unsigned long msg_len)
{
	char fname[BUFSIZ];
	char tmpname[BUFSIZ];
	struct utsname uts;
	int c;
	FILE *fp;
	time_t now;

	now=time(NULL);
	uname(&uts);
	snprintf(tmpname, BUFSIZ-1, "%s/tmp/%lu.%u.%s,S=%lu", directory, now, getpid(), uts.nodename, msg_len);
	snprintf(fname, BUFSIZ-1, "%s/new/%lu.%u.%s,S=%lu", directory, now, getpid(), uts.nodename, msg_len);

	fp=fopen(tmpname, "w");
	if(!fp) {
		perror("maildir-filter: do_deliver: fopen");
		exit(DEFERAL);
	}
	for(c=0;c<lines;c++) {
		fwrite(message[c],sizeof(char),strlen(message[c]),fp);
		//fprintf(fp,message[c]);
	}
	fclose(fp);
	if(rename(tmpname,fname)<0) {
		perror("maildir-filter: do_deliver: rename");
		exit(DEFERAL);
	}
	exit(STOP_PROCESSING);
}