Skip to content

Commit

Permalink
Splitting of messages sent to IRC, prepending TG username to each one.
Browse files Browse the repository at this point in the history
Should fix #53. Not tested in actual environment yet, so WIP.
  • Loading branch information
michalrud committed Dec 5, 2018
1 parent e18e4ef commit 007751e
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 197 deletions.
1 change: 1 addition & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let settings = {
showLeaveMessage: process.env.IRC_SHOW_LEAVE_MESSAGE === "true" || true,
nickservPassword: process.env.IRC_NICKSERV_PASS || "",
nickservService: process.env.IRC_NICKSERV_SERVICE || "",
maxMessageLength: Number(process.env.IRC_MAX_MESSAGE_LENGTH) || 400,
},
tg: {
chatId: process.env.TELEGRAM_CHAT_ID || "-000000000",
Expand Down
5 changes: 5 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ IRC

- IRC server you wish to connect to (default: ``chat.freenode.net``)

- **IRC_MAX_MESSAGE_LENGTH**:

- Maximum length of the message that can be sent to IRC. Longer messages
will be splitted into multiple messages. (default: ``400``)

Telegram
^^^^^^^^

Expand Down
28 changes: 23 additions & 5 deletions lib/TeleIrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TeleIrc {
this.defaultIrcSuffix = "";
this.defaultShowJoinMessage = false;
this.defaultShowLeaveMessage = false;
this.defaultMaxMessageLength = 400;
}

// ---------------- Functions ----------------
Expand Down Expand Up @@ -111,6 +112,12 @@ class TeleIrc {
this.defaultShowLeaveMessage,
"showLeaveMessage"
);

this.config.irc.maxMessageLength = this.checkConfigOption(
this.config.irc.maxMessageLength,
this.defaultMaxMessageLength,
"maxMessageLength"
);
} else {
throw new TeleIrcException(
this.errorCodes.MissingIrcConfig,
Expand Down Expand Up @@ -442,11 +449,22 @@ class TeleIrc {
* Sends a message to the IRC channel.
* @param {String} message - The message to send to the IRC channel.
*/
sendMessageToIrc(message) {
this.ircbot.say(
this.config.irc.channel,
message
);
sendMessageToIrc(from, message) {
const ircbot = this.ircbot;
const messagePrefix = this.config.irc.prefix + from + this.config.irc.suffix + " ";
const channel = this.config.irc.channel;
const maxLength = this.config.irc.maxMessageLength;

message.toString().split(/\r?\n/).filter(function(line) {
return line.length > 0;
}).forEach(function(line) {
var linesToSend = ircbot._splitLongLines(line, maxLength - messagePrefix.length, []);

linesToSend.forEach(function(toSend) {
ircbot.say(channel, messagePrefix + toSend);
});
});

}

/**
Expand Down
4 changes: 1 addition & 3 deletions lib/TelegramHandlers/TgMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class TgMessageHandler {
}

let username = TgHelpers.ResolveUserName(from);
let message = this._prefixSuffixConfig.prefix + username + this._prefixSuffixConfig.suffix + " " + userMessage;

this._action(message);
this._action(username, userMessage);
}
}

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
"irc": "~0.5.0",
"node": "~8",
"node-telegram-bot-api": "^0.30.0"
},
"devDependencies": {
"nodeunit": "^0.11.2"
}
}
65 changes: 32 additions & 33 deletions tests/IrcConfigValidationTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ function shouldThrow(assert, block, errorCode) {
assert.throws(
block,
function(exception) {
return exception.errorCode === errorCode
let retval = (exception.errorCode === errorCode);
if (!retval) {
console.log(JSON.stringify(exception));
console.trace();
}
return retval;
}
);
}

// -------- Missing IRC Config Tests --------

exports.ircConfigValidation = {};

/**
* Ensures if the IRC config is missing completely from our settings, an exception gets thrown.
*/
exports.ircConfigValidation_MissingIrcConfig = function(assert) {
exports.ircConfigValidation.MissingIrcConfig = function(assert) {
// Copy our default settings, but ignore the IRC portion.
let testSettings = {
token: config.token,
Expand Down Expand Up @@ -72,14 +79,14 @@ function doRequiredSettingsTest(assert, uut, expectedErrorCode) {
* Ensures if the IRC config is missing its server config, an
* exception gets thrown.
*/
exports.ircConfigValidation_missingServerConfig = function(assert) {
exports.ircConfigValidation["Missing IRC server config causes an exception"] = function(assert) {
// Copy our default settings, but ignore the server.
let testSettings = {
token: config.token,
irc: {
// No Server.
channel: config.irc.channel,
botName: config.irc.botName,
channel: "test channel",
botName: "test bot name",
sendStickerEmoji: config.sendStickerEmoji,
prefix: config.prefix,
suffix: config.suffix,
Expand Down Expand Up @@ -108,18 +115,14 @@ exports.ircConfigValidation_missingServerConfig = function(assert) {
assert.done();
}

/**
* Ensures if the IRC config is missing its channel config, an
* exception gets thrown.
*/
exports.ircConfigValidation_missingChannelConfig = function(assert) {
exports.ircConfigValidation["Missing IRC channel config causes an exception"] = function(assert) {
// Copy our default settings, but ignore the server.
let testSettings = {
token: config.token,
irc: {
server: config.irc.server,
server: "test irc server",
// No channel
botName: config.irc.botName,
botName: "test bot name",
sendStickerEmoji: config.sendStickerEmoji,
prefix: config.prefix,
suffix: config.suffix,
Expand Down Expand Up @@ -148,23 +151,19 @@ exports.ircConfigValidation_missingChannelConfig = function(assert) {
assert.done();
}

/**
* Ensures if the IRC config is missing its channel config, an
* exception gets thrown.
*/
exports.ircConfigValidation_missingBotNameConfig = function(assert) {
exports.ircConfigValidation["Missing IRC bot name causes an exception"] = function(assert) {
// Copy our default settings, but ignore the server.
let testSettings = {
token: config.token,
irc: {
server: config.irc.server,
channel: config.irc.channel,
server: "test irc server",
channel: "test channel",
// No bot name.
sendStickerEmoji: config.sendStickerEmoji,
prefix: config.prefix,
suffix: config.suffix,
showJoinMessage: config.showJoinMessage,
showLeaveMessage: config.showLeaveMessage
showLeaveMessage: config.showLeaveMessage,
},
ircBlacklist: config.ircBlacklist,
tg: config.tg,
Expand Down Expand Up @@ -216,16 +215,16 @@ function doDefaultSettingsTest(assert, testSettings) {
* Ensures if the IRC config is missing optional values, it gets
* added in as a default.
*/
exports.ircConfigValidation_defaultSettingsTest_notDefined = function(assert) {
exports.ircConfigValidation.defaultSettingsTest_notDefined = function(assert) {
// Copy our default settings, but ignore the IRC showEmoji.
let testSettings = {
token: config.token,
irc: {
// These three options are required, everything else is optional.
// If missing, we should use a default setting.
server: config.irc.server,
channel: config.irc.channel,
botName: config.irc.botName,
server: "test irc server",
channel: "test channel",
botName: "test bot name",
sendStickerEmoji: undefined,
prefix: undefined,
suffix: undefined,
Expand All @@ -246,16 +245,16 @@ exports.ircConfigValidation_defaultSettingsTest_notDefined = function(assert) {
* Ensures if the IRC config is missing optional values, it gets
* added in as a default.
*/
exports.ircConfigValidation_defaultSettingsTest_setToNull = function(assert) {
exports.ircConfigValidation.defaultSettingsTest_setToNull = function(assert) {
// Copy our default settings, but ignore the IRC showEmoji.
let testSettings = {
token: config.token,
irc: {
// These three options are required, everything else is optional.
// If missing, we should use a default setting.
server: config.irc.server,
channel: config.irc.channel,
botName: config.irc.botName,
server: "test irc server",
channel: "test channel",
botName: "test bot name",
sendStickerEmoji: null,
prefix: null,
suffix: null,
Expand All @@ -276,17 +275,17 @@ exports.ircConfigValidation_defaultSettingsTest_setToNull = function(assert) {
* Ensures if the IRC config's optional values are set to undefined, it gets
* set to its default.
*/
exports.ircConfigValidation_defaultSettingsTest_missingSettings = function(
exports.ircConfigValidation.defaultSettingsTest_missingSettings = function(
assert) {
// Copy our default settings, but ignore the IRC showEmoji.
let testSettings = {
token: config.token,
irc: {
// These three options are required, everything else is optional.
// If missing, we should use a default setting.
server: config.irc.server,
channel: config.irc.channel,
botName: config.irc.botName
server: "test irc server",
channel: "test channel",
botName: "test bot name"
},
ircBlacklist: config.ircBlacklist,
tg: config.tg,
Expand All @@ -298,7 +297,7 @@ exports.ircConfigValidation_defaultSettingsTest_missingSettings = function(
assert.done();
};

exports.ircConfigValidation_checkTypes = (assert) => {
exports.ircConfigValidation.checkTypes = (assert) => {
assert.strictEqual(typeof config.irc.sendStickerEmoji, 'boolean');
assert.strictEqual(typeof config.irc.showJoinMessage, 'boolean');
assert.strictEqual(typeof config.irc.showLeaveMessage, 'boolean');
Expand Down

0 comments on commit 007751e

Please sign in to comment.