1 /***************************************************************************
2 * Copyright (C) 2008 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 "DatabaseMgr.h"
28 #include "EditorAccountLogin.h"
29 #include "EditorOOC.h"
32 #include "AccountManager.h"
34 #include "ColourManager.h"
35 #include "GameVersion.h"
39 UBSocket::UBSocket(ISocketHandler
& h
) :
57 UBSocket::~UBSocket(void)
59 while(!m_editors
.empty())
61 delete m_editors
.front();
62 m_editors
.pop_front();
65 if(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
)
88 Assert(!m_editors
.empty());
94 m_editors
.front()->OnEmptyLine();
99 if(m_editors
.front()->supportPrefixes() && line
.size())
100 popLast
= handlePrefixes(line
);
102 m_editors
.front()->OnLine(line
);
109 catch(std::exception
& e
)
111 value_type accountid
= 0;
115 throw ExcecutionException(e
, accountid
, line
);
121 mud::AccountPtr
UBSocket::GetAccount() const
127 bool UBSocket::hasForcer() const
129 return m_forcer
!= NULL
;
132 UBSocket
* UBSocket::GetForcer() const
138 bool UBSocket::hasAccount() const
140 return m_account
!= NULL
;
143 void UBSocket::SetPrompt(const std::string
& prompt
)
148 void UBSocket::SendPrompt()
153 void UBSocket::SetEditor(Editor
* edit
, bool popLast
)
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");
170 void UBSocket::SwitchEditors()
172 if(!m_popeditor
&& !m_nexteditor
)
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();
185 SetPrompt(m_editors
.front()->prompt());
190 m_editors
.push_front(m_nexteditor
);
192 SetPrompt(m_editors
.front()->prompt());
195 m_editors
.front()->OnFocus();
197 if(m_nexteditor
|| m_popeditor
)
203 void UBSocket::PopEditor()
205 Assert(!m_popeditor
);
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());
219 std::string buf
= msg
;
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);
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());
243 SendBuf(buf
.c_str(), buf
.size());
247 void UBSocket::Sendf(const char* format
, ...)
250 va_start(args
, format
);
251 Send(Global::Get()->sprint(args
, format
));
255 UBSocket
* UBSocket::Cast(Socket
*sock
, bool error
)
257 UBSocket
* sock2
= dynamic_cast<UBSocket
*>(sock
);
258 Assert(sock2
|| !error
);
263 bool UBSocket::canReceiveChannel(mud::ChannelPtr channel
)
265 Editor
* editor
= m_editors
.front();
267 if(!editor
->canReceiveChannel(channel
))
270 if(m_account
&& !m_account
->wantReceiveChannel(channel
))
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));