hipchat_smart 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0f8fa0db1c04e003abf520a096e7f7ed39b932f4885ac9b6f3cbce0254a9f8e
4
- data.tar.gz: 1cb3ec7012e6131d7e73fc50ea27a212fd47f7e9bf68cab74f92b1ee67ffef59
3
+ metadata.gz: c3e585b909e8904d242c669afb747002c50d72a1101ba5277fba3f486c61f4f6
4
+ data.tar.gz: d57b2218f543ff1aa916b57f91bba57c82a37bfe12521f9da1e0d45102e28aa2
5
5
  SHA512:
6
- metadata.gz: 5e3fdeb06e15c5ec46278096f386646316390f70c0909991ceefbe5fd670f388fccd1a40275c6be773228b8267f3ce2c52a3a3f7c1e49f628732d8e9e70ec90b
7
- data.tar.gz: 1b1f535401b836a64babfcdefed3c4899ede8cc5a62cfdbd66050aabc0b7103a093b74cbeaf15f2594a7d3b240d004c79a8b44e2c10cd7344df724712b8ed4c7
6
+ metadata.gz: edfceedc17b581e3be9b6a219ad1927faa073d6a94d11e1c5600635a6980f087140a47fef4d773306c76dc33ebb5388ddb63db7bbec5a503658669cee398dcc7
7
+ data.tar.gz: cd2f376978be01e117f610f04ded45075fa1c59be60ce51400e89033be6b4685f0fd0e423e64bd9d743ceccc3049ddad788d7adf66f46dcaa2b36bc71b829073
data/README.md CHANGED
@@ -101,6 +101,8 @@ def rules(from, command, processed)
101
101
  end
102
102
 
103
103
  ```
104
+ ### How to access the bot
105
+ You can access the bot directly on the MASTER ROOM, on a secondary room where the bot is running and directly by opening a private chat with the bot, in this case the conversation will be just between you and the bot.
104
106
 
105
107
  ### Available commands even when the bot is not listening to you
106
108
  Some of the commands are available always even when the bot is not listening to you but it is running
data/lib/hipchat_smart.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  require 'xmpp4r'
2
2
  require 'xmpp4r/muc/helper/simplemucclient'
3
3
  require 'xmpp4r/muc/helper/mucbrowser'
4
+ require 'xmpp4r/roster'
5
+ require 'hipchat'
4
6
  require 'open-uri'
5
7
  require 'cgi'
6
8
  require 'json'
7
- require 'hipchat'
8
9
  require 'logger'
10
+ require 'fileutils'
11
+ require 'open3'
9
12
 
10
13
  if ARGV.size==0
11
14
  ROOM = MASTER_ROOM
@@ -13,9 +16,8 @@ if ARGV.size==0
13
16
  ADMIN_USERS = MASTER_USERS
14
17
  RULES_FILE = "#{$0.gsub('.rb', '_rules.rb')}" unless defined?(RULES_FILE)
15
18
  unless File.exist?(RULES_FILE)
16
- require 'fileutils'
17
- default_rules=(__FILE__).gsub(".rb", "_rules.rb")
18
- FileUtils.copy_file(default_rules, RULES_FILE)
19
+ default_rules=(__FILE__).gsub(".rb", "_rules.rb")
20
+ FileUtils.copy_file(default_rules, RULES_FILE)
19
21
  end
20
22
  STATUS_INIT = :on
21
23
  else
@@ -30,7 +32,7 @@ SHORTCUTS_FILE = "hipchat_smart_shortcuts_#{ROOM}.rb".gsub(" ", "_")
30
32
 
31
33
  class Bot
32
34
 
33
- attr_accessor :config, :client, :muc, :muc_browser
35
+ attr_accessor :config, :client, :muc, :muc_browser, :roster
34
36
 
35
37
  def initialize(config)
36
38
  Dir.mkdir("./logs") unless Dir.exist?("./logs")
@@ -98,6 +100,9 @@ class Bot
98
100
  end
99
101
  config.delete(:password)
100
102
  client.send(Jabber::Presence.new.set_type(:available))
103
+
104
+ self.roster = Jabber::Roster::Helper.new(client)
105
+
101
106
  @status = STATUS_INIT
102
107
  @questions = Hash.new()
103
108
  @rooms_jid=Hash.new()
@@ -129,95 +134,119 @@ class Bot
129
134
  }
130
135
  end
131
136
 
137
+
132
138
  def listen
133
139
  @salutations = [config[:nick].split(/\s+/).first, "bot"]
134
140
 
135
141
  muc.on_message do |time, nick, text|
136
- if nick==config[:nick] or nick==(config[:nick] + " · Bot") #if message is coming from the bot
137
- begin
138
- @logger.info "#{nick}: #{text}"
139
- case text
140
- when /^Bot has been killed by/
141
- exit!
142
- when /^Changed status on (.+) to :(.+)/i
143
- room=$1
144
- status=$2
145
- @bots_created[room][:status]=status.to_sym
146
- update_bots_file()
147
- end
148
- next #don't continue analyzing
149
- rescue Exception => stack
150
- @logger.fatal stack
151
- next
152
- end
142
+ jid_user = nil
143
+ res = process_first(time, nick, text, jid_user)
144
+ next if res.to_s=="next"
145
+ end
153
146
 
154
- end
147
+ muc.join(config[:room] + '/' + config[:nick])
148
+ respond "Bot started"
155
149
 
156
- if text.match?(/^\/(shortcut|sc)\s(.+)/i)
157
- shortcut=text.scan(/\/\w+\s*(.+)\s*/i).join.downcase
158
- if @shortcuts.keys.include?(nick) and @shortcuts[nick].keys.include?(shortcut)
159
- text=@shortcuts[nick][shortcut]
160
- elsif @shortcuts.keys.include?(:all) and @shortcuts[:all].keys.include?(shortcut)
161
- text=@shortcuts[:all][shortcut]
162
- else
163
- respond "Shortcut not found"
164
- next
150
+ #accept subscriptions from everyone
151
+ roster.add_subscription_request_callback do |item, pres|
152
+ roster.accept_subscription(pres.from)
153
+ end
154
+
155
+ client.add_message_callback do |m|
156
+ unless m.body.to_s==""
157
+ jid_user=m.from.node+"@"+m.from.domain
158
+ user=roster[jid_user]
159
+ unless user.nil?
160
+ res = process_first("", user.attributes["name"], m.body, jid_user)
161
+ next if res.to_s=="next"
165
162
  end
163
+ end
164
+ end
165
+
166
+ @logger.info "Bot listening"
167
+ self
168
+ end
166
169
 
170
+ def process_first(time, nick, text, jid_user)
171
+ if nick==config[:nick] or nick==(config[:nick] + " · Bot") #if message is coming from the bot
172
+ begin
173
+ @logger.info "#{nick}: #{text}"
174
+ case text
175
+ when /^Bot has been killed by/
176
+ exit!
177
+ when /^Changed status on (.+) to :(.+)/i
178
+ room=$1
179
+ status=$2
180
+ @bots_created[room][:status]=status.to_sym
181
+ update_bots_file()
182
+ end
183
+ return :next #don't continue analyzing
184
+ rescue Exception => stack
185
+ @logger.fatal stack
186
+ return :next
167
187
  end
168
188
 
169
- if @questions.keys.include?(nick)
170
- command=@questions[nick]
171
- @questions[nick]=text
189
+ end
190
+
191
+ if text.match?(/^\/(shortcut|sc)\s(.+)/i)
192
+ shortcut=text.scan(/\/\w+\s*(.+)\s*/i).join.downcase
193
+ if @shortcuts.keys.include?(nick) and @shortcuts[nick].keys.include?(shortcut)
194
+ text=@shortcuts[nick][shortcut]
195
+ elsif @shortcuts.keys.include?(:all) and @shortcuts[:all].keys.include?(shortcut)
196
+ text=@shortcuts[:all][shortcut]
172
197
  else
173
- command=text
198
+ respond "Shortcut not found", jid_user
199
+ return :next
174
200
  end
175
201
 
176
- begin
177
- t = Thread.new do
178
- begin
179
- processed = process(nick, command)
180
- @logger.info "command: #{nick}> #{command}" if processed
181
- if @status==:on and
182
- ((@questions.keys.include?(nick) or
183
- @listening.include?(nick) or
184
- command.match?(/^@?#{@salutations.join("|")}:*\s+(.+)$/i) or
185
- command.match?(/^!(.+)$/)))
186
- @logger.info "command: #{nick}> #{command}" unless processed
187
- begin
188
- eval(File.new(RULES_FILE).read) if File.exist?(RULES_FILE)
189
- rescue Exception => stack
190
- @logger.fatal "ERROR ON RULES FILE: #{RULES_FILE}"
191
- @logger.fatal stack
192
- end
193
- if defined?(rules)
194
- command[0]="" if command[0]=="!"
195
- command.gsub!(/^@\w+:*\s*/, "")
196
- rules(nick, command, processed)
197
- else
198
- @logger.warn "It seems like rules method is not defined"
199
- end
202
+ end
203
+
204
+ if @questions.keys.include?(nick)
205
+ command=@questions[nick]
206
+ @questions[nick]=text
207
+ else
208
+ command=text
209
+ end
210
+
211
+ begin
212
+ t = Thread.new do
213
+ begin
214
+ processed = process(nick, command, jid_user)
215
+ @logger.info "command: #{nick}> #{command}" if processed
216
+ if @status==:on and
217
+ ((@questions.keys.include?(nick) or
218
+ @listening.include?(nick) or
219
+ command.match?(/^@?#{@salutations.join("|")}:*\s+(.+)$/i) or
220
+ command.match?(/^!(.+)$/)))
221
+ @logger.info "command: #{nick}> #{command}" unless processed
222
+ begin
223
+ eval(File.new(RULES_FILE).read) if File.exist?(RULES_FILE)
224
+ rescue Exception => stack
225
+ @logger.fatal "ERROR ON RULES FILE: #{RULES_FILE}"
226
+ @logger.fatal stack
227
+ end
228
+ if defined?(rules)
229
+ command[0]="" if command[0]=="!"
230
+ command.gsub!(/^@\w+:*\s*/, "")
231
+ rules(nick, command, processed, jid_user)
232
+ else
233
+ @logger.warn "It seems like rules method is not defined"
200
234
  end
201
- rescue Exception => stack
202
- @logger.fatal stack
203
235
  end
204
-
236
+ rescue Exception => stack
237
+ @logger.fatal stack
205
238
  end
206
239
 
207
- rescue => e
208
- @logger.error "exception: #{e.inspect}"
209
240
  end
210
- end
211
241
 
212
- muc.join(config[:room] + '/' + config[:nick])
213
- respond "Bot started"
214
- @logger.info "Bot listening"
215
- self
242
+ rescue => e
243
+ @logger.error "exception: #{e.inspect}"
244
+ end
216
245
  end
217
246
 
218
247
  #help: Commands you can use:
219
248
  #help:
220
- def process(from, command)
249
+ def process(from, command, jid_user)
221
250
  firstname = from.split(/ /).first
222
251
  processed=true
223
252
 
@@ -231,7 +260,7 @@ class Bot
231
260
  when /^(Hello|Hallo|Hi|Hola|What's\sup|Hey|Zdravo|Molim|Hæ)\s(#{@salutations.join("|")})\s*$/i
232
261
  if @status==:on
233
262
  greetings=['Hello', 'Hallo', 'Hi', 'Hola', "What's up", "Hey", "Zdravo", "Molim", "Hæ"].sample
234
- respond "#{greetings} #{firstname}"
263
+ respond "#{greetings} #{firstname}", jid_user
235
264
  @listening<<from unless @listening.include?(from)
236
265
  end
237
266
 
@@ -243,7 +272,7 @@ class Bot
243
272
  when /^(Bye|Bæ|Good\sBye|Adiós|Ciao|Bless|Bless\sBless|Zbogom|Adeu)\s(#{@salutations.join("|")})\s*$/i
244
273
  if @status==:on
245
274
  bye=['Bye', 'Bæ', 'Good Bye', 'Adiós', "Ciao", "Bless", "Bless bless", "Zbogom", "Adeu"].sample
246
- respond "#{bye} #{firstname}"
275
+ respond "#{bye} #{firstname}", jid_user
247
276
  @listening.delete(from)
248
277
  end
249
278
 
@@ -257,15 +286,15 @@ class Bot
257
286
  if ON_MASTER_ROOM
258
287
  if ADMIN_USERS.include?(from) #admin user
259
288
  unless @questions.keys.include?(from)
260
- ask("are you sure?", command, from)
289
+ ask("are you sure?", command, from, jid_user)
261
290
  else
262
291
  case @questions[from]
263
292
  when /yes/i, /yep/i, /sure/i
264
- respond "Game over!"
265
- respond "Ciao #{firstname}!"
293
+ respond "Game over!", jid_user
294
+ respond "Ciao #{firstname}!", jid_user
266
295
  @bots_created.each {|key, value|
267
296
  value[:thread]=""
268
- send_msg(key, "Bot has been killed by #{from}")
297
+ send_msg_room(key, "Bot has been killed by #{from}")
269
298
  sleep 0.5
270
299
  }
271
300
  update_bots_file()
@@ -273,18 +302,18 @@ class Bot
273
302
  exit!
274
303
  when /no/i, /nope/i, /cancel/i
275
304
  @questions.delete(from)
276
- respond "Thanks, I'm happy to be alive"
305
+ respond "Thanks, I'm happy to be alive", jid_user
277
306
  else
278
- respond "I don't understand"
279
- ask("are you sure do you want me to close? (yes or no)", "quit bot", from)
307
+ respond "I don't understand", jid_user
308
+ ask("are you sure do you want me to close? (yes or no)", "quit bot", from, jid_user)
280
309
  end
281
310
  end
282
311
  else
283
- respond "Only admin users can kill me"
312
+ respond "Only admin users can kill me", jid_user
284
313
  end
285
314
 
286
315
  else
287
- respond "To do this you need to be an admin user in the master room"
316
+ respond "To do this you need to be an admin user in the master room", jid_user
288
317
  end
289
318
 
290
319
  #help: start bot
@@ -294,14 +323,14 @@ class Bot
294
323
  #help:
295
324
  when /^start\s(this\s)?bot$/i
296
325
  if ADMIN_USERS.include?(from) #admin user
297
- respond "This bot is running and listening from now on. You can pause again: pause this bot"
326
+ respond "This bot is running and listening from now on. You can pause again: pause this bot", jid_user
298
327
  @status=:on
299
328
  unless ON_MASTER_ROOM
300
329
  get_rooms_name_and_jid() unless @rooms_name.keys.include?(MASTER_ROOM) and @rooms_name.keys.include?(ROOM)
301
- send_msg @rooms_name[MASTER_ROOM], "Changed status on #{@rooms_name[ROOM]} to :on"
330
+ send_msg_room @rooms_name[MASTER_ROOM], "Changed status on #{@rooms_name[ROOM]} to :on"
302
331
  end
303
332
  else
304
- respond "Only admin users can change my status"
333
+ respond "Only admin users can change my status", jid_user
305
334
  end
306
335
 
307
336
 
@@ -312,15 +341,15 @@ class Bot
312
341
  #help:
313
342
  when /^pause\s(this\s)?bot$/i
314
343
  if ADMIN_USERS.include?(from) #admin user
315
- respond "This bot is paused from now on. You can start it again: start this bot"
316
- respond "zZzzzzZzzzzZZZZZZzzzzzzzz"
344
+ respond "This bot is paused from now on. You can start it again: start this bot", jid_user
345
+ respond "zZzzzzZzzzzZZZZZZzzzzzzzz", jid_user
317
346
  @status=:paused
318
347
  unless ON_MASTER_ROOM
319
348
  get_rooms_name_and_jid() unless @rooms_name.keys.include?(MASTER_ROOM) and @rooms_name.keys.include?(ROOM)
320
- send_msg @rooms_name[MASTER_ROOM], "Changed status on #{@rooms_name[ROOM]} to :paused"
349
+ send_msg_room @rooms_name[MASTER_ROOM], "Changed status on #{@rooms_name[ROOM]} to :paused"
321
350
  end
322
351
  else
323
- respond "Only admin users can put me on pause"
352
+ respond "Only admin users can put me on pause", jid_user
324
353
  end
325
354
 
326
355
 
@@ -329,12 +358,12 @@ class Bot
329
358
  #help: If on master room and admin user also it will display info about bots created
330
359
  #help:
331
360
  when /^bot\sstatus/i
332
- respond "Status: #{@status}. Rules file: #{File.basename RULES_FILE} "
361
+ respond "Status: #{@status}. Rules file: #{File.basename RULES_FILE} ", jid_user
333
362
  if @status==:on
334
- respond "I'm listening to [#{@listening.join(", ")}]"
363
+ respond "I'm listening to [#{@listening.join(", ")}]", jid_user
335
364
  if ON_MASTER_ROOM and ADMIN_USERS.include?(from)
336
365
  @bots_created.each {|key, value|
337
- respond "#{key}: #{value}"
366
+ respond "#{key}: #{value}", jid_user
338
367
  }
339
368
  end
340
369
  end
@@ -347,7 +376,7 @@ class Bot
347
376
  if ON_MASTER_ROOM
348
377
  room=$1
349
378
  if @bots_created.keys.include?(room)
350
- respond "There is already a bot in this room: #{room}, kill it before"
379
+ respond "There is already a bot in this room: #{room}, kill it before", jid_user
351
380
  else
352
381
  rooms=Hash.new()
353
382
  muc_browser.muc_rooms(@xmpp_namespace).each {|jid, name|
@@ -355,6 +384,7 @@ class Bot
355
384
  }
356
385
  if rooms.keys.include?(room)
357
386
  jid=rooms[room]
387
+ @rooms_jid[room]=jid
358
388
  if jid!=config[:room]
359
389
  jid=jid.to_s.gsub(/@.+/, '')
360
390
  begin
@@ -366,7 +396,6 @@ class Bot
366
396
  Dir.mkdir("rules/#{jid}") unless Dir.exist?("rules/#{jid}")
367
397
  rules_file="./rules/#{jid}/" + rules_file
368
398
  end
369
- require 'fileutils'
370
399
  default_rules=(__FILE__).gsub(".rb", "_rules.rb")
371
400
  File.delete(rules_file) if File.exist?(rules_file)
372
401
  FileUtils.copy_file(default_rules, rules_file) unless File.exist?(rules_file)
@@ -386,24 +415,24 @@ class Bot
386
415
  admins: admin_users.join(","),
387
416
  thread: t
388
417
  }
389
- respond "The bot has been created on room: #{room}. Rules file: #{File.basename rules_file}"
418
+ respond "The bot has been created on room: #{room}. Rules file: #{File.basename rules_file}", jid_user
390
419
  update_bots_file()
391
420
  rescue Exception => stack
392
421
  @logger.fatal stack
393
422
  message="Problem creating the bot on room #{room}. Error: <#{stack}>."
394
423
  @logger.error message
395
- respond message
424
+ respond message, jid_user
396
425
  end
397
426
  else
398
- respond "There is already a bot in this room: #{room}, and it is the Master Room!"
427
+ respond "There is already a bot in this room: #{room}, and it is the Master Room!", jid_user
399
428
  end
400
429
 
401
430
  else
402
- respond "There is no room with that name: #{room}, please be sure is written exactly the same"
431
+ respond "There is no room with that name: #{room}, please be sure is written exactly the same", jid_user
403
432
  end
404
433
  end
405
434
  else
406
- respond "Sorry I cannot create bots from this room, please visit the master room"
435
+ respond "Sorry I cannot create bots from this room, please visit the master room", jid_user
407
436
  end
408
437
 
409
438
  #help: kill bot on ROOM_NAME
@@ -420,16 +449,16 @@ class Bot
420
449
  end
421
450
  @bots_created.delete(room)
422
451
  update_bots_file()
423
- respond "Bot on room: #{room}, has been killed and deleted."
424
- send_msg(room, "Bot has been killed by #{from}")
452
+ respond "Bot on room: #{room}, has been killed and deleted.", jid_user
453
+ send_msg_room(room, "Bot has been killed by #{from}")
425
454
  else
426
- respond "You need to be the creator or an admin of that room"
455
+ respond "You need to be the creator or an admin of that room", jid_user
427
456
  end
428
457
  else
429
- respond "There is no bot in this room: #{room}"
458
+ respond "There is no bot in this room: #{room}", jid_user
430
459
  end
431
460
  else
432
- respond "Sorry I cannot kill bots from this room, please visit the master room"
461
+ respond "Sorry I cannot kill bots from this room, please visit the master room", jid_user
433
462
  end
434
463
 
435
464
  #help: bot help
@@ -439,8 +468,8 @@ class Bot
439
468
  when /^bot help/i, /^bot,? what can I do/i
440
469
  help_message = IO.readlines(__FILE__).join
441
470
  help_message_rules = IO.readlines(RULES_FILE).join
442
- respond "/quote " + help_message.scan(/#\s*help\s*:(.*)/).join("\n")
443
- respond "/quote " + help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n")
471
+ respond "/quote " + help_message.scan(/#\s*help\s*:(.*)/).join("\n"), jid_user
472
+ respond "/quote " + help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), jid_user
444
473
 
445
474
  else
446
475
  processed = false
@@ -482,31 +511,31 @@ class Bot
482
511
  @shortcuts[from]=Hash.new() unless @shortcuts.keys.include?(from)
483
512
 
484
513
  if !ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut_name) and !@shortcuts[from].include?(shortcut_name)
485
- respond "Only the creator of the shortcut or an admin user can modify it"
514
+ respond "Only the creator of the shortcut or an admin user can modify it", jid_user
486
515
  elsif !@shortcuts[from].include?(shortcut_name)
487
516
  #new shortcut
488
517
  @shortcuts[from][shortcut_name]=command_to_run
489
518
  @shortcuts[:all][shortcut_name]=command_to_run if for_all.to_s!=""
490
519
  update_shortcuts_file()
491
- respond "shortcut added"
520
+ respond "shortcut added", jid_user
492
521
  else
493
522
 
494
523
  #are you sure? to avoid overwriting existing
495
524
  unless @questions.keys.include?(from)
496
- ask("The shortcut already exists, are you sure you want to overwrite it?", command, from)
525
+ ask("The shortcut already exists, are you sure you want to overwrite it?", command, from, jid_user)
497
526
  else
498
527
  case @questions[from]
499
528
  when /^(yes|yep)/i
500
529
  @shortcuts[from][shortcut_name]=command_to_run
501
530
  @shortcuts[:all][shortcut_name]=command_to_run if for_all.to_s!=""
502
531
  update_shortcuts_file()
503
- respond "shortcut added"
532
+ respond "shortcut added", jid_user
504
533
  @questions.delete(from)
505
534
  when /^no/i
506
- respond "ok, I won't add it"
535
+ respond "ok, I won't add it", jid_user
507
536
  @questions.delete(from)
508
537
  else
509
- respond "I don't understand, yes or no?"
538
+ respond "I don't understand, yes or no?", jid_user
510
539
  end
511
540
  end
512
541
 
@@ -520,30 +549,30 @@ class Bot
520
549
  deleted=false
521
550
 
522
551
  if !ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut) and !@shortcuts[from].include?(shortcut)
523
- respond "Only the creator of the shortcut or an admin user can delete it"
552
+ respond "Only the creator of the shortcut or an admin user can delete it", jid_user
524
553
  elsif (@shortcuts.keys.include?(from) and @shortcuts[from].keys.include?(shortcut)) or
525
554
  (ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut))
526
555
  #are you sure? to avoid deleting by mistake
527
556
  unless @questions.keys.include?(from)
528
- ask("are you sure you want to delete it?", command, from)
557
+ ask("are you sure you want to delete it?", command, from, jid_user)
529
558
  else
530
559
  case @questions[from]
531
560
  when /^(yes|yep)/i
532
- respond "shortcut deleted!"
533
- respond "#{shortcut}: #{@shortcuts[from][shortcut]}"
561
+ respond "shortcut deleted!", jid_user
562
+ respond "#{shortcut}: #{@shortcuts[from][shortcut]}", jid_user
534
563
  @shortcuts[from].delete(shortcut)
535
564
  @shortcuts[:all].delete(shortcut)
536
565
  @questions.delete(from)
537
566
  update_shortcuts_file()
538
567
  when /^no/i
539
- respond "ok, I won't delete it"
568
+ respond "ok, I won't delete it", jid_user
540
569
  @questions.delete(from)
541
570
  else
542
- respond "I don't understand, yes or no?"
571
+ respond "I don't understand, yes or no?", jid_user
543
572
  end
544
573
  end
545
574
  else
546
- respond "shortcut not found"
575
+ respond "shortcut not found", jid_user
547
576
  end
548
577
 
549
578
  #help: see shortcuts
@@ -556,7 +585,7 @@ class Bot
556
585
  @shortcuts[:all].each {|name, value|
557
586
  msg+="#{name}: #{value}\n"
558
587
  }
559
- respond msg
588
+ respond msg, jid_user
560
589
  end
561
590
 
562
591
  if @shortcuts.keys.include?(from) and @shortcuts[from].keys.size>0
@@ -567,10 +596,10 @@ class Bot
567
596
  new_hash.each {|name, value|
568
597
  msg+="#{name}: #{value}\n"
569
598
  }
570
- respond msg
599
+ respond msg, jid_user
571
600
  end
572
601
  end
573
- respond "No shortcuts found" if msg==""
602
+ respond "No shortcuts found", jid_user if msg==""
574
603
 
575
604
  #help: jid room ROOM_NAME
576
605
  #help: shows the jid of a room name
@@ -579,9 +608,9 @@ class Bot
579
608
  room_name=$1
580
609
  get_rooms_name_and_jid()
581
610
  if @rooms_jid.keys.include?(room_name)
582
- respond "the jid of #{room_name} is #{@rooms_jid[room_name]}"
611
+ respond "the jid of #{room_name} is #{@rooms_jid[room_name]}", jid_user
583
612
  else
584
- respond "room: #{room_name} not found"
613
+ respond "room: #{room_name} not found", jid_user
585
614
  end
586
615
 
587
616
  # help: ruby RUBY_CODE
@@ -598,22 +627,21 @@ class Bot
598
627
  code.match?(/open3/i) or code.match?(/bundle/i) or code.match?(/gemfile/i) or code.include?("%x") or
599
628
  code.include?("ENV")
600
629
  begin
601
- require 'open3'
602
630
  stdout, stderr, status = Open3.capture3("ruby -e \"#{code.gsub('"', '\"')}\"")
603
631
  if stderr==""
604
632
  if stdout==""
605
- respond "Nothing returned. Remember you need to use p or puts to print"
633
+ respond "Nothing returned. Remember you need to use p or puts to print", jid_user
606
634
  else
607
- respond stdout
635
+ respond stdout, jid_user
608
636
  end
609
637
  else
610
- respond stderr
638
+ respond stderr, jid_user
611
639
  end
612
640
  rescue Exception => exc
613
- respond exc
641
+ respond exc, jid_user
614
642
  end
615
643
  else
616
- respond "Sorry I cannot run this due security issues"
644
+ respond "Sorry I cannot run this due security issues", jid_user
617
645
  end
618
646
 
619
647
  else
@@ -625,28 +653,46 @@ class Bot
625
653
  return processed
626
654
  end
627
655
 
628
- def respond(msg)
629
- muc.send Jabber::Message.new(muc.room, msg)
656
+ def respond(msg,jid_user=nil)
657
+ if jid_user.nil?
658
+ muc.send Jabber::Message.new(muc.room, msg)
659
+ else #private message
660
+ send_msg_user(jid_user, msg)
661
+ end
630
662
  end
631
663
 
632
664
  #context: previous message
633
665
  #to: user that should answer
634
- def ask(question, context, to)
635
- muc.send Jabber::Message.new(muc.room, "#{to}: #{question}")
666
+ def ask(question, context, to, jid_user=nil)
667
+ if jid_user.nil?
668
+ muc.send Jabber::Message.new(muc.room, "#{to}: #{question}")
669
+ else #private message
670
+ send_msg_user(jid_user, "#{to}: #{question}")
671
+ end
636
672
  @questions[to]=context
637
673
  end
638
674
 
675
+
639
676
  # Uses the hipchat gem (REST)
640
677
  # to: (String) Room name
641
678
  # msg: (String) message to send
642
- def send_msg(to, msg)
679
+ def send_msg_room(to, msg)
643
680
  unless msg==""
644
681
  hc_client=HipChat::Client.new(config[:token], :server_url => config[:jid].to_s.scan(/.+@(.+)\/.+/).join)
645
682
  hc_client[to].send("Bot", msg)
646
683
  end
647
-
648
684
  end
649
685
 
686
+ #to send messages without listening for a response to users
687
+ #to: jid
688
+ def send_msg_user(to, msg)
689
+ unless msg==""
690
+ to=to+"@chat."+@xmpp_namespace.scan(/\w+\.(.+)/).join unless to.include?("@")
691
+ message = Jabber::Message::new(to, msg)
692
+ message.type=:chat
693
+ client.send message
694
+ end
695
+ end
650
696
 
651
697
  def always
652
698
  loop {sleep 1}
@@ -5,13 +5,13 @@ else
5
5
  @testing = true
6
6
  @questions = Hash.new()
7
7
 
8
- def respond(message)
8
+ def respond(message, jid_user)
9
9
  puts message
10
10
  end
11
11
 
12
12
  #context: previous message
13
13
  #to: user that should answer
14
- def ask(question, context, to)
14
+ def ask(question, context, to, jid_user)
15
15
  puts "Bot: #{question}"
16
16
  @questions[to]=context
17
17
  end
@@ -28,7 +28,7 @@ end
28
28
  # help: @FIRST_NAME_BOT THE_COMMAND
29
29
  # help: FIRST_NAME_BOT THE_COMMAND
30
30
  # help:
31
- def rules(from, command, processed)
31
+ def rules(from, command, processed, jid_user)
32
32
  if @testing
33
33
  puts "#{from}: #{command}"
34
34
  if @questions.keys.include?(from)
@@ -44,32 +44,32 @@ def rules(from, command, processed)
44
44
  # help: repeats SOMETHING
45
45
  # help:
46
46
  when /echo\s(.+)/i
47
- respond $1
47
+ respond $1, jid_user
48
48
 
49
49
  # help: go to sleep
50
50
  # help: it will sleep the bot for 10 seconds
51
51
  # help:
52
52
  when /go\sto\ssleep/i
53
53
  unless @questions.keys.include?(from)
54
- ask("do you want me to take a siesta?", command, from)
54
+ ask("do you want me to take a siesta?", command, from, jid_user)
55
55
  else
56
56
  case @questions[from]
57
57
  when /yes/i, /yep/i, /sure/i
58
- respond "zZzzzzzZZZZZZzzzzzzz!"
59
- respond "I'll be sleeping for 10 secs... just for you"
58
+ respond "zZzzzzzZZZZZZzzzzzzz!", jid_user
59
+ respond "I'll be sleeping for 10 secs... just for you", jid_user
60
60
  sleep 10
61
61
  when /no/i, /nope/i, /cancel/i
62
62
  @questions.delete(from)
63
- respond "Thanks, I'm happy to be awake"
63
+ respond "Thanks, I'm happy to be awake", jid_user
64
64
  else
65
- respond "I don't understand"
66
- ask("are you sure do you want me to sleep? (yes or no)", "go to sleep", from)
65
+ respond "I don't understand", jid_user
66
+ ask("are you sure do you want me to sleep? (yes or no)", "go to sleep", from, jid_user)
67
67
  end
68
68
  end
69
69
  else
70
70
  unless processed
71
71
  resp = %w{ what huh sorry }.sample
72
- respond "#{firstname}: #{resp}?"
72
+ respond "#{firstname}: #{resp}?", jid_user
73
73
  end
74
74
  end
75
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hipchat_smart
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-15 00:00:00.000000000 Z
11
+ date: 2018-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xmpp4r