-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc.js
133 lines (128 loc) · 3.29 KB
/
irc.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
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
/*
* This code is based on the code seen here (http://webdevrefinery.com/forums/topic/8762-writing-a-very-simple-irc-bot-in-nodejs/)
*/
var net = require('net'),
fs = require('fs');
_keys = function(dict) {
var keys = []
for (var key in dict)
if(dict.hasOwnProperty(key))
keys.push(key)
return keys;
};
exports.irc = {
"listeners": {},
"handle": function(data){
var i, info,
keys = _keys(this.listeners);
for (i = 0; i < keys.length; i++)
{
//For each key
var key = keys[i];
//This checks the regex which is the first
//Will not be false if matches.
//Those pushed on first will win.
this.listeners[key].forEach(function (listener) {
info = listener[0].exec(data);
if (info)
{
listener[1](info, data);
}
})
this.listeners[key] = this.listeners[key].filter(function(listener) {
return ! listener[2];
});
}
},
"init": function(filename){
this.load(filename);
this.socket = new net.Socket();
//Set up connect callback
var that = this;
this.socket.on('connect', function () {
that.log("Established connection")
that.on(/^PING\s:(.*)\r/i, function(info){
that.raw('PONG :' + info[1]);
});
setTimeout(function() {
that.raw('NICK '+that.info.nick);
that.raw('USER '+that.info.nick+' 8 * '+that.info.real_name);
setTimeout( function() {
that.join(that.info.channel);
}, 500);
}, 500);
});
this.socket.setEncoding('ascii');
this.socket.setNoDelay();
this.socket.connect(this.info.port, this.info.host);
this.socket.on('data', function(data) {
data = data.split('\n');
data.forEach( function( line ) {
that.handle( line );
});
});
this.load_mods()
},
"load_mods" : function() {
var that = this;
this.info.mods.forEach( function(mod) {
var temp_listen = require("./"+mod).listeners;
temp_listen.forEach(function(listen) {
that.on(listen[0], listen[1], mod)
})
});
},
"load" : function(filename) {
this.filename = filename || "config.json";
this.info = JSON.parse(
fs.readFileSync("config.json"));
},
"join" : function(chan, callback) {
if (callback !== undefined) {
this.on_once(new RegExp('^:' + this.info.nick + '![^@]+@[^ ]+ JOIN :' + chan), callback);
}
this.info.names[chan] = {};
this.raw('JOIN '+ chan);
},
"msg" : function(chan, msg) {
var max_length, msg, interval;
max_length = 500 - chan.length;
msgs = msg.match(new RegExp('.{1,' + max_length + '}', 'g'));
var that=this;
interval = setInterval(function(){
that.raw('PRIVMSG ' + chan + ' :' + msgs[0]);
msgs.splice(0, 1);
if (msgs.length === 0)
{
clearInterval(interval);
}
}, 1000);
},
"on" : function(data, callback, key) {
var that = this;
key = key || "general";
if (callback !== undefined) {
var cb = function (info) {
return callback(info, that);
}
}
if(!(this.listeners.hasOwnProperty(key)))
this.listeners[key] = []
this.listeners[key].push([data, cb, false])
},
"on_once" : function(data, callback, key) {
key = key || "general";
if(!(this.listeners.hasOwnProperty(key)))
this.listeners[key] = []
this.listeners[key].push([data, callback, true])
},
"raw" : function(data) {
var that = this;
this.socket.write( data + "\n", "ascii", function() {
that.log('SENT -'+ data)
});
},
"log" : function(msg) {
console.log(msg)
}
}