root/msg/multipart.cc

Revision 99057e4d73dbb905f06f5ccc4ea49708b8aaa0a8, 3.4 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 20 months ago)

Rework entity handling

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2010 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20#include "content_type.hh"
21#include "multipart.hh"
22#include "../tlate/data_model.hh"
23#include "../tlate/list_value.hh"
24
25namespace msg
26{
27        multipart::multipart(entity_header::ptr hdr, std::string str)
28                : entity(hdr)
29        {
30                content_type ct = get_content_type();
31                if (ct.get_type() != content_type::MULTIPART)
32                        throw std::logic_error("not multipart");
33                std::string boundary = ct.get_param("boundary");
34                size_t bs = str.find("\r\n--"+boundary);
35                preamble = str.substr(0,bs);
36                std::string default_ct = ct.get_subtype() == "digest"
37                        ? "message/rfc822"
38                        : "text/plain; charset=\"US-ASCII\"";
39                while (bs != std::string::npos)
40                {
41                        size_t be = str.find("\r\n", bs+2);
42                        if (be == std::string::npos) be = str.length() - 2;
43                        if (str.substr(bs + 4 + boundary.length(), 2) == "--")
44                        {
45                                trailer = be + 2 < str.length()
46                                        ? str.substr(be+2)
47                                        : "";
48                                break;
49                        }
50                        size_t ns = str.find("\r\n--"+boundary, be);
51                        std::string sub = str.substr(be+2, ns - be);
52                        entity::ptr sen(entity::mk(sub, false, default_ct));
53                        parts.push_back(sen);
54                        bs = ns;
55                }
56        }
57
58        boost::shared_ptr<tlate::value>
59        multipart::get_tlate_value(bool hide_addresses) const
60        {
61                tlate::list_value::ptr li(new tlate::list_value);
62                for (size_t i = 0; i < parts.size(); i++)
63                {
64                        entity::ptr en = parts[i];
65                        li->push_back(en->get_tlate_value(hide_addresses));
66                }
67                tlate::data_model::ptr am(new tlate::data_model);
68                am->insert("multipart", li);
69                return am;
70        }
71
72        std::string multipart::get_body() const
73        {
74                content_type ct = get_content_type();
75                std::string boundary = ct.get_param("boundary");
76                std::ostringstream os;
77                os << preamble;
78                for (size_t i = 0; i < parts.size(); i++)
79                {
80                        os << "\r\n--" << boundary << "\r\n";
81                        os << parts[i]->get_entity();
82                }
83                os << "\r\n--" << boundary << "--\r\n";
84                os << trailer;
85                return os.str();
86        }
87}
Note: See TracBrowser for help on using the browser.