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 add_subject_line(char *line
)
94 while (isspace(*line
))
97 strcat(subject
, line
);
100 static void check_line(char *line
, int len
)
102 static int cont
= -1;
103 if (!memcmp(line
, "From:", 5) && isspace(line
[5])) {
108 if (!memcmp(line
, "Date:", 5) && isspace(line
[5])) {
113 if (!memcmp(line
, "Subject:", 8) && isspace(line
[8])) {
114 handle_subject(line
+9);
118 if (isspace(*line
)) {
121 fprintf(stderr
, "I don't do 'Date:' or 'From:' line continuations\n");
124 add_subject_line(line
);
133 static char * cleanup_subject(char *subject
)
140 if (!memcmp("e:", subject
+1, 2)) {
145 case ' ': case '\t': case ':':
150 p
= strchr(subject
, ']');
156 remove
= p
- subject
;
157 if (remove
<= len
*2) {
167 static void cleanup_space(char *buf
)
170 while ((c
= *buf
) != 0) {
176 int len
= strlen(buf
);
177 memmove(buf
, buf
+1, len
);
184 static void handle_rest(void)
186 char *sub
= cleanup_subject(subject
);
189 cleanup_space(email
);
191 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", name
, email
, sub
, date
);
195 if (!memcmp("diff -", line
, 6) ||
196 !memcmp("---", line
, 3) ||
197 !memcmp("Index: ", line
, 7))
201 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
203 if (out
== cmitmsg
) {
204 fprintf(stderr
, "No patch found\n");
212 static int eatspace(char *line
)
214 int len
= strlen(line
);
215 while (len
> 0 && isspace(line
[len
-1]))
220 static void handle_body(void)
225 /* First lines of body can have From: and Date: */
226 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
227 int len
= eatspace(line
);
230 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
231 if (!has_from
&& handle_from(line
+6)) {
236 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
249 static void usage(void)
251 fprintf(stderr
, "mailinfo msg-file path-file < email\n");
255 int main(int argc
, char ** argv
)
259 cmitmsg
= fopen(argv
[1], "w");
264 patchfile
= fopen(argv
[2], "w");
269 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
270 int len
= eatspace(line
);
275 check_line(line
, len
);