00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Command.h"
00023 #include "InSocket.h"
00024 #include "Player.h"
00025
00026
00027
00028 std::map<char,Command *> Command::m_ccs;
00029
00030
00031 Command::Command() : m_cmd(""),m_cc(0),m_need_wiz(false)
00032 {
00033 }
00034
00035
00036 Command::Command(const std::string& cmd,bool w) : m_cmd(cmd),m_cc(0),m_need_wiz(w)
00037 {
00038 }
00039
00040
00041 Command::Command(const std::string& cmd,char cc,bool w) : m_cmd(cmd),m_cc(cc),m_need_wiz(w)
00042 {
00043 if (m_ccs[cc] != NULL)
00044 {
00045 fprintf(stderr, "Two commands using the same char: %c - exiting...\n", cc);
00046 exit(-1);
00047 }
00048 m_ccs[cc] = this;
00049 }
00050
00051
00052 Command::~Command()
00053 {
00054 }
00055
00056
00057 int Command::Match(InSocket *from,const std::string& cmd,const std::string& params)
00058 {
00059 Player pl(from -> GetDatabase(),from -> GetAccountName());
00060 if (m_need_wiz && !pl.IsWizard())
00061 return 0;
00062 if (cmd.size() > m_cmd.size())
00063 return 0;
00064 if (!strcasecmp(cmd.c_str(), m_cmd.c_str()))
00065 return 2;
00066 if (!strncasecmp(cmd.c_str(), m_cmd.c_str(), cmd.size()))
00067 return 1;
00068 return 0;
00069 }
00070
00071
00072 Command *Command::GetCommandFromChar(char cc)
00073 {
00074 for (std::map<char,Command *>::iterator it = m_ccs.begin(); it != m_ccs.end(); it++)
00075 {
00076 if ((*it).first == cc)
00077 return (*it).second;
00078 }
00079 return NULL;
00080 }
00081
00082
00083 void Command::OnLine(InSocket *from,const std::string& line,int state)
00084 {
00085 from -> SetPrompt();
00086 }
00087
00088
00089 void Command::AddArgument(const std::string& arg,bool wiz)
00090 {
00091 m_args[arg] = wiz;
00092 }
00093
00094
00095 bool Command::CheckArgument(InSocket *from,const std::string& arg,std::string& complete)
00096 {
00097 Player pl(from -> GetDatabase(),from -> GetAccountName());
00098 bool wiz = pl.IsWizard();
00099 if (!m_args.size())
00100 {
00101 complete = arg;
00102 return true;
00103 }
00104 std::string found;
00105 int count = 0;
00106 for (strbool_m::iterator it = m_args.begin(); it != m_args.end(); it++)
00107 {
00108 std::string str = (*it).first;
00109 bool need_wiz = (*it).second;
00110 if (!need_wiz || wiz)
00111 {
00112 if (str.size() >= arg.size() &&
00113 !strncasecmp(str.substr(0, arg.size()).c_str(), arg.c_str(), arg.size()))
00114 {
00115 found = str;
00116 count++;
00117 }
00118 }
00119 }
00120 if (count == 1)
00121 {
00122 complete = found;
00123 return true;
00124 }
00125 if (!count)
00126 {
00127 from -> Send("Please specify:\n");
00128 for (strbool_m::iterator it = m_args.begin(); it != m_args.end(); it++)
00129 {
00130 std::string str = (*it).first;
00131 bool need_wiz = (*it).second;
00132 if (!need_wiz || wiz)
00133 {
00134 from -> Send(" " + str + "\n");
00135 }
00136 }
00137 }
00138 else
00139 {
00140 from -> Send("Please be more specific:\n");
00141 for (strbool_m::iterator it = m_args.begin(); it != m_args.end(); it++)
00142 {
00143 std::string str = (*it).first;
00144 bool need_wiz = (*it).second;
00145 if (!need_wiz || wiz)
00146 {
00147 if (str.size() >= arg.size() &&
00148 !strncasecmp(str.substr(0, arg.size()).c_str(), arg.c_str(), arg.size()))
00149 {
00150 from -> Send(" " + str + "\n");
00151 }
00152 }
00153 }
00154 }
00155 from -> SetPrompt();
00156 return false;
00157 }
00158
00159