summaryrefslogtreecommitdiffstats
path: root/libmail/addmessage.C
blob: fe33dc24c754ae80123234fcaf6a9f2d5d428f8a (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
138
139
140
141
142
143
144
145
146
147
148
/*
** Copyright 2002-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "addmessage.H"
#include "attachments.H"
#include <cstring>
#include <errno.h>

// Default method implementation.

mail::addMessage::addMessage(mail::account *ptr) : mail::ptr<mail::account>(ptr),
				       messageDate(0)
{
}

bool mail::addMessage::checkServer()
{
	if (isDestroyed())
	{
		fail("Server connection closed.");
		return false;
	}

	return true;
}

mail::addMessage::~addMessage()
{
}

mail::addMessagePull::addMessagePull() : messageDate(0)
{
}

mail::addMessagePull::~addMessagePull()
{
}

//
// Default MIME assembly implementation
//

void mail::addMessage::assembleContent(size_t &handleRet,
				       const mail::Attachment &a,
				       mail::callback &cb)
{
	att_list.push_back(a);

	std::list<mail::Attachment>::iterator i=att_list.end();
	att_list_vec.push_back(--i);
	handleRet=att_list_vec.size()-1;
	cb.success("Ok.");
}

void mail::addMessage::assembleMessageRfc822(size_t &handleRet,
					     std::string headers,
					     size_t n,
					     mail::callback &cb)
{
	if (n >= att_list_vec.size())
	{
		errno=EINVAL;
		throw strerror(errno);
	}

	std::vector<mail::Attachment *> a;

	a.push_back(&*att_list_vec[n]);

	assembleContent(handleRet, Attachment(headers, a), cb);
}

void mail::addMessage::assembleMultipart(size_t &handleRet,
					 std::string headers,
					 const std::vector<size_t> &atts,
					 std::string type,
					 const mail::mimestruct
					 ::parameterList &typeParams,
					 mail::callback &cb)
{
	std::vector<Attachment *> parts;
	std::vector<size_t>::const_iterator b=atts.begin(), e=atts.end();

	while (b != e)
	{
		if (*b >= att_list_vec.size())
		{
			errno=EINVAL;
			throw strerror(errno);
		}

		parts.push_back(&*att_list_vec[*b]);
		++b;
	}

	assembleContent(handleRet, Attachment(headers, parts, type,
					      typeParams), cb);
}

bool mail::addMessage::assemble()
{
	std::vector<std::list<mail::Attachment>::iterator>::const_iterator
		e=att_list_vec.end();

	if (e == att_list_vec.begin())
		return true;

	--e;

	(*e)->begin();

	bool errflag=false;

	std::string msg;

	while ((msg=(*e)->generate(errflag)).size() > 0)
	{
		saveMessageContents(msg);
	}

	if (errflag)
		return false;
	return true;
}

bool mail::addMessage::chkMsgNum(mail::account *ptr, std::string msgUid,
				 size_t &n)
{
	size_t msgCount=ptr->getFolderIndexSize();
	size_t i;

	if (n < msgCount && ptr->getFolderIndexInfo(n).uid == msgUid)
		return true;
	for (i=1; i<msgCount; i++)
	{
		size_t j= (n+msgCount-i) % msgCount;

		if (ptr->getFolderIndexInfo(j).uid == msgUid)
		{
			n=j;
			return true;
		}
	}
	return false;
}