1 //
2 // VMime library (http://www.vmime.org)
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3 of
8 // the License, or (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 GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // Linking this library statically or dynamically with other modules is making
20 // a combined work based on this library.  Thus, the terms and conditions of
21 // the GNU General Public License cover the whole combination.
22 //
23 
24 #include "vmime/config.hpp"
25 
26 
27 #if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_POP3
28 
29 
30 #include "vmime/net/pop3/POP3Utils.hpp"
31 #include "vmime/net/pop3/POP3Response.hpp"
32 
33 #include <sstream>
34 
35 
36 namespace vmime {
37 namespace net {
38 namespace pop3 {
39 
40 
41 // static
parseMultiListOrUidlResponse(shared_ptr<POP3Response> response,std::map<size_t,string> & result)42 void POP3Utils::parseMultiListOrUidlResponse(shared_ptr <POP3Response> response, std::map <size_t, string>& result)
43 {
44 	std::map <size_t, string> ids;
45 
46 	for (size_t i = 0, n = response->getLineCount() ; i < n ; ++i)
47 	{
48 		string line = response->getLineAt(i);
49 		string::iterator it = line.begin();
50 
51 		while (it != line.end() && (*it == ' ' || *it == '\t'))
52 			++it;
53 
54 		if (it != line.end())
55 		{
56 			size_t number = 0;
57 
58 			while (it != line.end() && (*it >= '0' && *it <= '9'))
59 			{
60 				number = (number * 10) + (*it - '0');
61 				++it;
62 			}
63 
64 			while (it != line.end() && !(*it == ' ' || *it == '\t')) ++it;
65 			while (it != line.end() && (*it == ' ' || *it == '\t')) ++it;
66 
67 			if (it != line.end())
68 			{
69 				result.insert(std::map <size_t, string>::value_type(number, string(it, line.end())));
70 			}
71 		}
72 	}
73 }
74 
75 
76 
77 class POP3MessageSetEnumerator : public messageSetEnumerator
78 {
79 public:
80 
POP3MessageSetEnumerator(const size_t msgCount)81 	POP3MessageSetEnumerator(const size_t msgCount)
82 		: m_msgCount(msgCount)
83 	{
84 
85 	}
86 
enumerateNumberMessageRange(const vmime::net::numberMessageRange & range)87 	void enumerateNumberMessageRange(const vmime::net::numberMessageRange& range)
88 	{
89 		size_t last = range.getLast();
90 		if (last == size_t(-1)) last = m_msgCount;
91 
92 		for (size_t i = range.getFirst() ; i <= last ; ++i)
93 			list.push_back(i);
94 	}
95 
enumerateUIDMessageRange(const vmime::net::UIDMessageRange &)96 	void enumerateUIDMessageRange(const vmime::net::UIDMessageRange& /* range */)
97 	{
98 		// Not supported
99 	}
100 
101 public:
102 
103 	std::vector <size_t> list;
104 
105 private:
106 
107 	size_t m_msgCount;
108 };
109 
110 
111 // static
messageSetToNumberList(const messageSet & msgs,const size_t msgCount)112 const std::vector <size_t> POP3Utils::messageSetToNumberList(const messageSet& msgs, const size_t msgCount)
113 {
114 	POP3MessageSetEnumerator en(msgCount);
115 	msgs.enumerate(en);
116 
117 	return en.list;
118 }
119 
120 
121 } // pop3
122 } // net
123 } // vmime
124 
125 
126 #endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_POP3
127 
128