-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathhipchat.coffee
293 lines (243 loc) · 9.72 KB
/
hipchat.coffee
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
{Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, User} = require "hubot"
HTTPS = require "https"
{inspect} = require "util"
Connector = require "./connector"
promise = require "./promises"
class HipChat extends Adapter
constructor: (robot) ->
super robot
@logger = robot.logger
reconnectTimer = null
emote: (envelope, strings...) ->
@send envelope, strings.map((str) -> "/me #{str}")...
send: (envelope, strings...) ->
for str in strings
@connector.message envelope.room, str
topic: (envelope, message) ->
{user, room} = envelope
user = envelope if not user # pre-2.4.2 style
target_jid =
# most common case - we're replying to a user in a room or 1-1
user?.reply_to or
# allows user objects to be passed in
user?.jid or
if user?.search?(/@/) >= 0
user # allows user to be a jid string
else
room # this will happen if someone uses robot.messageRoom(jid, ...)
if not target_jid
return @logger.error "ERROR: Not sure who to send to: envelope=#{inspect envelope}"
@connector.topic target_jid, message
reply: (envelope, strings...) ->
user = if envelope.user then envelope.user else envelope
@send envelope, "@#{user.mention_name} #{str}" for str in strings
waitAndReconnect: ->
if !@reconnectTimer
delay = Math.round(Math.random() * (20 - 5) + 5)
@logger.info "Waiting #{delay}s and then retrying..."
@reconnectTimer = setTimeout () =>
@logger.info "Attempting to reconnect..."
delete @reconnectTimer
@connector.connect()
, delay * 1000
run: ->
botjid = process.env.HUBOT_HIPCHAT_JID
if not botjid
throw new Error("Environment variable HUBOT_HIPCHAT_JID is required to contain your bot's user JID.")
botpw = process.env.HUBOT_HIPCHAT_PASSWORD
if not botpw
throw new Error("Environment variable HUBOT_HIPCHAT_PASSWORD is required to contain your bot's user password.")
@options =
jid: botjid
password: botpw
token: process.env.HUBOT_HIPCHAT_TOKEN or null
rooms: process.env.HUBOT_HIPCHAT_ROOMS or "All"
rooms_blacklist: process.env.HUBOT_HIPCHAT_ROOMS_BLACKLIST or ""
rooms_join_public: process.env.HUBOT_HIPCHAT_JOIN_PUBLIC_ROOMS isnt "false"
host: process.env.HUBOT_HIPCHAT_HOST or null
bosh: { url: process.env.HUBOT_HIPCHAT_BOSH_URL or null }
autojoin: process.env.HUBOT_HIPCHAT_JOIN_ROOMS_ON_INVITE isnt "false"
xmppDomain: process.env.HUBOT_HIPCHAT_XMPP_DOMAIN or null
reconnect: process.env.HUBOT_HIPCHAT_RECONNECT isnt "false"
@logger.debug "HipChat adapter options: #{JSON.stringify @options}"
# create Connector object
connector = new Connector
jid: @options.jid
password: @options.password
host: @options.host
logger: @logger
xmppDomain: @options.xmppDomain
bosh: @options.bosh
host = if @options.host then @options.host else "hipchat.com"
@logger.info "Connecting HipChat adapter..."
init = promise()
connector.onTopic (channel, from, message) =>
@logger.info "Topic change: " + message
author = getAuthor: => @robot.brain.userForName(from) or new User(from)
author.room = channel
@receive new TopicMessage(author, message, 'id')
connector.onDisconnect =>
@logger.info "Disconnected from #{host}"
if @options.reconnect
@waitAndReconnect()
connector.onError =>
@logger.error [].slice.call(arguments).map(inspect).join(", ")
if @options.reconnect
@waitAndReconnect()
firstTime = true
connector.onConnect =>
@logger.info "Connected to #{host} as @#{connector.mention_name}"
# Provide our name to Hubot
@robot.name = connector.mention_name
# Tell Hubot we're connected so it can load scripts
if firstTime
@emit "connected"
@logger.debug "Sending connected event"
saveUsers = (users) =>
# Save users to brain
for user in users
user.id = @userIdFromJid user.jid
# userForId will not merge to an existing user
if user.id of @robot.brain.data.users
oldUser = @robot.brain.data.users[user.id]
for key, value of oldUser
unless key of user
user[key] = value
delete @robot.brain.data.users[user.id]
@robot.brain.userForId user.id, user
joinRoom = (jid) =>
if jid and typeof jid is "object"
jid = "#{jid.local}@#{jid.domain}"
if jid in @options.rooms_blacklist.split(",")
@logger.info "Not joining #{jid} because it is blacklisted"
return
@logger.info "Joining #{jid}"
connector.join jid
# Fetch user info
connector.getRoster (err, users, stanza) =>
return init.reject err if err
init.resolve users
init
.done (users) =>
saveUsers(users)
# Join requested rooms
if @options.rooms is "All" or @options.rooms is "@All"
connector.getRooms (err, rooms, stanza) =>
if rooms
for room in rooms
if !@options.rooms_join_public && room.guest_url != ''
@logger.info "Not joining #{room.jid} because it is a public room"
else
joinRoom(room.jid)
else
@logger.error "Can't list rooms: #{errmsg err}"
# Join all rooms
else
for room_jid in @options.rooms.split ","
joinRoom(room_jid)
.fail (err) =>
@logger.error "Can't list users: #{errmsg err}" if err
connector.onRosterChange (users) =>
saveUsers(users)
handleMessage = (opts) =>
# buffer message events until the roster fetch completes
# to ensure user data is properly loaded
init.done =>
{getAuthor, message, room} = opts
author = getAuthor() or {}
author.room = room
@receive new TextMessage(author, message)
if firstTime
connector.onMessage (channel, from, message) =>
# reformat leading @mention name to be like "name: message" which is
# what hubot expects
mention_name = connector.mention_name
regex = new RegExp "^@#{mention_name}\\b", "i"
message = message.replace regex, "#{mention_name}: "
handleMessage
getAuthor: => @robot.brain.userForName(from) or new User(from)
message: message
room: channel
connector.onPrivateMessage (from, message) =>
# remove leading @mention name if present and format the message like
# "name: message" which is what hubot expects
mention_name = connector.mention_name
regex = new RegExp "^@?#{mention_name}\\b", "i"
message = "#{mention_name}: #{message.replace regex, ""}"
handleMessage
getAuthor: => @robot.brain.userForId(@userIdFromJid from)
message: message
room: from
changePresence = (PresenceMessage, user_jid, room_jid, currentName) =>
# buffer presence events until the roster fetch completes
# to ensure user data is properly loaded
init.done =>
user = @robot.brain.userForId(@userIdFromJid(user_jid)) or {}
if user
user.room = room_jid
# If an updated name was sent as part of a presence, update it now
user.name = currentName if currentName.length
@receive new PresenceMessage(user)
if firstTime
connector.onEnter (user_jid, room_jid, currentName) =>
changePresence EnterMessage, user_jid, room_jid, currentName
connector.onLeave (user_jid, room_jid) ->
changePresence LeaveMessage, user_jid, room_jid
connector.onInvite (room_jid, from_jid, message) =>
action = if @options.autojoin then "joining" else "ignoring"
@logger.info "Got invite to #{room_jid} from #{from_jid} - #{action}"
joinRoom(room_jid) if @options.autojoin
firstTime = false
connector.connect()
@connector = connector
userIdFromJid: (jid) ->
try
jid.match(/^\d+_(\d+)@chat\./)[1]
catch e
@logger.error "Bad user JID: #{jid}"
# Convenience HTTP Methods for posting on behalf of the token'd user
get: (path, callback) ->
@request "GET", path, null, callback
post: (path, body, callback) ->
@request "POST", path, body, callback
request: (method, path, body, callback) ->
@logger.debug "Request:", method, path, body
host = @options.host or "api.hipchat.com"
headers = "Host": host
unless @options.token
return callback "No API token provided to Hubot", null
options =
agent : false
host : host
port : 443
path : path += "?auth_token=#{@options.token}"
method : method
headers: headers
if method is "POST"
headers["Content-Type"] = "application/x-www-form-urlencoded"
options.headers["Content-Length"] = body.length
request = HTTPS.request options, (response) =>
data = ""
response.on "data", (chunk) ->
data += chunk
response.on "end", =>
if response.statusCode >= 400
@logger.error "HipChat API error: #{response.statusCode}"
try
callback null, JSON.parse(data)
catch err
callback null, data or { }
response.on "error", (err) ->
callback err, null
if method is "POST"
request.end(body, "binary")
else
request.end()
request.on "error", (err) =>
@logger.error err
@logger.error err.stack if err.stack
callback err
errmsg = (err) ->
err + (if err.stack then '\n' + err.stack else '')
exports.use = (robot) ->
new HipChat robot