Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | 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
 
30
var server = 'hitchcock.freenode.net';
31
var nickname = 'fmibot';
32
var nicknamePassword = fs.readFileSync('nickserv.txt');
33
 
34
var announceChannel = '#freemyipod';
35
var socketPath = '/tmp/fmibot.sock';
36
 
37
var config = {
38
    autoConnect: false,
39
    channels: [
40
        announceChannel
41
    ],
42
    debug: true,
43
    password: nickname + ' ' + nicknamePassword,
44
    port: 6697,
45
    realName: 'freemyipod IRC bot',
46
    secure: true,
47
    showErrors: true,
48
    userName: 'ircbot'
49
};
50
 
51
var ircbot = new irc.Client(server, nickname, config);
52
 
53
// error handler
54
ircbot.addListener('error', function(message) {
55
    console.error('IRCBOT', message);
56
});
57
 
58
// whois
59
ircbot.addListener('whois', function(info) {
60
    console.info(info.nick, 'is', info.user + '@' + info.host, '*', info.realname);
61
    console.info(info.nick, 'on', info.channels.join(' '));
62
    console.info(info.nick, 'using', info.server, info.serverinfo);
63
    console.info(info.nick, 'End of /WHOIS list.');
64
});
65
 
66
// help
67
ircbot.addListener('raw', function(message) {
68
    switch (message.rawCommand) {
69
        case '704':
70
        case '705':
71
        case '706':
72
            console.log(message.args[2]);
73
        break;
74
    }
75
});
76
 
77
// channel list
78
ircbot.addListener('channellist_start', function() {
79
    console.info('Listing channels...');
80
});
81
 
82
ircbot.addListener('channellist_item', function(info) {
83
    console.info(info.name, info.users, info.topic);
84
});
85
 
86
// control socket
87
var controlSocket = new control.Socket(socketPath, function() {
88
    ircbot.connect();
89
}, function(cmd) {
90
    var args = cmd.split(' ');
91
    var cmd = args.shift().toLowerCase();
92
 
93
    var argsOptional = false;
94
    var knownCmd = true;
95
    var sendCmd = true;
96
 
97
    switch (cmd) {
98
        case 'quote':
99
            cmd = 'send';
100
        case 'send':
101
            // no need to modify args
102
        break;
103
        case 'join':
104
            args = [
105
                args.slice(0, 2).join(' ')
106
            ];
107
        break;
108
        case 'part':
109
            args.splice(1, args.length - 1, args.slice(1).join(' '));
110
        break;
111
        case 'say':
112
        case 'action':
113
        case 'notice':
114
            args.splice(1, args.length - 1, args.slice(1).join(' '));
115
        break;
116
        case 'ctcp':
117
            args.splice(2, args.length - 2, args.slice(2).join(' '));
118
        break;
119
        case 'whois':
120
            args.splice(1, args.length - 1);
121
        break;
122
        case 'list':
123
            argsOptional = true;
124
        break;
125
        case 'connect':
126
        case 'activateFloodProtection':
127
            argsOptional = true;
128
 
129
            if (0 in args) {
130
                args = [
131
                    parseInt(args[0])
132
                ];
133
            }
134
        break;
135
        case 'disconnect':
136
            if (!ircbot.conn) {
137
                sendCmd = false;
138
                break;
139
            }
140
 
141
            argsOptional = true;
142
            var message = args.join(' ');
143
 
144
            if (message.length) {
145
                args = [
146
                    message
147
                ];
148
            }
149
            else {
150
                args = [];
151
            }
152
        break;
153
        default:
154
            sendCmd = false;
155
 
156
            switch (cmd) {
157
                case 'test':
158
                    ircbot.say(announceChannel, 'test');
159
                break;
160
                case 'svn':
161
                    var action = args.shift();
162
 
163
                    switch (action) {
164
                        case 'commit':
165
                            if (args.length < 3) {
166
                                break;
167
                            }
168
 
169
                            function announce(msg) {
170
                                ircbot.say(announceChannel, msg);
171
                            }
172
 
173
                            var who = args.shift();
174
                            var rev = args.shift();
175
                            var message = args.join(' ');
176
 
177
                            var announceMsg = 'New commit by ' + who + ' (' + irc.colors.wrap('bold', 'r' + rev) + '): ' + message;
178
 
179
                            http.get('http://is.gd/create.php?format=simple&url=' + encodeURIComponent('http://websvn.freemyipod.org/revision.php?repname=freemyipod&rev=' + rev), function(res) {
180
                                console.log('HTTP STATUS:' + res.statusCode);
181
 
182
                                var response = '';
183
 
184
                                res.on('data', function(chunk) {
185
                                    console.log('HTTP CHUNK:', chunk);
186
                                    response += chunk;
187
                                });
188
 
189
                                res.on('end', function() {
190
                                    console.log('HTTP RESPONSE:', response);
191
                                    announce(announceMsg + ' ' + response);
192
                                })
193
                            }).on('error', function(e) {
194
                                console.error('HTTP:' + e.message);
195
                                announce(announceMsg);
196
                            });
197
                        break;
198
                        case 'buildresult':
199
                            // TODO
200
                        break;
201
                    }
202
                break;
203
                case 'exit':
204
                    if (ircbot.conn) {
205
                        ircbot.disconnect('Exiting by control socket request', function() {
206
                            process.exit();
207
                        });
208
                    }
209
                    else {
210
                        process.exit();
211
                    }
212
                break;
213
                default:
214
                    knownCmd = false;
215
                break;
216
            }
217
        break;
218
    }
219
 
220
    if (knownCmd) {
221
        console.info('CMD: got CMD', cmd, 'ARGS', args);
222
 
223
        if (sendCmd) {
224
            if (args.length === 0 && !argsOptional) {
225
                console.warn('CMD: not enough arguments');
226
                return;
227
            }
228
 
229
            ircbot[cmd].apply(ircbot, args);
230
        }
231
    }
232
    else {
233
        console.error('CMD: unknown CMD', cmd, 'ARGS', args);
234
    }
235
});