summaryrefslogtreecommitdiff
path: root/src/mime/mailbox.cc
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
committerHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
commit5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch)
tree1a81af141708b826e9c61e8a04019994fcca8298 /src/mime/mailbox.cc
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mime/mailbox.cc')
-rw-r--r--src/mime/mailbox.cc137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/mime/mailbox.cc b/src/mime/mailbox.cc
new file mode 100644
index 0000000..69f8140
--- /dev/null
+++ b/src/mime/mailbox.cc
@@ -0,0 +1,137 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <mime/mailbox.h>
21
22static int FromHead(const sref& body)
23{
24 sref tag,from,rest=body;
25 Split(tag,rest);
26 Split(from,rest);
27 return tag==from_tag&&htcTime(0,rest)>=0;
28}
29
30/////////////////////////////////////////////////////////////////////
31
32const mstring nextmsg_tag="\nFrom ";
33const mstring sep_tag="--";
34
35Message::Message()
36{
37}
38
39int Message::Special(sref line)
40{
41 line=Rest(line);
42 from=from0=First(line);
43 date=Rest(line);
44 idate=htcTime(0,date);
45 return idate>=0;
46}
47
48int Message::Handle(const sref& name,
49 const sref& rawval,
50 const sref& val,
51 const rrpile& opts)
52{
53 mstring mime;
54 if(Mime::Handle(name,rawval,val,opts)) return 1;
55 else if(eqn(name,from_tag)) {
56 swrite sw(from);
57 decodeHeader(sw,mime,rawval);
58 }
59 else if(eqn(name,subject_tag)) {
60 swrite sw(subject);
61 decodeHeader(sw,mime,rawval);
62 }
63 else if(eqn(name,to_tag)) {
64 swrite sw(to);
65 decodeHeader(sw,mime,rawval);
66 }
67 else if(eqn(name,msgid_tag)) msgid=rawval;
68 else if(eqn(name,received_tag)) received.push_back(rawval);
69 else return 0;
70 return 1;
71}
72
73void Message::putHead(mstream& out) const
74{
75 if(from0.nempty()) { out<<"From "<<from0<<" "<<date<<"\r\n"; }
76 if(putval(out,from_tag,from)) putend(out);
77 if(putval(out,subject_tag,subject)) putend(out);
78 if(putval(out,to_tag,to)) putend(out);
79 if(putval(out,msgid_tag,msgid)) putend(out);
80 for(spile::const_iterator p=received.begin();p!=received.end();p++)
81 if(putval(out,received_tag,*p)) putend(out);
82 Mime::putHead(out);
83}
84
85///////////////////////////////////////////////////////////////
86
87void AppendMessage(FILE* fp,const sref& msg)
88{
89 fwrite(msg.data(),1,msg.size(),fp);
90}
91
92void AppendMessages(FILE* fp,const Mailbox& box)
93{
94 for(int i=0;i<box.size();i++) AppendMessage(fp,box[i]);
95}
96
97int PutMessages(const Mailbox& box,const mstring& path)
98{
99 FILE* fp=fopen(path.c_str(),"w+");
100 if(!fp) return 0;
101 AppendMessages(fp,box);
102 return fclose(fp)==0;
103}
104
105int AppendMessages(const Mailbox& box,const mstring& path)
106{
107 FILE* fp=fopen(path.c_str(),"a+");
108 if(!fp) return 0;
109 AppendMessages(fp,box);
110 return fclose(fp)==0;
111}
112
113///////////////////////////////////////////////////////////////
114
115int GetMessages(Mailbox& box,const mstring& path)
116{
117 mstring buffer; if(buffer.load(path)<0) return -1;
118 sref next,rest=buffer;
119 int old=box.size();
120 while(rest.nempty()) {
121 Message msg(rest);
122 if(msg.from0.empty()) break; // should not happen, I guess
123
124 if(msg.cont_length.nempty())
125 next=msg.body.right(atoi(msg.cont_length));
126 else if(msg.cont_type_boundary.nempty())
127 next=msg.body.past_first(sep_tag+msg.cont_type_boundary+sep_tag).right_first(nextmsg_tag).popf();
128 else
129 next=rest.right_first(nextmsg_tag).popf();
130
131 box.push_back(rest.left(next-rest));
132
133 rest=next.right_first_not_of(cset_ws);
134 while(rest.nempty()&&!FromHead(rest)) rest=rest.right_first(nextmsg_tag).popf();
135 }
136 return box.size()-old;
137}