bincimap

Log | Files | Refs | LICENSE

mime-getpart.cc (2585B)


      1 /* -*- Mode: c++; -*- */
      2 /*  --------------------------------------------------------------------
      3  *  Filename:
      4  *    mime-getpart.cc
      5  *  
      6  *  Description:
      7  *    Implementation of main mime parser components
      8  *
      9  *  Authors:
     10  *    Andreas Aardal Hanssen <andreas-binc curly bincimap spot org>
     11  *
     12  *  Bugs:
     13  *
     14  *  ChangeLog:
     15  *
     16  *  --------------------------------------------------------------------
     17  *  Copyright 2002-2005 Andreas Aardal Hanssen
     18  *
     19  *  This program is free software; you can redistribute it and/or modify
     20  *  it under the terms of the GNU General Public License as published by
     21  *  the Free Software Foundation; either version 2 of the License, or
     22  *  (at your option) any later version.
     23  *
     24  *  This program is distributed in the hope that it will be useful,
     25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     27  *  GNU General Public License for more details.
     28  *
     29  *  You should have received a copy of the GNU General Public License
     30  *  along with this program; if not, write to the Free Software
     31  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
     32  *  --------------------------------------------------------------------
     33  */
     34 #ifdef HAVE_CONFIG_H
     35 #include <config.h>
     36 #endif
     37 
     38 #include "mime.h"
     39 #include "convert.h"
     40 #include "io.h"
     41 #include <string>
     42 #include <vector>
     43 #include <map>
     44 #include <exception>
     45 #include <iostream>
     46 
     47 #include <string.h>
     48 #include <ctype.h>
     49 #include <stdio.h>
     50 #include <errno.h>
     51 
     52 using namespace ::std;
     53 
     54 //------------------------------------------------------------------------
     55 const Binc::MimePart *Binc::MimePart::getPart(const string &findpart,
     56 					      string genpart, FetchType fetchType) const
     57 {
     58   if (findpart == genpart)
     59     return this;
     60 
     61   if (isMultipart()) {
     62     if (members.size() != 0) {
     63       vector<MimePart>::const_iterator i = members.begin();
     64       int part = 1;
     65       while (i != members.end()) {
     66 	BincStream ss;
     67 
     68 	ss << genpart;
     69 	if (genpart != "")
     70 	  ss << ".";
     71 	ss << part;
     72 	
     73 	const MimePart *m;
     74 	if ((m = (*i).getPart(findpart, ss.str())) != 0) {
     75 	  if (fetchType == FetchHeader && m->isMessageRFC822())
     76 	    m = &m->members[0];
     77 
     78 	  return m;
     79 	}
     80 
     81 	++i;
     82 	++part;
     83       }
     84     }
     85   } else if (isMessageRFC822()) {
     86     if (members.size() == 1) {
     87       const MimePart *m = members[0].getPart(findpart, genpart);
     88       return m;
     89     } else {
     90       return 0;
     91     }
     92   } else {
     93     // Singlepart
     94     if (genpart != "")
     95       genpart += ".";
     96     genpart += "1";
     97 
     98     if (findpart == genpart)
     99       return this;
    100   }
    101 
    102   return 0;
    103 }