Added count functionality in SavableManager.
[UnsignedByte.git] / src / Core / Editor.cpp
blob763a032ba98ec433c72805c22daf8b3b329f8ee7
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 <stdarg.h>
23 #include "Editor.h"
24 #include "Parse.h"
25 #include "Permission.h"
26 #include "PermissionManager.h"
27 #include "Command.h"
28 #include "CommandManager.h"
29 #include "UBSocket.h"
30 #include "Account.h"
31 #include "Global.h"
32 #include "StringUtilities.h"
33 #include "Channel.h"
35 using mud::Permission;
36 using mud::Command;
38 Editor::Editor(UBSocket* sock) :
39 m_sock(sock)
41 Assert(sock);
44 Editor::~Editor()
49 std::string Editor::prompt()
51 return Global::Get()->EmptyString;
54 std::string Editor::lookup(const std::string& action)
56 return Global::Get()->EmptyString;
59 void Editor::Send(const std::string& msg)
61 m_sock->Send(msg);
64 void Editor::Sendf(const char* format, ...)
66 va_list args;
67 va_start(args, format);
68 Send(Global::Get()->sprint(args, format));
69 va_end(args);
72 void Editor::Disconnect()
74 m_sock->SetCloseAndDelete();
77 void Editor::OnLine(const std::string& line)
79 Parse p(line);
80 std::string rawaction = p.getword();
81 std::string action = String::Get()->tolower(rawaction);
83 bool helpLookup = false;
85 if(!action.compare("help"))
87 helpLookup = true;
88 rawaction = p.getword();
89 action = String::Get()->tolower(rawaction);
91 if(!action.compare("help"))
93 Send("Syntax: help <command>\n");
94 Send("This command will lookup information on <command> and display it.\n");
95 return;
99 std::string argument = p.getrest();
100 std::string actionname = lookup(action);
102 if(actionname.size() == 0)
104 // TODO: Log failure?
105 Sendf("Unknown action '%s', type ? for a list of available actions.\n", action.c_str());
106 return;
109 std::string commandname = name();
110 commandname.append("::");
111 commandname.append(actionname);
113 bool hasGrant = mud::PermissionManager::Get()->defaultGrant;
114 bool hasLog = mud::PermissionManager::Get()->defaultLog;
116 bool canLowForce = mud::CommandManager::Get()->defaultLowForce;
117 bool canForce = mud::CommandManager::Get()->defaultForce;
118 bool canHighForce = mud::CommandManager::Get()->defaultHighForce;
120 bool isLowForced = m_sock->isLowForced();
121 bool isNormalForced = m_sock->isForced();
122 bool isHighForced = m_sock->isHighForced();
123 bool isForced = isLowForced || isNormalForced || isHighForced;
127 long id = mud::CommandManager::Get()->lookupByName(commandname);
128 mud::CommandPtr cmd = mud::CommandManager::Get()->GetByKey(id);
130 if(helpLookup)
132 std::string help = cmd->getHelp();
133 if(help.size() == 0)
135 Sendf("Sorry, we do not have help available on '%s'.\n", action.c_str());
136 return;
139 Sendf("We have the following information on '%s':\n", action.c_str());
140 Send(help);
141 Send("\n\n");
142 return;
145 hasGrant = cmd->getGrant(m_sock);
146 hasLog = cmd->getLog(m_sock);
148 if(isForced)
150 canHighForce = cmd->canHighForce();
151 canForce = cmd->canForce();
152 canLowForce = cmd->canLowForce();
155 catch(RowNotFoundException& e)
157 if(helpLookup)
159 Sendf("Sorry, '%s' has not yet been added to the database, as a result no help is available for it yet.\n", action.c_str());
160 return;
165 if(isForced)
167 if(isLowForced && !canLowForce)
169 m_sock->GetForcer()->Sendf("You can't make them to '%s'!\n", action.c_str());
170 return;
173 if(isForced && !canForce)
175 m_sock->GetForcer()->Sendf("You can't force them to '%s'!\n", action.c_str());
176 return;
179 if(isHighForced && !canHighForce)
181 m_sock->GetForcer()->Sendf("There is no way you can make them '%s'!\n", action.c_str());
182 return;
186 if(hasGrant)
188 if(hasLog || isForced)
190 if(isForced)
192 // TODO log
194 else
196 // TODO log
200 dispatch(action, argument);
201 return;
203 else
205 if(hasLog || isForced)
207 if(isForced)
209 m_sock->GetForcer()->Sendf("They are not allowed to '%s'!\n", action.c_str());
210 // TODO log
212 else
215 // TODO log
219 Sendf("Sorry, you do not have permission to '%s'.\n", actionname.c_str());
220 return;
224 bool Editor::canReceiveChannel(mud::ChannelPtr channel) const
226 return true;
229 bool Editor::supportPrefixes() const
231 return true;