home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 35 Internet
/
35-Internet.zip
/
mr2iuucp.zip
/
UNMBOX.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-10-23
|
6KB
|
223 lines
/* ummbox.c
* program to take a uupc inbox and translate it into seperate mail messages
*
* Michael Taylor miket@pcug.org.au
*
* History:
* 1.0.00 Initial version. 12/10/1996
* 1.0.01 Fixes. 19/10/1996
* Fix parsing of UUPC variables
* Add fromsep logic - NO fromsep should be defined!
* Inprove get_next_fname so it does not
* return a filename that already exists.
* Rename UUPC mbox so it cannot be written
* to by uuxqt/rmail.
*
* This program is freely redistributable - source and all!
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char version[] =
"unmbox by Michael Taylor (miket@pcug.org.au) - Version 1.0.01";
/* logic
*
* 1. open MR/2 ICE mail-in control file "mr2i.ndx"
* 1. open mailbox.
* 2. if empty then stop.
* 3. find next file name to use.
* 4. read mailbox -
* write out to new file.
* increment file name.
* repeat until mail box is empty.
*
*/
char * get_next_fname (void);
int get_uupc_vars (void);
void err (char * str);
static char old_mailbox[256];
static char mailbox[256];
static char maildir[256];
static char mailext[256];
static char * mailbox_name = NULL;
static FILE * uupcmfile;
static char basename[256];
static char nxtfname[256];
char *
get_next_fname () {
char *name1;
if ((name1 = tempnam (basename, "mr2")) == NULL) {
err ("Cannot create unique filename\n");
return NULL;
}
strcpy (nxtfname, name1);
free (name1);
return (char *)nxtfname;
}
int
get_uupc_vars () {
char buf[256];
int i, cf;
char *p, *fn;
FILE *tmp;
mailbox_name = getenv ("UNMB_MAIL");
if (mailbox_name != NULL) {
return (1);
}
/*--------------------- load UUPC rc files ---------------------------*/
/* read the system file first */
for (cf = 0, i = 0; i < 2; i++) {
/* choose the file to open */
if (i == 0) {
fn = getenv ("UUPCSYSRC");
if (fn == NULL) {
err ("Enviroment variable UUPCSYSRC not defined\n");
exit (EXIT_FAILURE);
}
} else {
fn = getenv ("UUPCUSRRC");
if (fn == NULL) {
err ("Enviroment variable UUPCUSRRC not defined\n");
exit (EXIT_FAILURE);
}
}
if ((tmp = fopen (fn, "r")) != NULL) {
while (fgets (buf, 255, tmp)) {
p = buf + strlen (buf) - 1;
if (*p == '\n')
*p = '\0';
if (p > buf)
if (*(p-1) == '\n')
*(p-1) = '\0';
if (!cf && strnicmp (buf, "confdir=", 8) == 0) {
/* default root dir if mailbox var not found */
cf = 1;
strcpy (maildir, buf+8);
strcat (maildir, "\\mail"); /* 1.0.01 */
} else
if (strnicmp (buf, "mailbox=", 8) == 0) {
/* mailbox base name */
cf = 1;
strcpy (mailbox, buf+8);
} else
if (strnicmp (buf, "maildir=", 8) == 0) {
/* file name for mailbox */
strcpy (maildir, buf+8);
} else
if (strnicmp (buf, "mailext=", 8) == 0) {
/* extension of file name for mailbox */
strcpy (mailext, buf+8);
}
}
fclose (tmp);
} else {
fprintf (stderr, "Cannot open %s\n", fn);
}
}
if (cf) {
mailbox_name = maildir;
}
if (mailbox_name == NULL) {
return (0);
}
strcat (maildir, "\\");
strcat (maildir, mailbox);
strcat (maildir, ".");
strcpy (old_mailbox, maildir);
strcat (old_mailbox, mailext);
strcat (maildir, "tmp");
return (1);
}
void
err (char * str) {
FILE * errfile;
if ((errfile = fopen ("mail\\getmail.err", "w")) == NULL) {
fprintf (stderr, "unmbox: error - cannot open file for error message\n");
exit (EXIT_FAILURE);
}
fprintf (errfile, "%s", str);
fclose (errfile);
}
static char x01x20[20] =
"\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01\01";
static char xbuf[20480];
int
main (int argc, char * argv[]) {
FILE * mfile, * infile, * errfile;
char *p, *q, *r;
int nlines, n;
if (argc < 2) {
err ("usage: unmbox <SMTP directory>\n");
exit (EXIT_FAILURE);
}
strcpy (basename, argv[1]);
if (!get_uupc_vars ()) {
err ("error - cannot find UUPC mail box\n");
exit (EXIT_FAILURE);
}
remove (mailbox_name);
if (rename (old_mailbox, mailbox_name)) {
/* err ("error - cannot access UUPC mailbox - try again later\n");*/
exit (EXIT_FAILURE);
}
if ((infile = fopen (mailbox_name, "r")) == NULL) {
/* err ("error - cannot open UUPC mailbox\n");*/
exit (EXIT_FAILURE);
}
while (fgets (xbuf, 20479, infile)) {
if (0 == memcmp (xbuf, x01x20, 20))
continue;
if ((mfile = fopen (get_next_fname (), "w")) == NULL) {
/* this error most likely occurs if the SMTP directory doesn't exist */
err ("error - cannot open new file for mail\n");
/* just to be nice - rename mailbox so that the user can try again later */
rename (mailbox_name, old_mailbox);
exit (EXIT_FAILURE);
}
fputs (xbuf, mfile); nlines = 1;
while (fgets (xbuf, 20479, infile)) {
if (0 == memcmp (xbuf, x01x20, 20))
break;
fputs (xbuf, mfile); nlines++;
}
fclose (mfile);
if (feof (infile))
break;
}
fclose (mfile);
fclose (infile);
exit (EXIT_SUCCESS);
return (EXIT_SUCCESS); /* gets rid of dumb compiler warning */
}