Class: TeamSpeak3::Server
- Inherits:
-
Object
- Object
- TeamSpeak3::Server
- Defined in:
- lib/server.rb
Instance Attribute Summary collapse
-
#active_server ⇒ Object
readonly
Returns the value of attribute active_server.
-
#ip_address ⇒ Object
readonly
Returns the value of attribute ip_address.
-
#query_account ⇒ Object
readonly
Returns the value of attribute query_account.
-
#query_port ⇒ Object
readonly
Returns the value of attribute query_port.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #connect ⇒ Object
- #create(params = {}) ⇒ Object
- #execute(command, params = {}) ⇒ Object
-
#initialize(ip_address, query_port, opts = {}) ⇒ Server
constructor
A new instance of Server.
- #kick_client!(client_id, action, reason = nil) ⇒ Object
- #login(query_user, query_pass) ⇒ Object
- #prepare_command(command, params = {}) ⇒ Object
- #select_server(virtual_server_id) ⇒ Object
- #send_message_to(target, message, target_type = :auto) ⇒ Object
- #virtual_servers ⇒ Object
Constructor Details
#initialize(ip_address, query_port, opts = {}) ⇒ Server
Returns a new instance of Server.
9 10 11 12 13 |
# File 'lib/server.rb', line 9 def initialize(ip_address, query_port, opts = {}) @ip_address = ip_address @query_port = query_port @opts = opts end |
Instance Attribute Details
#active_server ⇒ Object (readonly)
Returns the value of attribute active_server.
7 8 9 |
# File 'lib/server.rb', line 7 def active_server @active_server end |
#ip_address ⇒ Object (readonly)
Returns the value of attribute ip_address.
3 4 5 |
# File 'lib/server.rb', line 3 def ip_address @ip_address end |
#query_account ⇒ Object (readonly)
Returns the value of attribute query_account.
4 5 6 |
# File 'lib/server.rb', line 4 def query_account @query_account end |
#query_port ⇒ Object (readonly)
Returns the value of attribute query_port.
5 6 7 |
# File 'lib/server.rb', line 5 def query_port @query_port end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
6 7 8 |
# File 'lib/server.rb', line 6 def socket @socket end |
Instance Method Details
#connect ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/server.rb', line 15 def connect begin @socket = Net::Telnet::new( 'Host' => @ip_address, 'Port' => @query_port, 'Timeout' => @opts[:timeout] || 3, 'Telnetmode' => false ) @socket.waitfor("Match" => /Welcome to the TeamSpeak 3 ServerQuery interface/, "FailEOF" => true) rescue Net::ReadTimeout raise TeamSpeak3::Exceptions::ServerConnectionFailed.new(@ip_address, @query_port, \ "Timeout while waiting for TeamSpeak 3 welcome message.") rescue Net::OpenTimeout, Errno::ECONNREFUSED raise TeamSpeak3::Exceptions::ServerConnectionFailed.new(@ip_address, @query_port, \ "Could not open connection to server at #{@ip_address}:#{@query_port}") rescue EOFError raise TeamSpeak3::Exceptions::ServerConnectionFailed.new(@ip_address, @query_port, \ "Server closed the connection.") end true end |
#create(params = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/server.rb', line 69 def create(params = {}) creation_params = {} creation_params[:virtualserver_name] = params[:name] if params[:name] creation_params[:virtualserver_port] = params[:port] if params[:port] creation_params[:virtualserver_maxclients] = params[:slots] if params[:slots] creation_params.merge!(params) begin response = execute :servercreate, creation_params { server_id: response[:data][0][:sid].to_i, token: response[:data][0][:token], port: response[:data][0][:virtualserver_port] } rescue Exceptions::CommandExecutionFailed => err raise Exceptions::MaxSlotLimitReached.new('Max slot limit has been reached') if err. =~ /max slot limit reached/ end end |
#execute(command, params = {}) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/server.rb', line 160 def execute(command, params = {}) allow_to_fail = params[:allow_to_fail] params.delete(:allow_to_fail) prepared_command = prepare_command(command, params) @socket.puts(prepared_command) # every response contains an error information. so we wait until we receive a response response = @socket.waitfor(/error id=.*/) response = TeamSpeak3::ServerResponse.parse(response) if response[:errors][:msg] != 'ok' && !allow_to_fail raise TeamSpeak3::Exceptions::CommandExecutionFailed.new( response[:errors][:id], response[:errors][:msg], prepared_command, ) end response end |
#kick_client!(client_id, action, reason = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/server.rb', line 86 def kick_client!(client_id, action, reason = nil) action_id = nil action_id = 4 if action == :channel action_id = 5 if action == :server raise TeamSpeak3::Exceptions::InvalidKickAction.new(action) unless action_id execute :clientkick, clid: client_id, reasonid: action_id, reasonmsg: reason end |
#login(query_user, query_pass) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/server.rb', line 39 def login(query_user, query_pass) verify_connection begin execute :login, client_login_name: query_user, client_login_password: query_pass rescue TeamSpeak3::Exceptions::CommandExecutionFailed => err raise TeamSpeak3::Exceptions::QueryLoginFailed, err. end @query_account = QueryAccount.new(self, query_user) true end |
#prepare_command(command, params = {}) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/server.rb', line 130 def prepare_command(command, params = {}) prepared_command = command.to_s if params[:options] raise ArgumentError, 'options must be an array!' unless params[:options].is_a?(Array) params[:options].each do |option| prepared_command += " -#{option}" end end params.each do |param, value| next if param == :options if value.is_a?(Array) prepared_command += ' ' value.each do |v| prepared_command += "#{param}=#{TeamSpeak3::CommandParameter.encode(v)}|" end prepared_command = prepared_command[0..-2] else prepared_command += " #{param}=#{TeamSpeak3::CommandParameter.encode(value)}" end end prepared_command end |
#select_server(virtual_server_id) ⇒ Object
53 54 55 56 |
# File 'lib/server.rb', line 53 def select_server(virtual_server_id) execute :use, sid: virtual_server_id @active_server = virtual_server_id end |
#send_message_to(target, message, target_type = :auto) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/server.rb', line 96 def (target, , target_type = :auto) if target_type == :auto if target.is_a?(TeamSpeak3::VirtualServer) target_id = target.id target_type_id = 3 select_server target.id elsif target.is_a?(TeamSpeak3::Channel) target_id = target.id target_type_id = 2 select_server target.virtual_server.id elsif target.is_a?(TeamSpeak3::Client) target_id = target.client_id target_type_id = 1 select_server target.virtual_server.id else raise TeamSpeak3::Exceptions::InvalidTarget.new(target) end else target_id = target target_type_id = 3 if target_type.to_sym == :server target_type_id = 2 if target_type.to_sym == :channel target_type_id = 1 if target_type.to_sym == :client raise TeamSpeak3::Exceptions::InvalidTargetType.new(target_type) unless target_type_id end execute :sendtextmessage, target: target_id, targetmode: target_type_id, msg: true end |
#virtual_servers ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/server.rb', line 58 def virtual_servers server_list = [] servers = execute :serverlist, options: [:uid, :all] servers[:data].each do |server| server_list << TeamSpeak3::VirtualServer.new(self, server) end server_list end |