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 extern bool g_nocatch
;
41 UBSocket::UBSocket(ISocketHandler
& h
) :
59 UBSocket::~UBSocket(void)
61 while(!m_editors
.empty())
63 delete m_editors
.front();
64 m_editors
.pop_front();
67 if(m_nexteditor
!= NULL
)
74 void UBSocket::OnAccept()
76 Global::Get()->logf("Login from: %s\n", GetRemoteAddress().c_str());
77 Sendf("Welcome to %s.\n", game::vname
);
78 Editor
* p
= new EditorAccountLogin(this);
79 Assert(m_editors
.empty());
81 m_editors
.push_front(p
);
84 void UBSocket::OnLine(const std::string
&line
)
87 Assert(!m_editors
.empty());
99 catch(std::exception
& e
)
102 value_type accountid
= 0;
105 accountid
= m_account
->getID();
108 throw ExcecutionException(e
, accountid
, line
);
112 void UBSocket::handleLine(const std::string
& line
)
114 bool popLast
= false;
118 m_editors
.front()->OnEmptyLine();
123 if(m_editors
.front()->supportPrefixes() && line
.size())
124 popLast
= handlePrefixes(line
);
126 m_editors
.front()->OnLine(line
);
134 mud::AccountPtr
UBSocket::GetAccount() const
140 bool UBSocket::hasForcer() const
142 return m_forcer
!= NULL
;
145 UBSocket
* UBSocket::GetForcer() const
151 bool UBSocket::hasAccount() const
153 return m_account
!= NULL
;
156 void UBSocket::SetPrompt(const std::string
& prompt
)
161 void UBSocket::SendPrompt()
166 void UBSocket::SetEditor(Editor
* edit
, bool popLast
)
168 Assert(!m_nexteditor
);
176 void UBSocket::SwitchEditors()
178 if(!m_popeditor
&& !m_nexteditor
)
181 Assert(!(m_popeditor
&& m_nexteditor
));
183 if(m_popeditor
|| m_popLast
)
185 Assert(!(m_editors
.empty()))
187 delete m_editors
.front();
188 m_editors
.pop_front();
191 SetPrompt(m_editors
.front()->prompt());
196 m_editors
.push_front(m_nexteditor
);
198 SetPrompt(m_editors
.front()->prompt());
201 m_editors
.front()->OnFocus();
203 if(m_nexteditor
|| m_popeditor
)
209 void UBSocket::PopEditor()
211 Assert(!m_popeditor
);
217 void UBSocket::Send(const std::string
& msg
)
219 if(!m_hascolor
|| msg
.find(m_colorcode
) == std::string::npos
)
221 SendBuf(msg
.c_str(), msg
.size());
225 std::string buf
= msg
;
228 for(size_t i
= buf
.find(m_colorcode
); i
!= std::string::npos
; i
= buf
.find(m_colorcode
))
230 Assert(i
< buf
.size());
232 std::string code
= buf
.substr(i
+1, 1);
239 mud::ColourPtr colour
= mud::Managers::Get()->Colour
->GetByCode(code
);
240 buf
.insert(i
, colour
->getColourString());
242 catch(RowNotFoundException
& e
)
244 Global::Get()->bugf("Unknown colour code %s!\n", code
.c_str());
249 SendBuf(buf
.c_str(), buf
.size());
253 void UBSocket::Sendf(const char* format
, ...)
256 va_start(args
, format
);
257 Send(Global::Get()->sprint(args
, format
));
261 UBSocket
* UBSocket::Cast(Socket
*sock
, bool error
)
263 UBSocket
* sock2
= dynamic_cast<UBSocket
*>(sock
);
264 Assert(sock2
|| !error
);
269 bool UBSocket::canReceiveChannel(mud::ChannelPtr channel
)
271 Editor
* editor
= m_editors
.front();
273 if(!editor
->canReceiveChannel(channel
))
276 if(m_account
&& !m_account
->wantReceiveChannel(channel
))
282 bool UBSocket::handlePrefixes(const std::string
& line
)
284 bool popLast
= false;
286 if(line
[0] == Global::Get()->OOCIdentifier
)
288 m_editors
.push_front(new EditorOOC(this));