Removed "Exception.h" references and replaced showShort with toString calls.
[UnsignedByte.git] / src / Core / EditorOOC.cpp
blob63238688c95b600eb25e0743d9aab530ac69a2db
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
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. *
9 * *
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. *
14 * *
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"
24 #include "UBSocket.h"
25 #include "UBHandler.h"
26 #include "StringUtilities.h"
27 #include "Account.h"
28 #include "Channel.h"
29 #include "ChannelManager.h"
30 #include "Array.h"
32 typedef EditorOOC E;
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[] = {
43 B("?", listCommands),
44 B("laston", listCharacters),
45 B("ooc", sendOOCMessage),
46 B("quit", quitEditor),
47 B("who", listOnlineCharacters),
50 EditorOOC::EditorOOC(UBSocket* sock) :
51 Editor(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);
65 if(act)
66 return act->getName();
68 std::string actionstripped = action;
69 actionstripped.erase(0, 1);
70 act = m_commands.getObject(actionstripped);
71 if(act)
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);
81 if(act)
83 act->Run(this, argument);
84 return;
87 std::string actionstripped = action;
88 actionstripped.erase(0, 1);
89 act = m_commands.getObject(actionstripped);
91 Assert(act);
92 act->Run(this, argument);
95 void EditorOOC::listCommands(const std::string& argument)
97 m_sock->Send(String::Get()->box(m_commands.getCommandsVector(), "OOC"));
98 m_sock->Send("\n");
99 return;
102 void EditorOOC::sendOOCMessage(const std::string& argument)
104 try {
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!");
118 return;
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);
127 if(sock)
129 if(!sock->hasAccount())
130 continue;
132 m_sock->Sendf("- %s -\n", sock->GetAccount()->getName().c_str());
135 return;
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");
146 m_sock->PopEditor();
147 return;
150 bool EditorOOC::supportPrefixes() const
152 return false;