1 /***************************************************************************
2 * Copyright (C) 2007 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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 ***************************************************************************/
27 #include "PCharacter.h"
29 #include "DatabaseMgr.h"
30 #include "EditorAccountLogin.h"
31 #include "EditorOOC.h"
34 #include "AccountManager.h"
36 #include "ColourManager.h"
38 UBSocket::UBSocket(ISocketHandler
& h
) :
56 UBSocket::~UBSocket(void)
58 while(!m_editors
.empty())
60 delete m_editors
.top();
64 if(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!");
82 void UBSocket::OnLine(const std::string
&line
)
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");
97 if(line
.size() && line
[0] == Global::Get()->OOCIdentifier
)
99 m_editors
.push(new EditorOOC(this));
103 m_editors
.top()->OnLine(line
);
111 mud::Account
* UBSocket::GetAccount() const
114 throw std::logic_error("UBSocket::GetAccount(), m_account == NULL!");
116 return m_account
.get();
119 UBSocket
* UBSocket::GetForcer() const
122 throw std::logic_error("UBSocket::GetForcer(), m_forcer == NULL!");
127 bool UBSocket::hasAccount() const
132 void UBSocket::SetPrompt(const std::string
& prompt
)
137 void UBSocket::SendPrompt()
142 void UBSocket::SetEditor(Editor
* edit
, bool popLast
)
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");
159 void UBSocket::SwitchEditors()
161 if(!m_popeditor
&& !m_nexteditor
)
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");
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");
184 delete m_editors
.top();
188 SetPrompt(m_editors
.top()->prompt());
193 m_editors
.push(m_nexteditor
);
195 SetPrompt(m_editors
.top()->prompt());
198 m_editors
.top()->OnFocus();
206 void UBSocket::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");
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());
229 std::string buf
= msg
;
232 for(size_t i
= buf
.find(m_colorcode
); i
!= std::string::npos
; i
= buf
.find(m_colorcode
))
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());
242 std::string code
= buf
.substr(i
+1, 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());
259 SendBuf(buf
.c_str(), buf
.size());
263 void UBSocket::Sendf(const char* format
, ...)
266 va_start(args
, format
);
267 Send(Global::Get()->sprint(args
, format
));
271 UBSocket
* UBSocket::Cast(Socket
*sock
, bool Error
)
273 UBSocket
* sock2
= dynamic_cast<UBSocket
*>(sock
);
278 Global::Get()->bugf("BUG: UBSocket::Cast(), Error = true, but sock could not be casted to an UBSocket!");