/* * econv by Davide Libenzi ( XMail spool to email format converter ) * Copyright (C) 1999,..,2002 Davide Libenzi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Davide Libenzi * */ #include #include #include #include #define MAIL_DATA_TAG "<>" #define MAIL_FROM "MAIL FROM:" #ifdef _MSC_VER #define strncasecmp strnicmp #define strcasecmp stricmp #endif /* #ifdef _MSC_VER */ char *extract_address(char const *str, char *addr, int asize) { int alen; char const *pat, *base, *end; if (!(pat = strchr(str, '@'))) return NULL; for (base = pat - 1; base >= str && !strchr(":,;<> \t\r\n\"'", *base); base--); base++; for (end = pat + 1; *end != '\0' && !strchr(":,;<> \t\r\n\"'", *end); end++); alen = (int) (end - base); if (alen > asize) alen = asize - 1; strncpy(addr, base, alen); addr[alen] = '\0'; return addr; } int main(int argc, char * argv[]) { int ii, rcode = 1, uxmode = 0, mbox = 0; time_t tcur = time(NULL); FILE *pfin = stdin, *pfout = stdout; char *fnin = NULL, *fnout = NULL; char buffer[2048], from[256] = "", astime[128]; for (ii = 1; ii < argc; ii++) { if (strcmp(argv[ii], "--input") == 0) { if (++ii < argc) fnin = argv[ii]; continue; } else if (strcmp(argv[ii], "--output") == 0) { if (++ii < argc) fnout = argv[ii]; continue; } else if (strcmp(argv[ii], "--unix") == 0) { ++uxmode; continue; } else if (strcmp(argv[ii], "--mbox") == 0) { ++mbox; continue; } } if (fnin && !(pfin = fopen(fnin, "rb"))) pfin = stdin; if (fnout && !(pfout = fopen(fnout, "wb"))) pfout = stdout; while (fgets(buffer, sizeof(buffer) - 1, pfin) && strncmp(buffer, MAIL_DATA_TAG, sizeof(MAIL_DATA_TAG) - 1)) if (mbox && !strncasecmp(buffer, MAIL_FROM, sizeof(MAIL_FROM) - 1)) extract_address(buffer, from, sizeof(from) - 1); if (feof(pfin)) goto clean_exit; if (mbox) { rcode = 2; if (!strlen(from)) goto clean_exit; strcpy(astime, ctime(&tcur)); astime[strlen(astime) - 1] = '\0'; if (!uxmode) fprintf(pfout, "From %s %s\r\n", from, astime); else fprintf(pfout, "From %s %s\n", from, astime); } rcode = 3; if (!uxmode) { do { unsigned int rsize = fread(buffer, 1, sizeof(buffer), pfin); if (rsize && fwrite(buffer, 1, rsize, pfout) != rsize) goto clean_exit; } while (!feof(pfin)); } else { while (fgets(buffer, sizeof(buffer) - 1, pfin)) { for (ii = strlen(buffer); ii > 0 && (buffer[ii - 1] == '\n' || buffer[ii - 1] == '\r'); ii--); buffer[ii] = '\0'; if (fputs(buffer, pfout) == EOF || fputs("\n", pfout) == EOF) goto clean_exit; } } fflush(pfout); rcode = 0; clean_exit: if (pfin != stdin) fclose(pfin); if (pfout != stdout) fclose(pfout); return rcode; }