dmc

dynamic mail client
git clone git://git.suckless.org/dmc
Log | Files | Refs | README | LICENSE

filter.c (1851B)


      1 /* dmc - dynamic mail client
      2  * See LICENSE file for copyright and license details.
      3  */
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <string.h>
      8 
      9 int main(int argc, char **argv) {
     10 	char b[1024], argv2[1024][1024], *ptr;
     11 	int edit = argc, body = 0, filter = 1, value = 0, print = 0, i, j;
     12 
     13 	if (argc > 1) {
     14 		if (!strcmp (argv[1], "-h")) {
     15 			printf ("Usage: %s [-bhv] [headers | :] [-e] [new headers] < mail\n", argv[0]);
     16 			return 1;
     17 		} else if (!strcmp (argv[1], "-v")) {
     18 			value = 1;
     19 			filter++;
     20 		} else if (!strcmp (argv[1], "-b")) {
     21 			body = 1;
     22 			filter++;
     23 		}
     24 		for (i = filter; i < argc; i++)
     25 			if (!strcmp (argv[i], "-e"))
     26 				edit = i;
     27 		for (i = 0; i < argc; i++) {
     28 			strncpy (argv2[i], argv[i], sizeof (argv2[i]));
     29 			argv2[i][1023] = '\0';
     30 		}
     31 	}
     32 	*b = '\0';
     33 	/* Headers */
     34 	while (fgets (b, sizeof (b), stdin) && b[0] != '\n') {
     35 		if (b[0] == '\r' || b[0] == '\n')
     36 			break;
     37 		if ((b[0] == ' ' || b[0] == '\t')) {
     38 			if (print) fputs (b, stdout);
     39 		} else for (i = filter; i < edit && argv[i]; i++)
     40 			if (!strncmp (b, argv[i], strlen(argv[i])) || argv[i][0] == ':') {
     41 				/* Edit/Remove Headers */
     42 				print = 1;
     43 				for (j = edit + 1; !value && j < argc && argv[j]; j++)
     44 					if ((ptr = strchr (argv[j], ':')) &&
     45 						!strncmp (b, argv[j], ptr - argv[j] + 1)) {
     46 						if (ptr[1] != '\0' && argv2[j][0])
     47 							puts (argv[j]);
     48 						argv2[j][0] = '\0';
     49 						print = 0;
     50 						break;
     51 					}
     52 				if (print) {
     53 					if (value && (ptr = strchr (b, ':')))
     54 						ptr += 2;
     55 					else ptr = b;
     56 					fputs (ptr, stdout);
     57 				}
     58 				break;
     59 			} else print = 0;
     60 	}
     61 	if (!value) {
     62 		/* New Headers */
     63 		for (i = edit + 1; i < argc; i++)
     64 			if (argv2[i][0]) puts (argv2[i]);
     65 		if (edit < argc) puts ("");
     66 		/* Body */
     67 		if (body)
     68 		while ((body || argc<2 || edit<argc) && fgets (b, sizeof (b), stdin))
     69 			fputs (b, stdout);
     70 	}
     71 	return 0;
     72 }