Main Page Class Hierarchy Alphabetical List Compound List Examples

rfc822/header.h
1/***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: header.h,v 1.16 2008-10-07 11:06:27 tat Exp $
6 ***************************************************************************/
7#ifndef _MIMETIC_RFC822_HEADER_H_
8#define _MIMETIC_RFC822_HEADER_H_
9#include <string>
10#include <deque>
11#include <cassert>
12#include <functional>
13#include <iostream>
14#include <mimetic/strutils.h>
15#include <mimetic/utils.h>
16#include <mimetic/rfc822/field.h>
17#include <mimetic/rfc822/mailbox.h>
18#include <mimetic/rfc822/messageid.h>
19#include <mimetic/rfc822/mailboxlist.h>
20#include <mimetic/rfc822/addresslist.h>
21
22 namespace mimetic
23{
24
25
26/// RFC822 header class object
27/*!
28 Use this class to build or parse message header fields.
29 This is a STL container so you can browse fields using iterators(see ex. below).
30
31 \sa <a href="../RFC/rfc822.txt">RFC822</a>
32 */
33 class Rfc822Header: public std::deque<Field>
34{
35 public:
36 struct find_by_name
37 {
38 typedef const Field argument_type;
39 typedef bool result_type;
40 find_by_name(const std::string&);
41 bool operator()(const Field&) const;
42 private:
43 const istring m_name;
44 };
45
46 bool hasField(const std::string&) const;
47
48 const Field& field(const std::string&) const;
49 Field& field(const std::string&);
50
51 const Mailbox& sender() const;
52 Mailbox& sender();
53 void sender(const Mailbox&);
54
55 const MailboxList& from() const;
56 MailboxList& from();
57 void from(const MailboxList&);
58
59 const AddressList& to() const;
60 AddressList& to();
61 void to(const AddressList&);
62
63 const std::string& subject() const;
64 std::string& subject();
65 void subject(const std::string&);
66
67 const AddressList& replyto() const;
68 AddressList& replyto();
69 void replyto(const AddressList&);
70
71 const AddressList& cc() const;
72 AddressList& cc();
73 void cc(const AddressList&);
74
75 const AddressList& bcc() const;
76 AddressList& bcc();
77 void bcc(const AddressList&);
78
79 const MessageId& messageid() const;
80 MessageId& messageid();
81 void messageid(const MessageId&);
82 protected:
83 template<typename T>
84 const T& getField(const std::string&) const;
85 template<typename T>
86 T& getField(const std::string&);
87 template<typename T>
88 void setField(const std::string&, const T&);
89};
90
91
92// template member functions
93 template<typename T>
94 const T& Rfc822Header::getField(const std::string& name) const
95{
96 const_iterator it;
97 it = find_if(begin(), end(), find_by_name(name));
98 if(it != end())
99 {
100 // cast away constness
101 Field& f = const_cast<Field&>(*it);
102 // to be sure that it's the correct type
103 FieldValue* pFv = f.m_pValue;
104 if(!pFv->typeChecked())
105 {
106 std::string val = pFv->str();
107 delete pFv;
108 pFv = new T(val);
109 f.m_pValue = pFv;
110 }
111 return static_cast<const T&>(*pFv);
112 } else {
113 static const T null;
114 return null;
115 }
116}
117 template<typename T>
118T& Rfc822Header::getField(const std::string& name)
119{
120 iterator it;
121 it = find_if(begin(), end(), find_by_name(name));
122 if(it != end())
123 {
124 FieldValue* pFv = it->m_pValue;
125 if(pFv == 0)
126 {
127 pFv = new T;
128 assert(pFv);
129 it->m_pValue = pFv;
130 }
131 // be sure that it's the correct type
132 else if(!pFv->typeChecked())
133 {
134 std::string val = pFv->str();
135 delete pFv;
136 pFv = new T(val);
137 it->m_pValue = pFv;
138 }
139 return static_cast<T&>(*pFv);
140 } else {
141 // insert and get the reference of the actual
142 // obj in the container, then modify its fields
143 Field f;
144 it = insert(end(), f);
145 it->name(name);
146 T* pT = new T;
147 assert(pT);
148 it->m_pValue = pT;
149 assert(it->m_pValue->typeChecked());
150 return *pT;
151 }
152}
153
154 template<typename T>
155 void Rfc822Header::setField(const std::string& name, const T& obj)
156{
157 // remove if already exists
158 iterator bit = begin(), eit = end();
159 iterator found = find_if(bit, eit, find_by_name(name));
160 if(found != eit)
161 erase(found);
162 // add field
163 Field f;
164 iterator it;
165 it = insert(end(), f);
166 it->name(name);
167 it->m_pValue = new T(obj);
168}
169
170}
171
172#endif
mimetic::Rfc822Header
RFC822 header class object.
Definition rfc822/header.h:34
mimetic
Definition body.h:18
mimetic::AddressList
List of Address.
Definition addresslist.h:39
mimetic::FieldValue
Value of an header field (base class).
Definition fieldvalue.h:18
mimetic::Field
Field class as defined by RFC822.
Definition field.h:43
mimetic::MailboxList
List of Mailbox objects.
Definition mailboxlist.h:35
mimetic::Mailbox
Represents a mailbox email address as defined in the RFC822.
Definition mailbox.h:47
mimetic::MessageId
Definition messageid.h:28