mime.cc (4547B)
1 /* -*- Mode: c++; -*- */ 2 /* -------------------------------------------------------------------- 3 * Filename: 4 * mime.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 Binc::MimeDocument::MimeDocument(void) : MimePart() 56 { 57 allIsParsed = false; 58 headerIsParsed = false; 59 } 60 61 //------------------------------------------------------------------------ 62 Binc::MimeDocument::~MimeDocument(void) 63 { 64 } 65 66 //------------------------------------------------------------------------ 67 void Binc::MimeDocument::clear(void) const 68 { 69 members.clear(); 70 h.clear(); 71 headerIsParsed = false; 72 allIsParsed = false; 73 } 74 75 //------------------------------------------------------------------------ 76 void Binc::MimePart::clear(void) const 77 { 78 members.clear(); 79 h.clear(); 80 } 81 82 //------------------------------------------------------------------------ 83 Binc::MimePart::MimePart(void) 84 { 85 size = 0; 86 messagerfc822 = false; 87 multipart = false; 88 89 nlines = 0; 90 nbodylines = 0; 91 } 92 93 //------------------------------------------------------------------------ 94 Binc::MimePart::~MimePart(void) 95 { 96 } 97 98 //------------------------------------------------------------------------ 99 Binc::HeaderItem::HeaderItem(void) 100 { 101 } 102 103 //------------------------------------------------------------------------ 104 Binc::HeaderItem::HeaderItem(const string &key, const string &value) 105 { 106 this->key = key; 107 this->value = value; 108 } 109 110 //------------------------------------------------------------------------ 111 Binc::Header::Header(void) 112 { 113 } 114 115 //------------------------------------------------------------------------ 116 Binc::Header::~Header(void) 117 { 118 } 119 120 //------------------------------------------------------------------------ 121 bool Binc::Header::getFirstHeader(const string &key, HeaderItem &dest) const 122 { 123 string k = key; 124 lowercase(k); 125 126 for (vector<HeaderItem>::const_iterator i = content.begin(); 127 i != content.end(); ++i) { 128 string tmp = (*i).getKey(); 129 lowercase(tmp); 130 131 if (tmp == k) { 132 dest = *i; 133 return true; 134 } 135 } 136 return false; 137 } 138 139 //------------------------------------------------------------------------ 140 bool Binc::Header::getAllHeaders(const string &key, vector<HeaderItem> &dest) const 141 { 142 string k = key; 143 lowercase(k); 144 145 for (vector<HeaderItem>::const_iterator i = content.begin(); 146 i != content.end(); ++i) { 147 string tmp = (*i).getKey(); 148 lowercase(tmp); 149 if (tmp == k) 150 dest.push_back(*i); 151 } 152 153 return (dest.size() != 0); 154 } 155 156 //------------------------------------------------------------------------ 157 void Binc::Header::clear(void) const 158 { 159 content.clear(); 160 } 161 162 //------------------------------------------------------------------------ 163 void Binc::Header::add(const string &key, const string &value) 164 { 165 content.push_back(HeaderItem(key, value)); 166 } 167 168 //------------------------------------------------------------------------ 169 void Binc::Header::print(void) const 170 { 171 IO &logger = IOFactory::getInstance().get(2); 172 173 vector<HeaderItem>::const_iterator i = content.begin(); 174 while (i != content.end()) { 175 logger << "[" << (*i).getKey() << "]=[" << (*i).getValue() << "]" << endl; 176 ++i; 177 } 178 }