-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathircdhmn.js
More file actions
173 lines (148 loc) · 4.34 KB
/
ircdhmn.js
File metadata and controls
173 lines (148 loc) · 4.34 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
161
162
163
164
165
166
167
168
169
170
171
172
173
var sys = require('sys');
var irc = require('irc-js');
// add trim method to String object -- removes white space from both the left and right of a string
if( typeof String.prototype.trim !== 'function' )
{
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
// connection options
var freenode = {
bot: 0,
opt: { server: 'verne.freenode.net', nick: 'ircdhmn' },
prefix: '(fn) ',
ready: false,
};
var espernet = {
bot: 0,
opt: { server: 'irc.esper.net', nick: 'ircdhmn' },
prefix: '(en) ',
ready: false,
};
freenode.bot = new irc( freenode.opt );
espernet.bot = new irc( espernet.opt );
var do_echo = false;
// list of command objects -- each object contains 2 properties: a regex value used to test against
// a command, and a operation function that gets executed if the test is a match
//
// op: function( orig, dest, rawmsg, cmd )
// orig: object representing the origin of the command, has the following properties:
// bot: the IRC object that handles communication with the IRC server
// opt: the options object associated with the bot
// prefix: a string identifying the bot's irc channel
// dest: object with same format as the orig parameter, except it represents the destination
// rawmsg: this is the message object as created in the IRC-js module
// cmd: this is the command text which was parsed out of the message text
// matches: this is the result of executing the test regex against the cmd
//
var commands = [
{
test: /^\s*yes\s*$/i,
op: function( orig, dest, rawmsg, cmd, matches ) {
say( [orig.bot, dest.bot], '#dhmn', 'Good. Very good.' );
},
},
{
test: /^\s*no\s*$/i,
op: function( orig, dest, rawmsg, cmd, matches ) {
say( [orig.bot, dest.bot], '#dhmn', 'Tsk. Tsk.' );
},
},
{
test: /^\s*src\s*$/i,
op: function( orig, dest, rawmsg, cmd, matches ) {
say( [orig.bot, dest.bot], '#dhmn', 'https://github.com/zombified/ircdhmn' );
},
},
{
test: /^\s*ls\s*$/i,
op: function( orig, dest, rawmsg, cmd, matches ) {
dest.bot.names('#dhmn', function( channel, names ) {
orig.bot.privmsg('#dhmn', dest.prefix + names.join(', '));
});
},
},
{
test: /^\s*agree with (.*)\s*$/i,
op: function( orig, dest, rawmsg, cmd, matches ) {
if( matches.length > 1 )
{
if( matches[1].trim().toLowerCase() == 'me' )
{
say( [orig.bot, dest.bot], '#dhmn', 'I agree with you, ' + rawmsg.person.nick );
}
else
{
say( [orig.bot, dest.bot], '#dhmn', 'I agree with ' + matches[1] );
}
}
},
},
];
function botready( botname )
{
if( botname == 'freenode' ) { freenode.ready = true; }
else if( botname == 'espernet' ) { espernet.ready = true; }
do_echo = freenode.ready && espernet.ready;
}
function freenode_listener( msg )
{
command_dispatch( freenode, espernet, msg );
}
function espernet_listener( msg )
{
command_dispatch( espernet, freenode, msg );
}
// sends the same message to the given list of IRC-js objects
function say( bots, to, msg )
{
for( var i = 0; i < bots.length; i++ )
{
bots[i].privmsg( to, msg );
}
}
function echo( orig, dest, msg )
{
if( !do_echo ) { return; } // || msg.person.nick == freenode.opt.nick || msg.person.nick == espernet.opt.nick ) { return; }
dest.bot.privmsg( '#dhmn', orig.prefix + msg.person.nick + '> ' + msg.params.slice( -1 ).toString() );
}
function command_dispatch( orig, dest, msg )
{
var msgtext = msg.params.slice( -1 ).toString();
var prefixre = new RegExp(orig.opt.nick + ':(.*)');
var prefixmatch = prefixre.exec(msgtext);
// if someone types in 'ircdhmn:' then it is considered a command, and should be handled as such
if( prefixmatch != null && prefixmatch.length > 1 )
{
var cmdmatch;
for( var i = 0; i < commands.length; i++ )
{
cmdmatch = commands[i].test.exec(prefixmatch[1]);
if( cmdmatch != null )
{
commands[i].op( orig, dest, msg, prefixmatch[1], cmdmatch );
}
}
}
else
{
echo( orig, dest, msg );
}
}
freenode.bot.addListener( 'privmsg', freenode_listener );
espernet.bot.addListener( 'privmsg', espernet_listener );
freenode.bot.connect(function() {
setTimeout( function() {
freenode.bot.join('#dhmn');
botready('freenode');
}, 5000 );
});
espernet.bot.connect(function() {
espernet.bot.listenOnce( 'ping', function() {
setTimeout( function() {
espernet.bot.join('#dhmn');
botready('espernet');
}, 5000 );
});
});