Fourth iteration, Core compiles and links.
[UnsignedByte.git] / UnsignedByte / Core / UBSocket.cpp
blob6f37a8cfbedbfed788c6e279e57236845da6443e
1 /***************************************************************************
2 * Copyright (C) 2007 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 "PCharacter.h"
29 #include "DatabaseMgr.h"
30 #include "EditorAccountLogin.h"
31 #include "EditorOOC.h"
32 #include "Global.h"
33 #include "Account.h"
34 #include "AccountManager.h"
35 #include "Colour.h"
36 #include "ColourManager.h"
38 UBSocket::UBSocket(ISocketHandler& h) :
39 TcpSocket(h),
40 m_prompt(),
41 m_account(),
42 m_popeditor(false),
43 m_popLast(false),
44 m_editors(),
45 m_nexteditor(NULL),
46 m_forcer(NULL),
47 m_lowforced(false),
48 m_forced(false),
49 m_highforced(false),
50 m_hascolor(true),
51 m_colorcode('`')
53 SetLineProtocol();
56 UBSocket::~UBSocket(void)
58 while(!m_editors.empty())
60 delete m_editors.top();
61 m_editors.pop();
64 if(m_nexteditor != NULL)
66 delete m_nexteditor;
67 m_nexteditor = NULL;
71 void UBSocket::OnAccept()
73 Global::Get()->logf("Login from: %s\n", GetRemoteAddress().c_str());
74 Sendf("Welcome to %s.\n", game::vname);
75 Editor* p = new EditorAccountLogin(this);
76 if(!m_editors.empty())
77 throw std::logic_error("UBSocket::OnAccept(), m_editor != NULL!");
79 m_editors.push(p);
82 void UBSocket::OnLine(const std::string &line)
84 bool popLast = false;
86 SwitchEditors();
88 if(m_editors.empty())
90 Global::Get()->bugf("UBSocket::OnLine(), m_editor == NULL!");
91 Send("You don't have an editor mode set?\n");
92 Send("Closing your connection now.\n");
93 SetCloseAndDelete();
94 return;
97 if(line.size() && line[0] == Global::Get()->OOCIdentifier)
99 m_editors.push(new EditorOOC(this));
100 popLast = true;
103 m_editors.top()->OnLine(line);
105 if(popLast)
106 PopEditor();
108 return;
111 mud::Account* UBSocket::GetAccount() const
113 if(!m_account)
114 throw std::logic_error("UBSocket::GetAccount(), m_account == NULL!");
116 return m_account.get();
119 UBSocket* UBSocket::GetForcer() const
121 if(m_forcer == NULL)
122 throw std::logic_error("UBSocket::GetForcer(), m_forcer == NULL!");
124 return m_forcer;
127 bool UBSocket::hasAccount() const
129 return m_account;
132 void UBSocket::SetPrompt(const std::string& prompt)
134 m_prompt = prompt;
137 void UBSocket::SendPrompt()
139 Send(m_prompt);
142 void UBSocket::SetEditor(Editor* edit, bool popLast)
144 if(m_nexteditor)
146 Global::Get()->bug("UBSocket::SetEditor() was called while we are already waiting to switch to a next editor!\n");
147 Send("Something went wrong, somehow you are switching to another editor while you were already doing just that!\n");
148 Send("Closing your connection now.\n");
149 SetCloseAndDelete();
150 return;
153 SetPrompt();
154 m_nexteditor = edit;
155 m_popLast = popLast;
156 return;
159 void UBSocket::SwitchEditors()
161 if(!m_popeditor && !m_nexteditor)
162 return;
164 if(m_popeditor && m_nexteditor)
166 Global::Get()->bug("UBSocket::SwitchEditors() was called, but we don't have both a new editor and we want to pop one?!");
167 Send("Something went wrong, somehow you are switching to another editor but you're also deleting the top one?!\n");
168 Send("Closing your connection now.\n");
169 SetCloseAndDelete();
170 return;
173 if(m_popeditor || m_popLast)
175 if(m_editors.empty()) // should never happen
177 Global::Get()->bug("UBSocket::SwitchEditors() was called, but we don't have a current editor?!");
178 Send("Something went wrong, somehow you are switching to another editor but you don't have one set?!\n");
179 Send("Closing your connection now.\n");
180 SetCloseAndDelete();
181 return;
184 delete m_editors.top();
185 m_editors.pop();
186 m_popeditor = false;
187 m_popLast = false;
188 SetPrompt(m_editors.top()->prompt());
191 if(m_nexteditor)
193 m_editors.push(m_nexteditor);
194 m_nexteditor = NULL;
195 SetPrompt(m_editors.top()->prompt());
198 m_editors.top()->OnFocus();
200 if(m_nexteditor)
201 SwitchEditors();
202 else
203 SendPrompt();
206 void UBSocket::PopEditor()
208 if(m_popeditor)
210 Global::Get()->bug("UBSocket::PopEditor() was called, but the top editor is already being popped?!");
211 Send("Something went wrong, somehow you are popping the top editor but it was already being popped?!\n");
212 Send("Closing your connection now.\n");
213 SetCloseAndDelete();
214 return;
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 if(i >= buf.size())
236 Global::Get()->bugf("Colour code %c was found as last character in output '%s'!\n", m_colorcode, msg.c_str());
237 std::string err = Global::Get()->sprintf("Colour code %c was found as last character in output '%s'!\n", m_colorcode, msg.c_str());
238 SendBuf(err.c_str(), err.size());
239 return;
242 std::string code = buf.substr(i+1, 1);
244 buf.erase(i+1, 1);
245 buf.erase(i, 1);
249 mud::ColourPtr colour = mud::ColourManager::Get()->GetByCode(code);
250 buf.insert(i, colour->getColourString());
252 catch(RowNotFoundException& e)
254 Global::Get()->bugf("Unknown colour code %s!\n", code.c_str());
255 continue;
259 SendBuf(buf.c_str(), buf.size());
263 void UBSocket::Sendf(const char* format, ...)
265 va_list args;
266 va_start(args, format);
267 Send(Global::Get()->sprint(args, format));
268 va_end(args);
271 UBSocket* UBSocket::Cast(Socket *sock, bool Error)
273 UBSocket* sock2 = dynamic_cast<UBSocket*>(sock);
274 if(!sock2)
276 if(Error)
278 Global::Get()->bugf("BUG: UBSocket::Cast(), Error = true, but sock could not be casted to an UBSocket!");
280 return NULL;
282 return sock2;