Subversion Repositories freemyipod

Rev

Rev 930 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
930 user890104 1
#!/usr/bin/env node
2
/*
3
 
4
    Copyright 2014 user890104
5
 
6
 
7
    This file is part of fmibot.
8
 
9
    fmibot is free software: you can redistribute it and/or
10
    modify it under the terms of the GNU General Public License as
11
    published by the Free Software Foundation, either version 2 of the
12
    License, or (at your option) any later version.
13
 
14
    fmibot is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
    See the GNU General Public License for more details.
18
 
19
    You should have received a copy of the GNU General Public License along
20
    with fmibot.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
*/
23
 
24
var irc = require('irc');
25
var fs = require('fs');
26
var http = require('http');
27
 
28
var control = require('./control');
29
 
970 user890104 30
var server = 'irc.freenode.net';
930 user890104 31
var nickname = 'fmibot';
32
var nicknamePassword = fs.readFileSync('nickserv.txt');
33
 
34
var announceChannel = '#freemyipod';
35
var socketPath = '/tmp/fmibot.sock';
970 user890104 36
var pingTimer = null;
930 user890104 37
 
38
var config = {
39
    autoConnect: false,
40
    channels: [
41
        announceChannel
42
    ],
43
    debug: true,
44
    password: nickname + ' ' + nicknamePassword,
45
    port: 6697,
46
    realName: 'freemyipod IRC bot',
47
    secure: true,
48
    showErrors: true,
49
    userName: 'ircbot'
50
};
51
 
52
var ircbot = new irc.Client(server, nickname, config);
53
 
54
// error handler
55
ircbot.addListener('error', function(message) {
56
    console.error('IRCBOT', message);
57
});
58
 
59
// whois
60
ircbot.addListener('whois', function(info) {
61
    console.info(info.nick, 'is', info.user + '@' + info.host, '*', info.realname);
62
    console.info(info.nick, 'on', info.channels.join(' '));
63
    console.info(info.nick, 'using', info.server, info.serverinfo);
64
    console.info(info.nick, 'End of /WHOIS list.');
65
});
66
 
67
// help
68
ircbot.addListener('raw', function(message) {
69
    switch (message.rawCommand) {
70
        case '704':
71
        case '705':
72
        case '706':
73
            console.log(message.args[2]);
74
        break;
75
    }
76
});
77
 
78
// channel list
79
ircbot.addListener('channellist_start', function() {
80
    console.info('Listing channels...');
81
});
82
 
83
ircbot.addListener('channellist_item', function(info) {
84
    console.info(info.name, info.users, info.topic);
85
});
86
 
970 user890104 87
function ircConnect() {
88
	ircbot.connect(Math.pow(2, 32) - 1);
89
}
90
 
91
// ping
92
ircbot.addListener('ping', function() {
93
	if (pingTimer !== null) {
94
		clearTimeout(pingTimer);
95
	}
96
 
97
	pingTimer = setTimeout(function() {
98
		pingTimer = null;
99
		ircbot.disconnect();
100
		setTimeout(ircConnect, 10 * 1000);
101
	}, 30 * 60 * 1000);
102
});
103
 
104
// misc
105
ircbot.addListener('action', function(from, to, message) {
106
    if (to.indexOf('#') === 0 && message === 'kicks ' + nickname) {
107
        ircbot.say(to, 'ouch!');
108
    }
109
});
110
 
930 user890104 111
// control socket
970 user890104 112
var controlSocket = new control.Socket(socketPath, ircConnect, function(cmd) {
930 user890104 113
    var args = cmd.split(' ');
114
    var cmd = args.shift().toLowerCase();
115
 
116
    var argsOptional = false;
117
    var knownCmd = true;
118
    var sendCmd = true;
119
 
120
    switch (cmd) {
121
        case 'quote':
122
            cmd = 'send';
123
        case 'send':
124
            // no need to modify args
125
        break;
126
        case 'join':
127
            args = [
128
                args.slice(0, 2).join(' ')
129
            ];
130
        break;
131
        case 'part':
132
            args.splice(1, args.length - 1, args.slice(1).join(' '));
133
        break;
134
        case 'say':
135
        case 'action':
136
        case 'notice':
137
            args.splice(1, args.length - 1, args.slice(1).join(' '));
138
        break;
139
        case 'ctcp':
140
            args.splice(2, args.length - 2, args.slice(2).join(' '));
141
        break;
142
        case 'whois':
143
            args.splice(1, args.length - 1);
144
        break;
145
        case 'list':
146
            argsOptional = true;
147
        break;
148
        case 'connect':
149
        case 'activateFloodProtection':
150
            argsOptional = true;
151
 
152
            if (0 in args) {
153
                args = [
154
                    parseInt(args[0])
155
                ];
156
            }
157
        break;
158
        case 'disconnect':
159
            if (!ircbot.conn) {
160
                sendCmd = false;
161
                break;
162
            }
163
 
164
            argsOptional = true;
165
            var message = args.join(' ');
166
 
167
            if (message.length) {
168
                args = [
169
                    message
170
                ];
171
            }
172
            else {
173
                args = [];
174
            }
175
        break;
176
        default:
177
            sendCmd = false;
178
 
179
            switch (cmd) {
180
                case 'test':
181
                    ircbot.say(announceChannel, 'test');
182
                break;
183
                case 'svn':
184
                    var action = args.shift();
185
 
186
                    switch (action) {
187
                        case 'commit':
188
                            if (args.length < 3) {
189
                                break;
190
                            }
191
 
192
                            function announce(msg) {
193
                                ircbot.say(announceChannel, msg);
194
                            }
195
 
196
                            var who = args.shift();
197
                            var rev = args.shift();
198
                            var message = args.join(' ');
199
 
200
                            var announceMsg = 'New commit by ' + who + ' (' + irc.colors.wrap('bold', 'r' + rev) + '): ' + message;
201
 
202
                            http.get('http://is.gd/create.php?format=simple&url=' + encodeURIComponent('http://websvn.freemyipod.org/revision.php?repname=freemyipod&rev=' + rev), function(res) {
203
                                console.log('HTTP STATUS:' + res.statusCode);
204
 
205
                                var response = '';
206
 
207
                                res.on('data', function(chunk) {
208
                                    console.log('HTTP CHUNK:', chunk);
209
                                    response += chunk;
210
                                });
211
 
212
                                res.on('end', function() {
213
                                    console.log('HTTP RESPONSE:', response);
214
                                    announce(announceMsg + ' ' + response);
215
                                })
216
                            }).on('error', function(e) {
217
                                console.error('HTTP:' + e.message);
218
                                announce(announceMsg);
219
                            });
220
                        break;
221
                        case 'buildresult':
222
                            // TODO
223
                        break;
224
                    }
225
                break;
226
                case 'exit':
227
                    if (ircbot.conn) {
228
                        ircbot.disconnect('Exiting by control socket request', function() {
229
                            process.exit();
230
                        });
231
                    }
232
                    else {
233
                        process.exit();
234
                    }
235
                break;
236
                default:
237
                    knownCmd = false;
238
                break;
239
            }
240
        break;
241
    }
242
 
243
    if (knownCmd) {
244
        console.info('CMD: got CMD', cmd, 'ARGS', args);
245
 
246
        if (sendCmd) {
247
            if (args.length === 0 && !argsOptional) {
248
                console.warn('CMD: not enough arguments');
249
                return;
250
            }
251
 
252
            ircbot[cmd].apply(ircbot, args);
253
        }
254
    }
255
    else {
256
        console.error('CMD: unknown CMD', cmd, 'ARGS', args);
257
    }
258
});