This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logbot.js
172 lines (128 loc) · 3.28 KB
/
logbot.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
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
var sys = require('sys');
var irc = require('irc');
var mysql = require('mysql');
var dgram = require('dgram');
var config = require('./config').config;
//
// we'll use the same db connection object over and over, connecting
// each time we need to write something
//
var db = new mysql.Client({
host: config.db_host,
user: config.db_user,
password: config.db_pass,
database: config.db_name,
});
//
// this will be the IRC bot component
//
function logbot(){
var self = this;
this.client = new irc.Client(config.irc_server, config.irc_nick, {
password: config.irc_password,
channels: config.irc_channels,
});
this.client.on('raw', function(msg){
var raw = msg.rawCommand + ' ' + msg.args.join(' ');
//console.log(" >>>> "+raw);
});
this.client.on('join', function(chan, nick){
if (nick == config.irc_nick){
console.log('Now logging in '+chan);
self.client.say(chan, 'Now logging in '+chan);
}else{
self.log_item(chan, nick, 'join');
}
});
this.client.on('part', function(chan, nick, reason){
if (nick != config.irc_nick){
self.log_item(chan, nick, 'part', reason);
}
});
this.client.on('message', function(nick, to, text){
self.log_item(to, nick, 'text', text);
});
this.client.on('topic', function(chan, topic, nick){
self.log_item(chan, nick, 'topic', topic);
});
this.send = function(chan, msg){
self.client.say(chan, msg);
self.log_item(chan, config.irc_nick, 'text', msg);
};
// notice
// nick
// quit
// mode
// kick
// ctcp
this.log_item = function(chan, nick, event, text){
console.log("event on "+chan+" : "+nick+" / "+event+" / "+text);
var hash = {
when : Math.round(new Date().getTime() / 1000),
chan : db.escape(chan),
user : db.escape(nick),
event : db.escape(event),
text : db.escape(text),
};
self.db_insert(config.db_table, hash);
}
this.db_insert = function(table, hash){
var fields = [];
var values = [];
for (var i in hash){
fields.push(i);
values.push(hash[i]);
}
fields = '`' + fields.join('`, `') + '`';
values = values.join(", ");
self.db_query("INSERT INTO "+table+" ("+fields+") VALUES ("+values+")");
}
this.db_query = function(sql){
//console.log('query: '+sql);
db.on('error', function(){
console.log('ERROR: '+sys.inspect(arguments));
});
db.connect();
db.query(sql, function(){
//console.log('query ok');
db.end();
}, function(error){
console.log('MySQL Error: '+sys.inpect(error));
});
}
}
//
// and this will be our udp server
//
function udpserver(logbot){
var self = this;
this.logbot = logbot;
this.server = dgram.createSocket('udp4', function(msg, rinfo){
var text = msg.toString('utf8');
self.handle_datagram(text);
});
this.handle_datagram = function(msg){
console.log("got datagram: "+msg);
var re = /^(#\S+)\s(.*)$/i;
var m = re.exec(msg);
if (m != null){
var chans = m[1].split(',');
msg = m[2];
for (var i=0; i<chans.length; i++){
this.send_message(chans[i], msg);
}
}else{
this.send_message(config.default_channel, msg);
}
};
this.send_message = function(chan, msg){
console.log("\tsending to "+chan+" :: "+msg);
self.logbot.send(chan, msg);
};
this.server.bind(config.udp_port);
}
//
// finally, create them
//
var logbot = new logbot();
var udpserver = new udpserver(logbot);