File: utils.c

package info (click to toggle)
filter 2.3.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 524 kB
  • ctags: 696
  • sloc: sh: 3,826; ansic: 3,795; makefile: 198
file content (385 lines) | stat: -rw-r--r-- 10,188 bytes parent folder | download
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385

static char rcsid[] ="@(#)$Id: utils.c,v 5.4 1993/08/03 19:28:39 syd Exp $";

/******************************************************************************
 *  The Elm Mail System  -  $Revision: 5.4 $   $State: Exp $
 *
 * 			Copyright (c) 1988-1992 USENET Community Trust
 * 			Copyright (c) 1986,1987 Dave Taylor
 ******************************************************************************
 * Bug reports, patches, comments, suggestions should be sent to:
 *
 *	Philip Brown    filter@bolthole.com
 *
 ******************************************************************************
 * $Log: utils.c,v $
 *
 *  2000/10/20: added make_tempfile(), remove_tempfile() -- phil
 *
 *  1998/07/18: added "printlist" -- phil
 *
 * Revision 5.4  1993/08/03  19:28:39  syd
 * Elm tries to replace the system toupper() and tolower() on current
 * BSD systems, which is unnecessary.  Even worse, the replacements
 * collide during linking with routines in isctype.o.  This patch adds
 * a Configure test to determine whether replacements are really needed
 * (BROKE_CTYPE definition).  The <ctype.h> header file is now included
 * globally through hdrs/defs.h and the BROKE_CTYPE patchup is handled
 * there.  Inclusion of <ctype.h> was removed from *all* the individual
 * files, and the toupper() and tolower() routines in lib/opt_utils.c
 * were dropped.
 * From: chip@chinacat.unicom.com (Chip Rosenthal)
 *
 * Revision 5.3  1993/01/27  19:40:01  syd
 * I implemented a change to filter's default verbose message format
 * including %x %X style date and time along with username
 * From: mark@drd.com (Mark Lawrence)
 *
 * Revision 5.2  1992/11/15  01:40:43  syd
 * Add regexp processing to filter.
 * Add execc operator
 * From: Jan Djarv <Jan.Djarv@sa.erisoft.se>
 *
 * Revision 5.1  1992/10/03  22:18:09  syd
 * Initial checkin as of 2.4 Release at PL0
 *
 *
 *****************************************************************************/

/** Utility routines for the filter program...

**/

#include <stdio.h>
#include <pwd.h>
#include <fcntl.h>

#include "defs.h"
#include "filter.h"
#include "s_filter.h"

extern char *date_n_user();
extern char *datestring();

void leave(reason)
char *reason;
{
      if (outfptr != NULL)
	{
	      fprintf(outfptr,"filter (%s): LEAVE %s\n", date_n_user(),
			reason);
	      fclose(outfptr);
	}
      
      exit(1);
}


/* we have a simple linked-list struct now. We sometimes want to print
 * it out. so print out contents, on a single line.
  * (Currently, will be used for "To:" list)
  */

void printlist(fptr, listing)
FILE *fptr;
LIST *listing;
{
	while(listing != NULL)
	{
		fprintf(fptr,"%s ", listing->str);
		listing=listing->next;
	}
}

void log_msg(what)
int what;
{
	/** make an entry in the log files for the specified entry **/

	FILE *fptr;
	char filename[SLEN];
	int subj_len;

	if (!logging)
		return;

	if (! show_only) {
	  sprintf(filename, "%s/%s", home, filtersum);	/* log action once! */
	  if ((fptr = fopen(filename, "a")) == NULL) {
	    if (outfptr != NULL)
	      fprintf(outfptr,catgets(elm_msg_cat,
				    FilterSet,FilterCouldntOpenLogFile,
				  "filter (%s): Couldn't open log file %s\n"), 
		      date_n_user(), filename);
	    fptr = stdout;
	  }
	  fprintf(fptr, "%d\n", rule_choosen);
	  fclose(fptr);
	}

	sprintf(filename, "%s/%s", home, filterlog);

	if (show_only)
	  fptr = stdout;
	else if ((fptr = fopen(filename, "a")) == NULL) {
	  if (outfptr != NULL)
	    fprintf(outfptr,catgets(elm_msg_cat,
				  FilterSet,FilterCouldntOpenLogFile,
				  "filter (%s): Couldn't open log file %s\n"), 
		    date_n_user(), filename);
	  fptr = stdout;
	}
	
#ifdef _IOFBF
	setvbuf(fptr, NULL, _IOFBF, BUFSIZ);
#endif

	subj_len = strlen(subject);
	fprintf(fptr,catgets(elm_msg_cat,FilterSet,FilterMailFrom,
			    "\n%s: Mail from "), datestring());
	       
	if (strlen(from) + subj_len > 60)
	  fprintf(fptr, "%s\n\t", from);
	else
	  fprintf(fptr, "%s ", from);

	if (subj_len > 0)
	  fprintf(fptr,catgets(elm_msg_cat,FilterSet,FilterAbout,
			     "about %s"),subject);
	fprintf(fptr, "\n");

	if (rule_choosen != -1)
	  if (rules[rule_choosen].condition->matchwhat == TO)
	    fprintf(fptr,catgets(elm_msg_cat,FilterSet,FilterAddressedTo,
			       "\t(addressed to "));
	    printlist(fptr, tolist);
    	    fprintf(fptr,"\n");

	switch (what) {
	  case BOUNCE : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterBouncedMesg,
					       "\tBOUNCED"));
	                    break;
	  case DELETE_MSG : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterDeletedMesg,
					       "\tDELETED"));
	                    break;
	  case FAILED_SAVE: fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterSaveFailedMesg,
				       "\tSAVE FAILED for file \"%s\""), 
				    rules[rule_choosen].argument2);
	                    break;
	  case SAVE       : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterSavedMesg,
					       "\tSAVED in file \"%s\""), 
				    rules[rule_choosen].argument2);
	                    break;
	  case SAVECC     : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterSavedAndPutMesg,
		            "\tSAVED in file \"%s\" AND PUT in mailbox"), 
				    rules[rule_choosen].argument2);
	                    break;
	  case FORWARD    : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterForwardedMesg,
					       "\tFORWARDED to \"%s\""), 
				    rules[rule_choosen].argument2);
	                    break;
	  case FORWARDC    : fprintf(fptr,catgets(elm_msg_cat,
						FilterSet,
						FilterForwardedAndPutMesg,
			    "\tFORWARDED to \"%s\" AND PUT in mailbox"), 
				     rules[rule_choosen].argument2);
	                     break;
	  case EXEC       : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterExecutedMesg,
					       "\tEXECUTED \"%s\""),
				    rules[rule_choosen].argument2);
	                    break;
	  case EXECC      : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterExecutedSMesg,
				"\tEXECUTED \"%s\" AND PUT in mailbox"),
				    rules[rule_choosen].argument2);
	                    break;
	  case LEAVE      : fprintf(fptr,catgets(elm_msg_cat,
					       FilterSet,FilterPutMesg,
					       "\tPUT in mailbox"));
		            break;
	}

	if (rule_choosen != -1)
	  fprintf(fptr,catgets(elm_msg_cat,FilterSet,FilterByRule,
			     " by rule #%d\n"), rule_choosen+1);
	else
	  fprintf(fptr,catgets(elm_msg_cat,FilterSet,FilterTheDefaultAction,
			      ": the default action\n"));

	fflush(fptr);
	fclose(fptr);
}

/* we should use this more. But right this second, I'm not doing retrofit*/
char *safemalloc(int bsize)
{
	char *retstr=(char*)malloc(bsize);
	if(retstr==NULL)
	{
	  if (outfptr != NULL)
	    fprintf(outfptr,catgets(elm_msg_cat,
				  FilterSet,FilterCantAllocRules,
			"filter (%s): Can't alloc memory!\n"),
			date_n_user());
	  if (outfptr != NULL) fclose(outfptr);
	  exit(1);
	}
	return retstr;
}

int
contains(str, pat)
char *str, *pat;
{
	/** Returns TRUE iff pat occurs IN IT'S ENTIRETY in str.
	    The comparison is case insensitive.   **/

	register int i = 0, j = 0;

	while (str[i] != '\0') {
	  while (tolower(str[i]) == tolower(pat[j])) {
	    i++; j++;
	    if (pat[j] == '\0') 
	      return(TRUE);
	  }
	  i = i - j + 1;
	  j = 0;
	}
	return(FALSE);
}

/* Note: Non-thread-safe static string returned */
char *itoa(i, two_digit)
int i, two_digit;
{	
	/** return 'i' as a null-terminated string.  If two-digit use that
	    size field explicitly!  **/

	static char value[10];
	
	if (two_digit)
	  sprintf(value, "%02d", i);
	else
	  sprintf(value, "%d", i);

	return( (char *) value);
}

void lowercase(string)
char *string;
{
	/** translate string into all lower case **/

	register int i;

	for (i= strlen(string); --i >= 0; )
	  if (isupper(string[i]))
	    string[i] = tolower(string[i]);
}

/* convenience function to use strncpy, and explicitly zero the
 * last byte, since strncpy does not do so.
 *  WARNING: "len" should be total length of src,
 *    or, the total length of the resulting string you want PLUS 0.
 *
 * In other words strlen(result)+1  
 */
void stringcopy(char *dest, char *src, int len)
{
	strncpy(dest, src, len);
	dest[len-1]='\0';
}

/* the following code is borrowed from elm src/file.c
 * it has been stripped down to only handle ~ expansion
 */

int expand_filename(filename)
char *filename;
{
	/** Expands	~/	to the current user's home directory

	    Side effect: strips off trailing blanks

	    Returns 	1	upon proper expansions
			0	upon failed expansions
	 **/

	char temp_filename[SLEN], *ptr;

	ptr = filename;
	while (*ptr == ' ') ptr++;	/* leading spaces GONE! */
	stringcopy(temp_filename, ptr, SLEN);

	/** New stuff - make sure no illegal char as last **/
	ptr = temp_filename + strlen(temp_filename) - 1;
	while (*ptr == '\n' || *ptr == '\r')
	  *ptr-- = '\0';
	  
	/** Strip off any trailing backslashes or blanks **/
	while (*ptr == '\\' || *ptr == ' ')
		*ptr-- = '\0';

	if ((temp_filename[0] == '~') &&
	    (temp_filename[1] == '/')) {
	    sprintf(filename, "%s%s%s",
		  home, (lastch(home) != '/' ? "/" : ""), &temp_filename[2]);

	/* any further expansion, such as = + < >
	 * would require parsing the elmrc file
	 */

	} else
	  stringcopy(filename, temp_filename, SLEN);
	  
	return(1);
}

static int tmpinuse=0;


/* create a temporary file in a SAFE manner.
 * And also save the name in tmpfilename for easier cleanup on panic-exit.
 *  [via remove_tempfile()]
 *
 * Return a FILE* to it, or NULL on error.
 */
FILE* make_tempfile(){
	FILE *returnfp;
	int fd,mypid;

	mypid=getpid();

	if(tmpinuse!=0){
		fprintf(stderr,
		      "tempfile already in use, but make_tempfile called?\n");
		return NULL;
	}
	sprintf(tmpfilename,"%s_%s.%d", filter_temp, username, mypid);
	fd=open(tmpfilename, O_RDWR|O_CREAT|O_EXCL, 0600);
	if(fd==-1){
		sprintf(tmpfilename,"%s_%s.%d.2", filter_temp, username,mypid);
		fd=open(tmpfilename, O_RDWR|O_CREAT|O_EXCL,0600);
	}
	if(fd==-1){
		return NULL;
	}

	returnfp=fdopen(fd, "w+");
	tmpinuse=1;
	return (returnfp);
}

/* remove actual file created by make_tempfile().
 */
void remove_tempfile(){
	unlink(tmpfilename);
	tmpinuse=0;
}