SHARE
TWEET

Blacklist+Chat+Spam

CodeHz Sep 1st, 2018 140 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. def eat(string src) {
  3.   auto r := range(src);
  4.   auto ret = "";
  5.   while (!r.empty() && r.front() == ' ') {
  6.     r.pop_front();
  7.   }
  8.   while (!r.empty() && r.front() != ' ') {
  9.     ret += r.front().to_string();
  10.     r.pop_front();
  11.   }
  12.   if (!r.empty()) { r.pop_front(); }
  13.   return [ret, r.join("")];
  14. }
  15.  
  16. def eat(string src, int n) {
  17.   auto r := range(src);
  18.   auto arr = [];
  19.   auto temp = "";
  20.   while (!r.empty() && r.front() == ' ') {
  21.     r.pop_front();
  22.   }
  23.   while (!r.empty() && arr.size() < n) {
  24.     if (r.front() == ' ') {
  25.       arr.push_back(temp);
  26.       temp = "";
  27.       r.pop_front();
  28.       while (!r.empty() && r.front() == ' ') {
  29.         r.pop_front();
  30.       }
  31.       continue;
  32.     }
  33.     temp += r.front().to_string();
  34.     r.pop_front();
  35.   }
  36.   if (!r.empty) {
  37.     arr.push_back(r.join(""));
  38.   }
  39.   return arr;
  40. }
  41.  
  42. auto BlackListDB := ProfileDB
  43. BlackListDB.exec("CREATE TABLE IF NOT EXISTS blacklist(uuid BLOB PRIMARY KEY CHECK(valid_uuid(uuid)), reason TEXT, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP) WITHOUT ROWID;");
  44. global blacklist_list = BlackListDB.prepare("SELECT uuid, reason, time FROM blacklist;");
  45. global blacklist_add = BlackListDB.prepare("INSERT INTO blacklist(uuid, reason) VALUES(?, ?);");
  46. global blacklist_remove = BlackListDB.prepare("DELETE FROM blacklist WHERE uuid=?;");
  47.  
  48. setTimeout(fun() {
  49.   while (blacklist_list.step() == ROW) {
  50.     auto uuid := blacklist_list.get(0);
  51.     auto reason = blacklist_list.get(1);
  52.     addToBlacklist(uuid, reason);
  53.   }
  54.   blacklist_list.reset();
  55. }, 0);
  56.  
  57. {
  58.   defcmd("blacklist", "Show blacklist", 0, 1, fun(CommandOrigin orig, string in) {
  59.     auto buffer = ""
  60.     while (blacklist_list.step() == ROW) {
  61.       auto uuid := blacklist_list.get(0);
  62.       auto reason = blacklist_list.get(1);
  63.       buffer += "${uuid}#${reason}\n";
  64.     }
  65.     buffer.rtrim();
  66.     blacklist_list.reset();
  67.     return buffer;
  68.   }).add(CommandOverload());
  69. }
  70.  
  71. {
  72.   auto banOverload = CommandOverload();
  73.   banOverload.add(Param_target("target", false));
  74.   banOverload.add(Param_message("reason", true));
  75.   defcmd2("ban", "Ban player for some reason", 0, 1, fun(CommandOrigin orig, PlayerList list, string in) {
  76.     if (list.size() == 0) {
  77.       return "No players selected";
  78.     }
  79.     auto buffer = ""
  80.     for (item : list) {
  81.       blacklist_add.bind(1, item.getUUID());
  82.       blacklist_add.bind(2, in);
  83.       blacklist_add.step();
  84.       blacklist_add.reset();
  85.       blacklist_add.clear();
  86.       addToBlacklist(item.getUUID(), in);
  87.       buffer += "Banned ${item.getNameTag()}\n";
  88.     }
  89.     buffer.rtrim();
  90.     return buffer;
  91.   }).add(banOverload);
  92. }
  93.  
  94. {
  95.   auto banOverload = CommandOverload();
  96.   banOverload.add(Param_string("uuid", false));
  97.   banOverload.add(Param_message("reason", true));
  98.   defcmd("banuuid", "Ban player for some reason (by UUID)", 0, 1, fun(CommandOrigin orig, string in) {
  99.     auto spd = in.eat();
  100.     auto uuid = UUID(spd[0])
  101.     blacklist_add.bind(1, uuid);
  102.     blacklist_add.bind(2, spd[1]);
  103.     blacklist_add.step();
  104.     blacklist_add.reset();
  105.     blacklist_add.clear();
  106.     addToBlacklist(uuid, spd[1]);
  107.     return "Banned UUID '${uuid}'"
  108.   }).add(banOverload);
  109. }
  110.  
  111. {
  112.   auto pardonOverload = CommandOverload();
  113.   pardonOverload.add(Param_string("uuid", false));
  114.   defcmd("pardon", "Pardon player", 0, 1, fun(CommandOrigin orig, string in) {
  115.     auto uuid = UUID(in)
  116.     Log.info("pardon", in);
  117.     blacklist_remove.bind(1, uuid);
  118.     blacklist_remove.step();
  119.     blacklist_remove.reset();
  120.     blacklist_remove.clear();
  121.     removeFromBlacklist(uuid, "Pardoned");
  122.     return "Pardoned UUID '${uuid}'"
  123.   })
  124. }
  125.  
  126. auto ChatModDB := ProfileDB
  127.  
  128. WorldDB.exec("CREATE TABLE IF NOT EXISTS chat_log(id INTEGER PRIMARY KEY AUTOINCREMENT, sender_uuid BLOB CHECK(valid_uuid(sender_uuid)), sender_name TEXT, content TEXT);")
  129. global chat_log_stmt = WorldDB.prepare("INSERT INTO chat_log(sender_uuid, sender_name, content) VALUES (?, ?, ?);")
  130. ChatModDB.exec("CREATE TABLE IF NOT EXISTS chat_mod(uuid BLOB PRIMARY KEY CHECK(valid_uuid(uuid)), prefix TEXT, suffix TEXT, tails TEXT) WITHOUT ROWID;")
  131. global chat_mod_query = ChatModDB.prepare("SELECT ifnull(prefix, ''), ifnull(suffix, ''), ifnull(tails, '') FROM chat_mod WHERE uuid=?;")
  132. global chat_mod_update = [
  133.   "prefix": ChatModDB.prepare("INSERT INTO chat_mod(uuid, prefix) VALUES (?, ?) ON CONFLICT(uuid) DO UPDATE SET prefix=excluded.prefix;"),
  134.   "suffix": ChatModDB.prepare("INSERT INTO chat_mod(uuid, suffix) VALUES (?, ?) ON CONFLICT(uuid) DO UPDATE SET suffix=excluded.suffix;"),
  135.   "tails": ChatModDB.prepare("INSERT INTO chat_mod(uuid, tails) VALUES (?, ?) ON CONFLICT(uuid) DO UPDATE SET tails=excluded.tails;")
  136. ];
  137.  
  138. MemDB.exec("CREATE TABLE spam_detect(uuid BLOB CHECK(valid_uuid(uuid)), time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);");
  139. global spam_add = MemDB.prepare("INSERT INTO spam_detect(uuid) VALUES(?);");
  140. global spam_clear = MemDB.prepare("DELETE FROM spam_detect");
  141. global spam_detect = MemDB.prepare("SELECT uuid, count(time) FROM spam_detect GROUP BY uuid;");
  142.  
  143. def addSpam(UUID uuid) {
  144.   spam_add.bind(1, uuid);
  145.   spam_add.step();
  146.   spam_add.reset();
  147.   spam_add.clear();
  148. }
  149.  
  150. setInterval(fun() {
  151.   try {
  152.     while (spam_detect.step() == ROW) {
  153.       auto uuid := spam_detect.get(0)
  154.       auto num = spam_detect.get(1)
  155.       if (num > 2) {
  156.         auto player = getPlayer(uuid);
  157.         if (!player.is_var_null()) {
  158.           player.kick();
  159.         }
  160.       }
  161.     }
  162.     spam_detect.reset();
  163.   } catch(e) {
  164.     Log.error("SPAMD", e.what());
  165.     spam_detect.reset();
  166.   }
  167.  
  168.  
  169.   spam_clear.reset();
  170.   spam_clear.step();
  171. }, 100);
  172.  
  173. dbus_chat_start(fun(string message) {
  174.   forEachPlayer(fun[message](ServerPlayer n) {
  175.     n.sendMessage("system։ " + message)
  176.   })
  177. })
  178.  
  179. setChatHook(fun(ServerPlayer sp, string message) {
  180.   auto sender = sp.getNameTag();
  181.   Log.info("CHAT", sender + ": " + message);
  182.   dbus_chat_log(sender, message);
  183.   auto uuid = sp.getUUID();
  184.   addSpam(uuid);
  185.   chat_log_stmt.bind(1, uuid);
  186.   chat_log_stmt.bind(2, sp.getNameTag());
  187.   chat_log_stmt.bind(3, message);
  188.   chat_log_stmt.step();
  189.   chat_log_stmt.reset();
  190.   chat_log_stmt.clear();
  191.  
  192.   auto send;
  193.  
  194.   chat_mod_query.bind(1, uuid);
  195.   switch (chat_mod_query.step()) {
  196.     case (ROW) {
  197.       send = fun[sender, message](n) { n.sendMessage(chat_mod_query.get(0) + sender + chat_mod_query.get(1) + "։ " + message + chat_mod_query.get(2)); };
  198.       break;
  199.     }
  200.     case (DONE) {
  201.       send = fun[sender, message](n) { n.sendMessage(sender + "։ " + message); };
  202.     }
  203.   }
  204.   forEachPlayer(fun[send](ServerPlayer n) {
  205.     send(n);
  206.   });
  207.   chat_mod_query.clear();
  208.   chat_mod_query.reset();
  209. });
  210.  
  211. {
  212.   auto chatOverload = CommandOverload()
  213.   chatOverload.add(Param_target_wildcard("target", false));
  214.   auto modEnum = EnumDef("chat_mod")
  215.   modEnum.add("prefix");
  216.   modEnum.add("suffix");
  217.   modEnum.add("tails");
  218.   chatOverload.add(Param_enum("attr", false, modEnum));
  219.   chatOverload.add(Param_message("text", false));
  220.   defcmd2("setchat", "Set chat option to players", 0, 0, fun(orig, list, str) {
  221.     if (list.size() == 0) {
  222.       return "No players selected";
  223.     }
  224.     auto spd = str.eat();
  225.     auto key := spd[0];
  226.     auto value := spd[1];
  227.     if (chat_mod_update.count(key) == 0) {
  228.       return "Illegal attr"
  229.     }
  230.     auto stmt := chat_mod_update[key];
  231.     auto res = "success";
  232.     for (item : list) {
  233.       stmt.bind(1, item.getUUID());
  234.       stmt.bind(2, value);
  235.       stmt.step();
  236.       stmt.reset();
  237.       stmt.clear();
  238.     }
  239.     return res;
  240.   }).add(chatOverload);
  241. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top