tools / mailinfo.con commit [PATCH] git-branch: avoid getting confused by empty or nonexisting branches. (a38e727)
   1/*
   2 * Another stupid program, this one parsing the headers of an
   3 * email to figure out authorship and subject
   4 */
   5#include <stdio.h>
   6#include <stdlib.h>
   7#include <string.h>
   8#include <ctype.h>
   9
  10static FILE *cmitmsg, *patchfile;
  11
  12static char line[1000];
  13static char date[1000];
  14static char name[1000];
  15static char email[1000];
  16static char subject[1000];
  17
  18static char *sanity_check(char *name, char *email)
  19{
  20        int len = strlen(name);
  21        if (len < 3 || len > 60)
  22                return email;
  23        if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
  24                return email;
  25        return name;
  26}
  27
  28static int handle_from(char *line)
  29{
  30        char *at = strchr(line, '@');
  31        char *dst;
  32
  33        if (!at)
  34                return 0;
  35
  36        /*
  37         * If we already have one email, don't take any confusing lines
  38         */
  39        if (*email && strchr(at+1, '@'))
  40                return 0;
  41
  42        while (at > line) {
  43                char c = at[-1];
  44                if (isspace(c) || c == '<')
  45                        break;
  46                at--;
  47        }
  48        dst = email;
  49        for (;;) {
  50                unsigned char c = *at;
  51                if (!c || c == '>' || isspace(c))
  52                        break;
  53                *at++ = ' ';
  54                *dst++ = c;
  55        }
  56        *dst++ = 0;
  57
  58        at = line + strlen(line);
  59        while (at > line) {
  60                unsigned char c = *--at;
  61                if (isalnum(c))
  62                        break;
  63                *at = 0;
  64        }
  65
  66        at = line;
  67        for (;;) {
  68                unsigned char c = *at;
  69                if (!c)
  70                        break;
  71                if (isalnum(c))
  72                        break;
  73                at++;
  74        }
  75
  76        at = sanity_check(at, email);
  77        
  78        strcpy(name, at);
  79        return 1;
  80}
  81
  82static void handle_date(char *line)
  83{
  84        strcpy(date, line);
  85}
  86
  87static void handle_subject(char *line)
  88{
  89        strcpy(subject, line);
  90}
  91
  92static void add_subject_line(char *line)
  93{
  94        while (isspace(*line))
  95                line++;
  96        *--line = ' ';
  97        strcat(subject, line);
  98}
  99
 100static void check_line(char *line, int len)
 101{
 102        static int cont = -1;
 103        if (!memcmp(line, "From:", 5) && isspace(line[5])) {
 104                handle_from(line+6);
 105                cont = 0;
 106                return;
 107        }
 108        if (!memcmp(line, "Date:", 5) && isspace(line[5])) {
 109                handle_date(line+6);
 110                cont = 0;
 111                return;
 112        }
 113        if (!memcmp(line, "Subject:", 8) && isspace(line[8])) {
 114                handle_subject(line+9);
 115                cont = 1;
 116                return;
 117        }
 118        if (isspace(*line)) {
 119                switch (cont) {
 120                case 0:
 121                        fprintf(stderr, "I don't do 'Date:' or 'From:' line continuations\n");
 122                        break;
 123                case 1:
 124                        add_subject_line(line);
 125                        return;
 126                default:
 127                        break;
 128                }
 129        }
 130        cont = -1;
 131}
 132
 133static char * cleanup_subject(char *subject)
 134{
 135        for (;;) {
 136                char *p;
 137                int len, remove;
 138                switch (*subject) {
 139                case 'r': case 'R':
 140                        if (!memcmp("e:", subject+1, 2)) {
 141                                subject +=3;
 142                                continue;
 143                        }
 144                        break;
 145                case ' ': case '\t': case ':':
 146                        subject++;
 147                        continue;
 148
 149                case '[':
 150                        p = strchr(subject, ']');
 151                        if (!p) {
 152                                subject++;
 153                                continue;
 154                        }
 155                        len = strlen(p);
 156                        remove = p - subject;
 157                        if (remove <= len *2) {
 158                                subject = p+1;
 159                                continue;
 160                        }       
 161                        break;
 162                }
 163                return subject;
 164        }
 165}                       
 166
 167static void cleanup_space(char *buf)
 168{
 169        unsigned char c;
 170        while ((c = *buf) != 0) {
 171                buf++;
 172                if (isspace(c)) {
 173                        buf[-1] = ' ';
 174                        c = *buf;
 175                        while (isspace(c)) {
 176                                int len = strlen(buf);
 177                                memmove(buf, buf+1, len);
 178                                c = *buf;
 179                        }
 180                }
 181        }
 182}
 183
 184static void handle_rest(void)
 185{
 186        char *sub = cleanup_subject(subject);
 187        cleanup_space(name);
 188        cleanup_space(date);
 189        cleanup_space(email);
 190        cleanup_space(sub);
 191        printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", name, email, sub, date);
 192        FILE *out = cmitmsg;
 193
 194        do {
 195                if (!memcmp("diff -", line, 6) ||
 196                    !memcmp("---", line, 3) ||
 197                    !memcmp("Index: ", line, 7))
 198                        out = patchfile;
 199
 200                fputs(line, out);
 201        } while (fgets(line, sizeof(line), stdin) != NULL);
 202
 203        if (out == cmitmsg) {
 204                fprintf(stderr, "No patch found\n");
 205                exit(1);
 206        }
 207
 208        fclose(cmitmsg);
 209        fclose(patchfile);
 210}
 211
 212static int eatspace(char *line)
 213{
 214        int len = strlen(line);
 215        while (len > 0 && isspace(line[len-1]))
 216                line[--len] = 0;
 217        return len;
 218}
 219
 220static void handle_body(void)
 221{
 222        int has_from = 0;
 223        int has_date = 0;
 224
 225        /* First lines of body can have From: and Date: */
 226        while (fgets(line, sizeof(line), stdin) != NULL) {
 227                int len = eatspace(line);
 228                if (!len)
 229                        continue;
 230                if (!memcmp("From:", line, 5) && isspace(line[5])) {
 231                        if (!has_from && handle_from(line+6)) {
 232                                has_from = 1;
 233                                continue;
 234                        }
 235                }
 236                if (!memcmp("Date:", line, 5) && isspace(line[5])) {
 237                        if (!has_date) {
 238                                handle_date(line+6);
 239                                has_date = 1;
 240                                continue;
 241                        }
 242                }
 243                line[len] = '\n';
 244                handle_rest();
 245                break;
 246        }
 247}
 248
 249static void usage(void)
 250{
 251        fprintf(stderr, "mailinfo msg-file path-file < email\n");
 252        exit(1);
 253}
 254
 255int main(int argc, char ** argv)
 256{
 257        if (argc != 3)
 258                usage();
 259        cmitmsg = fopen(argv[1], "w");
 260        if (!cmitmsg) {
 261                perror(argv[1]);
 262                exit(1);
 263        }
 264        patchfile = fopen(argv[2], "w");
 265        if (!patchfile) {
 266                perror(argv[2]);
 267                exit(1);
 268        }
 269        while (fgets(line, sizeof(line), stdin) != NULL) {
 270                int len = eatspace(line);
 271                if (!len) {
 272                        handle_body();
 273                        break;
 274                }
 275                check_line(line, len);
 276        }
 277        return 0;
 278}