File: sortmail.c

package info (click to toggle)
sortmail 19910421-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 120 kB
  • ctags: 153
  • sloc: ansic: 1,894; makefile: 46
file content (321 lines) | stat: -rw-r--r-- 6,383 bytes parent folder | download | duplicates (3)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321


#include <stdio.h>
#include <varargs.h>
#include <errno.h>
#include <alloca.h>
#include <sys/param.h>		/* defines MAXPATHLEN */

#include "sortmail.h"
#include "regex.h"	/* use gnu regex */

	FILE	*logfile ;

static	int
ReCompile(pat)
  char *pat ;
{
  char *err ;
  if( (err = re_comp(pat)) != NULL ) {
    fprintf(logfile, "sortmail: pattern %s: %s\n", pat, err) ;
    return 1 ;
  }
  return 0 ;
}

extern	int	sys_nerr ;
extern	const 	char	* const sys_errlist[] ;



	char	*user = NULL ;
	char	*home = NULL ;
	char	*mailbox = NULL ;
	char	*mailrc = NULL ;
	char	*sortmailrc = NULL ;
	char	*mbox = NULL ;
	char	*deflt = NULL ;
	char	*vacation = NULL ;
	char	*folder = NULL ;
	char	*logfilename = NULL ;
	int	verbose = 0 ;

	PatInfo	*patterns = NULL, *lastpat = NULL ;

static	char	tmpfilename[256] ;

static	void	dispose() ;
static	void	append() ;
static	void	put_pipe() ;
static	void	forward() ;

static int
argcheck(str, argv, nargs, argc)
	char	*str, *argv ;
	int	nargs ;
	int	*argc ;
{
	if( strcmp(str, argv) != 0 )
	  return 0 ;
	if( (*argc -= nargs) <= 0 )
	  die("sortmail: %s option requires %d arguments\n", str, nargs) ;
	return 1 ;
}



main(argc,argv)
	int	argc ;
	char	**argv ;
{
	FILE	*tmpfile ;
	char	line[1024] ;
	int	n ;
	int	header ;
	PatInfo	*pat ;
	char	*err ;
	int	firstline ;
	int	cont = 0 ;
	int	foundit = 0 ;

	logfile = fopen("/dev/console","w") ;
	if( logfile == NULL )
	  logfile = stderr ;

	while( --argc > 0 )
	{
	  ++argv ;
	  if( argcheck("-v", *argv, 0, &argc) )
	    ++verbose ;
	  else if( argcheck("-home", *argv, 1, &argc) )
	    home = *++argv ;
	  else if( argcheck("-mailbox", *argv, 1, &argc) )
	    mailbox = *++argv ;
	  else if( argcheck("-mailrc", *argv, 1, &argc) )
	    mailrc = *++argv ;
	  else if( argcheck("-sortmailrc", *argv, 1, &argc) )
	    sortmailrc = *++argv ;
	  else
	    user = *argv ;
	}

#ifdef	DEBUG
	verbose = 1 ;
#endif	/* DEBUG */

	read_initfiles() ;

	sprintf(tmpfilename, "/tmp/sortmail%d", getpid()) ;
	if( (tmpfile = fopen(tmpfilename, "w+")) == NULL )
	{
	  fprintf(stderr, "sortmail: can't open %s, mail to %s may be lost\n",
		tmpfile, user) ;
	  fprintf(logfile, "sortmail: can't open %s, %s\n",
		tmpfile, sys_errlist[errno]) ;
	  append(stdin, mailbox) ;		/* try to rescue situtation */
	  exit(1) ;
	}

	firstline = 1 ;
	while( fgets(line, sizeof(line), stdin) != NULL ) {
	  if( !firstline && strmatch(line, "From ") )
	    putc('>', tmpfile) ;
	  fputs(line, tmpfile) ;
	  firstline = 0 ;
	}
#ifdef	DEBUG
	fprintf(logfile, "got a message in %s\n", tmpfilename) ;
#endif	/* DEBUG */

	for( pat = patterns; pat != NULL && !foundit ; pat = pat->next )
	{
	  if( !ReCompile(pat->pattern) )
	  {
	    rewind(tmpfile) ;
	    while( fgets(line, sizeof(line), tmpfile) != NULL ) {
	      if( line[0] == '\n'  &&  pat->flags != P_ALL )
		break ;
	      if( pat->flags == P_HEADER  ||
		  pat->flags == P_SUBJECT && strmatch(line, "Subject: ") ||
		  pat->flags == P_FROM && strmatch(line, "From: ") ||
		  pat->flags == P_TO &&
			(strmatch(line, "To: ") || strmatch(line, "Cc: ") ||
			strmatch(line, "Apparently-To: ") ) ||
		  isspace(line[0]) && cont ||
		  pat->flags == P_ALL )
	      {
		if( re_exec(line) )
		{
		  dispose(pat,tmpfile) ;
		  foundit = 1 ;
		  break ;
		}
		else
		  /* if next line is a continuation line, test it too */
		  cont = 1 ;
	      }
	      else
		cont = 0 ;
	    }
	  }
	}

	if( vacation != NULL )
	{
	  rewind(tmpfile) ;
	  if( !ReCompile(user) )
	  {
	    while( fgets(line, sizeof(line), tmpfile) != NULL ) {
	      if( line[0] == '\n' )
		break ;
	      if( strmatch(line, "To: ") || strmatch(line, "Cc: ") ||
			strmatch(line, "Apparently-To: ") )
	      {
		if( re_exec(line) )
		{
		  sprintf(line, "/usr/ucb/vacation %s", user) ;
		  put_pipe(tmpfile, line) ;
		  if( verbose )
		    fprintf(logfile, "message piped through %s\n", line) ;
		  break ;
		}
	      }
	    }
	  }
	}

	unlink(tmpfilename) ;

	if( !foundit )
	  append(tmpfile, deflt != NULL ? deflt : mailbox) ;
	exit(0) ;
}


/*VARARGS0*/
die(va_alist)
	va_dcl
{
	va_list	args ;
	char	*fmt ;

	va_start(args) ;
	fprintf(logfile, "sortmail: ") ;
	fmt = va_arg(args, char *);
	vfprintf(logfile, fmt, args);
	va_end(args) ;
	exit(1) ;
}


	/* This function deals with the file described by 'file' in
	 * the manner described by "pat".
	 */

static	void
dispose(pat, file)
register PatInfo *pat ;
register FILE	*file ;
{
	char	outfile[MAXPATHLEN] ;
	char	*ptr ;
	struct passwd	*passwd ;

	switch( pat->command ) {
	  case C_MAIL:		/* append to user's mail file */
	    if( pat->dest == NULL || pat->dest[0] == '\0' )
	      append(file, mailbox) ;
	    else
	      forward(file, pat->dest) ;
	    break ;

	  case C_FILE:		/* append to named file. */
	    append(file, pat->dest) ;
	    break ;

	  case C_PIPE:		/* pipe through named command */
	    put_pipe(file, pat->dest) ;
	    if( verbose )
	      fprintf(logfile, "message piped through %s\n", pat->dest) ;
	    break ;

	  case C_JUNK:		/* throw it away */
	    if( verbose )
	      fprintf(logfile, "message deleted\n") ;
	    break ;
	}
}


static	void
append(file, outfile)
	FILE	*file ;
	char	*outfile ;
{
	FILE	*ofile ;
	char	line[1024] ;
	int	i ;

	filename_expand(line, outfile) ;
	if( verbose )
	  fprintf(logfile, "message filed to %s\n", line) ;

	rewind(file) ;

	if( (ofile = fopen(line, "a")) == NULL )
	{
	  fprintf(logfile, "sortmail: can't open %s, %s\n",
		line,sys_errlist[errno]);
	  if( strcmp(outfile, mailbox) == 0 )
	    fprintf(stderr,
	      "sortmail: Cannot open %s, %s.  Mail to %s may be lost\n",
		mailbox, sys_errlist[errno], user) ;
	  else
	    append(file, mailbox) ;
	}
	else
	{
	  while( fgets(line, sizeof(line), file) != NULL )
	    fputs(line, ofile) ;
	  fputs("\n", ofile) ;
	  fclose(ofile) ;
	}
}


static	void
put_pipe(file, dest)
	FILE	*file ;
	char	*dest ;
{
	FILE	*ofile ;
	char	line[1024] ;

	if( (ofile = popen(dest, "w")) == NULL )
	{
	  fprintf(logfile, "sortmail: cannot open pipe to command %s\n",
		dest ) ;
	  append(file, mailbox) ;
	}
	else
	{
	  rewind(file) ;
	  while( fgets(line, sizeof(line), file) != NULL )
	    fputs(line, ofile) ;
	  pclose(ofile) ;
	}
}


static	void
forward(file, dest)
	FILE	*file ;
	char	*dest ;
{
	char	cmd[1024] ;

	sprintf(cmd, "/usr/lib/sendmail %s", dest) ;
	put_pipe(file, cmd) ;
	if( verbose )
	  fprintf(logfile, "message mailed to %s\n", dest) ;
}