]> jfr.im git - irc/quakenet/newserv.git/blobdiff - lua/luabot.c
CHANSERV: fix issue where chanserv_relay doesn't wait for db to be loaded before...
[irc/quakenet/newserv.git] / lua / luabot.c
index 50d1c9c7a099a33bcbfaa7bc15af9b672916557a..8fed58f584ab1c888dc90274fb6657b0e6c5ce51 100644 (file)
@@ -6,6 +6,7 @@
 #include "../localuser/localuserchannel.h"
 #include "../core/schedule.h"
 #include "../lib/irc_string.h"
+#include "../core/config.h"
 
 #include "lua.h"
 #include "luabot.h"
@@ -26,12 +27,15 @@ void lua_onauth(int hooknum, void *arg);
 void lua_ondisconnect(int hooknum, void *arg);
 void lua_onmode(int hooknum, void *arg);
 void lua_onop(int hooknum, void *arg);
+void lua_onprequit(int hooknum, void *arg);
 void lua_onquit(int hooknum, void *arg);
 void lua_onrename(int hooknum, void *arg);
 void lua_onconnect(int hooknum, void *arg);
 void lua_onjoin(int hooknum, void *arg);
 void lua_onpart(int hooknum, void *arg);
 
+sstring *luabotnick;
+
 void lua_registerevents(void) {
   registerhook(HOOK_CHANNEL_MODECHANGE, &lua_onmode);
   registerhook(HOOK_NICK_NEWNICK, &lua_onnewnick);
@@ -42,6 +46,7 @@ void lua_registerevents(void) {
   registerhook(HOOK_CHANNEL_KICK, &lua_onkick);
   registerhook(HOOK_CHANNEL_OPPED, &lua_onop);
   registerhook(HOOK_CHANNEL_DEOPPED, &lua_onop);
+  registerhook(HOOK_NICK_PRE_LOSTNICK, &lua_onprequit);
   registerhook(HOOK_NICK_LOSTNICK, &lua_onquit);
   registerhook(HOOK_NICK_RENAME, &lua_onrename);
   registerhook(HOOK_IRC_CONNECTED, &lua_onconnect);
@@ -59,6 +64,7 @@ void lua_deregisterevents(void) {
   deregisterhook(HOOK_IRC_CONNECTED, &lua_onconnect);
   deregisterhook(HOOK_NICK_RENAME, &lua_onrename);
   deregisterhook(HOOK_NICK_LOSTNICK, &lua_onquit);
+  deregisterhook(HOOK_NICK_PRE_LOSTNICK, &lua_onprequit);
   deregisterhook(HOOK_CHANNEL_DEOPPED, &lua_onop);
   deregisterhook(HOOK_CHANNEL_OPPED, &lua_onop);
   deregisterhook(HOOK_CHANNEL_KICK, &lua_onkick);
@@ -75,7 +81,10 @@ void lua_startbot(void *arg) {
 
   myureconnect = NULL;
 
-  lua_nick = registerlocaluser("U", "lua", "quakenet.department.of.corrections", LUA_FULLVERSION, "U", UMODE_ACCOUNT | UMODE_DEAF | UMODE_OPER | UMODE_SERVICE, &lua_bothandler);
+  if(!luabotnick)
+    luabotnick = getcopyconfigitem("lua", "botnick", "U", NICKLEN);
+
+  lua_nick = registerlocaluser(luabotnick->content, "lua", "quakenet.department.of.corrections", LUA_FULLVERSION, "U", UMODE_ACCOUNT | UMODE_DEAF | UMODE_OPER | UMODE_SERVICE, &lua_bothandler);
   if(!lua_nick) {
     myureconnect = scheduleoneshot(time(NULL) + 1, &lua_startbot, NULL);
     return;
@@ -115,6 +124,11 @@ void lua_destroybot(void) {
 
   if(lua_nick)
     deregisterlocaluser(lua_nick, NULL);
+
+  if(luabotnick) {
+    freesstring(luabotnick);
+    luabotnick = NULL;
+  }
 }
 
 int _lua_vpcall(lua_State *l, void *function, int mode, const char *sig, ...) {
@@ -149,10 +163,17 @@ int _lua_vpcall(lua_State *l, void *function, int mode, const char *sig, ...) {
       case 'S':
         lua_pushstring(l, ((sstring *)(va_arg(va, sstring *)))->content);
         break;
+      case 'L': /* BE VERY CAREFUL USING THIS, MAKE SURE YOU CAST THE VALUE TO LONG */
+        {
+          char *p = va_arg(va, char *);
+          long len = va_arg(va, long);
+          lua_pushlstring(l, p, len);
+        }
+        break;
       case 'N':
         {
           nick *np = va_arg(va, nick *);
-          LUA_PUSHNICK(l, np);
+          lua_pushnumeric(l, np->numeric);
           break;
         }
       case 'C':
@@ -164,6 +185,12 @@ int _lua_vpcall(lua_State *l, void *function, int mode, const char *sig, ...) {
       case '0':
         lua_pushnil(l);
         break;
+      case 'R':
+        lua_rawgeti(l, LUA_REGISTRYINDEX, va_arg(va, long));
+        break;
+      case 'b':
+        lua_pushboolean(l, va_arg(va, int));
+        break;
       case '>':
         goto endwhile;
 
@@ -178,7 +205,7 @@ endwhile:
   nres = strlen(sig);
 
   if(lua_debugpcall(l, (mode==LUA_CHARMODE)?function:"some_handler", narg, nres, top + 1)) {
-    Error("lua", ERR_ERROR, "Error pcalling %s: %s.", (mode==LUA_CHARMODE)?function:"some_handler", lua_tostring(l, -1));
+    Error("lua", ERR_ERROR, "Error pcalling %s: %s.", (mode==LUA_CHARMODE)?(char *)function:"some_handler", lua_tostring(l, -1));
   } else {
     nres = -nres;
     while(*sig) {
@@ -224,7 +251,8 @@ void lua_bothandler(nick *target, int type, void **args) {
           p[le - 1] = '\000';
 
         lua_avpcall("irc_onctcp", "ls", np->numeric, p + 1);
-
+      } else {
+        lua_avpcall("irc_onnotice", "ls", np->numeric, p);
       }
 
       break;
@@ -270,14 +298,17 @@ void lua_onkick(int hooknum, void *arg) {
   nick *kicked = arglist[1];
   nick *kicker = arglist[2];
   char *message = (char *)arglist[3];
+  int mode = 1;
 
   if(!kicker || IsOper(kicker) || IsService(kicker) || IsXOper(kicker)) /* bloody Cruicky */
-    return;
+    mode = 0;
 
-  if(kicker) {
+  if(mode) {
     lua_avpcall("irc_onkick", "Slls", ci->name, kicked->numeric, kicker->numeric, message);
+  } else if(kicker) {
+    lua_avpcall("irc_onkickall", "Slls", ci->name, kicked->numeric, kicker->numeric, message);
   } else {
-    lua_avpcall("irc_onkick", "Sl0s", ci->name, kicked->numeric, message);
+    lua_avpcall("irc_onkickall", "Sl0s", ci->name, kicked->numeric, message);
   }
 }
 
@@ -325,20 +356,23 @@ void lua_onpart(int hooknum, void *arg) {
   void **arglist = (void **)arg;
   chanindex *ci = ((channel *)arglist[0])->index;
   nick *np = arglist[1];
+  char *reason = arglist[2];
 
   if(!ci || !np)
     return;
 
-  lua_avpcall("irc_onpart", "Sl", ci->name, np->numeric);
+  lua_avpcall("irc_onpart", "Sls", ci->name, np->numeric, reason);
 }
 
 void lua_onrename(int hooknum, void *arg) {
-  nick *np = (void *)arg;
+  void **harg = (void **)arg;
+  nick *np = harg[0];
+  char *oldnick = harg[1];
 
   if(!np)
     return;
 
-  lua_avpcall("irc_onrename", "l", np->numeric);
+  lua_avpcall("irc_onrename", "ls", np->numeric, oldnick);
 }
 
 void lua_onquit(int hooknum, void *arg) {
@@ -350,6 +384,15 @@ void lua_onquit(int hooknum, void *arg) {
   lua_avpcall("irc_onquit", "l", np->numeric);
 }
 
+void lua_onprequit(int hooknum, void *arg) {
+  nick *np = (nick *)arg;
+
+  if(!np)
+    return;
+
+  lua_avpcall("irc_onprequit", "l", np->numeric);
+}
+
 void lua_onauth(int hooknum, void *arg) {
   nick *np = (nick *)arg;
 
@@ -426,16 +469,25 @@ char *printallmodes(channel *cp) {
   return buf;
 }
 
+static char *printlimitedmodes(channel *cp, flag_t before) {
+  static char buf[1024];
+
+  snprintf(buf, sizeof(buf), "%s", printflags(cp->flags, cmodeflags));
+
+  return buf;
+}
+
 void lua_onmode(int hooknum, void *arg) {
   void **arglist = (void **)arg;
   channel *cp = (channel *)arglist[0];
   chanindex *ci = cp->index;
   nick *np = arglist[1];
+  flag_t beforeflags = (flag_t)(long)arglist[3];
 
   if(np) {
-    lua_avpcall("irc_onmode", "Sls", ci->name, np->numeric, printallmodes(cp));
+    lua_avpcall("irc_onmode", "Slss", ci->name, np->numeric, printallmodes(cp), printlimitedmodes(cp, beforeflags));
   } else {
-    lua_avpcall("irc_onmode", "S0s", ci->name, printallmodes(cp));
+    lua_avpcall("irc_onmode", "S0ss", ci->name, printallmodes(cp), printlimitedmodes(cp, beforeflags));
   }
 }