-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscottphillips.js
82 lines (65 loc) · 1.99 KB
/
scottphillips.js
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
var sys = require('sys');
var config = require('./config')['config'];
var irc = require('./lib/irc');
/* kynetx lib/setup */
var knsevents = require('kns');
// sets up an application with the needed parameters
kns = new knsevents(config.appid, {
'appversion': 'dev',
'eventdomain': 'irc',
'logging':'true'
});
sys.puts(sys.inspect(config));
var client = new irc.Client(config.host, config.port);
client.connect(config.user);
// On connect, join the given channel;
client.addListener('001', function() {
console.log("Connected!");
for(var i = 0; i < config.channels.length; i++){
this.send('JOIN', config.channels[i]);
}
});
// A user joined the channel
client.addListener('JOIN', function(prefix, channel, text) {
var user = irc.user(prefix);
kns.signal("joined", {"user":user, "channel":channel});
});
// A user left the channel
client.addListener('PART', function(prefix, channel, text) {
var user = irc.user(prefix);
kns.signal("leave", {"user":user, "channel":channel});
});
// Oh crap! We disconnected!
client.addListener('DISCONNECT', function() {
puts('Disconnected, re-connect in 5s');
setTimeout(function() {
puts('Trying to connect again ...');
inChannel = false;
client.connect(config.user);
setTimeout(function() {
if (!inChannel) {
puts('Re-connect timeout');
client.disconnect();
client.emit('DISCONNECT', 'timeout');
}
}, 5000);
}, 5000);
});
// Regular message
client.addListener('PRIVMSG', function(prefix, channel, text) {
var user = irc.user(prefix);
kns.signal("recieved", {'text':text,'channel':channel,'user':user});
});
// directive handlers (callbacks)
// error
kns.on("error", function(error){
console.log("ERROR:");
console.log(error);
client.send("PRIVMSG", config.channel, ":There was an error connecting to KNS");
});
// say something in the channel
kns.on("say", function(eventargs){
if(eventargs.to && eventargs.text){
client.send("PRIVMSG", eventargs.to, ":" +eventargs.text);
}
});