-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (98 loc) · 3.81 KB
/
index.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
var irc = require('irc')
var request = require('request')
var xtend = require('xtend')
var gitterClient = require('./gitter.js')
function escapeName (name) {
var t = '`'
while (name.indexOf(t) >= 0) t += '`'
return t + ' ' + name + ' ' + t
}
module.exports = function (opts) {
var gitter = gitterClient(opts.gitterApiKey)
var headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + opts.gitterApiKey
}
var ircOpts = xtend({
channels: [opts.ircChannel],
autoConnect: false,
retryCount: 20
}, opts.ircOpts)
var ircClient = new irc.Client(
opts.ircServer || 'irc.freenode.net',
opts.ircNick,
ircOpts
)
function log (message) {
console.error(message)
if (opts.ircAdmin) ircClient.say(opts.ircAdmin, message)
}
ircClient.on('error', function (message) {
console.error('IRC Error:', message)
})
console.log('Connecting to IRC..')
ircClient.connect(function () {
log('Connected to IRC, joined ' + opts.ircChannel)
request.post({ url: 'https://api.gitter.im/v1/rooms', headers: headers, json: {uri: opts.gitterRoom} }, function (err, req, json) {
if (err) return log(err)
if (json.error) {
return log('Error while communicating with the gitter API: ' + json.error);
}
var gitterRoomId = json.id
var postGitterMessageUrl = 'https://api.gitter.im/v1/rooms/' + gitterRoomId + '/chatMessages'
request({url: 'https://api.gitter.im/v1/user', headers: headers, json: true}, function (err, res, json) {
if (err) return log(err)
var gitterName = json[0].username
var gitterUserId = json[0].id
log('Gitterbot ' + gitterName + ' on channel ' + opts.gitterRoom + '(' + gitterRoomId + ')')
gitter.subscribe('/api/v1/rooms/' + gitterRoomId + '/chatMessages', gitterMessage, {})
function gitterMessage (data) {
if (data.operation !== 'create') return
var message = data.model
if (!message.fromUser) return
var userName = message.fromUser.username
if (userName === gitterName) return
var lines = message.text.split('\n')
if (lines.length > 4) {
lines.splice(3)
lines.push('[full message: https://gitter.im/' + opts.gitterRoom + '?at=' + message.id + ']')
}
var text = lines.map(function (line) {
return '(' + userName + ') ' + line
}).join('\n')
// mark message as read by bot
request.post({
url: 'https://api.gitter.im/v1/user/' + gitterUserId + '/rooms/' + gitterRoomId + '/unreadItems',
headers: headers,
json: {chat: [ message.id ]}
})
console.log('gitter:', text)
ircClient.say(opts.ircChannel, text)
}
ircClient.on('message' + opts.ircChannel, function (from, message) {
if (from === ircClient.nick) return
var text = escapeName(from) + ' ' + message
console.log('irc:', text)
request.post({url: postGitterMessageUrl, headers: headers, json: {text: text}})
})
ircClient.on('action', function (from, to, message) {
if (to !== opts.ircChannel || from === ircClient.nick) return
var text = '— ' + escapeName(from) + ' ' + message
request.post({url: postGitterMessageUrl, headers: headers, json: {text: text}})
})
ircClient.on('pm', function (from, message) {
if (from !== opts.ircAdmin) return ircClient.say('Your are not my master.')
var commands = [
'kill'
]
if (message === commands[0]) {
ircClient.say(from, 'Shutting down systems...')
process.exit()
} else {
ircClient.say(from, 'Hi! I only understand: ' + commands.join(', '))
}
})
})
})
})
}