Bincimap 2.0.16
Easy Imapping
Loading...
Searching...
No Matches
broker.cc
Go to the documentation of this file.
1
7#include <map>
8#include <string>
9
10#include "broker.h"
11#include "convert.h"
12#include "operators.h"
13#include "recursivedescent.h"
14#include "session.h"
15
16using namespace ::std;
17using namespace Binc;
18
19//----------------------------------------------------------------------
20BrokerFactory::BrokerFactory(void)
21{
22 brokers[Session::NONAUTHENTICATED] = new Broker();
23 brokers[Session::AUTHENTICATED] = new Broker();
24 brokers[Session::SELECTED] = new Broker();
25}
26
27//----------------------------------------------------------------------
29{
30 for (map<int, Broker *>::iterator i = brokers.begin();
31 i != brokers.end(); ++i)
32 delete i->second;
33}
34
35//----------------------------------------------------------------------
37{
38 static BrokerFactory brokerfactory;
39 return brokerfactory;
40}
41
42//----------------------------------------------------------------------
43void BrokerFactory::addCapability(const std::string &c)
44{
45 for (map<int, Broker *>::iterator i = brokers.begin();
46 i != brokers.end(); ++i) {
48 o = dynamic_cast<CapabilityOperator*>(i->second->get("CAPABILITY"));
49 if (o != 0) {
50 o->addCapability(c);
51 break;
52 }
53 }
54}
55
56//----------------------------------------------------------------------
57void BrokerFactory::assign(const string &fname, Operator *o)
58{
59 int deletable = true;
60 for (map<int, Broker *>::iterator i = brokers.begin();
61 i != brokers.end(); ++i)
62 if (i->first & o->getState()) {
63 i->second->assign(fname, o, deletable);
64 deletable = false;
65 }
66}
67
68//----------------------------------------------------------------------
69Operator *BrokerFactory::getOperator(int state, const string &name) const
70{
71 if (brokers.find(state) == brokers.end())
72 return 0;
73 else
74 return brokers.find(state)->second->get(name);
75}
76
77//----------------------------------------------------------------------
79{
80 if (brokers.find(state) == brokers.end()) {
81 setLastError("No appropriate broker for state.");
82 return 0;
83 }
84
85 return brokers[state];
86}
87
88//----------------------------------------------------------------------
90{
91}
92
93//----------------------------------------------------------------------
95{
96}
97
98//----------------------------------------------------------------------
99void Broker::assign(const string &fname, Operator *o, bool deletable)
100{
101 deletables[fname] = deletable;
102 operators[fname] = o;
103}
104
105//----------------------------------------------------------------------
106Operator *Broker::get(const string &name) const
107{
108 if (operators.find(name) == operators.end()) return 0;
109
110 return operators.find(name)->second;
111}
112
113//----------------------------------------------------------------------
115{
116 Session &session = Session::getInstance();
117
118 string tag;
119 string cmd;
120
121 switch (expectTag(tag)) {
122 case Operator::ACCEPT:
123 break;
124 case Operator::REJECT:
125 session.setLastError("Syntax error; first token must be a tag");
126 case Operator::ERROR:
127 return Operator::ERROR;
129 return Operator::TIMEOUT;
130 }
131
132 switch (expectSPACE()) {
133 case Operator::ACCEPT:
134 break;
135 case Operator::REJECT:
136 session.setLastError("Syntax error; second token must be a SPACE");
137 case Operator::ERROR:
138 return Operator::ERROR;
140 return Operator::TIMEOUT;
141 }
142
143 switch (expectAstring(cmd)) {
144 case Operator::ACCEPT:
145 break;
146 case Operator::REJECT:
147 session.setLastError("Syntax error; third token must be a command");
148 case Operator::ERROR:
149 return Operator::ERROR;
151 return Operator::TIMEOUT;
152 }
153
154 uppercase(cmd);
155
156 if (cmd == "UID") {
157 command.setUidMode();
158
159 switch (expectSPACE()) {
160 case Operator::ACCEPT:
161 break;
162 case Operator::REJECT:
163 session.setLastError("Syntax error; after UID there"
164 " must come a SPACE");
165 case Operator::ERROR:
166 return Operator::ERROR;
168 return Operator::TIMEOUT;
169 }
170
171 switch (expectAstring(cmd)) {
172 case Operator::ACCEPT:
173 break;
174 case Operator::REJECT:
175 session.setLastError("Syntax error; after UID "
176 "SPACE there must come a command");
177 case Operator::ERROR:
178 return Operator::ERROR;
180 return Operator::TIMEOUT;
181 }
182
183 uppercase(cmd);
184 }
185
186 command.setTag(tag);
187 command.setName(cmd);
188
189 return Operator::ACCEPT;
190}
Declaration of the Broker class.
Broker * getBroker(int state)
Definition: broker.cc:78
void assign(const std::string &fname, Operator *o)
Definition: broker.cc:57
void setLastError(const std::string &error) const
Definition: broker.h:51
Operator * getOperator(int state, const std::string &name) const
Definition: broker.cc:69
~BrokerFactory(void)
Definition: broker.cc:28
void addCapability(const std::string &c)
Definition: broker.cc:43
static BrokerFactory & getInstance(void)
Definition: broker.cc:36
Operator * get(const std::string &name) const
Definition: broker.cc:106
Operator::ParseResult parseStub(Request &cmd)
Definition: broker.cc:114
~Broker(void)
Definition: broker.cc:94
void assign(const std::string &fname, Operator *o, bool deletable=false)
Definition: broker.cc:99
Broker(void)
Definition: broker.cc:89
void addCapability(const std::string &cap)
virtual int getState(void) const =0
void setTag(std::string &t_in)
Definition: imapparser.cc:46
void setName(const std::string &s_in)
Definition: imapparser.cc:70
void setUidMode(void)
Definition: imapparser.cc:34
void setLastError(const std::string &error) const
Definition: session.cc:185
@ NONAUTHENTICATED
Definition: session.h:36
@ AUTHENTICATED
Definition: session.h:37
static Session & getInstance(void)
Definition: session.cc:33
Declaration of miscellaneous convertion functions.
Definition: bincimapd.cc:9
Operator::ParseResult expectAstring(std::string &s_in)
Operator::ParseResult expectSPACE(void)
Operator::ParseResult expectTag(std::string &s_in)
void uppercase(std::string &input)
Definition: convert.h:115
Declaration of all operators.
Declaration of a recursive descent IMAP command parser.