Added a EditorSocial.
[UnsignedByte.git] / src / Core / Socket / UBSocket.cpp
blobbffe32e7039952253082fccf202d521c4e372c61
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 <Parse.h>
22 #include <assert.h>
23 #include <stdarg.h>
25 #include "Account.h"
26 #include "AccountManager.h"
27 #include "Channel.h"
28 #include "Colour.h"
29 #include "ColourManager.h"
30 #include "DatabaseMgr.h"
31 #include "EditorAccountLogin.h"
32 #include "EditorOOC.h"
33 #include "GameVersion.h"
34 #include "Global.h"
35 #include "Managers.h"
36 #include "UBSocket.h"
38 extern bool g_nocatch;
40 UBSocket::UBSocket(ISocketHandler& h) :
41 TcpSocket(h),
42 m_prompt(),
43 m_account(),
44 m_popeditor(false),
45 m_popLast(false),
46 m_editors(),
47 m_nexteditor(NULL),
48 m_forcer(NULL),
49 m_lowforced(false),
50 m_forced(false),
51 m_highforced(false),
52 m_hascolor(true),
53 m_colorcode('`')
55 SetLineProtocol();
58 UBSocket::~UBSocket(void)
60 while(!m_editors.empty())
62 delete m_editors.front();
63 m_editors.pop_front();
66 if(m_nexteditor != NULL)
68 delete m_nexteditor;
69 m_nexteditor = NULL;
73 void UBSocket::OnAccept()
75 std::string msg = "Login from: '";
76 msg.append(GetRemoteAddress());
77 msg.append("'\n");
79 Global::Get()->printlog(msg);
81 Sendf("Welcome to %s.\n", game::vname);
82 Editor* p = new EditorAccountLogin(this);
83 Assert(m_editors.empty());
85 m_editors.push_front(p);
88 void UBSocket::OnLine(const std::string &line)
90 SwitchEditors();
91 Assert(!m_editors.empty());
93 if(g_nocatch)
95 handleLine(line);
96 return;
99 try
101 handleLine(line);
103 catch(std::exception& e)
106 value_type accountid = 0;
108 if(m_account) {
109 accountid = m_account->getID();
112 throw ExcecutionException(e, accountid, line);
116 void UBSocket::handleLine(const std::string& line)
118 bool popLast = false;
120 if(line.size() == 0)
122 m_editors.front()->OnEmptyLine();
123 SendPrompt();
124 return;
127 if(m_editors.front()->supportPrefixes() && line.size())
128 popLast = handlePrefixes(line);
130 m_editors.front()->OnLine(line);
132 if(popLast)
133 PopEditor();
135 SendPrompt();
138 mud::AccountPtr UBSocket::GetAccount() const
140 Assert(m_account);
141 return m_account;
144 bool UBSocket::hasForcer() const
146 return m_forcer != NULL;
149 UBSocket* UBSocket::GetForcer() const
151 Assert(m_forcer);
152 return m_forcer;
155 bool UBSocket::hasAccount() const
157 return m_account != NULL;
160 void UBSocket::SetPrompt(const std::string& prompt)
162 m_prompt = prompt;
165 void UBSocket::SendPrompt()
167 Send(m_prompt);
170 void UBSocket::SetEditor(Editor* edit, bool popLast)
172 Assert(!m_nexteditor);
174 SetPrompt();
175 m_nexteditor = edit;
176 m_popLast = popLast;
177 return;
180 void UBSocket::SwitchEditors()
182 if(!m_popeditor && !m_nexteditor)
183 return;
185 Assert(!(m_popeditor && m_nexteditor));
187 if(m_popeditor || m_popLast)
189 Assert(!(m_editors.empty()))
191 delete m_editors.front();
192 m_editors.pop_front();
193 m_popeditor = false;
194 m_popLast = false;
195 SetPrompt(m_editors.front()->prompt());
198 if(m_nexteditor)
200 m_editors.push_front(m_nexteditor);
201 m_nexteditor = NULL;
202 SetPrompt(m_editors.front()->prompt());
205 m_editors.front()->OnFocus();
207 if(m_nexteditor || m_popeditor)
208 SwitchEditors();
209 else
210 SendPrompt();
213 void UBSocket::PopEditor()
215 Assert(!m_popeditor);
217 m_popeditor = true;
218 SetPrompt();
221 void UBSocket::Send(const std::string& msg)
223 if(!m_hascolor || msg.find(m_colorcode) == std::string::npos)
225 SendBuf(msg.c_str(), msg.size());
226 return;
229 std::string buf = msg;
230 buf.append("`^");
232 for(size_t i = buf.find(m_colorcode); i != std::string::npos; i = buf.find(m_colorcode))
234 Assert(i < buf.size());
236 std::string code = buf.substr(i+1, 1);
238 buf.erase(i+1, 1);
239 buf.erase(i, 1);
243 mud::ColourPtr colour = mud::Managers::Get()->Colour->GetByCode(code);
244 buf.insert(i, colour->getColourString());
246 catch(RowNotFoundException& e)
248 std::string msg = "Unknown colour code '";
249 msg.append(code);
250 msg.append("'!\n");
252 Global::Get()->printbug(msg);
253 continue;
257 SendBuf(buf.c_str(), buf.size());
261 void UBSocket::Sendf(const char* format, ...)
263 va_list args;
264 va_start(args, format);
265 Send(Global::Get()->sprint(args, format));
266 va_end(args);
269 UBSocket* UBSocket::Cast(Socket *sock, bool error)
271 UBSocket* sock2 = dynamic_cast<UBSocket*>(sock);
272 Assert(sock2 || !error);
274 return sock2;
277 bool UBSocket::canReceiveChannel(mud::ChannelPtr channel)
279 Editor* editor = m_editors.front();
281 if(!editor->canReceiveChannel(channel))
282 return false;
284 if(m_account && !m_account->wantReceiveChannel(channel))
285 return false;
287 return true;
290 bool UBSocket::handlePrefixes(const std::string& line)
292 bool popLast = false;
294 if(line[0] == Global::Get()->OOCIdentifier)
296 m_editors.push_front(new EditorOOC(this));
297 popLast = true;
300 return popLast;