2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
10 static FILE *cmitmsg, *patchfile;
12 static char line[1000];
13 static char date[1000];
14 static char name[1000];
15 static char email[1000];
16 static char subject[1000];
18 static char *sanity_check(char *name, char *email)
20 int len = strlen(name);
21 if (len < 3 || len > 60)
23 if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
28 static int handle_from(char *line)
30 char *at = strchr(line, '@');
37 * If we already have one email, don't take any confusing lines
39 if (*email && strchr(at+1, '@'))
44 if (isspace(c) || c == '<')
50 unsigned char c = *at;
51 if (!c || c == '>' || isspace(c))
58 at = line + strlen(line);
60 unsigned char c = *--at;
68 unsigned char c = *at;
76 at = sanity_check(at, email);
82 static void handle_date(char *line)
87 static void handle_subject(char *line)
89 strcpy(subject, line);
92 static void check_line(char *line, int len)
94 if (!memcmp(line, "From:", 5) && isspace(line[5]))
96 else if (!memcmp(line, "Date:", 5) && isspace(line[5]))
98 else if (!memcmp(line, "Subject:", 8) && isspace(line[8]))
99 handle_subject(line+9);
102 static char * cleanup_subject(char *subject)
109 if (!memcmp("e:", subject+1, 2)) {
114 case ' ': case '\t': case ':':
119 p = strchr(subject, ']');
125 remove = p - subject;
126 if (remove <= len *2) {
136 static void cleanup_space(char *buf)
139 while ((c = *buf) != 0) {
145 int len = strlen(buf);
146 memmove(buf, buf+1, len);
153 static void handle_rest(void)
155 char *sub = cleanup_subject(subject);
158 cleanup_space(email);
160 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", name, email, sub, date);
164 if (!memcmp("diff -", line, 6) ||
165 !memcmp("---", line, 3) ||
166 !memcmp("Index: ", line, 7))
170 } while (fgets(line, sizeof(line), stdin) != NULL);
172 if (out == cmitmsg) {
173 fprintf(stderr, "No patch found\n");
181 static int eatspace(char *line)
183 int len = strlen(line);
184 while (len > 0 && isspace(line[len-1]))
189 static void handle_body(void)
194 /* First lines of body can have From: and Date: */
195 while (fgets(line, sizeof(line), stdin) != NULL) {
196 int len = eatspace(line);
199 if (!memcmp("From:", line, 5) && isspace(line[5])) {
200 if (!has_from && handle_from(line+6)) {
205 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
218 static int read_one_header_line(char *line, int sz, FILE *in)
223 if (fgets(line + ofs, sz - ofs, in) == NULL)
225 len = eatspace(line + ofs);
228 peek = fgetc(in); ungetc(peek, in);
229 if (peek == ' ' || peek == '\t') {
230 /* Yuck, 2822 header "folding" */
239 static void usage(void)
241 fprintf(stderr, "mailinfo msg-file patch-file < email\n");
245 int main(int argc, char ** argv)
249 cmitmsg = fopen(argv[1], "w");
254 patchfile = fopen(argv[2], "w");
260 int len = read_one_header_line(line, sizeof(line), stdin);
265 check_line(line, len);