Browse Source

instaserv base

isundil 6 years ago
parent
commit
c84970c7b4
7 changed files with 282 additions and 44 deletions
  1. 1 29
      .gitignore
  2. 25 0
      Makefile
  3. 109 0
      instaserv.cpp
  4. 22 0
      instaserv.example.conf
  5. 28 0
      instaserv.h
  6. 97 0
      is_create.cpp
  7. 0 15
      package.json

+ 1 - 29
.gitignore

@@ -1,30 +1,2 @@
-# ---> Node
-# Logs
-logs
-*.log
-npm-debug.log*
-
-# Runtime data
-pids
-*.pid
-*.seed
-
-# Directory for instrumented libs generated by jscoverage/JSCover
-lib-cov
-
-# Coverage directory used by tools like istanbul
-coverage
-
-# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
-.grunt
-
-# node-waf configuration
-.lock-wscript
-
-# Compiled binary addons (http://nodejs.org/api/addons.html)
-build/Release
-
-# Dependency directory
-# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
-node_modules
+*.so
 

+ 25 - 0
Makefile

@@ -0,0 +1,25 @@
+
+ANOPE_SRC=../anope
+
+ANOPE_INSTALL=../anope/testinstall/
+
+CP=	cp
+
+all:	instaserv.so is_create.so
+
+instaserv.so:
+	g++ -fPIC -shared -o instaserv.so instaserv.cpp -I ${ANOPE_SRC}/include -I ${ANOPE_SRC}/build/include
+	
+is_create.so:
+	g++ -fPIC -shared -o is_create.so is_create.cpp -I ${ANOPE_SRC}/include -I ${ANOPE_SRC}/build/include
+
+clean:
+	$(RM) instaserv.so is_create.so
+
+install: all
+	$(CP) instaserv.so ${ANOPE_INSTALL}/lib/
+	$(CP) is_create.so ${ANOPE_INSTALL}/lib/
+	$(CP) instaserv.example.conf ${ANOPE_INSTALL}/conf/instaserv.example.conf
+
+re:	clean all
+

+ 109 - 0
instaserv.cpp

@@ -0,0 +1,109 @@
+/* QuoteServ core functions
+ */
+
+#include "module.h"
+#include "instaserv.h"
+
+class InstaServService: public InstaServCore
+{
+	Reference<BotInfo> InstaServ;
+	std::vector<Anope::string> defaults;
+	std::map<User *, InstaMessageBuffer *> buffers;
+
+public:
+	InstaServService(const Anope::string &modname, const Anope::string &creator);
+	~InstaServService();
+
+	void OnReload(Configuration::Conf *conf) anope_override;
+	void OnShutdown() anope_override;
+	void OnRestart() anope_override;
+	virtual void OnExpire(User *u);
+	EventReturn OnBotPrivmsg(User *u, BotInfo *bi, Anope::string &msg) anope_override;
+
+	BotInfo* GetBotInfo();
+
+	virtual InstaMessageBuffer *GetBuffer(User *u);
+	virtual bool HasBuffer(User *u);
+	virtual void SetBuffer(User *u, InstaMessageBuffer *buffer);
+};
+
+InstaServService::InstaServService(const Anope::string &modname, const Anope::string &creator) : InstaServCore(modname, creator)
+{
+}
+
+InstaServService::~InstaServService()
+{
+	OnShutdown();
+}
+
+BotInfo *InstaServService::GetBotInfo()
+{ return *InstaServ; }
+
+void InstaServService::OnReload(Configuration::Conf *conf)
+{
+	const Anope::string &channick = conf->GetModule(this)->Get<const Anope::string>("client");
+
+	if (channick.empty())
+		throw ConfigException(Module::name + ": <client> must be defined");
+
+	BotInfo *bi = BotInfo::Find(channick, true);
+	if (!bi)
+		throw ConfigException(Module::name + ": no bot named " + channick);
+
+	InstaServ = bi;
+}
+
+void InstaServService::OnShutdown()
+{
+}
+
+void InstaServService::OnRestart()
+{
+	OnShutdown();
+}
+
+InstaMessageBuffer *InstaServService::GetBuffer(User *u)
+{
+	std::map<User*, InstaMessageBuffer *>::iterator it = buffers.find(u);
+	if (it == buffers.end())
+		return NULL;
+	return it->second;
+}
+
+bool InstaServService::HasBuffer(User *u)
+{
+	std::map<User*, InstaMessageBuffer *>::iterator it = buffers.find(u);
+	return it != buffers.end();
+}
+
+void InstaServService::SetBuffer(User *u, InstaMessageBuffer *buffer)
+{
+	buffers[u] = buffer;
+}
+
+EventReturn InstaServService::OnBotPrivmsg(User *u, BotInfo *bi, Anope::string &msg)
+{
+	if (bi == InstaServ)
+	{
+		InstaMessageBuffer *buffer = GetBuffer(u);
+		if (buffer)
+		{
+			if (msg == "EOF")
+				buffer->OnEndBuffer();
+			else
+				buffer->Add(msg);
+			return EVENT_STOP;
+		}
+	}
+	return EVENT_CONTINUE;
+}
+
+void InstaServService::OnExpire(User *u)
+{
+	std::map<User*, InstaMessageBuffer *>::iterator it = buffers.find(u);
+	if (it != buffers.end())
+		buffers.erase(it);
+}
+
+MODULE_INIT(InstaServService)
+

+ 22 - 0
instaserv.example.conf

@@ -0,0 +1,22 @@
+service
+{
+	nick = "InstaServ"
+	user = "services"
+	host = "services.knacki.info"
+	gecos = "Quote Service"
+}
+
+module
+{
+	name = "instaserv"
+	client = "InstaServ"
+}
+
+/* Give it a help command. */
+command { service = "InstaServ"; name = "HELP"; command = "generic/help"; }
+
+module
+{
+	name = "is_create"
+}
+command { service = "InstaServ"; name = "CREATE"; command = "instaserv/create"; }

+ 28 - 0
instaserv.h

@@ -0,0 +1,28 @@
+
+#ifndef INSTASERV_H_
+#define INSTASERV_H_
+
+class InstaServCore;
+
+class InstaMessageBuffer
+{
+public:
+	virtual void Add(Anope::string &msg) =0;
+	virtual size_t LineCount() const =0;
+	virtual void OnEndBuffer() =0;
+};
+
+class InstaServCore : public Module, public Service
+{
+public:
+	InstaServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, PSEUDOCLIENT | VENDOR), Service(this, "InstaServService", "InstaServ")
+	{};
+	virtual void OnExpire(User *u) =0;
+	virtual InstaMessageBuffer *GetBuffer(User *u) =0;
+	virtual bool HasBuffer(User *u) =0;
+	virtual void SetBuffer(User *u, InstaMessageBuffer *buffer) =0;
+	virtual BotInfo *GetBotInfo() =0;
+};
+
+#endif //INSTASERV_H_
+

+ 97 - 0
is_create.cpp

@@ -0,0 +1,97 @@
+
+#include "module.h"
+#include "instaserv.h"
+
+static ServiceReference<InstaServCore> instaServ("InstaServService", "InstaServ");
+
+class ISCreate;
+
+class InstaMessageBufferImpl: public InstaMessageBuffer, public Timer
+{
+	std::vector<Anope::string> lines;
+	User *u;
+	ISCreate *module;
+
+public:
+	InstaMessageBufferImpl(ISCreate *m, User *user);
+
+	void Tick(time_t t) anope_override;
+	void Add(Anope::string &msg) anope_override;
+	size_t LineCount() const anope_override;
+	void OnEndBuffer() anope_override;
+};
+
+class CommandISCreate : public Command
+{
+	ISCreate *creator;
+public:
+	CommandISCreate(ISCreate *c, const Anope::string &sname = "instaserv/create") : Command((Module*) c, sname, 0, 0), creator(c)
+	{
+		this->SetDesc(_("Create a new quote"));
+		this->SetSyntax("");
+	}
+
+	void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
+	{
+		if (!source.GetUser())
+			return;
+		source.Reply("Type in your quote, end with a line only containing `EOF'");
+		instaServ->SetBuffer(source.GetUser(), new InstaMessageBufferImpl(creator, source.GetUser()));
+	}
+
+	bool OnHelp(CommandSource &source, const Anope::string &) anope_override
+	{
+		this->SendSyntax(source);
+		source.Reply(_(" \n"
+			"Create a new quote image"));
+		return true;
+	}
+};
+
+class ISCreate : public Module
+{
+	CommandISCreate commandiscreate;
+
+ public:
+	ISCreate(const Anope::string &modname, const Anope::string &creator);
+	size_t GetMaxLines()
+	{
+		return 5;
+	}
+};
+
+ISCreate::ISCreate(const Anope::string &modname, const Anope::string &creator): Module(modname, creator, VENDOR), commandiscreate(this)
+{}
+
+InstaMessageBufferImpl::InstaMessageBufferImpl(ISCreate *m, User *user) :Timer(*instaServ, Config->GetModule("instaserv")->Get<time_t>("typingtimeout", "1m")), u(user), module(m)
+{}
+
+void InstaMessageBufferImpl::Tick(time_t t)
+{
+	(*instaServ)->OnExpire(u);
+}
+
+void InstaMessageBufferImpl::Add(Anope::string &msg)
+{
+	if (LineCount() < module->GetMaxLines())
+		lines.push_back(msg);
+	else
+	{
+		std::stringstream ss;
+		ss << "Cannot paste more than " << module->GetMaxLines() << " lines";
+		u->SendMessage((*instaServ)->GetBotInfo(), ss.str());
+	}
+}
+
+size_t InstaMessageBufferImpl::LineCount() const
+{
+	return lines.size();
+}
+
+void InstaMessageBufferImpl::OnEndBuffer()
+{
+	Log(LOG_DEBUG) << "::: received END";
+	(*instaServ)->OnExpire(u);
+}
+
+MODULE_INIT(ISCreate)

+ 0 - 15
package.json

@@ -1,15 +0,0 @@
-{
-  "name": "instaserv",
-  "version": "1.0.0",
-  "description": "IRC bot for generating quote images",
-  "main": "index.js",
-  "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1"
-  },
-  "repository": {
-    "type": "git",
-    "url": "https://git.knacki.info/isundil/instaserv"
-  },
-  "author": "isundil",
-  "license": "GPL-3.0"
-}