


#include <stdio.h>

#define MAX_ENTRIES 10000

typedef struct {
    char *name;
    char *val;
} entry;

char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);


mailit(entry *entries)
{
    FILE *fp;
    char filename[256];
    char command[256];

    sprintf(filename, "/tmp/mailin%d", (int)getpid());
    fp = fopen(filename, "w");
    if (fp != NULL)
    {
        fprintf(fp, "User: (%s [%s])\nSays:\n%s\n", entries[0].val, entries[1].val, entries[2].val);
        fclose(fp);
        sprintf(command, "mail ebina < %s", filename);
        system(command);
        unlink(filename);
    }
}

main(int argc, char *argv[]) {
    entry entries[MAX_ENTRIES];
    register int x,m=0;
    int cl;

    cl = atoi(argv[1]);

    printf("Content-type: text/html%c%c",10,10);

    for(x=0;cl && (!feof(stdin));x++) {
        m=x;
        entries[x].val = fmakeword(stdin,'&',&cl);
        plustospace(entries[x].val);
        unescape_url(entries[x].val);
        entries[x].name = makeword(entries[x].val,'=');
    }

/*
    mailit(entries);
*/

    if ((entries[0].val != NULL)&&(entries[0].val[0] != '\0'))
    {
        printf("<b>%s</b> (<i>%s</i>) says:<p>\n", entries[0].val, entries[1].val);
    }
    else
    {
        printf("<b>Unknown User</b> (<i>%s</i>) says:<p>\n", entries[1].val);
    }
    printf("%s\n", entries[2].val);
}
