1/*
2 * Copyright 2011, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef RESPONSE_H
6#define RESPONSE_H
7
8
9#include <stdexcept>
10
11#include <ObjectList.h>
12#include <String.h>
13
14
15namespace IMAP {
16
17
18class Argument;
19
20
21class ArgumentList : public BObjectList<Argument> {
22public:
23								ArgumentList();
24								~ArgumentList();
25
26			bool				Contains(const char* string) const;
27			BString				StringAt(int32 index) const;
28			bool				IsStringAt(int32 index) const;
29			bool				EqualsAt(int32 index,
30									const char* string) const;
31
32			ArgumentList&		ListAt(int32 index) const;
33			bool				IsListAt(int32 index) const;
34			bool				IsListAt(int32 index, char kind) const;
35
36			int32				IntegerAt(int32 index) const;
37			bool				IsIntegerAt(int32 index) const;
38
39			BString				ToString() const;
40};
41
42
43class Argument {
44public:
45								Argument();
46	virtual						~Argument();
47
48	virtual	BString				ToString() const = 0;
49};
50
51
52class ListArgument : public Argument {
53public:
54								ListArgument(char kind);
55
56			ArgumentList&		List() { return fList; }
57			char				Kind() { return fKind; }
58
59	virtual	BString				ToString() const;
60
61private:
62			ArgumentList		fList;
63			char				fKind;
64};
65
66
67class StringArgument : public Argument {
68public:
69								StringArgument(const BString& string);
70								StringArgument(const StringArgument& other);
71
72			const BString&		String() { return fString; }
73
74	virtual	BString				ToString() const;
75
76private:
77			BString				fString;
78};
79
80
81class ParseException : public std::exception {
82public:
83								ParseException();
84								ParseException(const char* message);
85	virtual						~ParseException();
86
87			const char*			Message() const { return fMessage; }
88
89protected:
90			const char*			fMessage;
91};
92
93
94class ExpectedParseException : ParseException {
95public:
96								ExpectedParseException(char expected,
97									char instead);
98
99protected:
100			char				fBuffer[64];
101};
102
103
104class Response : public ArgumentList {
105public:
106								Response();
107								~Response();
108
109			void				SetTo(const char* line) throw(ParseException);
110
111			bool				IsUntagged() const { return fTag == 0; }
112			int32				Tag() const { return fTag; }
113			bool				IsCommand(const char* command) const;
114			bool				IsContinuation() const { return fContinuation; }
115
116protected:
117			char				ParseLine(ArgumentList& arguments,
118									const char*& line);
119			void				Consume(const char*& line, char c);
120			void				ParseList(ArgumentList& arguments,
121									const char*& line, char start, char end);
122			void				ParseQuoted(ArgumentList& arguments,
123									const char*& line);
124			void				ParseLiteral(ArgumentList& arguments,
125									const char*& line);
126			void				ParseString(ArgumentList& arguments,
127									const char*& line);
128			BString				ExtractString(const char*& line);
129
130protected:
131			int32				fTag;
132			bool				fContinuation;
133};
134
135
136}	// namespace IMAP
137
138
139#endif // RESPONSE_H
140