Added g_nocatch support in UBSocket.
[UnsignedByte.git] / src / Core / Socket / UBSocket.cpp
blob95f0d1e9c484cc9bf5511166dcce0dd1c7a96b68
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (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 *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <assert.h>
22 #include <stdarg.h>
24 #include <Parse.h>
26 #include "UBSocket.h"
27 #include "DatabaseMgr.h"
28 #include "EditorAccountLogin.h"
29 #include "EditorOOC.h"
30 #include "Global.h"
31 #include "Account.h"
32 #include "AccountManager.h"
33 #include "Colour.h"
34 #include "ColourManager.h"
35 #include "GameVersion.h"
36 #include "Channel.h"
37 #include "Managers.h"
39 extern bool g_nocatch;
41 UBSocket::UBSocket(ISocketHandler& h) :
42 TcpSocket(h),
43 m_prompt(),
44 m_account(),
45 m_popeditor(false),
46 m_popLast(false),
47 m_editors(),
48 m_nexteditor(NULL),
49 m_forcer(NULL),
50 m_lowforced(false),
51 m_forced(false),
52 m_highforced(false),
53 m_hascolor(true),
54 m_colorcode('`')
56 SetLineProtocol();
59 UBSocket::~UBSocket(void)
61 while(!m_editors.empty())
63 delete m_editors.front();
64 m_editors.pop_front();
67 if(m_nexteditor != NULL)
69 delete m_nexteditor;
70 m_nexteditor = NULL;
74 void UBSocket::OnAccept()
76 Global::Get()->logf("Login from: %s\n", GetRemoteAddress().c_str());
77 Sendf("Welcome to %s.\n", game::vname);
78 Editor* p = new EditorAccountLogin(this);
79 Assert(m_editors.empty());
81 m_editors.push_front(p);
84 void UBSocket::OnLine(const std::string &line)
86 SwitchEditors();
87 Assert(!m_editors.empty());
89 if(g_nocatch)
91 handleLine(line);
92 return;
95 try
97 handleLine(line);
99 catch(std::exception& e)
102 value_type accountid = 0;
104 if(m_account) {
105 accountid = m_account->getID();
108 throw ExcecutionException(e, accountid, line);
112 void UBSocket::handleLine(const std::string& line)
114 bool popLast = false;
116 if(line.size() == 0)
118 m_editors.front()->OnEmptyLine();
119 SendPrompt();
120 return;
123 if(m_editors.front()->supportPrefixes() && line.size())
124 popLast = handlePrefixes(line);
126 m_editors.front()->OnLine(line);
128 if(popLast)
129 PopEditor();
131 SendPrompt();
134 mud::AccountPtr UBSocket::GetAccount() const
136 Assert(m_account);
137 return m_account;
140 bool UBSocket::hasForcer() const
142 return m_forcer != NULL;
145 UBSocket* UBSocket::GetForcer() const
147 Assert(m_forcer);
148 return m_forcer;
151 bool UBSocket::hasAccount() const
153 return m_account != NULL;
156 void UBSocket::SetPrompt(const std::string& prompt)
158 m_prompt = prompt;
161 void UBSocket::SendPrompt()
163 Send(m_prompt);
166 void UBSocket::SetEditor(Editor* edit, bool popLast)
168 Assert(!m_nexteditor);
170 SetPrompt();
171 m_nexteditor = edit;
172 m_popLast = popLast;
173 return;
176 void UBSocket::SwitchEditors()
178 if(!m_popeditor && !m_nexteditor)
179 return;
181 Assert(!(m_popeditor && m_nexteditor));
183 if(m_popeditor || m_popLast)
185 Assert(!(m_editors.empty()))
187 delete m_editors.front();
188 m_editors.pop_front();
189 m_popeditor = false;
190 m_popLast = false;
191 SetPrompt(m_editors.front()->prompt());
194 if(m_nexteditor)
196 m_editors.push_front(m_nexteditor);
197 m_nexteditor = NULL;
198 SetPrompt(m_editors.front()->prompt());
201 m_editors.front()->OnFocus();
203 if(m_nexteditor || m_popeditor)
204 SwitchEditors();
205 else
206 SendPrompt();
209 void UBSocket::PopEditor()
211 Assert(!m_popeditor);
213 m_popeditor = true;
214 SetPrompt();
217 void UBSocket::Send(const std::string& msg)
219 if(!m_hascolor || msg.find(m_colorcode) == std::string::npos)
221 SendBuf(msg.c_str(), msg.size());
222 return;
225 std::string buf = msg;
226 buf.append("`^");
228 for(size_t i = buf.find(m_colorcode); i != std::string::npos; i = buf.find(m_colorcode))
230 Assert(i < buf.size());
232 std::string code = buf.substr(i+1, 1);
234 buf.erase(i+1, 1);
235 buf.erase(i, 1);
239 mud::ColourPtr colour = mud::Managers::Get()->Colour->GetByCode(code);
240 buf.insert(i, colour->getColourString());
242 catch(RowNotFoundException& e)
244 Global::Get()->bugf("Unknown colour code %s!\n", code.c_str());
245 continue;
249 SendBuf(buf.c_str(), buf.size());
253 void UBSocket::Sendf(const char* format, ...)
255 va_list args;
256 va_start(args, format);
257 Send(Global::Get()->sprint(args, format));
258 va_end(args);
261 UBSocket* UBSocket::Cast(Socket *sock, bool error)
263 UBSocket* sock2 = dynamic_cast<UBSocket*>(sock);
264 Assert(sock2 || !error);
266 return sock2;
269 bool UBSocket::canReceiveChannel(mud::ChannelPtr channel)
271 Editor* editor = m_editors.front();
273 if(!editor->canReceiveChannel(channel))
274 return false;
276 if(m_account && !m_account->wantReceiveChannel(channel))
277 return false;
279 return true;
282 bool UBSocket::handlePrefixes(const std::string& line)
284 bool popLast = false;
286 if(line[0] == Global::Get()->OOCIdentifier)
288 m_editors.push_front(new EditorOOC(this));
289 popLast = true;
292 return popLast;