Subject: MIME-files

From: "Koos Starfield" <[email-masked]>
Date: Mon, 03 Jul 2000 07:27:52 PDT

Hello ,

I need to read files which are in MIME format. Since i don't want to code a 
MIME parser myself, my question is:
Is this possible with c-client?
(I thought of the rfc822_parse_msg function but it will also expect a rfc822 
header (I just have MIME, no rfc822 header at all))
Before I start at it I wanted to know if someone has experience doing this, 
is it possible ??

greetings
Koos Starreveld
the Netherlands.

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

-- 
------------------------------------------------------------------
 For information about this mailing list, and its archives, see: 
 http://www.washington.edu/imap/c-client-list.html
------------------------------------------------------------------

---

From: amaury habrard <[email-masked]>
Date: Tue, 04 Jul 2000 12:22:23 +0200

Koos Starfield <[email-masked]> wrote:
'I need to read files which are in MIME format.'

I am developping a little mime parser using the c-client library,
I give you an example of my program, it is not finished and I think
there are still some mistakes, but it can be an example to see how to
process. And I have test all the types, but it works with MULTIPART
messages. (if you see big bugs tell me)

I hope this can help you.

After you have the body of your mesg 
(for example like this:
BODY * body=NULL; 
mail_fetchstructure(stream,no_msg,&body);
if(body)  parse_body(stream,no_msg,body,NIL,(long) 0);
/*where stream is the MAILSTREAM * of your mailbox and no_msg the
message number you want to parse*/)

you can use a parse function like this one (be careful it is not
complete but it works on some multipart message):
int parse_body(MAILSTREAM * stream,int no_msg,BODY * corps,char *
pfx,long i)
{
  char tmp[10];
  PART *part=NULL;	
  if (corps->type == TYPEMULTIPART) {/* "multipart"*/
   
    /*pfx gives the section of the mail*/
    if (pfx) sprintf (tmp,"%s%ld.",pfx,++i);
    else tmp[0] = '\0'; 
	       /*we examine each part*/
    for (i = 0,part = corps->nested.part; part!=NULL; part=part->next)
      {
	parse_body(stream,no_msg,&part->body,tmp,++i);
      }
  }else{
    
    switch(corps->type)
      {/*NO MULTIPART HERE*/
      case TYPETEXT:
	{
	  /*let's show that*/
	   char * init_mesg=NULL;
	   char * decode_msg=NULL;
	   char section[100];
	   long decode_length=0;
	   printf("ii:%ld\n",i);
	   if( pfx==NULL || !strcmp(pfx,"") )
	     sprintf(section,"%ld",i);
	   else sprintf(section,"%s.%ld",pfx,i);
	 
	   init_mesg=mail_fetchbody(stream,no_msg,section,
                                    &(corps->size.bytes));
	   
	   /*if encoded*/
	   switch(corps->encoding)
	     {
	       
	     case ENCBASE64:
	                                                      
decode_msg=(char*)rfc822_base64(init_mesg,corps->size.bytes,
                               &decode_length);
	       break;
	     case ENCQUOTEDPRINTABLE:
	       decode_msg=(char *)
rfc822_qprint(init_mesg,corps->size.bytes,&decode_length);
	       break;
	       
	     case ENC7BIT: 
	     case ENC8BIT:
	     case ENCBINARY:
	     case ENCOTHER:   
	     default: 
	       decode_msg=init_mesg;
	     }
	  
	  printf("%s\n",decode_msg );	  
	  break;
	}
      case TYPEMESSAGE: /*encapsulated message*/
	{
	  /*I'm not very sure*/
	  
	  if (!strcmp (corps->subtype,"RFC822") 
	      && (corps = corps->nested.msg->body)) 
	    {
	      if (corps->type == TYPEMULTIPART)
parse_body(stream,no_msg,corps,pfx,i-1);
	      else {			
		sprintf(tmp,"%s%ld.",pfx,i);
		parse_body(stream,no_msg,corps,tmp,(long) 0);
	      }
	    }
	  
	  break;
	case TYPEAPPLICATION:
	  break;
	case TYPEAUDIO: 
	  break;
	case TYPEIMAGE: /*GIF,JPEG, ...........*/
	  {
	    long  F=-1; /*FILE descriptor*/
	    char * init_mesg=NULL;
	    char * decode_msg=NULL;
	    char section[100];
	    long decode_length=0;
	 
	    if( pfx==NULL || !strcmp(pfx,"") )
	      sprintf(section,"%ld",i);
	    else sprintf(section,"%s.%ld",pfx,i);
	    
	    /*GET the message*/
	    init_mesg=mail_fetchbody(stream, no_msg,
section,&(corps->size.bytes));
	    
	    /*if encoded*/
	    switch(corps->encoding)
	      {
		
	      case ENCBASE64:
		decode_msg=(char *)
rfc822_base64(init_mesg,corps->size.bytes,&decode_length);
		break;
	      case ENCQUOTEDPRINTABLE:
		decode_msg=(char *)
rfc822_qprint(init_mesg,corps->size.bytes,&decode_length);
		break;
		
	      case ENC7BIT:
		
	      case ENC8BIT:
		
	      case ENCBINARY:
		
	      case ENCOTHER: /*default*/
		
	      default: 
	      decode_msg=init_mesg;
 }
    	  
	    if(!strcmp(corps->parameter->attribute,"NAME")) /*we have the
name of the
file*/	                                    	                                                                  
F=open(corps->parameter->value,O_WRONLY|O_CREAT|O_TRUNC,0660);
    else F=open("/tmp/tmpaux",O_WRONLY|O_CREAT|O_TRUNC,0660);
	 
	  if(F==-1)
	    {
	      fprintf(stderr,"Problems with the image_file\n");
	    }
	  else{  
	    write(F,decode_msg,decode_length);
	    close(F);
	  }
	  break;
	}
	case TYPEVIDEO: /*type video */
	case TYPEOTHER:
	default:
	}
      }
  }
 return 1;
}

amaury.

-------------------------------------------------
A L i N T o - Restez joignable - www.alinto.com