basic irc plugin

This commit is contained in:
2018-11-23 14:38:39 +01:00
commit 90f7af7bdc
15 changed files with 571 additions and 0 deletions

44
src/IrcConfig.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef __IRC_CONFIG__
#define __IRC_CONFIG__
#include <JsonStruct.h>
#define JSON_IRC_SERVER "ircServer"
#define JSON_IRC_PORT "ircPort"
#define JSON_IRC_NICKNAME "ircNickname"
#define JSON_IRC_USER "ircUser"
struct IrcConfig
{
const char *server;
int port;
const char *nickname;
const char *user;
};
struct IrcConfigJson : public IrcConfig, public JsonStruct
{
void mapJsonObject(JsonObject &root)
{
root[JSON_IRC_SERVER] = server;
root[JSON_IRC_PORT] = port;
root[JSON_IRC_NICKNAME] = nickname;
root[JSON_IRC_USER] = user;
}
void fromJsonObject(JsonObject &json)
{
if (!verifyJsonObject(json))
{
Serial.println("ERROR: cannot parse JSON object");
valid = 0;
return;
}
server = getAttr(json, JSON_IRC_SERVER);
port = getIntAttrFromJson(json, JSON_IRC_PORT);
nickname = getAttr(json, JSON_IRC_NICKNAME);
user = getAttr(json, JSON_IRC_USER);
valid = 1;
};
};
#endif

118
src/IrcPlugin.cpp Normal file
View File

@@ -0,0 +1,118 @@
#include "IrcPlugin.h"
IrcPlugin::IrcPlugin(IrcConfig cfg)
{
applyConfig(cfg);
}
void IrcPlugin::applyConfig(IrcConfig cfg)
{
server = String(cfg.server);
port = cfg.port;
nick = String(cfg.nickname);
user = String(cfg.user);
}
void IrcPlugin::applyConfigFromFile(const char *fileName)
{
IrcConfigJson configFile;
configFile.fromFile(fileName);
if (configFile.valid)
{
PRINT_MSG(Serial, "MQTT", "apply config from file");
applyConfig(configFile);
}
}
void IrcPlugin::activate(Scheduler *scheduler)
{
applyConfigFromFile("/ircConfig.json");
client = new IRCClient(server.c_str(), port, wifiClient);
client->setCallback(bind(&IrcPlugin::callback, this, _1));
client->setSentCallback(bind(&IrcPlugin::debugSentCallback, this, _1));
String channel = "#illucat";
subscribe("irc/connect", bind(&IrcPlugin::connect, this));
subscribe("irc/join", bind(&IrcPlugin::join, this, _1));
subscribe("irc/sendMessage", bind(&IrcPlugin::sendMessage, this, channel, _1));
//enableConnectTask(scheduler);
enableProcessTask(scheduler);
PRINT_MSG(Serial, "MQTT", "plugin activated");
}
void IrcPlugin::enableConnectTask(Scheduler *scheduler)
{
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&IrcPlugin::connect, this));
scheduler->addTask(connectTask);
connectTask.enable();
}
void IrcPlugin::enableProcessTask(Scheduler *scheduler)
{
processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&IRCClient::loop, client));
scheduler->addTask(processTask);
processTask.enable();
}
void IrcPlugin::connect()
{
if (!client->connected())
{
PRINT_MSG(Serial, "IRC", String("Attempting connection to " + server).c_str());
if (client->connect(nick.c_str(), user.c_str()))
{
PRINT_MSG(Serial, "IRC", "connected");
}
else
{
PRINT_MSG(Serial, "IRC", "failed... ");
}
return;
}
}
void IrcPlugin::join(String channel)
{
if (client->connected())
{
String joinCmd = "JOIN :" + channel + "\r\n";
wifiClient.print(joinCmd.c_str());
PRINT_MSG(Serial, "IRC", joinCmd.c_str());
}
}
void IrcPlugin::sendMessage(String to, String msg)
{
client->sendMessage(to, msg);
}
void IrcPlugin::callback(IRCMessage ircMessage)
{
String message("<" + ircMessage.nick + "> " + ircMessage.text);
publish("irc/log", message);
PRINT_MSG(Serial, "IRC", message.c_str());
// PRIVMSG ignoring CTCP messages
/* if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001')
{
String message("<" + ircMessage.nick + "> " + ircMessage.text);
PRINT_MSG(Serial, "IRC", message.c_str());
if (ircMessage.nick == REPLY_TO)
{
client->sendMessage(ircMessage.nick, "Hi " + ircMessage.nick + "! I'm your IRC bot.");
}
return;
} */
PRINT_MSG(Serial, "IRC", ircMessage.original.c_str());
}
void IrcPlugin::debugSentCallback(String data)
{
PRINT_MSG(Serial, "IRC", data.c_str());
}

47
src/IrcPlugin.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef __IRC_PLUGIN__
#define __IRC_PLUGIN__
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#include <IRCClient.h>
#include <WiFiClient.h>
#include <Plugin.h>
#include "utils/print.h"
#include "IrcConfig.h"
using namespace std;
using namespace std::placeholders;
#define REPLY_TO "mentex" // Reply only to this nick
class IrcPlugin : public Plugin
{
public:
IrcPlugin(IrcConfig cfg);
void activate(Scheduler *scheduler);
private:
WiFiClient wifiClient;
IRCClient* client;
Task connectTask;
Task processTask;
String server;
int port;
String nick;
String user;
void applyConfig(IrcConfig cfg);
void applyConfigFromFile(const char *fileName);
void enableConnectTask(Scheduler *scheduler);
void enableProcessTask(Scheduler *scheduler);
virtual void connect();
virtual void callback(IRCMessage ircMessage);
virtual void debugSentCallback(String data);
void join(String channel);
void sendMessage(String to, String msg);
};
#endif

View File

@@ -0,0 +1,38 @@
#ifndef __DEVICE_CONFIG__
#define __DEVICE_CONFIG__
// Scheduler config
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#define _TASK_PRIORITY
// Chip config
#define SPROCKET_TYPE "SPROCKET"
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 1000
// network config
#define SPROCKET_MODE 1
#define WIFI_CHANNEL 11
#define MESH_PORT 5555
#define AP_SSID "sprocket"
#define AP_PASSWORD "th3r31sn0sp00n"
#define MESH_PREFIX "sprocket-mesh"
#define MESH_PASSWORD "th3r31sn0sp00n"
#define STATION_SSID "MyAP"
#define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "sprocket"
#define CONNECT_TIMEOUT 10000
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
//#define MESH_DEBUG_TYPES ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
// WebServer
#define WEB_CONTEXT_PATH "/"
#define WEB_DOC_ROOT "/www"
#define WEB_DEFAULT_FILE "index.html"
#define WEB_PORT 80
#define MQTT_CONFIG_FILE "/mqttConfig.json"
#endif

View File

@@ -0,0 +1,45 @@
//#include <ESP8266WiFi.h>
//#include <IRCClient.h>
#include "config.h"
#include "WiFiNet.h"
#include "Sprocket.h"
#include "IrcPlugin.h"
#define IRC_SERVER "chat.freenode.net"
#define IRC_PORT 6665
#define IRC_NICKNAME "illucat"
#define IRC_USER "illucat"
WiFiNet *network;
Sprocket *sprocket;
IrcPlugin *irc;
void setup()
{
delay(1000);
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
irc = new IrcPlugin({IRC_SERVER, IRC_PORT, IRC_NICKNAME, IRC_USER});
sprocket->addPlugin(irc);
network = new WiFiNet(
SPROCKET_MODE,
STATION_SSID,
STATION_PASSWORD,
AP_SSID,
AP_PASSWORD,
HOSTNAME,
CONNECT_TIMEOUT);
network->connect();
sprocket->activate();
sprocket->publish("irc/connect", "");
sprocket->publish("irc/join", "#illucat");
sprocket->publish("irc/sendMessage", "hoiii");
}
void loop()
{
sprocket->loop();
yield();
}