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 ***************************************************************************/
21 #include "EditorOOC.h"
22 #include "Character.h"
23 #include "CharacterManager.h"
25 #include "UBHandler.h"
26 #include "StringUtilities.h"
29 #include "ChannelManager.h"
33 typedef CommandObject
<E
> O
;
34 typedef CommandBinding
<E
> B
;
36 static O
listCommands("Commands", &E::listCommands
);
37 static O
sendOOCMessage("OOC", &E::sendOOCMessage
);
38 static O
listOnlineCharacters("Who", &E::listOnlineCharacters
);
39 static O
listCharacters("Laston", &E::listCharacters
);
40 static O
quitEditor("Quit", &E::quitEditor
);
42 static const B commands
[] = {
44 B("laston", listCharacters
),
45 B("ooc", sendOOCMessage
),
46 B("quit", quitEditor
),
47 B("who", listOnlineCharacters
),
50 EditorOOC::EditorOOC(UBSocket
* sock
) :
52 m_commands(commands
, array_size(commands
))
57 EditorOOC::~EditorOOC()
62 std::string
EditorOOC::lookup(const std::string
& action
)
64 const OOCCommand
* act
= m_commands
.getObject(action
);
66 return act
->getName();
68 std::string actionstripped
= action
;
69 actionstripped
.erase(0, 1);
70 act
= m_commands
.getObject(actionstripped
);
72 return act
->getName();
74 return Global::Get()->EmptyString
;
77 void EditorOOC::dispatch(const std::string
& action
, const std::string
& argument
)
79 const OOCCommand
* act
= m_commands
.getObject(action
);
83 act
->Run(this, argument
);
87 std::string actionstripped
= action
;
88 actionstripped
.erase(0, 1);
89 act
= m_commands
.getObject(actionstripped
);
92 act
->Run(this, argument
);
95 void EditorOOC::listCommands(const std::string
& argument
)
97 m_sock
->Send(String::Get()->box(m_commands
.getCommandsVector(), "OOC"));
102 void EditorOOC::sendOOCMessage(const std::string
& argument
)
105 mud::ChannelPtr channel
= mud::ChannelManager::Get()->GetByName("ooc");
106 std::map
<SOCKET
,Socket
*> ref
= UBHandler::Get()->Sockets();
107 for (std::map
<SOCKET
,Socket
*>::iterator it
= ref
.begin(); it
!= ref
.end(); it
++)
109 UBSocket
* sock
= UBSocket::Cast(it
->second
, false);
110 if(sock
&& sock
->canReceiveChannel(channel
)) {
111 sock
->Sendf("%s ooc> '%s'.\n", m_sock
->GetAccount()->getName().c_str(), argument
.c_str());
114 } catch(RowNotFoundException
& e
) {
115 Global::Get()->bug("Could not retreive the OOC chan!");
121 void EditorOOC::listOnlineCharacters(const std::string
& argument
)
123 std::map
<SOCKET
,Socket
*> ref
= UBHandler::Get()->Sockets();
124 for (std::map
<SOCKET
,Socket
*>::iterator it
= ref
.begin(); it
!= ref
.end(); it
++)
126 UBSocket
* sock
= UBSocket::Cast(it
->second
, false);
129 if(!sock
->hasAccount())
132 m_sock
->Sendf("- %s -\n", sock
->GetAccount()->getName().c_str());
138 void EditorOOC::listCharacters(const std::string
& argument
)
140 m_sock
->Send(String::Get()->box(mud::CharacterManager::Get()->List(), "Characters"));
143 void EditorOOC::quitEditor(const std::string
& argument
)
145 m_sock
->Send("Ok.\n");
150 bool EditorOOC::supportPrefixes() const