## tests/mm/test_mm.c

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. error_init
2. main
3. usage

       1 /***************************************
       2   $Revision: 1.17 $
       3
       4   Example code: MIME parser.
       5
       6   Design and implementation by: Daniele Arena
       7
       8   ******************/ /******************
       9   Modification History:
      10         daniele (20/07/2000) Created.
      11   ******************/ /******************
      12   Copyright (c) 2000                              RIPE NCC
      13
      14   All Rights Reserved
      15
      16   Permission to use, copy, modify, and distribute this software and its
      17   documentation for any purpose and without fee is hereby granted,
      18   provided that the above copyright notice appear in all copies and that
      19   both that copyright notice and this permission notice appear in
      20   supporting documentation, and that the name of the author not be
      21   used in advertising or publicity pertaining to distribution of the
      22   software without specific, written prior permission.
      23
      24   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      25   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      26   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      27   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      28   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      29   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      30   ***************************************/
      31
      32
      33 /* Included headers: */
      34
      35 /* Standard headers */
      36 #include <stdio.h>
      37 #include <signal.h>
      38 #include <string.h>
      39 #include <sys/time.h>
      40
      41
      42 /* This is the header to include the MM module*/
      43 #include "mm.h"
      44 #include "mail_parser.h"
      45 #include "erroutines.h"
      46
      47
      48 /* Other modules */
      49 #include "ca_configFns.h"
      50 #include "ca_dictSyms.h"
      51 #include "ca_macros.h"
      52
      53
      54 /* Global variables */
      55
      56 char *gpgcmd = NULL;
      57
      58
      59 /* This is the temporary file where stdin is stored by MM_store */
      60 /* #define MAIL_FILE "/tmp/daniele/mm.unprocessed" */
      61 #define DEFLT_CONFIG_FILENAME "ripedb.config"
      62
      63 /* Function declaration */
      64 int main (int argc, char* argv[]);
      65 void usage(char *argv[]);
      66
      67
      68 void error_init(int argc, char ** argv)
         /* [<][>][^][v][top][bottom][index][help] */
      69 {
      70
      71   char *slash;
      72   char progname[32];
      73
      74   slash = rindex(argv[0],'/');
      75   strncpy(progname, (slash != NULL) ? slash+1 : argv[0], 31);
      76   progname[31]=0;
      77
      78
      79   ER_init(progname, 0);
      80
      81   {
      82     char *err_msg = NULL;
      83     char *buf =
      84       "CREATE testdbg { FORMAT PROGNAME|PIDFULL|SEVCHAR|FACSYMB|TEXTLONG|DATETIME NAME /tmp/mmtest.dbg DATE }"
      85       "( FAC mm ASP MM_GEN SEV d-f ) "
      86       ;
      87     char *buf2 =
      88       "CREATE testinf { FORMAT PROGNAME|PIDFULL|SEVCHAR|FACSYMB|TEXTLONG|DATETIME NAME /tmp/mmtest.inf DATE }"
      89       "( FAC mm ASP MM_GEN SEV i-f ) "
      90       ;
      91
      92     int parsres = ER_parse_spec(buf, &err_msg);
      93
      94     if( parsres != 0 )
      95       {
      96         /* print only on failure */
      97         puts(err_msg);
      98       }
      99
     100     free(err_msg);
     101
     102     if( parsres == 0 )
     103       {
     104         /* success */
     105         char *pbuf = NULL;
     106
     107         er_print_paths(&pbuf);
     108         puts(pbuf);
     109         free(pbuf);
     110       }
     111     else
     112       die;
     113
     114
     115     parsres = ER_parse_spec(buf2, &err_msg);
     116
     117     if( parsres != 0 )
     118       {
     119         /* print only on failure */
     120         puts(err_msg);
     121       }
     122
     123     free(err_msg);
     124
     125     if( parsres == 0 )
     126       {
     127         /* success */
     128         char *pbuf = NULL;
     129
     130         er_print_paths(&pbuf);
     131         puts(pbuf);
     132         free(pbuf);
     133       }
     134     else
     135       die;
     136   }
     137
     138 }
     139
     140
     141 /* Main program - initialization */
     142
     143 int main (int argc, char *argv[])
         /* [<][>][^][v][top][bottom][index][help] */
     144 {
     145
     146   long debug = 0;
     147   long mesgno;
     148   int retcode;
     149   /* MM_header *mail_header = NULL; */
     150   /* EP_Mail_Descr *mail_header; */
     151   EP_Mail_Descr *mail_elmt;
     152   Mail_Header_Field *mhfp;
     153   char mail_file[FILENAMELEN];
     154   char *input_file;
     155   extern char *optarg;
     156   extern int optind;
     157   int ch;
     158   short sflg = 0;
     159   short fflg = 0;
     160   short errflg = 0;
     161   char *config_filename = NULL;
     162   char *tmpdir = NULL;
     163
     164   /* EP_mail_node *mailtree; */
     165
     166
     167   while ((ch = getopt(argc, argv, "sf:d:c:h")) != EOF)
     168     switch((char)ch)
     169       {
     170       case 's':
     171         if (fflg)
     172           errflg++;
     173         else
     174           {
     175             sflg++;
     176             input_file = (char *)malloc(STR_S);
     177             sprintf (input_file,"-");
     178           }
     179         break;
     180       case 'f':
     181         if (sflg)
     182           errflg++;
     183         else
     184           {
     185             fflg++;
     186             input_file = optarg;
     187           }
     188         break;
     189       case 'd':
     190         debug = atol (optarg);
     191         break;
     192       case 'c':
     193         config_filename = strdup(optarg);
     194         break;
     195       case 'h':
     196       default:
     197         errflg++;
     198       }
     199
     200   if (errflg)
     201     usage(argv);
     202
     203
     204   /* N.B.: There should be only one message in the file,
     205    * so mesgno should always be 1.
     206    * If you want a general MIME parser, you should add an option
     207    * to read mesgno.
     208    */
     209
     210
     211   /* For memory leak tests */
     212   /*
     213        for (;;)
     214        {
     215        printf ("Pass # %d\n",j++);
     216        for (k = 1; k < 10000000; k++)
     217        {}
     218        */
     219
     220   mesgno = 1;
     221
     222
     223
     224   /* Config stuff */
     225   /*  ca_populateDictionary(dictionary, VARS);
     226
     227    if(config_filename != NULL)
     228      ca_readConfig(config_filename, confVars, VARS);
     229    else
     230      ca_readConfig(DEFLT_CONFIG_FILENAME, confVars, VARS); */
     231
     232   if(config_filename != NULL)
     233     ca_init(config_filename);
     234   else
     235     ca_init(DEFLT_CONFIG_FILENAME);
     236
     237
     238   /* Get variables from config file */
     239   tmpdir = ca_get_tmpdir;
     240   gpgcmd = ca_get_pgpv;
     241
     242   /* Initialize error handling */
     243   error_init(argc, argv);
     244
     245
     246
     247   /*sprintf (mail_file,"%s",MAIL_FILE); */     /* the file where the mail message will be stored */
     248
     249
     250   /* Put stdin in the mail file */
     251
     252   /*if ((retcode = MM_store(input_file,mail_file, debug)) != 0)
     253     exit (retcode);
     254     */
     255
     256   /* Allocate memory for the header */
     257
     258   /* mail_header = (EP_Mail_Descr *)malloc(sizeof(EP_Mail_Descr)); */
     259
     260   /* mail_elmt = InitializeMailDescr(mail_file); */
     261
     262
     263   /* if ((retcode = MM_get_msg_headers(mail_file, mail_elmt, mesgno, debug)) != 0) */
     264   /* if ((mail_elmt = InitializeMailDescr(mail_file)) == NULL) */
     265   if ((mail_elmt = EP_ParseMail(input_file, tmpdir, "/home/daniele/.gnupg/pubring.gpg", gpgcmd)) == NULL)
     266     {
     267       /* Temporary... */
     268       retcode = 1;
     269
     270       printf ("Trouble in InitializeMailDescr()! Return code = %d\n",retcode);
     271       exit(retcode);
     272     }
     273   else
     274     {
     275
     276       printf ("Mail headers:\n\n");
     277
     278       printf ("From - %s",mail_elmt->from->field);
     279
     280       printf ("Subject - %s",mail_elmt->subject->field);
     281
     282       printf ("Date - %s",mail_elmt->date->field);
     283
     284       printf ("Message-ID - %s",mail_elmt->message_id->field);
     285
     286       printf ("Reply-To - ");
     287       for (mhfp = mail_elmt->reply_to; mhfp != NULL; mhfp = mhfp->next)
     288         printf ("%s",mhfp->field);
     289
     290       printf ("Cc - %s",mail_elmt->cc->field);
     291
     292       printf ("Content-Type - %s",mail_elmt->content_type->field);
     293
     294       /*  } */
     295
     296
     297   /* mailtree = EP_InitializeRootNode(mail_file); */
     298
     299       /* if ((retcode = MM_extract_mime(mail_file, NULL, mail_elmt->tree, debug)) != 0)
     300     {
     301       exit (retcode);
     302     }
     303   else
     304     { */
     305
     306       /* replace this with tree surfing */
     307       /* partptr = part_list->head;
     308
     309       while (partptr != NULL)
     310         {
     311
     312           printf("-----------------------------------------\n");
     313           printf ("Section: %s\n",partptr->number);
     314           printf ("Content-type: %s\n",partptr->type);
     315           if (partptr->supported)
     316             printf ("Supported\n");
     317           else
     318             printf ("Unsupported\n");
     319           if (debug) printf ("=== DEBUG: Reading file %s...\n",partptr->file);
     320           read_file(partptr->file);
     321
     322           printf ("\n\n");
     323           partptr = partptr->next;
     324         } */
     325
     326
     327       /* Clean up the temporary files */
     328       /* MM_cleanup(part_list,debug); */
     329
     330       EP_ShowTree(mail_elmt->tree);
     331
     332       /* EP_MailDescrCleanUp(mail_elmt); */
     333     }
     334
     335   /* Closing the memory leak test... */
     336   /* }  */
     337
     338   exit (0);
     339
     340
     341 } /* main */
     342
     343
     344
     345 void usage(char *argv[])
         /* [<][>][^][v][top][bottom][index][help] */
     346 {
     347   printf ("Usage: \n");
     348   printf ("%s -s [-d debug] < input-message \n", argv[0]);
     349   printf ("%s -f input-message [-d debug] \n", argv[0]);
     350   printf ("    Default debug level: %d\n",DEFAULT_DEBUG);
     351   /* DEFAULT_DEBUG is defined in mm.h */
     352   exit (1);
     353 }
     354

/* [<][>][^][v][top][bottom][index][help] */