diff --git a/colors.rb b/colors.rb index 8eee4a2..b4efdb2 100644 --- a/colors.rb +++ b/colors.rb @@ -2,6 +2,7 @@ class String def black; "\e[30m#{self}\e[0m" end def red; "\e[31m#{self}\e[0m" end def green; "\e[32m#{self}\e[0m" end +def yellow; "\e[33m#{self}\e[0m" end def brown; "\e[33m#{self}\e[0m" end def blue; "\e[34m#{self}\e[0m" end def magenta; "\e[35m#{self}\e[0m" end @@ -11,6 +12,7 @@ def gray; "\e[37m#{self}\e[0m" end def bg_black; "\e[40m#{self}\e[0m" end def bg_red; "\e[41m#{self}\e[0m" end def bg_green; "\e[42m#{self}\e[0m" end +def bg_yellow; "\e[43m#{self}\e[0m" end def bg_brown; "\e[43m#{self}\e[0m" end def bg_blue; "\e[44m#{self}\e[0m" end def bg_magenta; "\e[45m#{self}\e[0m" end diff --git a/commands.rb b/commands.rb index 9da5ff4..42557e7 100644 --- a/commands.rb +++ b/commands.rb @@ -13,7 +13,7 @@ def message_from_admin?(message, adm) #puts message.from.id #puts adm if adm.include?(message.from.id) - puts "Command is from an admin. [" + "#{message.from.username}".cyan.bold + "]" + puts "Command is from an admin. [" + "#{message.from.username}".green.bold + "]" return true else puts "Command is NOT from an admin! [" + "#{message.from.username}".yellow.bold + "]" diff --git a/run.rb b/run.rb index ffb7284..a8dfa5c 100755 --- a/run.rb +++ b/run.rb @@ -4,9 +4,14 @@ require 'rubygems' require 'yaml' require 'telegram/bot' require 'pp' +require 'time' require_relative 'commands.rb' require_relative 'colors.rb' +def timestamp + Time.now.strftime("%F %H:%M:%S").yellow +end + #conf = YAML.load(File.read("bot_config.yaml")) @conf = YAML.load_file("bot_config.yaml") @botname = @conf['botname'] @@ -17,6 +22,7 @@ admin = @conf['admin'] @allowed_sources = @conf['allowed_sources'] ### Begin sanity check ### +STDOUT.sync = true errcount = 0 puts "Checking if environment is sane...\n\n" print "Checking bot token ...................... " @@ -55,7 +61,7 @@ if admin.nil? else print "OK\n".green.bold end -print "Checking authorised chats ............... " +print "Checking authorized chats ............... " if @auth_chat.nil? errcount += 1 print "FAIL!\n\n".red.bold @@ -71,14 +77,16 @@ if errcount > 0 else print "Environment appears sane.\n\n".green.bold end +STDOUT.sync = false ### End sanity check ### - puts "Starting [#{@botname}]...\n\n" puts "Empyrion Host: #{telnet['host']}" puts "Empyrion Telnet Port: #{telnet['port']}" puts "Authorized administrator IDs: #{admin}" puts "Authorized chat IDs: #{@auth_chat}" -puts "Bot token: #{token}\n\n\n\n" +puts "Bot token: #{token}" +puts "Start time: " + timestamp + "\n\n\n\n" +STDOUT.flush #def process_command_srvstart(message) # puts "Received command: srvstart" @@ -123,7 +131,7 @@ def handle_message(message) #conf = YAML.load_file("bot_config.yaml") telnet = @conf['telnet'] adm = @conf['admin'] - puts "Received command from " + "#{message.from.username}".cyan.bold + ": #{command}" + puts Time.now.strftime("%F %H:%M:%S").yellow + ": Received command from " + "#{message.from.username}".cyan.bold + " [" + "#{message.from.id}".cyan + "]: " + "#{command}".magenta.bold case command when '/start' reply = "Introduction: I am #{@botname}, and I am here to make life easier for meatbag admins like you. Currently I can retrieve information " + @@ -131,13 +139,19 @@ def handle_message(message) "Commands available:\n/start (Shows this message)\n/srvstart (Currently non-functional)\n/srvstop\n/status\n/whoami or /chatinfo\n/whereareyou or /location\n\n" + "Check again later to see if any new functions have been added, or use /patchnotes to learn about recent updates.\n" + "You can also view the source code at the following location:\nhttps://git.skyfall.tech/skyfall/empyrion-bot" - if ! message_from_admin?(message, adm) && ! is_chat_authorized?(message, @auth_chat) + if message_from_admin?(message, adm) + msg_from_admin = true + end + if is_chat_authorized?(message, @auth_chat) + chat_authorized = true + end + if ! msg_from_admin && ! chat_authorized if message.from.id == message.chat.id reply = reply + "\n\nWARNING: I am not authorized to work with you directly. My functionality is limited." else reply = reply + "\n\nWARNING: I am not authorized to participate with this group. My functionality is limited." end - elsif message_from_admin?(message, adm) && ! is_chat_authorized?(message, @auth_chat) + elsif msg_from_admin && ! chat_authorized reply = reply + "\n\nWARNING: Although you are my master, I have not been authorized to participate in this group. My functionality is limited." end when '/srvstart' @@ -179,7 +193,8 @@ def handle_message(message) reply = "Mockery: My name is #{message.from.username}, I am a meatbag, and I think #{command} is a valid command." end # Verbose output: - puts "Sending #{reply.inspect}\n\n" + puts timestamp + ": Sending #{reply.inspect}\n\n" + STDOUT.flush #puts "End of case" return reply end