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
22namespace 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 */
33class Rfc822Header: public std::deque<Field>
34{
35public:
36 struct find_by_name:
37 public std::unary_function<const Field, bool>
38 {
39 find_by_name(const std::string&);
40 bool operator()(const Field&) const;
41 private:
42 const istring m_name;
43 };
44
45 bool hasField(const std::string&) const;
46
47 const Field& field(const std::string&) const;
48 Field& field(const std::string&);
49
50 const Mailbox& sender() const;
51 Mailbox& sender();
52 void sender(const Mailbox&);
53
54 const MailboxList& from() const;
55 MailboxList& from();
56 void from(const MailboxList&);
57
58 const AddressList& to() const;
59 AddressList& to();
60 void to(const AddressList&);
61
62 const std::string& subject() const;
63 std::string& subject();
64 void subject(const std::string&);
65
66 const AddressList& replyto() const;
67 AddressList& replyto();
68 void replyto(const AddressList&);
69
70 const AddressList& cc() const;
71 AddressList& cc();
72 void cc(const AddressList&);
73
74 const AddressList& bcc() const;
75 AddressList& bcc();
76 void bcc(const AddressList&);
77
78 const MessageId& messageid() const;
79 MessageId& messageid();
80 void messageid(const MessageId&);
81protected:
82 template<typename T>
83 const T& getField(const std::string&) const;
84 template<typename T>
85 T& getField(const std::string&);
86 template<typename T>
87 void setField(const std::string&, const T&);
88};
89
90
91// template member functions
92template<typename T>
93const T& Rfc822Header::getField(const std::string& name) const
94{
95 const_iterator it;
96 it = find_if(begin(), end(), find_by_name(name));
97 if(it != end())
98 {
99 // cast away constness
100 Field& f = const_cast<Field&>(*it);
101 // to be sure that it's the correct type
102 FieldValue* pFv = f.m_pValue;
103 if(!pFv->typeChecked())
104 {
105 std::string val = pFv->str();
106 delete pFv;
107 pFv = new T(val);
108 f.m_pValue = pFv;
109 }
110 return static_cast<const T&>(*pFv);
111 } else {
112 static const T null;
113 return null;
114 }
115}
116template<typename T>
117T& Rfc822Header::getField(const std::string& name)
118{
119 iterator it;
120 it = find_if(begin(), end(), find_by_name(name));
121 if(it != end())
122 {
123 FieldValue* pFv = it->m_pValue;
124 if(pFv == 0)
125 {
126 pFv = new T;
127 assert(pFv);
128 it->m_pValue = pFv;
129 }
130 // be sure that it's the correct type
131 else if(!pFv->typeChecked())
132 {
133 std::string val = pFv->str();
134 delete pFv;
135 pFv = new T(val);
136 it->m_pValue = pFv;
137 }
138 return static_cast<T&>(*pFv);
139 } else {
140 // insert and get the reference of the actual
141 // obj in the container, then modify its fields
142 Field f;
143 it = insert(end(), f);
144 it->name(name);
145 T* pT = new T;
146 assert(pT);
147 it->m_pValue = pT;
148 assert(it->m_pValue->typeChecked());
149 return *pT;
150 }
151}
152
153template<typename T>
154void Rfc822Header::setField(const std::string& name, const T& obj)
155{
156 // remove if already exists
157 iterator bit = begin(), eit = end();
158 iterator found = find_if(bit, eit, find_by_name(name));
159 if(found != eit)
160 erase(found);
161 // add field
162 Field f;
163 iterator it;
164 it = insert(end(), f);
165 it->name(name);
166 it->m_pValue = new T(obj);
167}
168
169}
170
171#endif
RFC822 header class object.
Definition: rfc822/header.h:34
Definition: body.h:18
List of Address.
Definition: addresslist.h:39
Value of an header field (base class)
Definition: fieldvalue.h:18
Field class as defined by RFC822.
Definition: field.h:43
List of Mailbox objects.
Definition: mailboxlist.h:35
Represents a mailbox email address as defined in the RFC822.
Definition: mailbox.h:47
Definition: messageid.h:28