diff --git a/bot_config.yaml.example b/bot_config.yaml.example index 3ee7ddd..883d036 100644 --- a/bot_config.yaml.example +++ b/bot_config.yaml.example @@ -3,4 +3,8 @@ telnet: host: '' port: '30004' pass: 'guest' +admin: + - 11111111 +authorized_chats: + - 11111111 diff --git a/commands.rb b/commands.rb new file mode 100644 index 0000000..7183c16 --- /dev/null +++ b/commands.rb @@ -0,0 +1,65 @@ +def message_from_admin?(message, adm) + #return @admin_userids.include? message.from.id.to_s + #puts adm.class + #puts message.from.id + #puts adm + if adm.include?(message.from.id) + puts "Command is from an admin. [#{message.from.username}]" + return true + else + puts "Command is NOT from an admin! [#{message.from.username}]" + return false + end +end + +def is_chat_authorized?(message, auth_chat) + if auth_chat.include?(message.chat.id) + puts "Group [#{message.chat.id}][#{message.chat.title}] is authorized" + return true + else + puts "Group [#{message.chat.id}][#{message.chat.title}] is NOT authorized!" + return false + end +end + +def process_command_srvstart(message, command, adm) + puts "Received command: srvstart" + #pp message + if ! message_from_admin?(message, adm) + ####if ! message.from.username.nil? + #### message.from.username = "@" + message.from.username + ####elsif ! message.from.first_name.nil? + #### message.from.username = message.from.first_name + ####end + #return "#{message.from.username} is not an admin!" + return "Refusal: The meatbag #{message.from.username} is not my master." + elsif message_from_admin?(message, adm) && ! is_chat_authorized?(message, @auth_chat) + return "Refusal: Although I respect your wishes, master, I am not authorized to perform this function for this group." + else + telnet = @conf['telnet'] + puts `./srvstart #{telnet['host']}` + #return "Starting up the Empyrion service." + return "This function is currently broken. An attempt was made, but odds are against the server having actually started.\n\n" + + "Use /status for more info." + end +end + +def process_command_srvstop(message, command, adm) + puts "Received command: srvstop" + if ! message_from_admin?(message, adm) + ####if ! message.from.username.nil? + #### message.from.username = "@" + message.from.username + ####elsif ! message.from.first_name.nil? + #### message.from.username = message.from.first_name + ####end + #return "#{message.from.username} is not an admin!" + return "Refusal: The meatbag #{message.from.username} is not my master." + elsif message_from_admin?(message, adm) && ! is_chat_authorized?(message, @auth_chat) + return "Refusal: Although I respect your wishes, master, I am not authorized to perform this function for this group." + else + telnet = @conf['telnet'] + `./srvstop #{telnet['host']} #{telnet['port']} #{telnet['pass']}` + return "Affirmation: I am shutting down the Empyrion service." + end +end + diff --git a/empyrion-bot.rb b/empyrion-bot.rb index c22e1db..3ac85e2 100755 --- a/empyrion-bot.rb +++ b/empyrion-bot.rb @@ -3,44 +3,142 @@ require 'rubygems' require 'yaml' require 'telegram/bot' +require 'pp' +require_relative 'commands.rb' #conf = YAML.load(File.read("bot_config.yaml")) -conf = YAML.load_file("bot_config.yaml") -token = conf['token'] -telnet = conf['telnet'] -puts conf['telnet'] +@conf = YAML.load_file("bot_config.yaml") +@botname = @conf['botname'] +token = @conf['token'] +telnet = @conf['telnet'] +admin = @conf['admin'] +@auth_chat = @conf['authorized_chats'] + +# Sanity check +errcount = 0 +puts "Checking if environment is sane...\n\n" +if token.nil? + puts "No bot token defined in bot_config.yaml!\nTHIS IS REQUIRED! Bot initialization failed; exiting..." + exit(1) +end +if @botname.nil? + errcount += 1 + puts "Error(#{errcount.to_s}): No bot name defined. This is superficial. We'll call him Bob.\n\n" + @botname = "Bob" +end +if telnet.nil? + errcount += 1 + puts "Error(#{errcount.to_s}): No telnet information provided in bot_config.yaml.\nThis is required for nearly all Empyrion-related " + + "functions.\nTHIS SHOULD BE ADDRESSED. Continuing. (some commands will return broken messages)\n\n" +end +if admin.nil? + errcount += 1 + puts "Error(#{errcount.to_s}): No admin Telegram IDs provided in bot_config.yaml.\nThis is required for many functions.\n" + + "THIS SHOULD BE ADDRESSED. Continuing. (some commands will not be available)\n\n" + admin = ["0"] +end +if @auth_chat.nil? + errcount += 1 + puts "Error(#{errcount.to_s}): No authorized Telegram group IDs provided in bot_config.yaml.\nThis is required for most Empyrion-related " + + "functions.\nTHIS SHOULD BE ADDRESSED. Continuing. (some commands will not be available)\n\n" + @auth_chat = ["0"] +end +puts "Errors found: #{errcount.to_s}\n\n" + +puts "Starting [#{@botname}]...\n\n" puts "Empyrion Host: #{telnet['host']}" -puts "Bot token: #{token}" +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" + +#def process_command_srvstart(message) +# puts "Received command: srvstart" +# reply = `./srvstart` +#end +# +#def process_command_srvstop(message) +# puts "Received command: srvstop" +# reply = `./srvstop` +#end def handle_message(message) - reply = 'Empty String' - conf = YAML.load_file("bot_config.yaml") - telnet = conf['telnet'] - case message.text.split(" ")[0].split("@")[0].downcase #Strip command from arguments and @tags - when '/start' - puts "Received command: start" - reply = "This bot will eventually (hopefully) be able retrieve information and pass start/stop commands from/to an Empyrion server, to make life easier for meatbags like you.\n\n" + - "Commands available:\n/start (Shows this message)\n/srvstart\n/srvstop\n/status" - when '/srvstart' - puts "Received command: srvstart" - reply = `./srvstart` - when '/srvstop' - puts "Received command: srvstop" - reply = `./srvstop` - when '/status' - puts "Received command: status" - reply = `./srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` - when '/location', '/whereareyou' - reply = "I am currently located at:\n\nHost: #{`echo $HOSTNAME`}ExtIP: #{`curl myip.contegix.com`}" - end - puts "Sending #{reply.inspect}" - return reply + #Format sender name + if ! message.from.username.nil? + message.from.username = "@" + message.from.username + elsif ! message.from.first_name.nil? + message.from.username = message.from.first_name + end + + #Format command + command = message.text.split(" ")[0].split("@")[0].downcase #Strip command from arguments and @tags + + reply = 'Empty String' + #conf = YAML.load_file("bot_config.yaml") + telnet = @conf['telnet'] + adm = @conf['admin'] + #case message.text.split(" ")[0].split("@")[0].downcase #Strip command from arguments and @tags + puts "Received command from #{message.from.username}: #{command}" + 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 " + + "from an Empyrion server as well as kill the server process.\n\n" + + "Commands available:\n/start (Shows this message)\n/srvstart (Probably won't work)\n/srvstop\n/status\n/whoami\n\n" + + "Check again later to see if any new functions have been added." + if ! message_from_admin?(message, adm) && ! is_chat_authorized?(message, @auth_chat) + if message.from.id == message.chat.id + reply = reply + "\n\nWARNING: I am not authorized to work 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) + 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' + if message_from_admin?(message, adm) || is_chat_authorized?(message, @auth_chat) + reply = process_command_srvstart(message, command, adm) + else + reply = "Refusal: I am not authorized to perform this operation here." + end + when '/srvstop' + if message_from_admin?(message, adm) || is_chat_authorized?(message, @auth_chat) + reply = process_command_srvstop(message, command, adm) + else + reply = "Refusal: I am not authorized to perform this operation here." + end + when '/status' + if message_from_admin?(message, adm) || is_chat_authorized?(message, @auth_chat) + reply = `./srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` + else + reply = "Refusal: I am not authorized to provide this information here." + end + when '/location', '/whereareyou' + if message_from_admin?(message, adm) || is_chat_authorized?(message, @auth_chat) + reply = "I am currently located at:\n\nHost: #{`echo $HOSTNAME`}ExtIP: #{`curl myip.contegix.com 2>/dev/null`}" + else + reply = "Refusal: I am not authorized to provide this information here." + end + when '/whoami', '/chatinfo' + #reply = "Answer: You are a meatbag named #{message.from.username}\n\nUser ID: #{message.from.id}\n\nChat ID: #{message.chat.id}" + reply = "User ID: #{message.from.id}\nChat ID: #{message.chat.id}" + if command == '/whoami' + reply = "Answer: You are a meatbag named #{message.from.username}\n\n" + reply + end + when '/pp', '/debug' + pp message + reply = "Confirmation: Message debug information sent to console." + else + reply = "Mockery: My name is #{message.from.username}, I am a meatbag, and I think #{command} is a valid command." + end + puts "Sending #{reply.inspect}\n\n" + #puts "End of case" + return reply end Telegram::Bot::Client.run(token) do |bot| - bot.listen do |message| - reply = handle_message message - bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") - end + bot.listen do |message| + reply = handle_message(message) + bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") + end end diff --git a/srvstop b/srvstop index 224ef68..a23ac36 100755 --- a/srvstop +++ b/srvstop @@ -9,7 +9,8 @@ spawn telnet $host $port expect "password:" send "$pass\r" expect "Logged in successfully" -send "say 'This is a test (shutdown command) (no, it is not actually shutting down, hence the test).'\r" +#send "say 'This is a test (shutdown command) (no, it is not actually shutting down, hence the test).'\r" +send "saveandexit 0\r" expect eof