slack-smart-bot 0.6.10 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/slack-smart-bot.rb +80 -59
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7687e436ba930b00bcd514331e99c3523f914643a507c32431f21eb609a356a
4
- data.tar.gz: 940318fcaae2f3ecf2164e0733170ec60e4e9ddc158f555fc0c417ec4b787c6c
3
+ metadata.gz: 9499651b070a6364a9daf33589636ce8bf9665b80f8c59fa0e3becbe52a82ba9
4
+ data.tar.gz: 33fe9c633e35e193dbdb0435addf17a8abc3cd5e9d09e0e79b94ccd790be744a
5
5
  SHA512:
6
- metadata.gz: 1437b5b76934fb52f2a4f3c02d0c7b10d16fc2bcc9a7ddd72795bbdb6636caeeb1b965ff89e96cfeb60bd2892bc868286d3f948ff8c89cb8c753b4bde928d8b2
7
- data.tar.gz: af8ad60ba1199b929a22dc7728e0b5d2057f9bbd186b6fb5750946976f3ace333e4312ef4065570579b0136d382b8427b5950d1064f1b0082ee544fb4a7c2da4
6
+ metadata.gz: 64645e494cdfad6b8165315fedce94c38725cd365fb2264de54e86c6a17142fd0afef99228e49ed38e94e1f4edd05ca4924d7541372f0c004ce5c6c1de0e7960
7
+ data.tar.gz: d61e30d1a6b9199285b8a9a3c5c488be6da85e1066dea6226f5a1443d3c3425f7e37eea5f80036bc4238447de876f417f6f83d7feb76888d928ef5b0e790456f
@@ -195,14 +195,14 @@ class SlackSmartBot
195
195
  ((data.text[0] == "`" and data.text[-1] == "`") or (data.text[0] == "*" and data.text[-1] == "*") or (data.text[0] == "_" and data.text[-1] == "_"))
196
196
  data.text = data.text[1..-2]
197
197
  end
198
-
199
198
  if !data.files.nil? and data.files.size == 1 and
200
- (data.text.match(/^(ruby|code)\s*$/) or (data.text.match(/^\s*$/) and data.files[0].filetype=='ruby') )
199
+ (data.text.match?(/^(ruby|code)\s*$/) or (data.text.match?(/^\s*$/) and data.files[0].filetype=='ruby') or
200
+ (data.text.match?(/^<@#{config[:nick_id]}>\s(on\s)?<#(\w+)\|(.+)>/im) and data.files[0].filetype=='ruby'))
201
201
  res=Faraday.new('https://files.slack.com', headers: { "Authorization" => "Bearer #{config[:token]}" }).get(data.files[0].url_private)
202
- data.text = "ruby #{res.body.to_s}"
202
+ data.text = "#{data.text} ruby #{res.body.to_s}"
203
203
  end
204
204
 
205
- if data.text.match(/^<@#{config[:nick_id]}>\s(on\s)?<#(\w+)\|(.+)>\s*:?\s*(.+)$/i)
205
+ if data.text.match(/^<@#{config[:nick_id]}>\s(on\s)?<#(\w+)\|(.+)>\s*:?\s*(.+)/im)
206
206
  channel_rules = $2
207
207
  channel_rules_name = $3
208
208
  command = $4
@@ -210,8 +210,10 @@ class SlackSmartBot
210
210
  ((command[0] == "`" and command[-1] == "`") or (command[0] == "*" and command[-1] == "*") or (command[0] == "_" and command[-1] == "_"))
211
211
  command = command[1..-2]
212
212
  end
213
-
213
+ p command
214
+
214
215
  command = "!" + command unless command[0] == "!"
216
+ p command
215
217
  if @channels_id[CHANNEL] == channel_rules #to be treated only on the bot of the requested channel
216
218
  dest = data.channel
217
219
 
@@ -251,6 +253,36 @@ class SlackSmartBot
251
253
 
252
254
  def process_first(user, text, dest, dchannel)
253
255
  nick = user.name
256
+ rules_file = ''
257
+
258
+ if dest[0] == "C" # on a channel
259
+ rules_file = RULES_FILE
260
+
261
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
262
+ unless @bots_created.key?(@rules_imported[user.id][dchannel])
263
+ file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
264
+ unless file_conf.to_s() == ""
265
+ @bots_created = eval(file_conf)
266
+ end
267
+ end
268
+ if @bots_created.key?(@rules_imported[user.id][dchannel])
269
+ rules_file = @bots_created[@rules_imported[user.id][dchannel]][:rules_file]
270
+ end
271
+ end
272
+ elsif dest[0] == "D" and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
273
+ unless @bots_created.key?(@rules_imported[user.id][user.id])
274
+ file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
275
+ unless file_conf.to_s() == ""
276
+ @bots_created = eval(file_conf)
277
+ end
278
+ end
279
+ if @bots_created.key?(@rules_imported[user.id][user.id])
280
+ rules_file = @bots_created[@rules_imported[user.id][user.id]][:rules_file]
281
+ end
282
+ end
283
+
284
+
285
+
254
286
  #todo: verify if on slack on anytime nick == config[:nick]
255
287
  if nick == config[:nick] #if message is coming from the bot
256
288
  begin
@@ -279,9 +311,9 @@ class SlackSmartBot
279
311
  end
280
312
 
281
313
  #only for shortcuts
282
- if text.match(/^@?(#{config[:nick]}):*\s+(.+)\s*$/i) or
283
- text.match(/^()!\s*(.+)\s*$/i) or
284
- text.match(/^()<@#{config[:nick_id]}>\s+(.+)\s*$/i)
314
+ if text.match(/^@?(#{config[:nick]}):*\s+(.+)\s*/im) or
315
+ text.match(/^()!\s*(.+)\s*/im) or
316
+ text.match(/^()<@#{config[:nick_id]}>\s+(.+)\s*/im)
285
317
  command = $2
286
318
  addexcl = true
287
319
  else
@@ -312,13 +344,12 @@ class SlackSmartBot
312
344
  begin
313
345
  t = Thread.new do
314
346
  begin
315
- done = false
316
- processed = process(user, command, dest, dchannel)
347
+ processed = process(user, command, dest, dchannel, rules_file)
317
348
  @logger.info "command: #{nick}> #{command}" if processed
318
349
  on_demand = false
319
- if command.match(/^@?(#{config[:nick]}):*\s+(.+)$/i) or
320
- command.match(/^()!(.+)$/i) or
321
- command.match(/^()<@#{config[:nick_id]}>\s+(.+)$/i)
350
+ if command.match(/^@?(#{config[:nick]}):*\s+(.+)/im) or
351
+ command.match(/^()!(.+)/im) or
352
+ command.match(/^()<@#{config[:nick_id]}>\s+(.+)/im)
322
353
  command = $2
323
354
  on_demand = true
324
355
  end
@@ -328,25 +359,18 @@ class SlackSmartBot
328
359
  dest[0] == "D" or on_demand)
329
360
  @logger.info "command: #{nick}> #{command}" unless processed
330
361
  #todo: verify this
362
+
363
+
331
364
  if dest[0] == "C" #only for channels, not for DM
332
- rules_file = RULES_FILE
333
365
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
334
- unless @bots_created.key?(@rules_imported[user.id][dchannel])
335
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
336
- unless file_conf.to_s() == ""
337
- @bots_created = eval(file_conf)
338
- end
339
- end
340
366
  if @bots_created.key?(@rules_imported[user.id][dchannel])
341
- if @bots_created[@rules_imported[user.id][dchannel]][:status] == :on
342
- rules_file = @bots_created[@rules_imported[user.id][dchannel]][:rules_file]
343
- else
367
+ if @bots_created[@rules_imported[user.id][dchannel]][:status] != :on
344
368
  respond "The bot on that channel is not :on", dest
345
- done = true
369
+ rules_file = ''
346
370
  end
347
371
  end
348
372
  end
349
- unless done
373
+ unless rules_file.empty?
350
374
  begin
351
375
  eval(File.new(rules_file).read) if File.exist?(rules_file)
352
376
  rescue Exception => stack
@@ -362,16 +386,8 @@ class SlackSmartBot
362
386
  end
363
387
  end
364
388
  elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id)
365
- unless @bots_created.key?(@rules_imported[user.id][user.id])
366
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
367
- unless file_conf.to_s() == ""
368
- @bots_created = eval(file_conf)
369
- end
370
- end
371
-
372
389
  if @bots_created.key?(@rules_imported[user.id][user.id])
373
390
  if @bots_created[@rules_imported[user.id][user.id]][:status] == :on
374
- rules_file = @bots_created[@rules_imported[user.id][user.id]][:rules_file]
375
391
  begin
376
392
  eval(File.new(rules_file).read) if File.exist?(rules_file)
377
393
  rescue Exception => stack
@@ -380,11 +396,11 @@ class SlackSmartBot
380
396
  end
381
397
  else
382
398
  respond "The bot on <##{@rules_imported[user.id][user.id]}|#{@bots_created[@rules_imported[user.id][user.id]][:channel_name]}> is not :on", dest
383
- done = true
399
+ rules_file = ''
384
400
  end
385
401
  end
386
402
 
387
- unless done
403
+ unless rules_file.empty?
388
404
  if defined?(rules)
389
405
  command[0] = "" if command[0] == "!"
390
406
  command.gsub!(/^@\w+:*\s*/, "")
@@ -414,7 +430,7 @@ class SlackSmartBot
414
430
  #help:
415
431
  #help: *General commands:*
416
432
  #help:
417
- def process(user, command, dest, dchannel)
433
+ def process(user, command, dest, dchannel, rules_file)
418
434
  from = user.name
419
435
  firstname = from.split(/ /).first
420
436
  processed = true
@@ -791,45 +807,40 @@ class SlackSmartBot
791
807
  respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), dest
792
808
  end
793
809
  if dest[0] == "C" # on a channel
794
- rules_file = RULES_FILE
795
-
796
810
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
797
- unless @bots_created.key?(@rules_imported[user.id][dchannel])
798
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
799
- unless file_conf.to_s() == ""
800
- @bots_created = eval(file_conf)
801
- end
802
- end
803
811
  if @bots_created.key?(@rules_imported[user.id][dchannel])
804
- rules_file = @bots_created[@rules_imported[user.id][dchannel]][:rules_file]
805
812
  respond "*You are using rules from another channel: <##{@rules_imported[user.id][dchannel]}>. These are the specific commands for that channel:*", dest
806
813
  end
807
814
  end
808
815
  help_message_rules = IO.readlines(rules_file).join
809
816
  respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
810
817
  elsif dest[0] == "D" and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
811
- unless @bots_created.key?(@rules_imported[user.id][user.id])
812
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
813
- unless file_conf.to_s() == ""
814
- @bots_created = eval(file_conf)
815
- end
816
- end
817
818
  if @bots_created.key?(@rules_imported[user.id][user.id])
818
- rules_file = @bots_created[@rules_imported[user.id][user.id]][:rules_file]
819
819
  respond "*You are using rules from channel: <##{@rules_imported[user.id][user.id]}>. These are the specific commands for that channel:*", dest
820
820
  help_message_rules = IO.readlines(rules_file).join
821
821
  respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
822
822
  end
823
823
  end
824
- respond "Github project: https://github.com/MarioRuiz/slack-smart-bot", dest if !specific
824
+ if specific
825
+ unless defined?(rules) or rules_file.empty?
826
+ begin
827
+ eval(File.new(rules_file).read) if File.exist?(rules_file)
828
+ end
829
+ end
830
+ if defined?(git_project)
831
+ respond "Git project: #{git_project}", dest
832
+ end
833
+ else
834
+ respond "Slack Smart Bot Github project: https://github.com/MarioRuiz/slack-smart-bot", dest
835
+ end
825
836
  else
826
837
  processed = false
827
838
  end
828
839
 
829
840
  on_demand = false
830
- if command.match(/^@?(#{config[:nick]}):*\s+(.+)$/i) or
831
- command.match(/^()!(.+)$/i) or
832
- command.match(/^()<@#{config[:nick_id]}>\s+(.+)$/i)
841
+ if command.match(/^@?(#{config[:nick]}):*\s+(.+)/im) or
842
+ command.match(/^()!(.+)/im) or
843
+ command.match(/^()<@#{config[:nick_id]}>\s+(.+)/im)
833
844
  command = $2
834
845
  on_demand = true
835
846
  end
@@ -982,19 +993,29 @@ class SlackSmartBot
982
993
  #help: ----------------------------------------------
983
994
  #help: `ruby RUBY_CODE`
984
995
  #help: `code RUBY_CODE`
985
- #help: runs the code supplied and returns the output. Also you can send a Ruby file. Examples:
996
+ #help: runs the code supplied and returns the output. Also you can send a Ruby file instead. Examples:
986
997
  #help: _code puts (34344/99)*(34+14)_
987
998
  #help: _ruby require 'json'; res=[]; 20.times {res<<rand(100)}; my_json={result: res}; puts my_json.to_json_
988
999
  #help:
989
- when /^ruby\s(.+)/im, /code\s(.+)/im
1000
+ when /^\s*ruby\s(.+)/im, /^\s*code\s(.+)/im
990
1001
  code = $1
991
1002
  code.gsub!("\\n", "\n")
1003
+ code.gsub!("\\r", "\r")
1004
+ @logger.info code
992
1005
  unless code.match?(/System/i) or code.match?(/Kernel/i) or code.include?("File") or
993
1006
  code.include?("`") or code.include?("exec") or code.include?("spawn") or code.include?("IO") or
994
1007
  code.match?(/open3/i) or code.match?(/bundle/i) or code.match?(/gemfile/i) or code.include?("%x") or
995
1008
  code.include?("ENV")
1009
+ unless rules_file.empty?
1010
+ begin
1011
+ eval(File.new(rules_file).read) if File.exist?(rules_file)
1012
+ end
1013
+ end
1014
+
996
1015
  begin
997
- stdout, stderr, status = Open3.capture3("ruby -e \"#{code.gsub('"', '\"')}\"")
1016
+ ruby = "ruby -e \"#{code.gsub('"', '\"')}\""
1017
+ ruby = ("cd #{project_folder} &&" + ruby) if defined?(project_folder)
1018
+ stdout, stderr, status = Open3.capture3(ruby)
998
1019
  if stderr == ""
999
1020
  if stdout == ""
1000
1021
  respond "Nothing returned. Remember you need to use p or puts to print", dest
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-smart-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.10
4
+ version: 0.6.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-27 00:00:00.000000000 Z
11
+ date: 2019-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-ruby-client