Skip to content

Commit

Permalink
1) Move IRC code out of CNHmqtt and into CNHmqtt_irc
Browse files Browse the repository at this point in the history
2) Add example app using the IRC bot over MQTT
  • Loading branch information
[email protected] committed Jul 15, 2011
1 parent 0111065 commit 3cef421
Show file tree
Hide file tree
Showing 22 changed files with 375 additions and 127 deletions.
67 changes: 7 additions & 60 deletions CNHmqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@



CNHmqtt::CNHmqtt(int argc, char *argv[])
CNHmqtt::CNHmqtt(int argc, char *argv[])
{
log = NULL;
string config_file = "";
Expand Down Expand Up @@ -217,8 +217,6 @@ int CNHmqtt::mosq_connect()
}
mosq_connected = true;



return 0;
}

Expand Down Expand Up @@ -256,6 +254,12 @@ int CNHmqtt::subscribe(string topic)
return -1;
}

if (!mosq_connected)
{
log->dbg("Subscribe failed - Not connected!");
return -1;
}

if(mosquitto_subscribe(mosq, NULL, topic.c_str(), 0))
{
log->dbg("Subscribe failed!");
Expand Down Expand Up @@ -348,60 +352,3 @@ string CNHmqtt::itos(int n)
return out.str();
}

// irc_in = base topic, e.g. "nh/irc/rx"
// topic = topic actaully received, e.g. "nh/irc/rx/nottinghack/daniel1111
bool CNHmqtt::decode_irc_topic(string irc_in, string topic, string &nick, string &channel)
{

if (irc_in.length() >= topic.length())
{
nick="";
channel="";
return false;
}

// remove irc_in from front
topic = topic.substr(irc_in.length()+1);

if (topic.find_first_of("/") != string::npos)
{
// topic indicates it's a channel chat message
channel = topic.substr(0, topic.find_first_of("/"));
nick = topic.substr(topic.find_first_of("/")+1);
}

if (channel == "pm") // A 'Channel' of pm means it was actaully a private message
channel = "";

return true;
}

int CNHmqtt::irc_send(string message, irc_dest dst)
{
if (dst.channel=="")
return irc_send_nick (message, dst.nick);
else
return irc_send_channel (message, dst.channel);
}

int CNHmqtt::irc_send_nick (string message, string nick)
{
return message_send(irc_out + "/pm/" + nick, message);
}


int CNHmqtt::irc_send_channel (string message, string channel)
{
return message_send(irc_out + "/" + channel, message);
}

bool CNHmqtt::is_irc(string topic, irc_dest *dst)
{
if (topic.length() > irc_in.length())
if(topic.substr(0, irc_in.length())==irc_in)
return decode_irc_topic(irc_in, topic, dst->nick, dst->channel);

dst->nick = "";
dst->channel = "";
return false;
}
14 changes: 0 additions & 14 deletions CNHmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,20 @@ class CNHmqtt
bool mosq_connected;
static bool debug_mode;
static bool daemonized;

static string itos(int n);

protected:

struct irc_dest {
string nick;
string channel;
};

string mqtt_topic;
string mqtt_rx;
string mqtt_tx;
string status; // returned in response to status mqtt message (so should be short!)
string mosq_server;
int mosq_port;
CLogging *log;
string irc_out;
string irc_in;

int message_send(string topic, string message);
int get_int_option(string section, string option, int def_value);
string get_str_option(string section, string option, string def_value);
static bool decode_irc_topic(string irc_in, string topic, string &nick, string &channel);
int irc_send(string message, irc_dest dst);
int irc_send_nick (string message, string nick);
int irc_send_channel (string message, string channel);
bool is_irc(string topic, struct irc_dest *dst);

private:
static void connect_callback(void *obj, int result);
Expand Down
95 changes: 95 additions & 0 deletions CNHmqtt_irc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "CNHmqtt_irc.h"


CNHmqtt_irc::CNHmqtt_irc(int argc, char *argv[]) : CNHmqtt(argc, argv)
{
irc_in = get_str_option("irc", "irc_in", "irc/rx");
irc_out = get_str_option("irc", "irc_out", "irc/tx");
}

CNHmqtt_irc::~CNHmqtt_irc()
{

}

bool CNHmqtt_irc::init()
{
if (mosq_connect())
return false;

subscribe(irc_in + "/#");
daemonize();
return true;
}

void CNHmqtt_irc::process_message(string topic, string message)
{
string nick;
string channel;

if (is_irc_msg(topic))
{
decode_irc_topic(irc_in, topic, nick, channel);
irc_msg msg = irc_msg(message, channel, nick, this);

process_irc_message(msg);
}

CNHmqtt::process_message(topic, message);
}

// irc_in = base topic, e.g. "nh/irc/rx"
// topic = topic actaully received, e.g. "nh/irc/rx/nottinghack/daniel1111
bool CNHmqtt_irc::decode_irc_topic(string irc_in, string topic, string &nick, string &channel)
{

if (irc_in.length() >= topic.length())
{
nick="";
channel="";
return false;
}

// remove irc_in from front
topic = topic.substr(irc_in.length()+1);

if (topic.find_first_of("/") != string::npos)
{
// topic indicates it's a channel chat message
channel = topic.substr(0, topic.find_first_of("/"));
nick = topic.substr(topic.find_first_of("/")+1);
}

if (channel == "pm") // A 'Channel' of pm means it was actaully a private message
channel = "";

return true;
}

int CNHmqtt_irc::irc_reply(string message, irc_msg msg)
{
if (msg.channel=="")
return irc_send_nick (message, msg.nick);
else
return irc_send_channel (message, msg.channel);
}

int CNHmqtt_irc::irc_send_nick (string message, string nick)
{
return message_send(irc_out + "/pm/" + nick, message);
}


int CNHmqtt_irc::irc_send_channel (string message, string channel)
{
return message_send(irc_out + "/" + channel, message);
}

bool CNHmqtt_irc::is_irc_msg(string topic)
{
if (topic.length() > irc_in.length())
if(topic.substr(0, irc_in.length())==irc_in)
return true;

return false;
}
85 changes: 85 additions & 0 deletions CNHmqtt_irc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#pragma once
#include "CNHmqtt.h"

using namespace std;


class CNHmqtt_irc : CNHmqtt
{
public:
CNHmqtt_irc(int argc, char *argv[]);
~CNHmqtt_irc();

class irc_msg {
public:
string nick;
string channel;
string message;

irc_msg(string msg, string chan, string nk, CNHmqtt_irc *irc_con)
{
message = msg;
channel = chan;
nick = nk;
con = irc_con;
}

int reply(string rep)
{
if (channel=="")
return con->irc_send_nick (rep, nick);
else
return con->irc_send_channel (rep, channel);
}

int reply_pm(string rep)
{
return con->irc_send_nick (rep, nick);
}

bool is_pm()
{
return (channel=="");
}

// Allow comparision to a string to work (and only consider message not nick/chan)
bool operator==(const string &str_message) const
{
return (message==str_message);
}

// Allow cast to string
operator string()
{
return message;
}

private:
CNHmqtt_irc *con;
};

string irc_out;
string irc_in;

static bool decode_irc_topic(string irc_in, string topic, string &nick, string &channel);
int irc_reply(string message, irc_msg dst);
int irc_send_nick (string message, string nick);
int irc_send_channel (string message, string channel);
bool is_irc_msg(string topic);
void process_message(string topic, string message);
virtual void process_irc_message(irc_msg msg) {};
bool init();

using CNHmqtt::get_str_option;
using CNHmqtt::get_int_option;
using CNHmqtt::message_send;
using CNHmqtt::log;
using CNHmqtt::subscribe;
using CNHmqtt::message_loop;


// private:

};


Loading

0 comments on commit 3cef421

Please sign in to comment.