-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
160 lines (132 loc) · 6.07 KB
/
plugin.js
File metadata and controls
160 lines (132 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var format = require('util').format;
var Promise = require('bluebird');
var split = require('split-fwd-slash');
var c = require('irc-colors');
var _ = require('lodash');
const helps = {
"correction": [
"s/<target>/<replacement>",
"Correct a previously said message."
]
};
const _getNotice = function(msg) {
return {
intent: 'notice',
query: true,
message: msg
};
}
var TennuCorrection = {
configDefaults: {
"correction": {
"lookBackLimit": 60
},
},
init: function(client) {
var correctionConfig = client.config("correction");
var queueHandler = require('./lib/queue-handler')(correctionConfig.lookBackLimit);
var middleware;
function router(IRCMessage) {
return Promise.try(function() {
var isSearchAndReplace = IRCMessage.message.match(/^[S|s]\/(.+)/);
var isSearchAndRandomReplace = IRCMessage.message.match(/^[R|r][A|a]\/(.+)/);
if (!isSearchAndReplace && !isSearchAndRandomReplace) {
queueHandler.update(IRCMessage.message, IRCMessage.channel, IRCMessage.nickname);
return;
}
var splitMessage = split(IRCMessage.message);
if(isSearchAndReplace)
{
if (splitMessage.length !== 3) {
return _getNotice('Your search and replace did not have exactly one target, and one response. Make sure youre escaping slashes properlly.');
}
var target = splitMessage[1];
var replacement = splitMessage[2];
return Promise.try(function() {
if (_.isFunction(middleware)) {
return callMiddleware(target, IRCMessage.channel, replacement);
}
})
.then(function(middlewareResponse) {
if (!_.isUndefined(middlewareResponse)) {
return middlewareResponse;
} else {
return handleCorrection(target, IRCMessage.channel, replacement);
}
})
.catch(function(err) {
client._logger.error('Error in tennu-correction middleware');
return err;
});
}
else if(isSearchAndRandomReplace)
{
var replacement = splitMessage[1];
return Promise.try(function() {
if (_.isFunction(middleware)) {
return callMiddleware(null, IRCMessage.channel, replacement);
}
})
.then(function(middlewareResponse) {
if (!_.isUndefined(middlewareResponse)) {
return middlewareResponse;
} else {
return handleRandomCorrection(IRCMessage.channel, replacement);
}
})
.catch(function(err) {
client._logger.error('Error in tennu-correction middleware');
return err;
});
}
})
.catch(function(err) {
client._logger.error(err);
});
}
function callMiddleware(target, channel, replacement) {
return middleware(correctionConfig.lookBackLimit, target, channel, replacement);
}
function handleCorrection(target, channel, replacement) {
return queueHandler.findCorrectable(target, channel).then(function(maybeFound) {
if (!maybeFound) {
return _getNotice(format('I searched the last %s in the cache but couldnt find anything with "%s" in it', correctionConfig.lookBackLimit, target));
}
return getCorrected(maybeFound, target, replacement);
});
}
function handleRandomCorrection(channel, replacement) {
return queueHandler.getRandomMessage(channel).then(function(randomMessage){
return correctRandomWord(randomMessage, replacement);
});
}
function correctRandomWord(IRCMessage, replacement) {
var randomMessage = IRCMessage.message;
var words = randomMessage.split(/\s/);
var randomWord = words[Math.floor(Math.random()*words.length)];
var randomReplaceResult = randomMessage.replace(randomWord, c.bold(replacement));
return format('Correction, <%s> %s', IRCMessage.nickname, randomReplaceResult);
}
function getCorrected(IRCMessage, target, replacement) {
var replacementValue = c.bold(replacement);
var corrected = IRCMessage.message.replace(new RegExp(_.escapeRegExp(target), 'g'), replacementValue);
return format('Correction, <%s> %s', IRCMessage.nickname, corrected);
}
var addMiddleware = function(newMiddleware) {
middleware = newMiddleware;
};
return {
handlers: {
"privmsg": router,
},
help: {
"correction": helps.correction
},
exports: {
addMiddleware: addMiddleware,
queueHandler: queueHandler
}
};
}
};
module.exports = TennuCorrection;