Added a EditorSocial.
[UnsignedByte.git] / src / Core / Editors / Editor.cpp
blob6f8cb72f00bc96ff07f4ccb7ba1820856887bb5d
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 "Account.h"
24 #include "Channel.h"
25 #include "Command.h"
26 #include "CommandManager.h"
27 #include "Editor.h"
28 #include "Global.h"
29 #include "Managers.h"
30 #include "Parse.h"
31 #include "Permission.h"
32 #include "PermissionManager.h"
33 #include "StringUtilities.h"
34 #include "UBSocket.h"
36 using mud::Permission;
37 using mud::Command;
39 Editor::Editor(UBSocket* sock) :
40 m_sock(sock)
42 Assert(sock);
45 Editor::~Editor()
50 std::string Editor::prompt()
52 return Global::Get()->EmptyString;
55 std::string Editor::lookup(const std::string& action)
57 return Global::Get()->EmptyString;
60 void Editor::Send(const std::string& msg)
62 m_sock->Send(msg);
65 void Editor::Sendf(const char* format, ...)
67 va_list args;
68 va_start(args, format);
69 Send(Global::Get()->sprint(args, format));
70 va_end(args);
73 void Editor::Disconnect()
75 m_sock->SetCloseAndDelete();
78 void Editor::OnLine(const std::string& line)
80 Assert(m_sock);
81 Assert(m_sock->hasAccount());
83 Parse p(line);
84 std::string rawaction = p.getword();
85 std::string action = String::Get()->tolower(rawaction);
87 bool helpLookup = false;
89 if(!action.compare("help"))
91 helpLookup = true;
92 rawaction = p.getword();
93 action = String::Get()->tolower(rawaction);
95 if(!action.compare("help"))
97 Send("Syntax: help <command>\n");
98 Send("This command will lookup information on <command> and display it.\n");
99 return;
103 std::string argument = p.getrest();
104 std::string actionname = lookup(action);
106 if(actionname.size() == 0)
108 // TODO: Log failure?
109 Sendf("Unknown action '%s', type ? for a list of available actions.\n", action.c_str());
110 return;
113 std::string commandname = name();
114 commandname.append("::");
115 commandname.append(actionname);
117 bool hasGrant = mud::Managers::Get()->Permission->defaultGrant;
118 bool hasLog = mud::Managers::Get()->Permission->defaultLog;
120 bool canLowForce = mud::Managers::Get()->Command->defaultLowForce;
121 bool canForce = mud::Managers::Get()->Command->defaultForce;
122 bool canHighForce = mud::Managers::Get()->Command->defaultHighForce;
124 bool isLowForced = m_sock->isLowForced();
125 bool isNormalForced = m_sock->isForced();
126 bool isHighForced = m_sock->isHighForced();
127 bool isForced = isLowForced || isNormalForced || isHighForced;
131 long id = mud::Managers::Get()->Command->lookupByName(commandname);
132 mud::CommandPtr cmd = mud::Managers::Get()->Command->GetByKey(id);
134 if(helpLookup)
136 std::string help = cmd->getHelp();
137 if(help.size() == 0)
139 Sendf("Sorry, we do not have help available on '%s'.\n", action.c_str());
140 return;
143 Sendf("We have the following information on '%s':\n", action.c_str());
144 Send(help);
145 Send("\n\n");
146 return;
149 hasGrant = cmd->getGrant(m_sock->GetAccount()->getID());
150 hasLog = cmd->getLog(m_sock->GetAccount()->getID());
152 if(isForced)
154 canHighForce = cmd->canHighForce();
155 canForce = cmd->canForce();
156 canLowForce = cmd->canLowForce();
159 catch(RowNotFoundException& e)
161 if(helpLookup)
163 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());
164 return;
169 if(isForced)
171 if(isLowForced && !canLowForce)
173 m_sock->GetForcer()->Sendf("You can't make them to '%s'!\n", action.c_str());
174 return;
177 if(isForced && !canForce)
179 m_sock->GetForcer()->Sendf("You can't force them to '%s'!\n", action.c_str());
180 return;
183 if(isHighForced && !canHighForce)
185 m_sock->GetForcer()->Sendf("There is no way you can make them '%s'!\n", action.c_str());
186 return;
190 if(hasGrant)
192 if(hasLog || isForced)
194 if(isForced)
196 // TODO log
198 else
200 // TODO log
204 dispatch(action, argument);
205 return;
207 else
209 if(hasLog || isForced)
211 if(isForced)
213 m_sock->GetForcer()->Sendf("They are not allowed to '%s'!\n", action.c_str());
214 // TODO log
216 else
219 // TODO log
223 Sendf("Sorry, you do not have permission to '%s'.\n", actionname.c_str());
224 return;
228 bool Editor::canReceiveChannel(mud::ChannelPtr channel) const
230 return true;
233 bool Editor::supportPrefixes() const
235 return true;