Teamspeak2.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * This file is part of GameQ.
  4. *
  5. * GameQ is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GameQ is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. namespace GameQ\Protocols;
  19. use GameQ\Protocol;
  20. use GameQ\Buffer;
  21. use GameQ\Result;
  22. use GameQ\Server;
  23. use GameQ\Exception\Protocol as Exception;
  24. /**
  25. * Teamspeak 2 Protocol Class
  26. *
  27. * All values are utf8 encoded upon processing
  28. *
  29. * This code ported from GameQ v1/v2. Credit to original author(s) as I just updated it to
  30. * work within this new system.
  31. *
  32. * @author Austin Bischoff <[email protected]>
  33. */
  34. class Teamspeak2 extends Protocol
  35. {
  36. /**
  37. * Array of packets we want to look up.
  38. * Each key should correspond to a defined method in this or a parent class
  39. *
  40. * @type array
  41. */
  42. protected $packets = [
  43. self::PACKET_DETAILS => "sel %d\x0asi\x0a",
  44. self::PACKET_CHANNELS => "sel %d\x0acl\x0a",
  45. self::PACKET_PLAYERS => "sel %d\x0apl\x0a",
  46. ];
  47. /**
  48. * The transport mode for this protocol is TCP
  49. *
  50. * @type string
  51. */
  52. protected $transport = self::TRANSPORT_TCP;
  53. /**
  54. * The query protocol used to make the call
  55. *
  56. * @type string
  57. */
  58. protected $protocol = 'teamspeak2';
  59. /**
  60. * String name of this protocol class
  61. *
  62. * @type string
  63. */
  64. protected $name = 'teamspeak2';
  65. /**
  66. * Longer string name of this protocol class
  67. *
  68. * @type string
  69. */
  70. protected $name_long = "Teamspeak 2";
  71. /**
  72. * The client join link
  73. *
  74. * @type string
  75. */
  76. protected $join_link = "teamspeak://%s:%d/";
  77. /**
  78. * Normalize settings for this protocol
  79. *
  80. * @type array
  81. */
  82. protected $normalize = [
  83. // General
  84. 'general' => [
  85. 'dedicated' => 'dedicated',
  86. 'hostname' => 'server_name',
  87. 'password' => 'server_password',
  88. 'numplayers' => 'server_currentusers',
  89. 'maxplayers' => 'server_maxusers',
  90. ],
  91. // Player
  92. 'player' => [
  93. 'id' => 'p_id',
  94. 'team' => 'c_id',
  95. 'name' => 'nick',
  96. ],
  97. // Team
  98. 'team' => [
  99. 'id' => 'id',
  100. 'name' => 'name',
  101. ],
  102. ];
  103. /**
  104. * Before we send off the queries we need to update the packets
  105. *
  106. * @param \GameQ\Server $server
  107. *
  108. * @throws \GameQ\Exception\Protocol
  109. */
  110. public function beforeSend(Server $server)
  111. {
  112. // Check to make sure we have a query_port because it is required
  113. if (!isset($this->options[Server::SERVER_OPTIONS_QUERY_PORT])
  114. || empty($this->options[Server::SERVER_OPTIONS_QUERY_PORT])
  115. ) {
  116. throw new Exception(__METHOD__ . " Missing required setting '" . Server::SERVER_OPTIONS_QUERY_PORT . "'.");
  117. }
  118. // Let's loop the packets and set the proper pieces
  119. foreach ($this->packets as $packet_type => $packet) {
  120. // Update with the client port for the server
  121. $this->packets[$packet_type] = sprintf($packet, $server->portClient());
  122. }
  123. }
  124. /**
  125. * Process the response
  126. *
  127. * @return array
  128. * @throws \GameQ\Exception\Protocol
  129. */
  130. public function processResponse()
  131. {
  132. // Make a new buffer out of all of the packets
  133. $buffer = new Buffer(implode('', $this->packets_response));
  134. // Check the header [TS]
  135. if (($header = trim($buffer->readString("\n"))) !== '[TS]') {
  136. throw new Exception(__METHOD__ . " Expected header '{$header}' does not match expected '[TS]'.");
  137. }
  138. // Split this buffer as the data blocks are bound by "OK" and drop any empty values
  139. $sections = array_filter(explode("OK", $buffer->getBuffer()), function ($value) {
  140. $value = trim($value);
  141. return !empty($value);
  142. });
  143. // Trim up the values to remove extra whitespace
  144. $sections = array_map('trim', $sections);
  145. // Set the result to a new result instance
  146. $result = new Result();
  147. // Now we need to iterate over the sections and off load the processing
  148. foreach ($sections as $section) {
  149. // Grab a snip of the data so we can figure out what it is
  150. $check = substr($section, 0, 7);
  151. // Offload to the proper method
  152. if ($check == 'server_') {
  153. // Server settings and info
  154. $this->processDetails($section, $result);
  155. } elseif ($check == "id\tcode") {
  156. // Channel info
  157. $this->processChannels($section, $result);
  158. } elseif ($check == "p_id\tc_") {
  159. // Player info
  160. $this->processPlayers($section, $result);
  161. }
  162. }
  163. unset($buffer, $sections, $section, $check);
  164. return $result->fetch();
  165. }
  166. /*
  167. * Internal methods
  168. */
  169. /**
  170. * Handles processing the details data into a usable format
  171. *
  172. * @param string $data
  173. * @param \GameQ\Result $result
  174. */
  175. protected function processDetails($data, Result &$result)
  176. {
  177. // Create a buffer
  178. $buffer = new Buffer($data);
  179. // Always dedicated
  180. $result->add('dedicated', 1);
  181. // Let's loop until we run out of data
  182. while ($buffer->getLength()) {
  183. // Grab the row, which is an item
  184. $row = trim($buffer->readString("\n"));
  185. // Split out the information
  186. list($key, $value) = explode('=', $row, 2);
  187. // Add this to the result
  188. $result->add($key, utf8_encode($value));
  189. }
  190. unset($data, $buffer, $row, $key, $value);
  191. }
  192. /**
  193. * Process the channel listing
  194. *
  195. * @param string $data
  196. * @param \GameQ\Result $result
  197. */
  198. protected function processChannels($data, Result &$result)
  199. {
  200. // Create a buffer
  201. $buffer = new Buffer($data);
  202. // The first line holds the column names, data returned is in column/row format
  203. $columns = explode("\t", trim($buffer->readString("\n")), 9);
  204. // Loop through the rows until we run out of information
  205. while ($buffer->getLength()) {
  206. // Grab the row, which is a tabbed list of items
  207. $row = trim($buffer->readString("\n"));
  208. // Explode and merge the data with the columns, then parse
  209. $data = array_combine($columns, explode("\t", $row, 9));
  210. foreach ($data as $key => $value) {
  211. // Now add the data to the result
  212. $result->addTeam($key, utf8_encode($value));
  213. }
  214. }
  215. unset($data, $buffer, $row, $columns, $key, $value);
  216. }
  217. /**
  218. * Process the user listing
  219. *
  220. * @param string $data
  221. * @param \GameQ\Result $result
  222. */
  223. protected function processPlayers($data, Result &$result)
  224. {
  225. // Create a buffer
  226. $buffer = new Buffer($data);
  227. // The first line holds the column names, data returned is in column/row format
  228. $columns = explode("\t", trim($buffer->readString("\n")), 16);
  229. // Loop through the rows until we run out of information
  230. while ($buffer->getLength()) {
  231. // Grab the row, which is a tabbed list of items
  232. $row = trim($buffer->readString("\n"));
  233. // Explode and merge the data with the columns, then parse
  234. $data = array_combine($columns, explode("\t", $row, 16));
  235. foreach ($data as $key => $value) {
  236. // Now add the data to the result
  237. $result->addPlayer($key, utf8_encode($value));
  238. }
  239. }
  240. unset($data, $buffer, $row, $columns, $key, $value);
  241. }
  242. }