From b78a950dda1fa300f09241a5ff49ceaa644aef2a Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 19 Dec 2018 01:58:52 -0600 Subject: [PATCH 01/15] Added YAML config and basic response abilities --- .gitignore | 2 ++ bot_config.yaml.example | 2 ++ empyrion-bot.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 .gitignore create mode 100644 bot_config.yaml.example create mode 100755 empyrion-bot.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4129f9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bot_config.yaml + diff --git a/bot_config.yaml.example b/bot_config.yaml.example new file mode 100644 index 0000000..4b24371 --- /dev/null +++ b/bot_config.yaml.example @@ -0,0 +1,2 @@ +token: '' + diff --git a/empyrion-bot.rb b/empyrion-bot.rb new file mode 100755 index 0000000..491ddbf --- /dev/null +++ b/empyrion-bot.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby + +require 'rubygems' +require 'yaml' +require 'telegram/bot' + +conf = YAML.load(File.read("bot_config.yaml")) +token = conf['token'] + +puts 'Bot token: ' + token + +Telegram::Bot::Client.run(token) do |bot| + bot.listen do |message| + case message.text + when '/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." + puts "Sending #{reply.inspect}" + bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") + when '/srv-start' + reply = "UNAVAILABLE: This command would start the game server if the code were there." + puts "Sending #{reply.inspect}" + bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") + when '/srv-stop' + reply = "UNAVAILABLE: This command would stop the game server if the code were there." + puts "Sending #{reply.inspect}" + bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") + when '/status' + reply = "UNAVAILABLE: This command would provide server status and possibly version... If the code were there." + puts "Sending #{reply.inspect}" + bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") + end + end +end + From 753e6f09c65721004834635d83dbae1db4c29d16 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Wed, 19 Dec 2018 02:09:24 -0600 Subject: [PATCH 02/15] Added command list to /start --- empyrion-bot.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/empyrion-bot.rb b/empyrion-bot.rb index 491ddbf..46b7bfd 100755 --- a/empyrion-bot.rb +++ b/empyrion-bot.rb @@ -13,14 +13,15 @@ Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| case message.text when '/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." + 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\n/srvstart\n/srvstop\n/status" puts "Sending #{reply.inspect}" bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") - when '/srv-start' + when '/srvstart' reply = "UNAVAILABLE: This command would start the game server if the code were there." puts "Sending #{reply.inspect}" bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") - when '/srv-stop' + when '/srvstop' reply = "UNAVAILABLE: This command would stop the game server if the code were there." puts "Sending #{reply.inspect}" bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") From a4452e3272d5eb26a0cfc6904b421b6764cb82e0 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Mon, 24 Dec 2018 12:09:17 -0600 Subject: [PATCH 03/15] Modularization --- empyrion-bot.rb | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/empyrion-bot.rb b/empyrion-bot.rb index 46b7bfd..4d47ad9 100755 --- a/empyrion-bot.rb +++ b/empyrion-bot.rb @@ -9,27 +9,29 @@ token = conf['token'] puts 'Bot token: ' + token +def parse_message(message) + reply = 'Empty String' + case message.text + when '/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\n/address" + when '/srvstart' + reply = "UNAVAILABLE: This command would start the game server if the code were there." + when '/srvstop' + reply = "UNAVAILABLE: This command would stop the game server if the code were there." + when '/status' + reply = "UNAVAILABLE: This command would provide server status and possibly version... If the code were there." + when '/location', '/ip', '/address' + reply = `curl myip.contegix.com` + end + puts "Sending #{reply.inspect}" + return reply +end + Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| - case message.text - when '/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\n/srvstart\n/srvstop\n/status" - puts "Sending #{reply.inspect}" - bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") - when '/srvstart' - reply = "UNAVAILABLE: This command would start the game server if the code were there." - puts "Sending #{reply.inspect}" - bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") - when '/srvstop' - reply = "UNAVAILABLE: This command would stop the game server if the code were there." - puts "Sending #{reply.inspect}" - bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") - when '/status' - reply = "UNAVAILABLE: This command would provide server status and possibly version... If the code were there." - puts "Sending #{reply.inspect}" - bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") - end + reply = parse_message message + bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") end end From e31dc7ab85dffb58e13d193a35dcc7b84533bea7 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Tue, 25 Dec 2018 01:55:16 -0600 Subject: [PATCH 04/15] Added telnet settings --- bot_config.yaml.example | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bot_config.yaml.example b/bot_config.yaml.example index 4b24371..3ee7ddd 100644 --- a/bot_config.yaml.example +++ b/bot_config.yaml.example @@ -1,2 +1,6 @@ token: '' +telnet: + host: '' + port: '30004' + pass: 'guest' From af66d5ff7b6303cdfb32912ec7b9c399e6dcdb3a Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Tue, 25 Dec 2018 01:55:47 -0600 Subject: [PATCH 05/15] Added tons of functionality --- empyrion-bot.rb | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/empyrion-bot.rb b/empyrion-bot.rb index 4d47ad9..770e8dd 100755 --- a/empyrion-bot.rb +++ b/empyrion-bot.rb @@ -4,23 +4,28 @@ require 'rubygems' require 'yaml' require 'telegram/bot' -conf = YAML.load(File.read("bot_config.yaml")) +#conf = YAML.load(File.read("bot_config.yaml")) +conf = YAML.load_file("bot_config.yaml") token = conf['token'] +telnet = conf['telnet'] +puts conf['telnet'] +puts "Empyrion Host: #{telnet['host']}" +puts "Bot token: #{token}" -puts 'Bot token: ' + token - -def parse_message(message) +def handle_message(message) reply = 'Empty String' - case message.text + 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' 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\n/address" when '/srvstart' - reply = "UNAVAILABLE: This command would start the game server if the code were there." + reply = `./srvstart` when '/srvstop' - reply = "UNAVAILABLE: This command would stop the game server if the code were there." + reply = `./srvstop` when '/status' - reply = "UNAVAILABLE: This command would provide server status and possibly version... If the code were there." + reply = `./srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` when '/location', '/ip', '/address' reply = `curl myip.contegix.com` end @@ -30,7 +35,7 @@ end Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| - reply = parse_message message + reply = handle_message message bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") end end From 047ae541769f2fac21ee5b55cfb14fc4b02f86b2 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Tue, 25 Dec 2018 01:56:27 -0600 Subject: [PATCH 06/15] Added function scripts --- srvstart | 2 ++ srvstatus | 18 ++++++++++++++++++ srvstatus.expect | 14 ++++++++++++++ srvstop | 28 ++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100755 srvstart create mode 100755 srvstatus create mode 100755 srvstatus.expect create mode 100755 srvstop diff --git a/srvstart b/srvstart new file mode 100755 index 0000000..db1e43d --- /dev/null +++ b/srvstart @@ -0,0 +1,2 @@ +ssh ajohnson@tartarus srvstart.bat + diff --git a/srvstatus b/srvstatus new file mode 100755 index 0000000..3c4df63 --- /dev/null +++ b/srvstatus @@ -0,0 +1,18 @@ +#!/usr/bin/env sh + +host=$1 +port=$2 +pass=$3 + +if nc -zvw3 $host $port 2>/dev/null; then + ./srvstatus.expect $host $port $pass > /tmp/.stat + grep -B1 -A8 Empyrion\ dedicated\ server /tmp/.stat | egrep -v Playfield\|Game\ seed + echo "\nOnline players:\n- - - - - - - - - - - - - -" + sed -n '/Global\ online/,/Global\ players/p' /tmp/.stat | awk -F\= '{print $3}' | sed 's/....$//' + rm /tmp/.stat + exit 0 +else + echo "Server is DOWN!" + exit 1 +fi + diff --git a/srvstatus.expect b/srvstatus.expect new file mode 100755 index 0000000..db9b981 --- /dev/null +++ b/srvstatus.expect @@ -0,0 +1,14 @@ +#!/usr/bin/expect + +set timeout 10 +set host [lindex $argv 0] +set port [lindex $argv 1] +set pass [lindex $argv 2] + +spawn telnet $host $port +expect "password:" +send "$pass\r" +expect "Logged in successfully" +send "plys\r" +expect eof + diff --git a/srvstop b/srvstop new file mode 100755 index 0000000..224ef68 --- /dev/null +++ b/srvstop @@ -0,0 +1,28 @@ +#!/usr/bin/expect + +set timeout 10 +set host [lindex $argv 0] +set port [lindex $argv 1] +set pass [lindex $argv 2] + +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" +expect eof + + +##!/usr/bin/env sh +#host=$1 +#port=$2 +#pass=$3 +# +#telnet $host $port < Date: Tue, 25 Dec 2018 15:13:39 -0600 Subject: [PATCH 07/15] Added /whereareyou and a couple other small alterations --- empyrion-bot.rb | 10 +++++++--- srvstatus | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/empyrion-bot.rb b/empyrion-bot.rb index 770e8dd..c22e1db 100755 --- a/empyrion-bot.rb +++ b/empyrion-bot.rb @@ -18,16 +18,20 @@ def handle_message(message) 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\n/address" + "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', '/ip', '/address' - reply = `curl myip.contegix.com` + 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 diff --git a/srvstatus b/srvstatus index 3c4df63..5642308 100755 --- a/srvstatus +++ b/srvstatus @@ -7,12 +7,12 @@ pass=$3 if nc -zvw3 $host $port 2>/dev/null; then ./srvstatus.expect $host $port $pass > /tmp/.stat grep -B1 -A8 Empyrion\ dedicated\ server /tmp/.stat | egrep -v Playfield\|Game\ seed - echo "\nOnline players:\n- - - - - - - - - - - - - -" + printf "\nOnline players:\n- - - - - - - - - - - - -" sed -n '/Global\ online/,/Global\ players/p' /tmp/.stat | awk -F\= '{print $3}' | sed 's/....$//' rm /tmp/.stat exit 0 else - echo "Server is DOWN!" + printf "Server is DOWN!" exit 1 fi From 4c03a0952b4ad84e43189ac3307efef8c7ce5f38 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 01:12:56 -0600 Subject: [PATCH 08/15] Attempt to implement srvstart (needs work) --- srvstart | 16 +++++++++++++++- srvstart.expect | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100755 srvstart.expect diff --git a/srvstart b/srvstart index db1e43d..61fe211 100755 --- a/srvstart +++ b/srvstart @@ -1,2 +1,16 @@ -ssh ajohnson@tartarus srvstart.bat +#!/usr/bin/env sh +#ssh ajohnson@tartarus srvstart.bat +#ssh ajohnson@tartarus 'X:\server\alpha9\start-nova9.cmd' +ssh ajohnson@tartarus '.\srvstart.bat' +#echo "Yay you did it" + +#set timeout 10 +#set host [lindex $argv 0] +# +#spawn ssh ajohnson@${host} +#expect "Microsoft Windows" +#send ".\\srvstart.bat\r" +##expect "Logged in successfully" +##send "saveandexit 0\r" +#expect eof diff --git a/srvstart.expect b/srvstart.expect new file mode 100755 index 0000000..0db1955 --- /dev/null +++ b/srvstart.expect @@ -0,0 +1,15 @@ +#!/usr/bin/expect +#ssh ajohnson@tartarus srvstart.bat +#ssh ajohnson@tartarus "X:\server\alpha9\start-nova9.cmd" +#echo "Yay you did it" + +set timeout 10 +set host [lindex $argv 0] + +spawn ssh ajohnson@${host} +expect "Microsoft Windows" +send ".\\srvstart.bat\r" +#expect "Logged in successfully" +#send "saveandexit 0\r" +expect eof + From dd98e97d1b0f7829fd47e6b85261b8e7e59de4f4 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 01:13:34 -0600 Subject: [PATCH 09/15] Added majority of functionality --- bot_config.yaml.example | 4 + commands.rb | 65 ++++++++++++++++ empyrion-bot.rb | 160 ++++++++++++++++++++++++++++++++-------- srvstop | 3 +- 4 files changed, 200 insertions(+), 32 deletions(-) create mode 100644 commands.rb 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 From a7a88a7886b2051397289673c182e042e24b02e6 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 01:28:49 -0600 Subject: [PATCH 10/15] Renamed main executable and organized scripts into a directory --- commands.rb | 4 ++-- empyrion-bot.rb => run.rb | 2 +- srvstart => scripts/srvstart | 0 srvstart.expect => scripts/srvstart.expect | 0 srvstatus => scripts/srvstatus | 0 srvstatus.expect => scripts/srvstatus.expect | 0 srvstop => scripts/srvstop | 0 7 files changed, 3 insertions(+), 3 deletions(-) rename empyrion-bot.rb => run.rb (98%) rename srvstart => scripts/srvstart (100%) rename srvstart.expect => scripts/srvstart.expect (100%) rename srvstatus => scripts/srvstatus (100%) rename srvstatus.expect => scripts/srvstatus.expect (100%) rename srvstop => scripts/srvstop (100%) diff --git a/commands.rb b/commands.rb index 7183c16..c1cb47f 100644 --- a/commands.rb +++ b/commands.rb @@ -37,7 +37,7 @@ def process_command_srvstart(message, command, adm) 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']}` + puts `./scripts/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." @@ -58,7 +58,7 @@ def process_command_srvstop(message, command, adm) 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']}` + `./scripts/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/run.rb similarity index 98% rename from empyrion-bot.rb rename to run.rb index 3ac85e2..ed0d4b8 100755 --- a/empyrion-bot.rb +++ b/run.rb @@ -108,7 +108,7 @@ def handle_message(message) end when '/status' if message_from_admin?(message, adm) || is_chat_authorized?(message, @auth_chat) - reply = `./srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` + reply = `./scripts/srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` else reply = "Refusal: I am not authorized to provide this information here." end diff --git a/srvstart b/scripts/srvstart similarity index 100% rename from srvstart rename to scripts/srvstart diff --git a/srvstart.expect b/scripts/srvstart.expect similarity index 100% rename from srvstart.expect rename to scripts/srvstart.expect diff --git a/srvstatus b/scripts/srvstatus similarity index 100% rename from srvstatus rename to scripts/srvstatus diff --git a/srvstatus.expect b/scripts/srvstatus.expect similarity index 100% rename from srvstatus.expect rename to scripts/srvstatus.expect diff --git a/srvstop b/scripts/srvstop similarity index 100% rename from srvstop rename to scripts/srvstop From f6e775a7593a8769517fa2c132f65a9df1463a8c Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 21:30:05 -0600 Subject: [PATCH 11/15] optimized srvstart/srvstop permission checks --- commands.rb | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/commands.rb b/commands.rb index c1cb47f..918a9cc 100644 --- a/commands.rb +++ b/commands.rb @@ -25,15 +25,13 @@ 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!" + from_admin = message_from_admin?(message, adm) + auth_grp = is_chat_authorized?(message, @auth_chat) + if ! auth_grp && ! from_admin + return "Refusal: I am not authorized to perform this function for this group, meatbag." + elsif ! from_admin return "Refusal: The meatbag #{message.from.username} is not my master." - elsif message_from_admin?(message, adm) && ! is_chat_authorized?(message, @auth_chat) + elsif ! auth_grp return "Refusal: Although I respect your wishes, master, I am not authorized to perform this function for this group." else telnet = @conf['telnet'] @@ -46,15 +44,13 @@ 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!" + from_admin = message_from_admin?(message, adm) + auth_grp = is_chat_authorized?(message, @auth_chat) + if ! auth_grp && ! from_admin + return "Refusal: I am not authorized to perform this function for this group, meatbag." + elsif ! from_admin return "Refusal: The meatbag #{message.from.username} is not my master." - elsif message_from_admin?(message, adm) && ! is_chat_authorized?(message, @auth_chat) + elsif ! auth_grp return "Refusal: Although I respect your wishes, master, I am not authorized to perform this function for this group." else telnet = @conf['telnet'] From bc527dcb7854de08fc47d6825283c74d03a39aef Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 21:30:19 -0600 Subject: [PATCH 12/15] Added dance easter egg --- run.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/run.rb b/run.rb index ed0d4b8..8575dea 100755 --- a/run.rb +++ b/run.rb @@ -95,19 +95,11 @@ def handle_message(message) 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 + reply = process_command_srvstart(message, command, adm) 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 + reply = process_command_srvstop(message, command, adm) when '/status' - if message_from_admin?(message, adm) || is_chat_authorized?(message, @auth_chat) + if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) reply = `./scripts/srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` else reply = "Refusal: I am not authorized to provide this information here." @@ -127,6 +119,12 @@ def handle_message(message) when '/pp', '/debug' pp message reply = "Confirmation: Message debug information sent to console." + when '/dance' + if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) + reply = "dance" + else + reply = "Refusal: I am not authorized to bust a move in this location." + end else reply = "Mockery: My name is #{message.from.username}, I am a meatbag, and I think #{command} is a valid command." end @@ -138,7 +136,11 @@ 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}") + if reply == "dance" + bot.api.sendVideo(chat_id: message.chat.id, video: 'https://img.skyfalltech.net/togra/dance.gif') + else + bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") + end end end From 1ba4deb867ad07b8ed3f5b2b2d0f9a76c592f031 Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 21:58:42 -0600 Subject: [PATCH 13/15] added flex easter egg --- run.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/run.rb b/run.rb index 8575dea..99a2ee7 100755 --- a/run.rb +++ b/run.rb @@ -99,6 +99,7 @@ def handle_message(message) when '/srvstop' reply = process_command_srvstop(message, command, adm) when '/status' + #reply = process_command_srvstatus(message, command, adm) if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) reply = `./scripts/srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` else @@ -125,6 +126,12 @@ def handle_message(message) else reply = "Refusal: I am not authorized to bust a move in this location." end + when '/flex' + if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) + reply = "flex" + else + reply = "Refusal: I am not authorized to bring the gun show to this location." + end else reply = "Mockery: My name is #{message.from.username}, I am a meatbag, and I think #{command} is a valid command." end @@ -136,8 +143,8 @@ end Telegram::Bot::Client.run(token) do |bot| bot.listen do |message| reply = handle_message(message) - if reply == "dance" - bot.api.sendVideo(chat_id: message.chat.id, video: 'https://img.skyfalltech.net/togra/dance.gif') + if reply == "dance" || reply == "flex" + bot.api.sendVideo(chat_id: message.chat.id, video: "https://img.skyfalltech.net/togra/#{reply}.gif") else bot.api.send_message(chat_id: message.chat.id, text: "#{reply}") end From fd134708fe447397a192e5f72284f5f831aa185c Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 22:06:12 -0600 Subject: [PATCH 14/15] fixed path for expect script --- scripts/srvstatus | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/srvstatus b/scripts/srvstatus index 5642308..b0fe0bf 100755 --- a/scripts/srvstatus +++ b/scripts/srvstatus @@ -5,7 +5,7 @@ port=$2 pass=$3 if nc -zvw3 $host $port 2>/dev/null; then - ./srvstatus.expect $host $port $pass > /tmp/.stat + ./scripts/srvstatus.expect $host $port $pass > /tmp/.stat grep -B1 -A8 Empyrion\ dedicated\ server /tmp/.stat | egrep -v Playfield\|Game\ seed printf "\nOnline players:\n- - - - - - - - - - - - -" sed -n '/Global\ online/,/Global\ players/p' /tmp/.stat | awk -F\= '{print $3}' | sed 's/....$//' From e366377175cddc22ec754d1c2d76eaf096adbc8d Mon Sep 17 00:00:00 2001 From: Aaron Johnson Date: Thu, 27 Dec 2018 22:06:35 -0600 Subject: [PATCH 15/15] moved status to commands.rb --- commands.rb | 10 ++++++++++ run.rb | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/commands.rb b/commands.rb index 918a9cc..ee7dfaf 100644 --- a/commands.rb +++ b/commands.rb @@ -59,3 +59,13 @@ def process_command_srvstop(message, command, adm) end end +def process_command_srvstatus(message, command, adm) + if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) + telnet = @conf['telnet'] + reply = `./scripts/srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` + else + reply = "Refusal: I am not authorized to provide this information here." + end + return reply +end + diff --git a/run.rb b/run.rb index 99a2ee7..a43ed15 100755 --- a/run.rb +++ b/run.rb @@ -99,12 +99,12 @@ def handle_message(message) when '/srvstop' reply = process_command_srvstop(message, command, adm) when '/status' - #reply = process_command_srvstatus(message, command, adm) - if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) - reply = `./scripts/srvstatus #{telnet['host']} #{telnet['port']} #{telnet['pass']}` - else - reply = "Refusal: I am not authorized to provide this information here." - end + reply = process_command_srvstatus(message, command, adm) + #if is_chat_authorized?(message, @auth_chat) || message_from_admin?(message, adm) + # reply = `./scripts/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`}"