/** * The Ts3Exception constructor. * * @param string $mesg * @param integer $code * @return Ts3Exception */ public function __construct($mesg, $code = 0x0) { parent::__construct($mesg, $code); if (array_key_exists((int) $code, self::$messages)) { $this->message = $this->prepareCustomMessage(self::$messages[intval($code)]); } Signal::getInstance()->emit("errorException", $this); }
/** * Connects the AbstractTransport object and performs initial actions on the remote * server. * * @return void */ public function syn() { if (!isset($this->options["host"]) || empty($this->options["host"])) { $this->options["host"] = $this->default_host; } if (!isset($this->options["port"]) || empty($this->options["port"])) { $this->options["port"] = $this->default_port; } $this->initTransport($this->options, "UDP"); $this->transport->setAdapter($this); Profiler::init(spl_object_hash($this)); Signal::getInstance()->emit("blacklistConnected", $this); }
/** * Creates a new Event object. * * @param StringHelper $evt * @param Host $con * @throws Ts3Exception * @return Event */ public function __construct(StringHelper $evt, Host $con = null) { if (!$evt->startsWith(TeamSpeak3::EVENT)) { throw new Ts3Exception("invalid notification event format"); } list($type, $data) = $evt->split(TeamSpeak3::SEPARATOR_CELL, 2); if (empty($data)) { throw new Ts3Exception("invalid notification event data"); } $fake = new StringHelper(TeamSpeak3::ERROR . TeamSpeak3::SEPARATOR_CELL . "id" . TeamSpeak3::SEPARATOR_PAIR . 0 . TeamSpeak3::SEPARATOR_CELL . "msg" . TeamSpeak3::SEPARATOR_PAIR . "ok"); $repl = new Reply(array($data, $fake), $type); $this->type = $type->substr(strlen(TeamSpeak3::EVENT)); $this->data = $repl->toList(); $this->mesg = $data; Signal::getInstance()->emit("notifyEvent", $this, $con); Signal::getInstance()->emit("notify" . ucfirst($this->type), $this, $con); }
/** * EventWorker constructor. * * @param $logger */ public function __construct($logger) { $cfg = new Config($logger); $this->config = $cfg->get(); $this->logger = $logger; $TsServerConfig = array($this->config['teamspeak']['user'], $this->config['teamspeak']['password'], $this->config['teamspeak']['host'], $this->config['teamspeak']['server_port']); $serverUri = vsprintf("serverquery://%s:%s@%s/?server_port=%d&blocking=0", $TsServerConfig); try { $this->ts3Server = TeamSpeak3::factory($serverUri); } catch (\Exception $e) { echo '[slack-teamspeak-integration] Fail to connect to the TeamSpeak server'; exit; } // Listen for user connection / disconnection from the server $this->ts3Server->notifyRegister(self::EVENT_CATEGORY); Signal::getInstance()->subscribe(self::EVENT_CONNECT, array($this, self::EVENT_CONNECT)); Signal::getInstance()->subscribe(self::EVENT_DISCONNECT, array($this, self::EVENT_DISCONNECT)); }
/** * Blocks a stream until data is available for reading if the stream is connected * in non-blocking mode. * * @param integer $time * @return void */ protected function waitForReadyRead($time = 0) { if (!$this->isConnected() || $this->config["blocking"]) { return; } do { $read = array($this->stream); $null = null; if ($time) { Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter()); } $time = $time + $this->config["timeout"]; } while (@stream_select($read, $null, $null, $this->config["timeout"]) == 0); }
/** * Writes data to the stream. * * @param string $data * @return void */ public function send($data) { $this->connect(); @stream_socket_sendto($this->stream, $data); Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data); }
/** * Returns the content of a downloaded file as a TeamSpeak3_Helper_String object. * * @param string $ftkey * @param integer $size * @param boolean $passthru * @throws TeamSpeak3_Adapter_FileTransfer_Exception * @return TeamSpeak3_Helper_String */ public function download($ftkey, $size, $passthru = FALSE) { $this->init($ftkey); if ($passthru) { return $this->passthru($size); } $buff = new \TeamSpeak3\Helper\String(""); $size = intval($size); $pack = 4096; \TeamSpeak3\Helper\Signal::getInstance()->emit("filetransferDownloadStarted", $ftkey, count($buff), $size); for ($seek = 0; $seek < $size;) { $rest = $size - $seek; $pack = $rest < $pack ? $rest : $pack; $data = $this->getTransport()->read($rest < $pack ? $rest : $pack); $seek = $seek + $pack; $buff->append($data); \TeamSpeak3\Helper\Signal::getInstance()->emit("filetransferDownloadProgress", $ftkey, count($buff), $size); } $this->getProfiler()->stop(); \TeamSpeak3\Helper\Signal::getInstance()->emit("filetransferDownloadFinished", $ftkey, count($buff), $size); if (strlen($buff) != $size) { throw new \TeamSpeak3\Adapter\FileTransfer\Exception("incomplete file download (" . count($buff) . " of " . $size . " bytes)"); } return $buff; }
/** * Queries the TSDNS server for a specified virtual hostname and returns the result. * * @param string $tsdns * @throws Ts3Exception * @return String */ public function resolve($tsdns) { $this->getTransport()->sendLine($tsdns); $repl = $this->getTransport()->readLine(); $this->getTransport()->disconnect(); if ($repl->section(":", 0)->toInt() == 404) { throw new Ts3Exception("unable to resolve TSDNS hostname (" . $tsdns . ")"); } Signal::getInstance()->emit("tsdnsResolved", $tsdns, $repl); return $repl; }
/** * Sends a prepared command to the server and returns the result. * * @param string $cmd * @param boolean $throw * @throws TeamSpeak3_Adapter_Exception * @return TeamSpeak3_Adapter_ServerQuery_Reply */ public function request($cmd, $throw = TRUE) { $query = \TeamSpeak3\Helper\String::factory($cmd)->section(TeamSpeak3::SEPARATOR_CELL); if (strstr($cmd, "\r") || strstr($cmd, "\n")) { throw new Exception("illegal characters in command '" . $query . "'"); } elseif (in_array($query, $this->block)) { throw new ServerQuery\Exception("command not found", 0x100); } \TeamSpeak3\Helper\Signal::getInstance()->emit("serverqueryCommandStarted", $cmd); $this->getProfiler()->start(); $this->getTransport()->sendLine($cmd); $this->timer = time(); $this->count++; $rpl = array(); do { $str = $this->getTransport()->readLine(); $rpl[] = $str; } while ($str instanceof \TeamSpeak3\Helper\String && $str->section(TeamSpeak3::SEPARATOR_CELL) != TeamSpeak3::ERROR); $this->getProfiler()->stop(); $reply = new \TeamSpeak3\Adapter\ServerQuery\Reply($rpl, $cmd, $this->getHost(), $throw); \TeamSpeak3\Helper\Signal::getInstance()->emit("serverqueryCommandFinished", $cmd, $reply); return $reply; }
/** * Parses a ServerQuery error and throws a TeamSpeak3_Adapter_ServerQuery_Exception object if * there's an error. * * @param string $err * @throws TeamSpeak3_Adapter_ServerQuery_Exception * @return void */ protected function fetchError($err) { $cells = $err->section(TeamSpeak3::SEPARATOR_CELL, 1, 3); foreach ($cells->split(TeamSpeak3::SEPARATOR_CELL) as $pair) { list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR); $this->err[$ident->toString()] = $value->isInt() ? $value->toInt() : $value->unescape(); } \TeamSpeak3\Helper\Signal::getInstance()->emit("notifyError", $this); if ($this->getErrorProperty("id", 0x0) != 0x0 && $this->exp) { if ($permid = $this->getErrorProperty("failed_permid")) { if ($permsid = key($this->con->request("permget permid=" . $permid, FALSE)->toAssocArray("permsid"))) { $suffix = " (failed on " . $permsid . ")"; } else { $suffix = " (failed on " . $this->cmd->section(TeamSpeak3::SEPARATOR_CELL) . " " . $permid . "/0x" . strtoupper(dechex($permid)) . ")"; } } elseif ($details = $this->getErrorProperty("extra_msg")) { $suffix = " (" . trim($details) . ")"; } else { $suffix = ""; } throw new Exception($this->getErrorProperty("msg") . $suffix, $this->getErrorProperty("id")); } }
/** * Connects the AbstractTransport object and performs initial actions on the remote * server. * * @throws Ts3Exception * @return void */ public function syn() { if (!isset($this->options["host"]) || empty($this->options["host"])) { $this->options["host"] = $this->default_host; } if (!isset($this->options["port"]) || empty($this->options["port"])) { $this->options["port"] = $this->default_port; } $this->initTransport($this->options, "UDP"); $this->transport->setAdapter($this); Profiler::init(spl_object_hash($this)); $this->getTransport()->send(HelperString::fromHex(33)); if (!preg_match_all("/,?(\\d+)#([0-9a-zA-Z\\._-]+),?/", $this->getTransport()->read(96), $matches) || !isset($matches[1]) || !isset($matches[2])) { throw new Ts3Exception("invalid reply from the server"); } $this->build_datetimes = $matches[1]; $this->version_strings = $matches[2]; Signal::getInstance()->emit("updateConnected", $this); }
/** * Deselects the active virtual server and logs out from the server instance. * * @return void */ public function logout() { $this->request("logout"); $this->whoamiReset(); $this->delStorage("_login_user"); $this->delStorage("_login_pass"); Signal::getInstance()->emit("notifyLogout", $this); }
/** * Deploys snapshot data on the selected virtual server. If no virtual server is selected (ID 0), * the data will be used to create a new virtual server from scratch. * * @param string $data * @param string $mode * @return array */ public function snapshotDeploy($data, $mode = TeamSpeak3::SNAPSHOT_STRING) { switch ($mode) { case TeamSpeak3::SNAPSHOT_BASE64: $data = \TeamSpeak3\Helper\String::fromBase64($data); break; case TeamSpeak3::SNAPSHOT_HEXDEC: $data = \TeamSpeak3\Helper\String::fromHex($data); break; default: $data = \TeamSpeak3\Helper\String::factory($data); break; } $detail = $this->request("serversnapshotdeploy " . $data)->toList(); if (array_key_exists("sid", $detail)) { \TeamSpeak3\Helper\Signal::getInstance()->emit("notifyServercreated", $this->getParent(), $detail["sid"]); } return $detail; }
/** * Creates a new privilege key (token) and returns the key. * * @param integer $type * @param integer $id1 * @param integer $id2 * @param string $description * @param string $customset * @return TeamSpeak3_Helper_String */ public function privilegeKeyCreate($type = TeamSpeak3::TOKEN_SERVERGROUP, $id1, $id2 = 0, $description = null, $customset = null) { $token = $this->execute("privilegekeyadd", array("tokentype" => $type, "tokenid1" => $id1, "tokenid2" => $id2, "tokendescription" => $description, "tokencustomset" => $customset))->toList(); Signal::getInstance()->emit("notifyTokencreated", $this, $token["token"]); return $token["token"]; }