This repository has been archived by the owner on May 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
110 lines (97 loc) · 2.2 KB
/
bot.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
var irc = require('irc');
var https = require('https');
var Bitly = require('bitly');
var bitly = new Bitly('', '');
var lpid = 0;
var ltid = 0;
client = new irc.Client( 'localhost', 'Forum', {
userName: 'Forum',
realName: 'Forum',
debug: false,
showErrors: false,
autoRejoin: true,
autoConnect: true,
port: 6667,
channels: [
'#relaychan',
],
});
function doProcess( data ) {
try {
data = JSON.parse( data );
// Threads
for( var x=0; x < data['t'].length; x++ ) {
(function(x) {
if( ltid > 0 ) {
bitly.shorten( data['t'][x]['url'], function(err, response) {
if( !err && response.data.url ) {
data['t'][x]['url'] = response.data.url;
}
try {
client.say(
'#relaychan',
'New thread by ' + data['t'][x]['username'] +
' (' + data['t'][x]['title'] + ')' +
' - ' + data['t'][x]['url']
);
} catch( e ) {}
});
}
if( parseInt( data['t'][x]['id'] ) > ltid ) {
ltid = parseInt( data['t'][x]['id'] );
}
})(x);
}
// Posts
for( var xx=0; xx < data['p'].length; xx++ ) {
(function(xx) {
if( lpid > 0 ) {
bitly.shorten( data['p'][xx]['url'], function(err, response) {
if( !err && response.data.url ) {
data['p'][xx]['url'] = response.data.url;
}
try {
client.say(
'#relaychan',
'New post by ' + data['p'][xx]['username'] +
' in ' + data['p'][xx]['title'] +
' - ' + data['p'][xx]['url']
);
} catch( e ) {}
});
}
if( parseInt( data['p'][xx]['id'] ) > lpid ) {
lpid = parseInt( data['p'][xx]['id'] );
}
})(xx);
}
} catch( e ) {
// console.error( e );
}
}
function doRun() {
args = 'key=something';
if( lpid > 0 ) {
args += '&lastpid=' + lpid;
}
if( ltid > 0 ) {
args += '&lasttid=' + ltid;
}
https.get({
host: 'www.forum.com',
path: '/forumbot.php?' + args,
}, function( response ) {
response.on('data', function( data ) {
doProcess( data );
});
}).on('error', function(e) {
console.error(e);
});
setTimeout( doRun, 5000 );
}
client.addListener('motd', function(message) {
doRun();
});
client.addListener('error', function(message) {
console.error('ERROR: %s: %s', message.command, message.args.join(' '));
});