libreboot.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Libre Boot, an automated boot for removing spammers off IRC channels
  3. * Copyright (C) 2018 Fedja Beader fedja@protonmail.ch
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (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 Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /* TODO ideas:
  19. * - ban (and unban after a set time)
  20. * - save bans to a file
  21. * - privmsg channel operators in case we are not opped
  22. * - chanserv op if we are not opped but are +o on access list
  23. */
  24. #include <znc/Chan.h>
  25. #include <znc/IRCNetwork.h>
  26. class CLibreBootMod : public CModule {
  27. public:
  28. MODCONSTRUCTOR(CLibreBootMod) {
  29. m_thresh_nicks = 3;
  30. AddHelpCommand();
  31. }
  32. ~CLibreBootMod() override {}
  33. void ProcessMessage(const CChan& channel, const CNick& nick, const CString& text) {
  34. // 1. Are we OP in channel?
  35. if (! channel.HasPerm(CChan::Op))
  36. return;
  37. // 2. Does text include at least m_thresh_nicks mentions?
  38. SCString tokens;
  39. text.Split(" ", tokens, false, "", "", false, true);
  40. unsigned hits = 0;
  41. CString tokenised;
  42. for (const CString& token : tokens) {
  43. if (nullptr != channel.FindNick(token))
  44. hits++;
  45. tokenised + '"' + token + "\", ";
  46. }
  47. CString sHits (hits);
  48. if (text.size() > 150)
  49. PutModule("Hits " + sHits + " for " + tokenised);
  50. // 3. ...
  51. if (hits < m_thresh_nicks)
  52. return;
  53. // 4) Profit! Err... I mean kickban
  54. PutModule("Kickbanned " + nick.GetNickMask() + " for " + sHits + " mentions in \""
  55. + text + "\" i.e. [" + tokenised + "]");
  56. //MODE <channel> <flags> [<args>]
  57. PutIRC ("MODE " + channel.GetName() + " +q *!*@" + nick.GetHost());
  58. //KICK <channel> <client> [<message>]
  59. PutIRC("KICK " + channel.GetName() + " " + nick.GetNick() + " automatic kick for mass-highlight spam.");
  60. //PRIVMSG <msgtarget> <message>
  61. //Sends <message> to <msgtarget>, which is usually a user or channel.[31] Defined in RFC 1459
  62. }
  63. EModRet OnChanMsg(CNick& nick, CChan& channel, CString& text) override {
  64. ProcessMessage(channel, nick, text);
  65. return CONTINUE;
  66. }
  67. EModRet OnChanAction(CNick& nick, CChan& channel, CString& text) override {
  68. ProcessMessage(channel, nick, text);
  69. return CONTINUE;
  70. }
  71. private:
  72. unsigned m_thresh_nicks;
  73. };
  74. template <>
  75. void TModInfo<CLibreBootMod>(CModInfo& Info) {
  76. Info.SetHasArgs(false);
  77. }
  78. USERMODULEDEFS(CLibreBootMod, "The Libre Boot boots spammers from controlled channels")