[project @ 2005-06-23 18:49:38 by unknown_lamer]
[clinton/bobotpp.git] / source / ScriptCommands.C
index 62b9f75..b2bdeaf 100644 (file)
@@ -1,6 +1,6 @@
 // ScriptCommands.C  -*- C++ -*-
 // Copyright (c) 1998 Etienne BERNARD
-// Copyright (C) 2002 Clinton Ebadi
+// Copyright (C) 2002,2005 Clinton Ebadi
 
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 #include "Utils.H"
 #include "Server.H"
 #include "ServerList.H"
+#include "ServerQueue.H"
 #include "ScriptCommands.H"
 #include "Interp.H"
+#include "DCCPerson.H"
+#include "DCCManager.H"
+#include "Parser.H"
+#include "Commands.H"
+#include "Message.H"
 #include <libguile.h>
 
 #define VERIFY_STRING(par) if (!SCM_STRINGP((par))) \
@@ -40,8 +46,8 @@ ScriptCommands::Action(SCM channel, SCM message)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(message);
-  Message m = Commands::Action(Interp::bot, Utils::scm2String(channel),
-                               Utils::scm2String(message));
+  Message m = Commands::Action(Interp::bot, Utils::scm2str(channel),
+                               Utils::scm2str(message));
   return scm_long2num(m.getCode());
 }
 
@@ -54,8 +60,8 @@ ScriptCommands::AddUser(SCM who, SCM maskChannel, SCM level,
   VERIFY_STRING (maskChannel);
   VERIFY_NUMBER (level);
 
-  String wwho = Utils::scm2String (who);
-  String mask = Utils::scm2String (maskChannel);
+  String wwho = Utils::scm2str (who);
+  String mask = Utils::scm2str (maskChannel);
   String passwd; 
   std::time_t eexpire;
 
@@ -64,14 +70,14 @@ ScriptCommands::AddUser(SCM who, SCM maskChannel, SCM level,
   else
     {
       VERIFY_STRING (password);
-      passwd = Utils::scm2String (password);
+      passwd = Utils::scm2str (password);
     }
   if (SCM_UNBNDP (expire))
     eexpire = -1;
   else
     {
       VERIFY_STRING (expire);
-      eexpire = Utils::strToTime (Utils::scm2String (expire));
+      eexpire = Utils::str2time (Utils::scm2str (expire));
       if (!eexpire) eexpire = -1;
     }
 
@@ -93,7 +99,7 @@ ScriptCommands::AddServer(SCM servername, SCM port)
   if (SCM_NUMBERP(port))
     p = scm_num2long(port, SCM_ARG1, "ScriptCommands::AddServer");
   Message m = Commands::AddServer(Interp::bot,
-                                  Utils::scm2String(servername),
+                                  Utils::scm2str(servername),
                                   p);
   return scm_long2num(m.getCode());
 }
@@ -106,8 +112,8 @@ ScriptCommands::AddShit(SCM mask, SCM maskChannel, SCM level,
   VERIFY_STRING (mask);
   VERIFY_STRING (maskChannel);
   VERIFY_NUMBER (level);
-  String mmask = Utils::scm2String (mask);
-  String mmaskChannel = Utils::scm2String (maskChannel);
+  String mmask = Utils::scm2str (mask);
+  String mmaskChannel = Utils::scm2str (maskChannel);
   int llevel = scm_num2int (level, SCM_ARG1, "ScriptCommands::AddShit");
   std::time_t expire;
   String rreason;
@@ -117,7 +123,7 @@ ScriptCommands::AddShit(SCM mask, SCM maskChannel, SCM level,
   else
     {
       VERIFY_STRING (expiration);
-      expire = Utils::strToTime (Utils::scm2String (expiration));
+      expire = Utils::str2time (Utils::scm2str (expiration));
       if (!expire) expire = -1;
     }
   if (SCM_UNBNDP (reason))
@@ -125,7 +131,7 @@ ScriptCommands::AddShit(SCM mask, SCM maskChannel, SCM level,
   else
     {
       VERIFY_STRING (reason);
-      rreason = Utils::scm2String (reason);
+      rreason = Utils::scm2str (reason);
     }
   Message m = Commands::AddShit (Interp::bot, mmask, mmaskChannel,
                                 llevel, expire, rreason);
@@ -138,16 +144,43 @@ ScriptCommands::Ban(SCM channel, SCM who)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(who);
-  Message m = Commands::Ban(Interp::bot, Utils::scm2String(channel),
-                            Utils::scm2String(who));
+  Message m = Commands::Ban(Interp::bot, Utils::scm2str(channel),
+                            Utils::scm2str(who));
   return scm_long2num(m.getCode());
 }
 
+SCM
+ScriptCommands::ChangeCommandLevel(SCM command, SCM level)
+{
+  VERIFY_STRING (command);
+  VERIFY_NUMBER (level);
+
+  SCM_STRING_COERCE_0TERMINATION_X (command);
+  std::string ccommand = SCM_STRING_CHARS (command);
+  unsigned int llevel = scm_num2uint (level, 0, "ScriptCommands::ChangeCommandLevel");
+
+  if (llevel > 4)
+    return SCM_BOOL_F;
+
+  std::map<std::string, class userFunction*, 
+    std::less<std::string> >::const_iterator  uf_iter 
+    = Interp::bot->userFunctions.find (ccommand);
+  userFunction * f = 0;
+  
+  if (uf_iter != Interp::bot->userFunctions.end ())
+    f = uf_iter->second;
+  else
+    return SCM_BOOL_F;
+
+  f->minLevel = llevel;
+  return SCM_BOOL_T;
+}
+
 SCM
 ScriptCommands::Cycle(SCM channel)
 {
   VERIFY_STRING(channel);
-  Message m = Commands::Cycle(Interp::bot, Utils::scm2String(channel));
+  Message m = Commands::Cycle(Interp::bot, Utils::scm2str(channel));
   return scm_long2num(m.getCode());
 }
 
@@ -156,8 +189,8 @@ ScriptCommands::Deban(SCM channel, SCM who)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(who);
-  Message m = Commands::Deban(Interp::bot, Utils::scm2String(channel),
-                              Utils::scm2String(who));
+  Message m = Commands::Deban(Interp::bot, Utils::scm2str(channel),
+                              Utils::scm2str(who));
   return scm_long2num(m.getCode());
 }
 
@@ -176,8 +209,8 @@ ScriptCommands::DelUser(SCM who, SCM maskChannel)
 {
   VERIFY_STRING(who);
   VERIFY_STRING(maskChannel);
-  Message m = Commands::DelUser(Interp::bot, Utils::scm2String(who),
-                                Utils::scm2String(maskChannel));
+  Message m = Commands::DelUser(Interp::bot, Utils::scm2str(who),
+                                Utils::scm2str(maskChannel));
   return scm_long2num(m.getCode());
 }
 
@@ -186,8 +219,8 @@ ScriptCommands::DelShit(SCM who, SCM maskChannel)
 {
   VERIFY_STRING(who);
   VERIFY_STRING(maskChannel);
-  Message m = Commands::DelShit(Interp::bot, Utils::scm2String(who),
-                                Utils::scm2String(maskChannel));
+  Message m = Commands::DelShit(Interp::bot, Utils::scm2str(who),
+                                Utils::scm2str(maskChannel));
   return scm_long2num(m.getCode());
 }
 
@@ -196,8 +229,8 @@ ScriptCommands::Deop(SCM channel, SCM who)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(who);
-  Message m = Commands::Deop(Interp::bot, Utils::scm2String(channel),
-                             Utils::scm2String(who));
+  Message m = Commands::Deop(Interp::bot, Utils::scm2str(channel),
+                             Utils::scm2str(who));
   return scm_long2num(m.getCode());
 }
 
@@ -206,7 +239,7 @@ ScriptCommands::Die(SCM reason)
 {
   String r = "Leaving";
   if (SCM_STRINGP(reason))
-    r = Utils::scm2String(reason);
+    r = Utils::scm2str(reason);
   Message m = Commands::Die(Interp::bot, r);
   return scm_long2num(m.getCode());
 }
@@ -215,7 +248,7 @@ SCM
 ScriptCommands::Do(SCM command)
 {
   VERIFY_STRING(command);
-  Message m = Commands::Do(Interp::bot, Utils::scm2String(command));
+  Message m = Commands::Do(Interp::bot, Utils::scm2str(command));
   return scm_long2num(m.getCode());
 }
 
@@ -224,8 +257,8 @@ ScriptCommands::Invite(SCM channel, SCM who)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(who);
-  Message m = Commands::Invite(Interp::bot, Utils::scm2String(channel),
-                               Utils::scm2String(who));
+  Message m = Commands::Invite(Interp::bot, Utils::scm2str(channel),
+                               Utils::scm2str(who));
   return scm_long2num(m.getCode());
 }
 
@@ -235,8 +268,8 @@ ScriptCommands::Join(SCM channel, SCM key)
   VERIFY_STRING(channel);
   String k = "";
   if (SCM_STRINGP(key))
-    k = Utils::scm2String(key);
-  Message m = Commands::Join(Interp::bot, Utils::scm2String(channel),
+    k = Utils::scm2str(key);
+  Message m = Commands::Join(Interp::bot, Utils::scm2str(channel),
                              k);
   return scm_long2num(m.getCode());
 }
@@ -246,8 +279,8 @@ ScriptCommands::Keep(SCM channel, SCM modes)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(modes);
-  Message m = Commands::Keep(Interp::bot, Utils::scm2String(channel),
-                             Utils::scm2String(modes));
+  Message m = Commands::Keep(Interp::bot, Utils::scm2str(channel),
+                             Utils::scm2str(modes));
   return scm_long2num(m.getCode());
 }
 
@@ -259,10 +292,10 @@ ScriptCommands::Kick(SCM channel, SCM who, SCM reason)
 
   String r = "";
   if (SCM_STRINGP(reason))
-    r = Utils::scm2String(reason);
+    r = Utils::scm2str(reason);
 
-  Message m = Commands::Kick(Interp::bot, Utils::scm2String(channel),
-                             Utils::scm2String(who), r);
+  Message m = Commands::Kick(Interp::bot, Utils::scm2str(channel),
+                             Utils::scm2str(who), r);
   return scm_long2num(m.getCode());
 }
 
@@ -273,9 +306,9 @@ ScriptCommands::KickBan(SCM channel, SCM who, SCM reason)
   VERIFY_STRING(who);
   String r = "";
   if (SCM_STRINGP(reason))
-    r = Utils::scm2String(reason);
-  Message m = Commands::KickBan(Interp::bot, Utils::scm2String(channel),
-                                Utils::scm2String(who), r);
+    r = Utils::scm2str(reason);
+  Message m = Commands::KickBan(Interp::bot, Utils::scm2str(channel),
+                                Utils::scm2str(who), r);
   return scm_long2num(m.getCode());
 }
 
@@ -283,7 +316,7 @@ SCM
 ScriptCommands::Lock(SCM channel)
 {
   VERIFY_STRING(channel);
-  Message m = Commands::Lock(Interp::bot, Utils::scm2String(channel));
+  Message m = Commands::Lock(Interp::bot, Utils::scm2str(channel));
   return scm_long2num(m.getCode());
 }
 
@@ -298,8 +331,8 @@ ScriptCommands::Mode(SCM channel, SCM mode)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(mode);
-  Message m = Commands::Mode(Interp::bot, Utils::scm2String(channel),
-                             Utils::scm2String(mode));
+  Message m = Commands::Mode(Interp::bot, Utils::scm2str(channel),
+                             Utils::scm2str(mode));
   return scm_long2num(m.getCode());
 }
 
@@ -308,8 +341,8 @@ ScriptCommands::Msg(SCM nick, SCM message)
 {
   VERIFY_STRING(nick);
   VERIFY_STRING(message);
-  Message m = Commands::Msg(Interp::bot, Utils::scm2String(nick),
-                            Utils::scm2String(message));
+  Message m = Commands::Msg(Interp::bot, Utils::scm2str(nick),
+                            Utils::scm2str(message));
   return scm_long2num(m.getCode());
 
 }
@@ -325,7 +358,7 @@ SCM
 ScriptCommands::Nick(SCM nick)
 {
   VERIFY_STRING(nick);
-  Message m = Commands::Nick(Interp::bot, Utils::scm2String(nick));
+  Message m = Commands::Nick(Interp::bot, Utils::scm2str(nick));
   return scm_long2num(m.getCode());
 }
 
@@ -334,8 +367,8 @@ ScriptCommands::Op(SCM channel, SCM who)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(who);
-  Message m = Commands::Op(Interp::bot, Utils::scm2String(channel),
-                           Utils::scm2String(who));
+  Message m = Commands::Op(Interp::bot, Utils::scm2str(channel),
+                           Utils::scm2str(who));
   return scm_long2num(m.getCode());
 }
 
@@ -343,7 +376,7 @@ SCM
 ScriptCommands::Part(SCM channel)
 {
   VERIFY_STRING(channel);
-  Message m = Commands::Part(Interp::bot, Utils::scm2String(channel));
+  Message m = Commands::Part(Interp::bot, Utils::scm2str(channel));
   return scm_long2num(m.getCode());
 }
 
@@ -359,8 +392,8 @@ ScriptCommands::Say(SCM channel, SCM message)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(message);
-  Message m = Commands::Say(Interp::bot, Utils::scm2String(channel),
-                            Utils::scm2String(message));
+  Message m = Commands::Say(Interp::bot, Utils::scm2str(channel),
+                            Utils::scm2str(message));
   return scm_long2num(m.getCode());
 }
 
@@ -372,10 +405,19 @@ ScriptCommands::Server(SCM number)
   return scm_long2num(m.getCode());
 }
 
+SCM
+ScriptCommands::SetFloodRate(SCM rate)
+{
+  VERIFY_NUMBER(rate);
+  Message m = Commands::SetFloodRate(Interp::bot, scm_num2uint 
+                                    (rate, 0, "SetFloodRate"));
+  return scm_long2num(m.getCode());
+}
+
 SCM
 ScriptCommands::SetVersion(SCM version)
 {
-  Message m = Commands::SetVersion(Interp::bot, Utils::scm2String(version));
+  Message m = Commands::SetVersion(Interp::bot, Utils::scm2str(version));
   return scm_long2num(m.getCode());
 }
 
@@ -385,8 +427,8 @@ ScriptCommands::TBan(SCM channel, SCM who, SCM seconds)
   VERIFY_STRING(channel);
   VERIFY_STRING(who);
   VERIFY_NUMBER(seconds);
-  Message m = Commands::TBan(Interp::bot, Utils::scm2String(channel),
-                             Utils::scm2String(who), gh_scm2long(seconds));
+  Message m = Commands::TBan(Interp::bot, Utils::scm2str(channel),
+                             Utils::scm2str(who), gh_scm2long(seconds));
   return scm_long2num(m.getCode());
 }
 
@@ -398,9 +440,9 @@ ScriptCommands::TKBan(SCM channel, SCM who, SCM seconds, SCM reason)
   VERIFY_NUMBER(seconds);
   String r = "";
   if (SCM_STRINGP(reason))
-    r = Utils::scm2String(reason);
-  Message m = Commands::TKBan(Interp::bot, Utils::scm2String(channel),
-                              Utils::scm2String(who),
+    r = Utils::scm2str(reason);
+  Message m = Commands::TKBan(Interp::bot, Utils::scm2str(channel),
+                              Utils::scm2str(who),
                               gh_scm2long(seconds), r);
   return scm_long2num(m.getCode());
 }
@@ -410,8 +452,8 @@ ScriptCommands::Topic(SCM channel, SCM topic)
 {
   VERIFY_STRING(channel);
   VERIFY_STRING(topic);
-  Message m = Commands::Topic(Interp::bot, Utils::scm2String(channel),
-                              Utils::scm2String(topic));
+  Message m = Commands::Topic(Interp::bot, Utils::scm2str(channel),
+                              Utils::scm2str(topic));
   return scm_long2num(m.getCode());
 }
 
@@ -419,17 +461,14 @@ SCM
 ScriptCommands::Unlock(SCM channel)
 {
   VERIFY_STRING(channel);
-  Message m = Commands::Unlock(Interp::bot, Utils::scm2String(channel));
+  Message m = Commands::Unlock(Interp::bot, Utils::scm2str(channel));
   return scm_long2num(m.getCode());
 }
 
-
-
-
 SCM
 ScriptCommands::getNickname(void)
 {
-  return Utils::string2SCM(Interp::bot->nickName);
+  return Utils::str2scm(Interp::bot->nickName);
 }
 
 SCM
@@ -439,9 +478,9 @@ ScriptCommands::getServer(void)
   int serverNumber = Interp::bot->serverList->currentNumber;
 
   return gh_list(scm_long2num(serverNumber),
-                 Utils::string2SCM(serv->getHostName()),
+                 Utils::str2scm(serv->getHostName()),
                  scm_long2num(serv->getPort()),
-                 Utils::string2SCM(serv->getPassword()),
+                 Utils::str2scm(serv->getPassword()),
                  SCM_UNDEFINED);
 }
 
@@ -457,9 +496,9 @@ ScriptCommands::getServerList(void)
     s = (*it);
     res = gh_append2(res,
                      gh_list(gh_list(scm_long2num(i++),
-                                     Utils::string2SCM(s->getHostName()),
+                                     Utils::str2scm(s->getHostName()),
                                      scm_long2num(s->getPort()),
-                                     Utils::string2SCM(s->getPassword()),
+                                     Utils::str2scm(s->getPassword()),
                                      SCM_UNDEFINED), SCM_UNDEFINED));
   }
   return res;
@@ -501,18 +540,14 @@ ScriptCommands::addCommand(SCM scm_commandName, SCM scm_function,
     return SCM_BOOL_F;
 
   // We check that the command does not exist
-  String commandName = Utils::scm2String(scm_commandName).toUpper();
-  std::list<class userFunction *>::iterator it =
-    Interp::bot->userFunctions.begin();
-  for ( ; it != Interp::bot->userFunctions.end(); ++it) {
-    if ((*it)->name == commandName)
-      return SCM_BOOL_F;
-  }
+  String commandName = Utils::to_upper (Utils::scm2str(scm_commandName));
+  if (Interp::bot->userFunctions[commandName])
+    return SCM_BOOL_F;
 
   // Next we check that needsChannel is a boolean
   if (!gh_boolean_p(scm_needsChannel))
     return SCM_BOOL_F;
-  bool needsChannel = (bool)gh_scm2bool(scm_needsChannel);
+  bool needsChannel = gh_scm2bool(scm_needsChannel);
 
   // We check that minLevel is an integer and that it's
   // a valid level
@@ -536,9 +571,9 @@ ScriptCommands::addCommand(SCM scm_commandName, SCM scm_function,
     return SCM_BOOL_F;
 
   // We add the command in the commands list
-  Interp::bot->userFunctions.push_back(new userFunction(commandName, 
-(void (*)(ServerConnection *, Person *, String, String))0, minLevel, needsChannel, args, scm_function));
-
+  Interp::bot->userFunctions[commandName] = 
+    new userFunction(0, minLevel, needsChannel, args, scm_function);
+  
   return SCM_BOOL_T;
 }
 
@@ -550,37 +585,33 @@ ScriptCommands::delCommand(SCM scm_commandName)
     return SCM_BOOL_F;
 
   // We check that the command does exist
-  String commandName = Utils::scm2String(scm_commandName).toUpper();
-  std::list<class userFunction *>::iterator it =
-    Interp::bot->userFunctions.begin();
-  for ( ; it != Interp::bot->userFunctions.end(); ++it) {
-    if ((*it)->name == commandName)
-      break;
-  }
-
-  if (it == Interp::bot->userFunctions.end())
+  String commandName = Utils::to_upper (Utils::scm2str(scm_commandName));
+  if (!Interp::bot->userFunctions[commandName])
     return SCM_BOOL_F;
 
   // We delete the command
-  Interp::bot->userFunctions.erase(it);
-  delete (*it);
+  Interp::bot->userFunctions.erase(commandName);
 
   return SCM_BOOL_T;
 }
 
 SCM
-ScriptCommands::AddHook(SCM type, SCM regex, SCM function, SCM pri, SCM fall)
+ScriptCommands::AddHook(SCM type, SCM regex, SCM function, SCM pri, SCM fall,
+                       SCM name)
 {
   int priority = 0;
-  bool fallt = false; // does this hook fall through?
+  bool fallt = true; // does this hook fall through?
+  String rname = "DEFAULT";
 
   if (!SCM_UNBNDP (pri))
     priority = scm_num2int (pri, SCM_ARG1, "ScriptCommands::AddHook");
   if (!SCM_UNBNDP (fall))
     fallt = SCM_NFALSEP (fall);
+  if (!SCM_UNBNDP (name))
+    rname = Utils::scm2str (name);
   return SCM_BOOL (Interp::bot->botInterp->AddHook(gh_scm2long(type),
                                                   regex, function,
-                                                  priority, fallt));
+                                                  priority, fallt, rname));
 }
 
 SCM
@@ -592,7 +623,51 @@ ScriptCommands::AddTimer(SCM when, SCM function)
 SCM
 ScriptCommands::DelTimer(SCM timer)
 {
-  return gh_bool2scm(Interp::bot->botInterp->DelTimer(timer));
+  return SCM_BOOL (Interp::bot->botInterp->DelTimer(timer));
 }
 
+SCM
+ScriptCommands::sendDCCChatMessage (SCM to, SCM message)
+{
+
+  return SCM_BOOL (Interp::bot->dccConnections->sendMessage 
+                  (Utils::scm2str (to),
+                   Utils::scm2str (message)));
+}
+
+// Message sending
+// FIXME: write these
+
+#define IQUEUE Interp::bot->serverConnection->queue
+
+SCM
+ScriptCommands::sendCTCP(SCM to, SCM command , SCM message)
+{
+  VERIFY_STRING(to);
+  VERIFY_STRING(command);
+  VERIFY_STRING(message);
+
+  IQUEUE->sendCTCP (Utils::scm2str (to), Utils::scm2str (command),
+                  Utils::scm2str (message));
+  return SCM_UNSPECIFIED;
+}
+
+SCM
+ScriptCommands::sendNotice (SCM to, SCM message)
+{
+  VERIFY_STRING (to);
+  VERIFY_STRING (message);
+
+  if (Commands::Notice (Interp::bot, 
+                       Utils::scm2str (to),
+                       Utils::scm2str (message))
+      == Ok)
+    {
+      return SCM_BOOL_T;
+    }
+  else
+    {
+      return SCM_BOOL_F;
+    }
+}
 #endif