-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1) Move IRC code out of CNHmqtt and into CNHmqtt_irc
2) Add example app using the IRC bot over MQTT
- Loading branch information
1 parent
0111065
commit 3cef421
Showing
22 changed files
with
375 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
||
}; | ||
|
||
|
Oops, something went wrong.