is_create.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "module.h"
  2. #include "instaserv.h"
  3. static ServiceReference<InstaServCore> instaServ("InstaServService", "InstaServ");
  4. class ISCreate;
  5. class InstaMessageBufferImpl: public InstaMessageBuffer, public Timer
  6. {
  7. std::vector<Anope::string> lines;
  8. User *u;
  9. ISCreate *module;
  10. public:
  11. InstaMessageBufferImpl(ISCreate *m, User *user);
  12. void Tick(time_t t) anope_override;
  13. void Add(Anope::string &msg) anope_override;
  14. size_t LineCount() const anope_override;
  15. void OnEndBuffer() anope_override;
  16. };
  17. class CommandISCreate : public Command
  18. {
  19. ISCreate *creator;
  20. public:
  21. CommandISCreate(ISCreate *c, const Anope::string &sname = "instaserv/create") : Command((Module*) c, sname, 0, 0), creator(c)
  22. {
  23. this->SetDesc(_("Create a new quote"));
  24. this->SetSyntax("");
  25. }
  26. void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
  27. {
  28. if (!source.GetUser())
  29. return;
  30. source.Reply("Type in your quote, end with a line only containing `EOF'");
  31. instaServ->SetBuffer(source.GetUser(), new InstaMessageBufferImpl(creator, source.GetUser()));
  32. }
  33. bool OnHelp(CommandSource &source, const Anope::string &) anope_override
  34. {
  35. this->SendSyntax(source);
  36. source.Reply(_(" \n"
  37. "Create a new quote image"));
  38. return true;
  39. }
  40. };
  41. class ISCreate : public Module
  42. {
  43. CommandISCreate commandiscreate;
  44. public:
  45. ISCreate(const Anope::string &modname, const Anope::string &creator);
  46. size_t GetMaxLines()
  47. {
  48. return 5;
  49. }
  50. };
  51. ISCreate::ISCreate(const Anope::string &modname, const Anope::string &creator): Module(modname, creator, VENDOR), commandiscreate(this)
  52. {}
  53. InstaMessageBufferImpl::InstaMessageBufferImpl(ISCreate *m, User *user) :Timer(*instaServ, Config->GetModule("instaserv")->Get<time_t>("typingtimeout", "1m")), u(user), module(m)
  54. {}
  55. void InstaMessageBufferImpl::Tick(time_t t)
  56. {
  57. (*instaServ)->OnExpire(u);
  58. }
  59. void InstaMessageBufferImpl::Add(Anope::string &msg)
  60. {
  61. if (LineCount() < module->GetMaxLines())
  62. lines.push_back(msg);
  63. else
  64. {
  65. std::stringstream ss;
  66. ss << "Cannot paste more than " << module->GetMaxLines() << " lines";
  67. u->SendMessage((*instaServ)->GetBotInfo(), ss.str());
  68. }
  69. }
  70. size_t InstaMessageBufferImpl::LineCount() const
  71. {
  72. return lines.size();
  73. }
  74. void InstaMessageBufferImpl::OnEndBuffer()
  75. {
  76. Log(LOG_DEBUG) << "::: received END";
  77. (*instaServ)->OnExpire(u);
  78. }
  79. MODULE_INIT(ISCreate)