slack-smart-bot 1.10.0 → 1.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +91 -21
  3. data/lib/slack/smart-bot/comm/delete.rb +13 -0
  4. data/lib/slack/smart-bot/comm/dont_understand.rb +2 -2
  5. data/lib/slack/smart-bot/comm/get_channel_members.rb +7 -3
  6. data/lib/slack/smart-bot/comm/get_presence.rb +20 -0
  7. data/lib/slack/smart-bot/comm/get_users.rb +1 -1
  8. data/lib/slack/smart-bot/comm/respond.rb +18 -13
  9. data/lib/slack/smart-bot/comm/send_msg_user.rb +12 -11
  10. data/lib/slack/smart-bot/comm.rb +2 -0
  11. data/lib/slack/smart-bot/commands/general/add_admin.rb +51 -0
  12. data/lib/slack/smart-bot/commands/general/add_announcement.rb +1 -1
  13. data/lib/slack/smart-bot/commands/general/add_team.rb +80 -0
  14. data/lib/slack/smart-bot/commands/general/allow_access.rb +67 -0
  15. data/lib/slack/smart-bot/commands/general/bot_help.rb +20 -11
  16. data/lib/slack/smart-bot/commands/general/delete_announcement.rb +1 -1
  17. data/lib/slack/smart-bot/commands/general/delete_share.rb +1 -1
  18. data/lib/slack/smart-bot/commands/general/delete_team.rb +34 -0
  19. data/lib/slack/smart-bot/commands/general/deny_access.rb +36 -0
  20. data/lib/slack/smart-bot/commands/general/ping_team.rb +100 -0
  21. data/lib/slack/smart-bot/commands/general/poster.rb +116 -0
  22. data/lib/slack/smart-bot/commands/general/remove_admin.rb +58 -0
  23. data/lib/slack/smart-bot/commands/general/see_access.rb +24 -0
  24. data/lib/slack/smart-bot/commands/general/see_admins.rb +33 -0
  25. data/lib/slack/smart-bot/commands/general/see_announcements.rb +6 -4
  26. data/lib/slack/smart-bot/commands/general/see_command_ids.rb +29 -0
  27. data/lib/slack/smart-bot/commands/general/see_favorite_commands.rb +3 -4
  28. data/lib/slack/smart-bot/commands/general/see_statuses.rb +34 -21
  29. data/lib/slack/smart-bot/commands/general/see_teams.rb +252 -0
  30. data/lib/slack/smart-bot/commands/general/share_messages.rb +1 -1
  31. data/lib/slack/smart-bot/commands/general/update_team.rb +109 -0
  32. data/lib/slack/smart-bot/commands/general_bot_commands.rb +271 -10
  33. data/lib/slack/smart-bot/commands/on_bot/add_shortcut.rb +2 -1
  34. data/lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb +2 -1
  35. data/lib/slack/smart-bot/commands/on_bot/admin/extend_rules.rb +2 -1
  36. data/lib/slack/smart-bot/commands/on_bot/admin/pause_bot.rb +2 -1
  37. data/lib/slack/smart-bot/commands/on_bot/admin/pause_routine.rb +2 -1
  38. data/lib/slack/smart-bot/commands/on_bot/admin/remove_routine.rb +2 -1
  39. data/lib/slack/smart-bot/commands/on_bot/admin/run_routine.rb +3 -2
  40. data/lib/slack/smart-bot/commands/on_bot/admin/see_result_routine.rb +2 -1
  41. data/lib/slack/smart-bot/commands/on_bot/admin/see_routines.rb +10 -9
  42. data/lib/slack/smart-bot/commands/on_bot/admin/start_bot.rb +2 -1
  43. data/lib/slack/smart-bot/commands/on_bot/admin/start_routine.rb +2 -1
  44. data/lib/slack/smart-bot/commands/on_bot/admin/stop_using_rules_on.rb +2 -1
  45. data/lib/slack/smart-bot/commands/on_bot/admin_master/delete_message.rb +25 -0
  46. data/lib/slack/smart-bot/commands/on_bot/admin_master/get_bot_logs.rb +1 -0
  47. data/lib/slack/smart-bot/commands/on_bot/admin_master/react_to.rb +3 -1
  48. data/lib/slack/smart-bot/commands/on_bot/admin_master/send_message.rb +15 -2
  49. data/lib/slack/smart-bot/commands/on_bot/delete_repl.rb +2 -1
  50. data/lib/slack/smart-bot/commands/on_bot/delete_shortcut.rb +5 -4
  51. data/lib/slack/smart-bot/commands/on_bot/general/bot_stats.rb +400 -0
  52. data/lib/slack/smart-bot/commands/{general → on_bot/general}/bot_status.rb +1 -0
  53. data/lib/slack/smart-bot/commands/{general → on_bot/general}/leaderboard.rb +1 -0
  54. data/lib/slack/smart-bot/commands/{general → on_bot/general}/stop_using_rules.rb +1 -0
  55. data/lib/slack/smart-bot/commands/{general → on_bot/general}/suggest_command.rb +6 -0
  56. data/lib/slack/smart-bot/commands/{general → on_bot/general}/use_rules.rb +1 -0
  57. data/lib/slack/smart-bot/commands/{general → on_bot/general}/whats_new.rb +2 -1
  58. data/lib/slack/smart-bot/commands/on_bot/get_repl.rb +2 -1
  59. data/lib/slack/smart-bot/commands/on_bot/repl.rb +72 -15
  60. data/lib/slack/smart-bot/commands/on_bot/ruby_code.rb +1 -0
  61. data/lib/slack/smart-bot/commands/on_bot/run_repl.rb +11 -2
  62. data/lib/slack/smart-bot/commands/on_bot/see_repls.rb +2 -1
  63. data/lib/slack/smart-bot/commands/on_bot/see_shortcuts.rb +3 -2
  64. data/lib/slack/smart-bot/commands/on_master/admin/kill_bot_on_channel.rb +5 -4
  65. data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +3 -2
  66. data/lib/slack/smart-bot/commands/on_master/admin_master/notify_message.rb +2 -1
  67. data/lib/slack/smart-bot/commands/on_master/admin_master/publish_announcements.rb +3 -2
  68. data/lib/slack/smart-bot/commands/on_master/admin_master/set_general_message.rb +2 -1
  69. data/lib/slack/smart-bot/commands/on_master/admin_master/set_maintenance.rb +2 -1
  70. data/lib/slack/smart-bot/commands/on_master/create_bot.rb +1 -0
  71. data/lib/slack/smart-bot/commands/on_master/where_smartbot.rb +41 -0
  72. data/lib/slack/smart-bot/commands.rb +22 -7
  73. data/lib/slack/smart-bot/listen.rb +30 -30
  74. data/lib/slack/smart-bot/process.rb +38 -18
  75. data/lib/slack/smart-bot/process_first.rb +2 -2
  76. data/lib/slack/smart-bot/treat_message.rb +13 -17
  77. data/lib/slack/smart-bot/utils/build_help.rb +1 -1
  78. data/lib/slack/smart-bot/utils/create_routine_thread.rb +1 -1
  79. data/lib/slack/smart-bot/utils/get_access_channels.rb +13 -0
  80. data/lib/slack/smart-bot/utils/get_admins_channels.rb +13 -0
  81. data/lib/slack/smart-bot/utils/get_bots_created.rb +27 -10
  82. data/lib/slack/smart-bot/utils/get_channels_name_and_id.rb +7 -2
  83. data/lib/slack/smart-bot/utils/get_command_ids.rb +84 -0
  84. data/lib/slack/smart-bot/utils/get_help.rb +34 -18
  85. data/lib/slack/smart-bot/utils/get_repls.rb +22 -2
  86. data/lib/slack/smart-bot/utils/get_routines.rb +22 -2
  87. data/lib/slack/smart-bot/utils/get_teams.rb +22 -0
  88. data/lib/slack/smart-bot/utils/has_access.rb +25 -9
  89. data/lib/slack/smart-bot/utils/is_admin.rb +27 -0
  90. data/lib/slack/smart-bot/utils/save_stats.rb +46 -43
  91. data/lib/slack/smart-bot/utils/save_status.rb +21 -6
  92. data/lib/slack/smart-bot/utils/update_access_channels.rb +8 -0
  93. data/lib/slack/smart-bot/utils/update_admins_channels.rb +8 -0
  94. data/lib/slack/smart-bot/utils/update_bots_file.rb +28 -7
  95. data/lib/slack/smart-bot/utils/update_repls.rb +7 -4
  96. data/lib/slack/smart-bot/utils/update_routines.rb +9 -3
  97. data/lib/slack/smart-bot/utils/update_shortcuts_file.rb +13 -6
  98. data/lib/slack/smart-bot/utils/update_teams.rb +16 -0
  99. data/lib/slack/smart-bot/utils.rb +8 -0
  100. data/lib/slack-smart-bot.rb +28 -10
  101. data/lib/slack-smart-bot_general_commands.rb +16 -1
  102. data/whats_new.txt +16 -29
  103. metadata +64 -19
  104. data/lib/slack/smart-bot/commands/general/bot_stats.rb +0 -314
@@ -99,6 +99,8 @@ class SlackSmartBot
99
99
  /\Akill\s+bot\s+on\s+#(.+)\s*$/i, /\Akill\s+bot\s+on\s+(.+)\s*$/i
100
100
  channel = $1
101
101
  kill_bot_on_channel(dest, from, channel)
102
+ when /\A\s*(where\s+is|which\s+channels|where\s+is\s+a\s+member)\s+(#{@salutations.join("|")})\??\s*$/i
103
+ where_smartbot(user)
102
104
  when /\A\s*(add|create)\s+(silent\s+)?(bgroutine|routine)\s+([\w\.]+)\s+(every)\s+(\d+)\s*(days|hours|minutes|seconds|mins|min|secs|sec|d|h|m|s)\s*(\s#(\w+)\s*)(\s.+)?\s*\z/im,
103
105
  /\A\s*(add|create)\s+(silent\s+)?(bgroutine|routine)\s+([\w\.]+)\s+(every)\s+(\d+)\s*(days|hours|minutes|seconds|mins|min|secs|sec|d|h|m|s)\s*(\s<#(C\w+)\|.+>\s*)?(\s.+)?\s*\z/im,
104
106
  /\A\s*(add|create)\s+(silent\s+)?(bgroutine|routine)\s+([\w\.]+)\s+(every)\s+(\d+)\s*(days|hours|minutes|seconds|mins|min|secs|sec|d|h|m|s)\s*(\s<#(\w+)\|>\s*)?(\s.+)?\s*\z/im,
@@ -150,29 +152,42 @@ class SlackSmartBot
150
152
  see_routines(dest, from, user, all)
151
153
  when /\A\s*get\s+bot\s+logs?\s*$/i
152
154
  get_bot_logs(dest, from, typem)
153
- when /\A\s*send\s+message\s+(on|to|in)\s*([^\s]+)\s+([^\s]+)\s*:\s*(.+)\s*$/i,
154
- /\A\s*send\s+message\s+(on|to|in)\s*([^\s]+)\s*():\s*(.+)\s*$/i
155
- to = $2
156
- thread_ts = $3.to_s
157
- message = $4
158
- to_channel = to.scan(/<#([^>]+)\|.*>/).join
159
- to_channel = to.scan(/#([^\s]+)/).join if to_channel == ''
160
- if to_channel == ''
161
- to_user = to.scan(/<@(\w+)>/).join
162
- if to_user == ''
163
- # message_id
164
- else
165
- to = to_user
155
+ when /\A\s*send\s+message\s+(on|to|in)\s*(.+)\s*:\s*(.+)\s*$/im
156
+ opts = $2
157
+ message = $3
158
+ thread_ts = ''
159
+ to_channel = ''
160
+ to = []
161
+
162
+ opts.split(' ').each do |opt|
163
+ if opt.match?(/\Ahttps:/i)
164
+ to_channel, thread_ts = opt.scan(/\/archives\/(\w+)\/(\w\d+)/)[0]
165
+ to << to_channel
166
+ elsif opt.match(/<#([^>]+)\|.*>/) #channel
167
+ to << $1
168
+ elsif opt.match(/#([^\s]+)/) #channel
169
+ to << $1
170
+ elsif opt.match(/<@(\w+)>/)
171
+ to << $1
166
172
  end
167
- else
168
- to = to_channel
169
173
  end
174
+
175
+ thread_ts.gsub!('.','')
170
176
  send_message(dest, from, typem, to, thread_ts, message)
171
- when /\A\s*react\s+(on|to|in)\s*([^\s]+)\s+([^\s]+)\s+(.+)\s*$/i
177
+ when /\A\s*delete\s+message\s+(.+)\s*$/i
178
+ url = $1
179
+ delete_message(from, typem, url)
180
+ when /\A\s*react\s+(on|to|in)\s*([^\s]+)\s+([p\d\.]+)\s+(.+)\s*$/i,
181
+ /\A\s*react\s+(on|to|in)\s*([^\s]+)\s+()(.+)\s*$/i
172
182
  to = $2
173
183
  thread_ts = $3.to_s
174
184
  emojis = $4
175
- to_channel = to.scan(/<#([^>]+)\|.*>/).join
185
+
186
+ if to.match?(/\A<?https:/i)
187
+ to_channel, thread_ts = to.scan(/\/archives\/(\w+)\/(\w\d+)/)[0]
188
+ else
189
+ to_channel = to.scan(/<#([^>]+)\|.*>/).join
190
+ end
176
191
  if to_channel == ''
177
192
  to_channel = to.scan(/#([^\s]+)/).join
178
193
  to_channel = @channels_id[to_channel].to_s
@@ -251,6 +266,10 @@ class SlackSmartBot
251
266
 
252
267
  when /\A\s*bot\s+stats\s*(.*)\s*$/i
253
268
  opts = $1.to_s
269
+ exclude_members_channel = opts.scan(/exclude\s+members\s+<#(\w+)\|.*>/i).join #todo: add test
270
+ opts.gsub!(/exclude\s+members\s+<#\w+\|.*>/,'')
271
+ members_channel = opts.scan(/members\s+<#(\w+)\|.*>/i).join #todo: add test
272
+ opts.gsub!(/members\s+<#\w+\|.*>/,'')
254
273
  all_opts = opts.downcase.split(' ')
255
274
  all_data = all_opts.include?('alldata')
256
275
  st_channel = opts.scan(/<#(\w+)\|.*>/).join
@@ -259,6 +278,7 @@ class SlackSmartBot
259
278
  st_to = opts.scan(/to\s+(\d\d\d\d[\/\-\.]\d\d[\/\-\.]\d\d)/).join
260
279
  st_to = st_to.gsub('.','-').gsub('/','-')
261
280
  st_user = opts.scan(/<@([^>]+)>/).join
281
+ st_user = opts.scan(/@([^\s]+)/).join if st_user == ''
262
282
  st_command = opts.scan(/\s+command\s+(\w+)/i).join.downcase
263
283
  st_command = opts.scan(/^command\s+(\w+)/i).join.downcase if st_command == ''
264
284
  exclude_masters = all_opts.include?('exclude') && all_opts.include?('masters')
@@ -284,7 +304,7 @@ class SlackSmartBot
284
304
  if (typem == :on_master or typem == :on_bot) and dest[0]!='D' #routine bot stats to be published on DM
285
305
  st_channel = dchannel
286
306
  end
287
- bot_stats(dest, user, typem, st_channel, st_from, st_to, st_user, st_command, exclude_masters, exclude_routines, exclude_command, monthly, all_data)
307
+ bot_stats(dest, user, typem, st_channel, st_from, st_to, st_user, st_command, exclude_masters, exclude_routines, exclude_command, monthly, all_data, members_channel, exclude_members_channel)
288
308
  when /\A(set|turn)\s+maintenance\s+(on|off)\s*()\z/im, /\A(set|turn)\s+maintenance\s+(on)\s*(.+)\s*\z/im
289
309
  status = $2.downcase
290
310
  message = $3.to_s
@@ -1,5 +1,5 @@
1
1
  class SlackSmartBot
2
- def process_first(user, text, dest, dchannel, typem, files, ts, thread_ts, routine, routine_name, routine_type)
2
+ def process_first(user, text, dest, dchannel, typem, files, ts, thread_ts, routine, routine_name, routine_type, command_orig)
3
3
  nick = user.name
4
4
  rules_file = ""
5
5
  text.gsub!(/^!!/,'^') # to treat it just as ^
@@ -46,7 +46,6 @@ class SlackSmartBot
46
46
  rules_file = "/rules/general_rules.rb"
47
47
  end
48
48
  end
49
-
50
49
  if nick == config[:nick] #if message is coming from the bot
51
50
  begin
52
51
  case text
@@ -197,6 +196,7 @@ class SlackSmartBot
197
196
  Thread.current[:routine_name] = routine_name
198
197
  Thread.current[:routine_type] = routine_type
199
198
  Thread.current[:dchannel] = dchannel
199
+ Thread.current[:command_orig] = command_orig
200
200
  if thread_ts.to_s == ''
201
201
  Thread.current[:on_thread] = false
202
202
  Thread.current[:thread_ts] = Thread.current[:ts] # to create the thread if necessary
@@ -3,6 +3,7 @@ class SlackSmartBot
3
3
  @buffered = false if config[:testing]
4
4
  begin
5
5
  begin
6
+ command_orig = data.text
6
7
  unless data.text.to_s.match(/\A\s*\z/)
7
8
  #to remove italic, bold... from data.text since there is no method on slack api
8
9
  if remove_blocks and !data.blocks.nil? and data.blocks.size > 0
@@ -86,6 +87,7 @@ class SlackSmartBot
86
87
  end
87
88
  end
88
89
  if !dest.nil? and !data.text.nil? and !data.text.to_s.match?(/\A\s*\z/)
90
+ get_bots_created()
89
91
  if data.channel[0] == "D" and !data.text.to_s.match?(/^\s*<@#{config[:nick_id]}>\s+/) and
90
92
  (data.text.to_s.match?(/^\s*(on)?\s*<#\w+\|[^>]*>/i) or data.text.to_s.match?(/^\s*(on)?\s*#\w+/i))
91
93
  data.text = "<@#{config[:nick_id]}> " + data.text.to_s
@@ -183,7 +185,8 @@ class SlackSmartBot
183
185
  end
184
186
  end
185
187
  end
186
- load "#{config.path}/rules/general_commands.rb" if File.exists?("#{config.path}/rules/general_commands.rb") and @datetime_general_commands != File.mtime("#{config.path}/rules/general_commands.rb")
188
+ load "#{config.path}/rules/general_commands.rb" if File.exist?("#{config.path}/rules/general_commands.rb") and @datetime_general_commands != File.mtime("#{config.path}/rules/general_commands.rb")
189
+
187
190
  unless typem == :dont_treat or user_info.nil?
188
191
  if (Time.now - @last_activity_check) > 60 * 30 #every 30 minutes
189
192
  @last_activity_check = Time.now
@@ -241,7 +244,6 @@ class SlackSmartBot
241
244
  command += " ruby" if command != "ruby"
242
245
  command = "#{command} #{res.body.to_s.force_encoding("UTF-8")}"
243
246
  end
244
-
245
247
  if typem == :on_call
246
248
  command = "!" + command unless command[0] == "!" or command.match?(/^\s*$/) or command[0] == "^"
247
249
 
@@ -256,24 +258,24 @@ class SlackSmartBot
256
258
  elsif @status != :on
257
259
  respond "The bot in that channel is not :on", data.channel
258
260
  elsif data.user == channel_found.creator or members.include?(data.user)
259
- process_first(user_info, command, dest, channel_rules, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type)
261
+ process_first(user_info, command, dest, channel_rules, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type, command_orig)
260
262
  else
261
263
  respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", data.channel
262
264
  end
263
265
  elsif config.on_master_bot and typem == :on_extended and
264
266
  command.size > 0 and command[0] != "-"
265
267
  # to run ruby only from the master bot for the case more than one extended
266
- process_first(user_info, command, dest, @channel_id, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type)
268
+ process_first(user_info, command, dest, @channel_id, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type, command_orig)
267
269
  elsif !config.on_master_bot and @bots_created[@channel_id].key?(:extended) and
268
270
  @bots_created[@channel_id][:extended].include?(@channels_name[data.channel]) and
269
271
  command.size > 0 and command[0] != "-"
270
- process_first(user_info, command, dest, @channel_id, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type)
272
+ process_first(user_info, command, dest, @channel_id, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type, command_orig)
271
273
  elsif (dest[0] == "D" or @channel_id == data.channel or data.user == config[:nick_id]) and
272
274
  command.size > 0 and command[0] != "-"
273
- process_first(user_info, command, dest, data.channel, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type)
275
+ process_first(user_info, command, dest, data.channel, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type, command_orig)
274
276
  # if @botname on #channel_rules: do something
275
277
  elsif typem == :on_pub or typem == :on_pg
276
- process_first(user_info, command, dest, channel_rules, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type)
278
+ process_first(user_info, command, dest, channel_rules, typem, data.files, data.ts, data.thread_ts, data.routine, data.routine_name, data.routine_type, command_orig)
277
279
  end
278
280
  rescue Exception => stack
279
281
  @logger.fatal stack
@@ -337,19 +339,13 @@ class SlackSmartBot
337
339
  get_bots_created()
338
340
  when /global shortcut added/
339
341
  sleep 2
340
- if File.exist?("#{config.path}/shortcuts/shortcuts_global.rb")
341
- file_sc = IO.readlines("#{config.path}/shortcuts/shortcuts_global.rb").join
342
- unless file_sc.to_s() == ""
343
- @shortcuts_global = eval(file_sc)
344
- end
342
+ if File.exist?("#{config.path}/shortcuts/shortcuts_global.yaml")
343
+ @shortcuts_global = YAML.load(File.read("#{config.path}/shortcuts/shortcuts_global.yaml"))
345
344
  end
346
345
  when /global shortcut deleted/
347
346
  sleep 2
348
- if File.exist?("#{config.path}/shortcuts/shortcuts_global.rb")
349
- file_sc = IO.readlines("#{config.path}/shortcuts/shortcuts_global.rb").join
350
- unless file_sc.to_s() == ""
351
- @shortcuts_global = eval(file_sc)
352
- end
347
+ if File.exist?("#{config.path}/shortcuts/shortcuts_global.yaml")
348
+ @shortcuts_global = YAML.load(File.read("#{config.path}/shortcuts/shortcuts_global.yaml"))
353
349
  end
354
350
  end
355
351
  end
@@ -9,8 +9,8 @@ class SlackSmartBot
9
9
  else
10
10
  return help_message
11
11
  end
12
- files.each do |t|
13
12
 
13
+ files.each do |t|
14
14
  if Dir.exist?(t)
15
15
  res = build_help(t, expanded)
16
16
  help_message[:master][t.scan(/\/(\w+)$/).join.to_sym] = res[:master]
@@ -25,7 +25,7 @@ class SlackSmartBot
25
25
  (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s!='weekday' and @routines[@channel_id][name][:dayweek].to_s!='weekend') or
26
26
  (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s=='weekday' and Date.today.wday>=1 and Date.today.wday<=5) or
27
27
  (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s=='weekend' and (Date.today.wday==6 or Date.today.wday==0))
28
- File.delete "#{config.path}/routines/#{@channel_id}/#{name}_output.txt" if File.exists?("#{config.path}/routines/#{@channel_id}/#{name}_output.txt")
28
+ File.delete "#{config.path}/routines/#{@channel_id}/#{name}_output.txt" if File.exist?("#{config.path}/routines/#{@channel_id}/#{name}_output.txt")
29
29
  if @routines[@channel_id][name][:file_path] != ""
30
30
  process_to_run = "#{ruby}#{Dir.pwd}#{@routines[@channel_id][name][:file_path][1..-1]}"
31
31
  process_to_run = ("cd #{project_folder} &&" + process_to_run) if defined?(project_folder)
@@ -0,0 +1,13 @@
1
+ class SlackSmartBot
2
+
3
+ def get_access_channels()
4
+ if File.exist?("#{config.path}/rules/#{@channel_id}/access_channels.rb")
5
+ file_conf = IO.readlines("#{config.path}/rules/#{@channel_id}/access_channels.rb").join
6
+ unless file_conf.to_s() == ""
7
+ @access_channels = eval(file_conf)
8
+ end
9
+ else
10
+ @access_channels = {}
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class SlackSmartBot
2
+
3
+ def get_admins_channels()
4
+ if File.exist?("#{config.path}/rules/#{@channel_id}/admins_channels.rb")
5
+ file_conf = IO.readlines("#{config.path}/rules/#{@channel_id}/admins_channels.rb").join
6
+ unless file_conf.to_s() == ""
7
+ @admins_channels = eval(file_conf)
8
+ end
9
+ else
10
+ @admins_channels = {}
11
+ end
12
+ end
13
+ end
@@ -1,16 +1,33 @@
1
1
  class SlackSmartBot
2
2
  def get_bots_created
3
- if File.exist?(config.file_path.gsub(".rb", "_bots.rb"))
3
+ require 'yaml'
4
+ bots_file = config.file_path.gsub(".rb", "_bots.yaml")
5
+
6
+ if File.exist?(config.file_path.gsub(".rb", "_bots.rb")) #backwards compatible
7
+ file_conf = IO.readlines(config.file_path.gsub(".rb", "_bots.rb")).join
8
+ if file_conf.to_s() == ""
9
+ @bots_created = {}
10
+ else
11
+ @bots_created = eval(file_conf)
12
+ end
13
+ File.open(bots_file, 'w') {|file| file.write(@bots_created.to_yaml) }
14
+ File.delete(config.file_path.gsub(".rb", "_bots.rb"))
15
+ end
16
+
17
+ if File.exist?(bots_file)
4
18
 
5
- if !defined?(@datetime_bots_created) or @datetime_bots_created != File.mtime(config.file_path.gsub(".rb", "_bots.rb"))
6
-
7
- file_conf = IO.readlines(config.file_path.gsub(".rb", "_bots.rb")).join
8
- if file_conf.to_s() == ""
9
- @bots_created = {}
10
- else
11
- @bots_created = eval(file_conf)
19
+ if !defined?(@datetime_bots_created) or @datetime_bots_created != File.mtime(bots_file)
20
+ bots_created = @bots_created
21
+ 10.times do
22
+ bots_created = YAML.load(File.read(bots_file))
23
+ if bots_created.is_a?(Hash)
24
+ break
25
+ else
26
+ sleep (0.1*(rand(2)+1))
27
+ end
12
28
  end
13
- @datetime_bots_created = File.mtime(config.file_path.gsub(".rb", "_bots.rb"))
29
+ @bots_created = bots_created unless bots_created.is_a?(FalseClass)
30
+ @datetime_bots_created = File.mtime(bots_file)
14
31
  @extended_from = {}
15
32
  @bots_created.each do |k, v|
16
33
  v[:extended] = [] unless v.key?(:extended)
@@ -20,7 +37,7 @@ class SlackSmartBot
20
37
  end
21
38
  v[:rules_file] ||= ''
22
39
  v[:rules_file].gsub!(/^\./, '')
23
- end
40
+ end
24
41
  end
25
42
  end
26
43
  end
@@ -1,13 +1,18 @@
1
1
  class SlackSmartBot
2
2
 
3
3
  def get_channels_name_and_id
4
- channels = get_channels()
4
+ @channels_list = get_channels()
5
5
  @channels_id = Hash.new()
6
6
  @channels_name = Hash.new()
7
- channels.each do |ch|
7
+ @channels_creator = Hash.new()
8
+ @users = get_users() if @users.empty?
9
+ @channels_list.each do |ch|
8
10
  unless ch.is_archived
9
11
  @channels_id[ch.name] = ch.id
10
12
  @channels_name[ch.id] = ch.name
13
+ user_info = @users.select{|u| u.id == ch.creator or (u.key?(:enterprise_user) and u.enterprise_user.id == ch.creator)}[-1]
14
+ @channels_creator[ch.id] = user_info.name unless user_info.nil?
15
+ @channels_creator[ch.name] = user_info.name unless user_info.nil?
11
16
  end
12
17
  end
13
18
  end
@@ -0,0 +1,84 @@
1
+ class SlackSmartBot
2
+ def get_command_ids
3
+ commands = {
4
+ general: [],
5
+ on_bot_general: [],
6
+ on_bot_on_demand: [],
7
+ on_bot_admin: [],
8
+ on_bot_master_admin: [],
9
+ on_extended: [],
10
+ on_master: [],
11
+ on_master_admin: [],
12
+ on_master_master_admin: [],
13
+ general_commands: [],
14
+ general_rules: [],
15
+ rules: []
16
+ }
17
+ typem = Thread.current[:typem]
18
+ user = Thread.current[:user]
19
+ # :on_call, :on_bot, :on_extended, :on_dm, :on_master, :on_pg, :on_pub
20
+ admin = is_admin?(user.name)
21
+
22
+ commands[:general] = (Dir.entries("#{__dir__}/../commands/general/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
23
+ general = File.read("#{__dir__}/../commands/general_bot_commands.rb")
24
+ commands[:general] += general.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
25
+ commands[:general].uniq!
26
+
27
+ if typem == :on_bot or typem == :on_master
28
+ commands[:on_bot_general] = (Dir.entries("#{__dir__}/../commands/on_bot/general/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
29
+ end
30
+
31
+ if typem == :on_bot or typem == :on_master
32
+ commands[:on_bot_on_demand] = (Dir.entries("#{__dir__}/../commands/on_bot/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
33
+ end
34
+
35
+ if (typem == :on_bot or typem == :on_master) and admin
36
+ commands[:on_bot_admin] = (Dir.entries("#{__dir__}/../commands/on_bot/admin/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
37
+ end
38
+
39
+ if (typem == :on_bot or typem == :on_master) and config.masters.include?(user.name)
40
+ commands[:on_bot_master_admin] = (Dir.entries("#{__dir__}/../commands/on_bot/admin_master/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
41
+ end
42
+
43
+ if typem == :on_extended
44
+ commands[:on_extended] = (Dir.entries("#{__dir__}/../commands/on_extended/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
45
+ commands[:on_extended]+= ['repl', 'see_repls', 'get_repl', 'run_repl', 'delete_repl', 'ruby_code']
46
+ end
47
+
48
+ if typem == :on_master
49
+ commands[:on_master] = (Dir.entries("#{__dir__}/../commands/on_master/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
50
+ end
51
+
52
+ if typem == :on_master and admin
53
+ commands[:on_master_admin] = (Dir.entries("#{__dir__}/../commands/on_master/admin/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
54
+ end
55
+
56
+ if typem == :on_master and config.masters.include?(user.name)
57
+ commands[:on_master_master_admin] = (Dir.entries("#{__dir__}/../commands/on_master/admin_master/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
58
+ end
59
+
60
+ if File.exist?("#{config.path}/rules/general_commands.rb")
61
+ general_commands = File.read("#{config.path}/rules/general_commands.rb")
62
+ commands[:general_commands] = general_commands.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
63
+ commands[:general_commands]+= general_commands.scan(/^\s*save_stats\(?\s*:(\w+)\s*,?/i).flatten
64
+ commands[:general_commands].uniq!
65
+ end
66
+
67
+ if typem == :on_extended or typem ==:on_call or typem == :on_bot or typem == :on_master or (typem == :on_dm and Thread.current[:using_channel].to_s != '')
68
+ if Thread.current.key?(:rules_file) and File.exist?(config.path + Thread.current[:rules_file])
69
+ rules = File.read(config.path + Thread.current[:rules_file])
70
+ commands[:rules] = rules.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
71
+ commands[:rules]+= rules.scan(/^\s*save_stats\(?\s*:(\w+)\s*,?/i).flatten
72
+ commands[:rules].uniq!
73
+
74
+ if File.exist?("#{config.path}/rules/general_rules.rb")
75
+ general_rules = File.read("#{config.path}/rules/general_rules.rb")
76
+ commands[:general_rules] = general_rules.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
77
+ commands[:general_rules]+= general_rules.scan(/^\s*save_stats\(?\s*:(\w+)\s*,?/i).flatten
78
+ commands[:general_rules].uniq!
79
+ end
80
+ end
81
+ end
82
+ return commands
83
+ end
84
+ end
@@ -1,14 +1,17 @@
1
1
  class SlackSmartBot
2
2
  def get_help(rules_file, dest, from, only_rules, expanded, descriptions: true, only_normal_user: false)
3
3
  order = {
4
- general: [:whats_new, :hi_bot, :bye_bot, :bot_help, :suggest_command, :bot_status, :use_rules, :stop_using_rules, :bot_stats, :leaderboard],
4
+ general: [:bot_help, :hi_bot, :bye_bot, :add_admin, :remove_admin, :see_admins, :poster, :add_announcement, :delete_announcement,
5
+ :see_announcements, :see_command_ids, :share_messages, :see_shares, :delete_share, :see_favorite_commands, :see_statuses,
6
+ :allow_access, :see_access, :deny_access, :add_team, :see_teams, :update_team, :ping_team, :delete_team],
7
+ on_bot_general: [:whats_new, :suggest_command, :bot_status, :use_rules, :stop_using_rules, :bot_stats, :leaderboard],
5
8
  on_bot: [:ruby_code, :repl, :get_repl, :run_repl, :delete_repl, :see_repls, :add_shortcut, :delete_shortcut, :see_shortcuts],
6
9
  on_bot_admin: [:extend_rules, :stop_using_rules_on, :start_bot, :pause_bot, :add_routine,
7
10
  :see_routines, :start_routine, :pause_routine, :remove_routine, :see_result_routine, :run_routine]
8
11
  }
9
12
  if config.masters.include?(from)
10
13
  user_type = :master # master admin
11
- elsif config.admins.include?(from)
14
+ elsif is_admin?(from)
12
15
  user_type = :admin
13
16
  else
14
17
  user_type = :normal #normal user
@@ -66,9 +69,15 @@ class SlackSmartBot
66
69
  end
67
70
 
68
71
  help[:general_commands_file] = build_help("#{__dir__}/../commands/general_bot_commands.rb", expanded)[user_type].values.join("\n") + "\n" unless only_rules
69
- if File.exists?(config.path + '/rules/general_commands.rb') and !only_rules
72
+ if File.exist?(config.path + '/rules/general_commands.rb') and !only_rules
70
73
  help[:general_commands_file] += build_help(config.path+'/rules/general_commands.rb', expanded)[user_type].values.join("\n") + "\n"
71
74
  end
75
+ if help.key?(:on_bot)
76
+ commands_on_extended_from_on_bot = [:repl, :see_repls, :get_repl, :run_repl, :delete_repl, :ruby_code]
77
+ commands_on_extended_from_on_bot.each do |cm|
78
+ help[:on_extended][cm] = help[:on_bot][cm] if help[:on_bot].key?(cm)
79
+ end
80
+ end
72
81
  help = remove_hash_keys(help, :admin_master) unless user_type == :master
73
82
  help = remove_hash_keys(help, :admin) unless user_type == :admin or user_type == :master
74
83
  help = remove_hash_keys(help, :on_master) unless channel_type == :master_bot
@@ -101,36 +110,33 @@ class SlackSmartBot
101
110
  You need to join the specified channel to be able to use those rules.
102
111
  Also you can use this command to call another bot from a channel with a running bot.
103
112
  \n"
104
- txt +="
105
- The commands you will be able to use from a channel without a bot:
106
- *bot rules*, *ruby CODE*, *add shortcut NAME: COMMAND*, *delete shortcut NAME*, *see shortcuts*, *shortcut NAME*
107
- *And all the specific rules of the Channel*\n"
108
113
  end
109
114
 
110
- if help.key?(:general) and channel_type != :external and channel_type != :extended
115
+ if help.key?(:general_commands_file)
116
+ txt += "===================================
117
+ *General commands on any channel where the Smart Bot is a member:*\n" if descriptions
118
+ txt += help.general_commands_file
119
+ end
120
+
121
+ if help.key?(:on_bot) and help.on_bot.key?(:general) and channel_type != :external and channel_type != :extended
111
122
  if descriptions
112
123
  if channel_type == :direct
113
124
  txt += "===================================
114
125
  *General commands:*\n"
115
126
  else
116
127
  txt += "===================================
117
- *General commands even when the Smart Bot is not listening to you:*\n"
128
+ *General commands on Bot channel even when the Smart Bot is not listening to you:*\n"
118
129
  end
119
130
  end
120
- order.general.each do |o|
121
- txt += help.general[o]
131
+ order.on_bot_general.each do |o|
132
+ txt += help.on_bot.general[o]
122
133
  end
123
134
  if channel_type == :master_bot
124
135
  txt += help.on_master.create_bot
136
+ txt += help.on_master.where_smartbot
125
137
  end
126
138
  end
127
139
 
128
- if help.key?(:general_commands_file)
129
- txt += "===================================
130
- *General commands on any channel where the Smart Bot is a member:*\n" if descriptions
131
- txt += help.general_commands_file
132
- end
133
-
134
140
  if help.key?(:on_bot) and channel_type != :external and channel_type != :extended
135
141
  if descriptions
136
142
  if channel_type == :direct
@@ -138,13 +144,23 @@ class SlackSmartBot
138
144
  *General commands on bot, DM or on external call on demand:*\n"
139
145
  else
140
146
  txt += "===================================
141
- *General commands only when the Smart Bot is listening to you or on demand:*\n"
147
+ *General commands on Bot channel only when the Smart Bot is listening to you or on demand:*\n"
142
148
  end
143
149
  end
144
150
  order.on_bot.each do |o|
145
151
  txt += help.on_bot[o]
146
152
  end
147
153
  end
154
+ if help.key?(:on_extended) and channel_type == :extended and help[:on_extended].keys.size > 0
155
+ if descriptions
156
+ txt += "===================================
157
+ *General commands on Extended channel only on demand:*\n"
158
+ end
159
+ commands_on_extended_from_on_bot.each do |o|
160
+ txt += help.on_extended[o]
161
+ end
162
+ end
163
+
148
164
  if help.key?(:on_bot) and help.on_bot.key?(:admin) and channel_type != :external and channel_type != :extended
149
165
  txt += "===================================
150
166
  *Admin commands:*\n\n" if descriptions
@@ -1,11 +1,31 @@
1
1
  class SlackSmartBot
2
2
 
3
3
  def get_repls(channel = @channel_id)
4
- if File.exist?("#{config.path}/repl/repls_#{channel}.rb")
4
+ require 'yaml'
5
+ repl_file = "#{config.path}/repl/repls_#{channel}.yaml"
6
+
7
+ if File.exist?("#{config.path}/repl/repls_#{channel}.rb") #backwards compatible
5
8
  file_conf = IO.readlines("#{config.path}/repl/repls_#{channel}.rb").join
6
- unless file_conf.to_s() == ""
9
+ if file_conf.to_s() == ""
10
+ @repls = {}
11
+ else
7
12
  @repls = eval(file_conf)
8
13
  end
14
+ File.open(repl_file, 'w') {|file| file.write(@repls.to_yaml) }
15
+ File.delete("#{config.path}/repl/repls_#{channel}.rb")
16
+ end
17
+
18
+ if File.exist?(repl_file)
19
+ repls = @repls
20
+ 10.times do
21
+ repls = YAML.load(File.read(repl_file))
22
+ if repls.is_a?(Hash)
23
+ break
24
+ else
25
+ sleep (0.1*(rand(2)+1))
26
+ end
27
+ end
28
+ @repls = repls unless repls.is_a?(FalseClass)
9
29
  end
10
30
  end
11
31
  end
@@ -1,11 +1,31 @@
1
1
  class SlackSmartBot
2
2
 
3
3
  def get_routines(channel = @channel_id)
4
- if File.exist?("#{config.path}/routines/routines_#{channel}.rb")
4
+ require 'yaml'
5
+ routines_file = "#{config.path}/routines/routines_#{channel}.yaml"
6
+
7
+ if File.exist?("#{config.path}/routines/routines_#{channel}.rb") #backwards compatible
5
8
  file_conf = IO.readlines("#{config.path}/routines/routines_#{channel}.rb").join
6
- unless file_conf.to_s() == ""
9
+ if file_conf.to_s() == ""
10
+ @routines = {}
11
+ else
7
12
  @routines = eval(file_conf)
8
13
  end
14
+ File.open(routines_file, 'w') {|file| file.write(@routines.to_yaml) }
15
+ File.delete("#{config.path}/routines/routines_#{channel}.rb")
16
+ end
17
+
18
+ if File.exist?(routines_file)
19
+ routines = @routines
20
+ 10.times do
21
+ routines = YAML.load(File.read(routines_file))
22
+ if routines.is_a?(Hash)
23
+ break
24
+ else
25
+ sleep (0.1*(rand(2)+1))
26
+ end
27
+ end
28
+ @routines = routines unless routines.is_a?(FalseClass)
9
29
  end
10
30
  end
11
31
  end
@@ -0,0 +1,22 @@
1
+ class SlackSmartBot
2
+ def get_teams
3
+ @teams ||= {}
4
+ teams_file = config.file_path.gsub(".rb", "_teams.yaml")
5
+ if File.exist?(teams_file)
6
+ if !defined?(@datetime_teams_file) or @datetime_teams_file != File.mtime(teams_file)
7
+ require 'yaml'
8
+ teams = @teams
9
+ 10.times do
10
+ teams = YAML.load(File.read(teams_file))
11
+ if teams.is_a?(Hash)
12
+ break
13
+ else
14
+ sleep (0.1*(rand(2)+1))
15
+ end
16
+ end
17
+ @teams = teams unless teams.is_a?(FalseClass)
18
+ @datetime_teams_file = File.mtime(teams_file)
19
+ end
20
+ end
21
+ end
22
+ end