Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Socket / UBSocket.cpp
blobbdf21e862420858c54d3065636a5283598807865
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 UBSocket::UBSocket(ISocketHandler& h) :
40 TcpSocket(h),
41 m_prompt(),
42 m_account(),
43 m_popeditor(false),
44 m_popLast(false),
45 m_editors(),
46 m_nexteditor(NULL),
47 m_forcer(NULL),
48 m_lowforced(false),
49 m_forced(false),
50 m_highforced(false),
51 m_hascolor(true),
52 m_colorcode('`')
54 SetLineProtocol();
57 UBSocket::~UBSocket(void)
59 while(!m_editors.empty())
61 delete m_editors.front();
62 m_editors.pop_front();
65 if(m_nexteditor != NULL)
67 delete m_nexteditor;
68 m_nexteditor = NULL;
72 void UBSocket::OnAccept()
74 Global::Get()->logf("Login from: %s\n", GetRemoteAddress().c_str());
75 Sendf("Welcome to %s.\n", game::vname);
76 Editor* p = new EditorAccountLogin(this);
77 Assert(m_editors.empty());
79 m_editors.push_front(p);
82 void UBSocket::OnLine(const std::string &line)
84 bool popLast = false;
86 SwitchEditors();
88 Assert(!m_editors.empty());
90 try
92 if(line.size() == 0)
94 m_editors.front()->OnEmptyLine();
95 SendPrompt();
96 return;
99 if(m_editors.front()->supportPrefixes() && line.size())
100 popLast = handlePrefixes(line);
102 m_editors.front()->OnLine(line);
104 if(popLast)
105 PopEditor();
107 SendPrompt();
109 catch(std::exception& e)
111 value_type accountid = 0;
112 if(m_account)
113 m_account->getID();
115 throw ExcecutionException(e, accountid, line);
118 return;
121 mud::AccountPtr UBSocket::GetAccount() const
123 Assert(m_account);
124 return m_account;
127 bool UBSocket::hasForcer() const
129 return m_forcer != NULL;
132 UBSocket* UBSocket::GetForcer() const
134 Assert(m_forcer);
135 return m_forcer;
138 bool UBSocket::hasAccount() const
140 return m_account != NULL;
143 void UBSocket::SetPrompt(const std::string& prompt)
145 m_prompt = prompt;
148 void UBSocket::SendPrompt()
150 Send(m_prompt);
153 void UBSocket::SetEditor(Editor* edit, bool popLast)
155 if(m_nexteditor)
157 Global::Get()->bug("UBSocket::SetEditor() was called while we are already waiting to switch to a next editor!\n");
158 Send("Something went wrong, somehow you are switching to another editor while you were already doing just that!\n");
159 Send("Closing your connection now.\n");
160 SetCloseAndDelete();
161 return;
164 SetPrompt();
165 m_nexteditor = edit;
166 m_popLast = popLast;
167 return;
170 void UBSocket::SwitchEditors()
172 if(!m_popeditor && !m_nexteditor)
173 return;
175 Assert(!(m_popeditor && m_nexteditor));
177 if(m_popeditor || m_popLast)
179 Assert(!(m_editors.empty()))
181 delete m_editors.front();
182 m_editors.pop_front();
183 m_popeditor = false;
184 m_popLast = false;
185 SetPrompt(m_editors.front()->prompt());
188 if(m_nexteditor)
190 m_editors.push_front(m_nexteditor);
191 m_nexteditor = NULL;
192 SetPrompt(m_editors.front()->prompt());
195 m_editors.front()->OnFocus();
197 if(m_nexteditor || m_popeditor)
198 SwitchEditors();
199 else
200 SendPrompt();
203 void UBSocket::PopEditor()
205 Assert(!m_popeditor);
207 m_popeditor = true;
208 SetPrompt();
211 void UBSocket::Send(const std::string& msg)
213 if(!m_hascolor || msg.find(m_colorcode) == std::string::npos)
215 SendBuf(msg.c_str(), msg.size());
216 return;
219 std::string buf = msg;
220 buf.append("`^");
222 for(size_t i = buf.find(m_colorcode); i != std::string::npos; i = buf.find(m_colorcode))
224 Assert(i < buf.size());
226 std::string code = buf.substr(i+1, 1);
228 buf.erase(i+1, 1);
229 buf.erase(i, 1);
233 mud::ColourPtr colour = mud::Managers::Get()->Colour->GetByCode(code);
234 buf.insert(i, colour->getColourString());
236 catch(RowNotFoundException& e)
238 Global::Get()->bugf("Unknown colour code %s!\n", code.c_str());
239 continue;
243 SendBuf(buf.c_str(), buf.size());
247 void UBSocket::Sendf(const char* format, ...)
249 va_list args;
250 va_start(args, format);
251 Send(Global::Get()->sprint(args, format));
252 va_end(args);
255 UBSocket* UBSocket::Cast(Socket *sock, bool error)
257 UBSocket* sock2 = dynamic_cast<UBSocket*>(sock);
258 Assert(sock2 || !error);
260 return sock2;
263 bool UBSocket::canReceiveChannel(mud::ChannelPtr channel)
265 Editor* editor = m_editors.front();
267 if(!editor->canReceiveChannel(channel))
268 return false;
270 if(m_account && !m_account->wantReceiveChannel(channel))
271 return false;
273 return true;
276 bool UBSocket::handlePrefixes(const std::string& line)
278 bool popLast = false;
280 if(line[0] == Global::Get()->OOCIdentifier)
282 m_editors.push_front(new EditorOOC(this));
283 popLast = true;
286 return popLast;