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 ***************************************************************************/
26 #include "AccountManager.h"
29 #include "ColourManager.h"
30 #include "DatabaseMgr.h"
31 #include "EditorAccountLogin.h"
32 #include "EditorOOC.h"
33 #include "GameVersion.h"
38 extern bool g_nocatch
;
40 UBSocket::UBSocket(ISocketHandler
& h
) :
58 UBSocket::~UBSocket(void)
60 while(!m_editors
.empty())
62 delete m_editors
.front();
63 m_editors
.pop_front();
66 if(m_nexteditor
!= NULL
)
73 void UBSocket::OnAccept()
75 std::string msg
= "Login from: '";
76 msg
.append(GetRemoteAddress());
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
)
91 Assert(!m_editors
.empty());
103 catch(std::exception
& e
)
106 value_type accountid
= 0;
109 accountid
= m_account
->getID();
112 throw ExcecutionException(e
, accountid
, line
);
116 void UBSocket::handleLine(const std::string
& line
)
118 bool popLast
= false;
122 m_editors
.front()->OnEmptyLine();
127 if(m_editors
.front()->supportPrefixes() && line
.size())
128 popLast
= handlePrefixes(line
);
130 m_editors
.front()->OnLine(line
);
138 mud::AccountPtr
UBSocket::GetAccount() const
144 bool UBSocket::hasForcer() const
146 return m_forcer
!= NULL
;
149 UBSocket
* UBSocket::GetForcer() const
155 bool UBSocket::hasAccount() const
157 return m_account
!= NULL
;
160 void UBSocket::SetPrompt(const std::string
& prompt
)
165 void UBSocket::SendPrompt()
170 void UBSocket::SetEditor(Editor
* edit
, bool popLast
)
172 Assert(!m_nexteditor
);
180 void UBSocket::SwitchEditors()
182 if(!m_popeditor
&& !m_nexteditor
)
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();
195 SetPrompt(m_editors
.front()->prompt());
200 m_editors
.push_front(m_nexteditor
);
202 SetPrompt(m_editors
.front()->prompt());
205 m_editors
.front()->OnFocus();
207 if(m_nexteditor
|| m_popeditor
)
213 void UBSocket::PopEditor()
215 Assert(!m_popeditor
);
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
))
234 Assert(i
< buf
.size());
236 std::string code
= buf
.substr(i
+1, 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 '";
252 Global::Get()->printbug(msg
);
257 SendBuf(buf
.c_str(), buf
.size());
261 void UBSocket::Sendf(const char* format
, ...)
264 va_start(args
, format
);
265 Send(Global::Get()->sprint(args
, format
));
269 UBSocket
* UBSocket::Cast(Socket
*sock
, bool error
)
271 UBSocket
* sock2
= dynamic_cast<UBSocket
*>(sock
);
272 Assert(sock2
|| !error
);
277 bool UBSocket::canReceiveChannel(mud::ChannelPtr channel
)
279 Editor
* editor
= m_editors
.front();
281 if(!editor
->canReceiveChannel(channel
))
284 if(m_account
&& !m_account
->wantReceiveChannel(channel
))
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));