summaryrefslogtreecommitdiff
path: root/src/mime/mailbox.cc
blob: 69f814069b543aec604348bb050c5fedb4264c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <mime/mailbox.h>

static int FromHead(const sref& body)
{
  sref tag,from,rest=body;
  Split(tag,rest);
  Split(from,rest);
  return tag==from_tag&&htcTime(0,rest)>=0;
}

/////////////////////////////////////////////////////////////////////

const mstring nextmsg_tag="\nFrom ";
const mstring sep_tag="--";

Message::Message()
{
}

int Message::Special(sref line)
{
  line=Rest(line);
  from=from0=First(line);
  date=Rest(line);
  idate=htcTime(0,date);
  return idate>=0;
}

int Message::Handle(const sref& name,
                    const sref& rawval,
                    const sref& val,
                    const rrpile& opts)
{
  mstring mime;
  if(Mime::Handle(name,rawval,val,opts)) return 1;
  else if(eqn(name,from_tag)) {
    swrite sw(from);
    decodeHeader(sw,mime,rawval);
  }
  else if(eqn(name,subject_tag)) {
    swrite sw(subject);
    decodeHeader(sw,mime,rawval);
  }
  else if(eqn(name,to_tag)) {
    swrite sw(to);
    decodeHeader(sw,mime,rawval);
  }
  else if(eqn(name,msgid_tag)) msgid=rawval;
  else if(eqn(name,received_tag)) received.push_back(rawval);
  else return 0;
  return 1;
}

void Message::putHead(mstream& out) const
{
  if(from0.nempty()) { out<<"From "<<from0<<" "<<date<<"\r\n"; }
  if(putval(out,from_tag,from)) putend(out);
  if(putval(out,subject_tag,subject)) putend(out);
  if(putval(out,to_tag,to)) putend(out);
  if(putval(out,msgid_tag,msgid)) putend(out);
  for(spile::const_iterator p=received.begin();p!=received.end();p++)
    if(putval(out,received_tag,*p)) putend(out);
  Mime::putHead(out);
}

///////////////////////////////////////////////////////////////

void AppendMessage(FILE* fp,const sref& msg)
{
  fwrite(msg.data(),1,msg.size(),fp);
}

void AppendMessages(FILE* fp,const Mailbox& box)
{
  for(int i=0;i<box.size();i++) AppendMessage(fp,box[i]);
}

int PutMessages(const Mailbox& box,const mstring& path)
{
  FILE* fp=fopen(path.c_str(),"w+");
  if(!fp) return 0;
  AppendMessages(fp,box);
  return fclose(fp)==0;
}

int AppendMessages(const Mailbox& box,const mstring& path)
{
  FILE* fp=fopen(path.c_str(),"a+");
  if(!fp) return 0;
  AppendMessages(fp,box);
  return fclose(fp)==0;
}

///////////////////////////////////////////////////////////////

int GetMessages(Mailbox& box,const mstring& path)
{
  mstring buffer; if(buffer.load(path)<0) return -1;
  sref next,rest=buffer;
  int old=box.size();
  while(rest.nempty()) {
    Message msg(rest);
    if(msg.from0.empty()) break; // should not happen, I guess

    if(msg.cont_length.nempty())
      next=msg.body.right(atoi(msg.cont_length));
    else if(msg.cont_type_boundary.nempty())
      next=msg.body.past_first(sep_tag+msg.cont_type_boundary+sep_tag).right_first(nextmsg_tag).popf();
    else
      next=rest.right_first(nextmsg_tag).popf();

    box.push_back(rest.left(next-rest));

    rest=next.right_first_not_of(cset_ws);
    while(rest.nempty()&&!FromHead(rest)) rest=rest.right_first(nextmsg_tag).popf();
  }
  return box.size()-old;
}