[comp.mail.elm] Mh -> Elm Conversion Program

boerio@orchestra.ecn.purdue.edu (Jeff Boerio) (12/04/90)

It's been a little while, but a few people have asked me about my mh2elm
conversion program.  I wrote this program when someone wanted to switch
from mh to elm, but couldn't because all the mail files are different.

This program is proven to work.  It can handle any number of mh messages as
indicated on the command line.  See the comments for directions on using
it.

     - Jeff

--
Jeff Boerio (boerio@orchestra.ecn.purdue.edu)
Purdue University ECN Software Support Programmer
Rush Quote: "Don't ask me, I'm just improvising"
Pink Quote: "There's someone in my head, but it's not me."



/*
 *
 *  $Date: 89/10/29 23:48:14 $
 *  $Revision: 1.3 $
 *
 *  mh2elm is a utility that will take any number of mh mail messages and
 *  convert their headers into a format that Elm will understand.
 *
 *  Usage:  mh2elm file [file ...]
 *
 *  where file is and number of mh messages.  mh2elm will automatically
 *  store the result in ~/mbox.
 *
 *  The problem:  The first line of a mh message header is like this:
 *
 *          Return-Path: userid
 *
 *  Elm doesn't like this at all!  Elm expects to see:
 *
 *          From userid  time
 *
 *  where time is the time the message was received.  The problem with this
 *  is that this time is not consistant with the way the time is recorded
 *  in other parts of the message header.
 *
 *    Jeffrey P. Boerio, ECN Student Programmer
 *    Copyright (c) 1989 Tg Programming
 *
 *  $Log:	mh2elm.c,v $
 * Revision 1.3  89/10/29  23:39:45  boerio
 * Cleaned up some unused variables and formatting.
 * 
 * Revision 1.2  89/10/29  23:30:08  boerio
 * Working version.  Handles any number of mh messages, and converts them
 * all to an Elm-readable format.
 * 
 * Revision 1.1  89/10/29  23:48:14  boerio
 * Initial revision
 * 
 *
 */

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define RETURN_PATH 14
#define LINES 3
#define MAX 80
#define MBOX "/mbox"
#define DAY_OFFSET 13
#define MO_OFFSET 18
#define YR_OFFSET 25
#define TIME_OFFSET 28

main(argc, argv)
  int argc;
  char *argv[];

{
  FILE *fp, *mbox;
  int i, j, k, c;
  char fname[MAX];
  char str[RETURN_PATH], login[MAX], buffer[MAX], *result;
  char header[LINES][MAX];
  char *getenv();
  char *pathvar, *dir;
  char day[4], month[7], year[3], time[9];

  /*  Do we have any arguments, or not?  */

  if (argc == 1) {
    printf ("Usage: %s file ...\n", argv[0]);
    exit(1);
  }

  /*  OK, let's start reading them  */

  for (i = 1; i < 3; i++);
    bzero(&header[i][0], 80);
  for (i = 1; i < argc; i++) {
    printf("Processing mh file %s ... ", argv[i]);
    if ((fp = fopen(argv[i], "r")) != NULL) {
      result=fgets(str, RETURN_PATH, fp);  /*  this is meaningless  */
      result=fgets(login, MAX, fp);  /* here's who its from  */
      for (j = 0; j <= strlen(result); j++) /* get rid of nasty NL  */
	if (isspace(login[j]))
	  login[j] = ' ';

      /*  Let's get the mailbox ready  */

      result=fgets(str, MAX, fp);         /*  This is the second line of  */
      sprintf(&header[2][0], "%s", str);  /*  the header, no proc. needed */

      result=fgets(str, MAX, fp);
      sprintf(&header[3][0], "%s", str);  /*  this line has our time!  */

      for (k = 0; k < 3; k++)                   /*  Get the day  */
	day[k] = header[3][k + DAY_OFFSET];
      day[3] = '\0';

      for (k = 0; k < 6; k++)                   /*  Get the month  */
	month[k] = header[3][k + MO_OFFSET];
      month[6] = '\0';

      for (k = 0; k < 2; k++)                   /*  Get the year  */
	year[k] = header[3][k + YR_OFFSET];
      year[2] = '\0';


      for (k = 0; k < 8; k++)                   /*  Get the time  */
	time[k] = header[3][k + TIME_OFFSET];
      time[8] = '\0';

      dir=MBOX;
      pathvar=getenv("HOME");
      sprintf(fname, "%s%s", pathvar, dir);
      if ((mbox = fopen(fname, "a+")) != NULL) {
	sprintf(buffer, "From %s %s %s %s 19%s\n",     /*  Put it in the */
	  login, day, month, time, year);              /*  right format  */
	fprintf(mbox, "%s", buffer);                   /*  and file it!  */
      } else {
        printf("%s: cannot open file %s\n", argv[0], argv[i]);
	break;
      }
      while (feof(fp) != EOF) {                 /*  get rest of message  */
	c=getc(fp);
	if (c == -1)
	  break;
	putc(c, mbox);
      }
      fclose(fp);                               /*  close our files      */
      fclose(mbox);
    } else printf("%s: cannot open file %s\n", argv[0], argv[i]);
    printf("finished.\n");
  }

}
--
Jeff Boerio (boerio@orchestra.ecn.purdue.edu)
Purdue University ECN Software Support Programmer
"Don't ask me, I'm just improvising" - Neil Peart, Rush
"There's someone in my head, but it's not me." - Pink Floyd