root/msg/entity_header.hh

Revision 11cf105fe323361072281747558cb61752f33fa8, 4.3 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 20 months ago)

#60: Add a footer to email distributed posts

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#ifndef GUARD_MSG_ENTITY_HEADER_HH
21#define GUARD_MSG_ENTITY_HEADER_HH
22
23#include "content_type.hh"
24
25#include <boost/algorithm/string/case_conv.hpp>
26#include <boost/date_time/posix_time/posix_time.hpp>
27#include <boost/shared_ptr.hpp>
28#include <list>
29#include <map>
30#include <string>
31
32namespace msg
33{
34        class entity_header
35        {
36        public:
37                typedef boost::shared_ptr<entity_header> ptr;
38                typedef boost::shared_ptr<const entity_header> const_ptr;
39                typedef std::list<std::string>::const_iterator field_iterator;
40
41                // convenience wrapper
42                static ptr mk(std::string &str, bool validate,
43                              std::string default_ct) {
44                        ptr rv(new entity_header(str, validate, default_ct));
45                        return rv;
46                }
47
48                explicit entity_header(std::string default_ct)
49                        : the_date(boost::posix_time::not_a_date_time)
50                        , default_ct(default_ct)
51                        {}
52
53                // NOTE! Overwrites msg, leaving the body in it
54                explicit entity_header(std::string &msg, bool validate,
55                                       std::string default_ct);
56
57                entity_header(const entity_header &o)
58                        : field_content(o.field_content)
59                        , the_date(o.the_date)
60                        , default_ct(o.default_ct)
61                        { reindex(); }
62
63                entity_header &operator=(const entity_header &o) {
64                        if (this != &o)
65                        {
66                                field_content = o.field_content;
67                                the_date = o.the_date;
68                                default_ct = o.default_ct;
69                                reindex();
70                        }
71                        return *this;
72                }
73
74                boost::posix_time::ptime parsed_date() const;
75
76                field_iterator fields_begin() const {
77                        return field_content.begin();
78                }
79
80                field_iterator fields_end() const {
81                        return field_content.end();
82                }
83
84                std::string get_header() const;
85
86                bool has_field(std::string s) const{
87                        boost::to_lower(s);
88                        return fields.find(s) != fields.end();
89                }
90
91                // returns the field unprocessed
92                std::string get_field(std::string s, bool full) const;
93
94                void add_field(std::string name, std::string body) {
95                        field_content.push_front(name + ": " + body + "\r\n");
96                        boost::to_lower(name);
97                        fields[name] = field_content.begin();
98                        if (name == "date")
99                                the_date = boost::posix_time::not_a_date_time;
100                }
101                void replace_field(std::string name, std::string body);
102                void delete_field(std::string name);
103
104                content_type get_content_type() const;
105
106        private:
107                // this split is to both provide a fast name lookup
108                // and to preserve the field ordering
109                std::list<std::string> field_content; // includes the names
110                std::map<std::string, std::list<std::string>::iterator> fields;
111                mutable boost::posix_time::ptime the_date;
112                std::string default_ct;
113
114                void reindex();
115        };
116}
117
118#endif /* GUARD_MSG_ENTITY_HEADER_HH */
Note: See TracBrowser for help on using the browser.