1 /*
2  * Small program to generate a fake email given a wordlist to choose words
3  * from.
4  *
5  * Copyright 2007 Andrew Wood, distributed under the Artistic License.
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <errno.h>
13 #include "config.h"
14 
15 
16 /*
17  * Return a random word from the given word list.
18  */
random_word(char ** wordlist,int wordcount)19 char *random_word(char **wordlist, int wordcount)
20 {
21 	return wordlist[rand() % wordcount];
22 }
23 
24 
25 /*
26  * Output between "min" and "max" (inclusive) random words to stdout.
27  */
random_words(char ** wordlist,int wordcount,int wmin,int wmax)28 void random_words(char **wordlist, int wordcount, int wmin, int wmax)
29 {
30 	int numtodo, i;
31 
32 	numtodo = wmin + (rand() % (wmax - wmin));
33 
34 	for (i = 0; i < numtodo; i++) {
35 		if (i > 0) printf(" ");
36 		printf("%s", random_word(wordlist, wordcount));
37 	}
38 }
39 
40 
41 /*
42  * Main program.
43  */
main(int argc,char ** argv)44 int main(int argc, char **argv) {
45 	char **wordlist = NULL;
46 	char **newptr;
47 	char *strptr;
48 	int wordcount = 0;
49 	char buf[1024];		/* RATS: ignore (checked) */
50 	char *suffix[] = {
51 	  ".co.uk",
52 	  ".com",
53 	  ".net",
54 	  ".org",
55 	  ".org.uk"
56 	};
57 	int mailnum;
58 	time_t t;
59 	FILE *fptr;
60 
61 	if (argc != 3) {
62 		fprintf(stderr, "Usage: fakemail WORDFILE NUMMESSAGES\n");
63 		return(1);
64 	}
65 
66 	fptr = fopen(argv[1], "r");
67 	if (!fptr) {
68 		fprintf(stderr, "fakemail: %s: %s\n", argv[1], strerror(errno));
69 		return(1);
70 	}
71 
72 	while (fgets(buf, sizeof(buf) - 1, fptr)) {
73 		wordcount++;
74 		if (wordlist) {
75 			newptr = realloc(wordlist, wordcount * sizeof(char *));	/* RATS: ignore */
76 		} else {
77 			newptr = malloc(wordcount * sizeof(char *));
78 		}
79 		if (!newptr) {
80 			fprintf(stderr, "fakemail: %s\n", strerror(errno));
81 			fclose(fptr);
82 			return (1);
83 		}
84 
85 		strptr = strchr(buf, '\n');
86 		if (strptr) *strptr = 0;
87 
88 		wordlist = newptr;
89 		wordlist[wordcount-1] = strdup(buf);
90 	}
91 	fclose(fptr);
92 
93 	srand(time(NULL));	/* RATS: ignore (randomness not important) */
94 
95 	for (mailnum = 0; mailnum < atoi(argv[2]); mailnum++) {
96 #ifdef HAVE_SNPRINTF
97 		snprintf(buf, sizeof(buf),
98 #else
99 		sprintf(buf,	/* RATS: ignore */
100 #endif
101 			"%s.%s@%s%s",
102 			random_word(wordlist, wordcount),
103 			random_word(wordlist, wordcount),
104 			random_word(wordlist, wordcount),
105 			suffix[rand()%5]);
106 		t = time(NULL);
107 		printf("From %s  %s", buf, ctime(&t));
108 		printf("Return-Path: <%s>\n", buf);
109 		printf("From: <%s>\n", buf);
110 		printf("To: you@your.com\n");
111 		printf("Date: %s", ctime(&t));
112 		printf("Subject: ");
113 		random_words(wordlist, wordcount, 2, 10);
114 		printf("\n\n");
115 		random_words(wordlist, wordcount, 20, 400);
116 		printf("\n\n");
117 	}
118 
119 	return(0);
120 }
121 
122 /* EOF */
123