Esempio n. 1
0
 /**
  * Triggered when a client sends data through the socket
  * @param  \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
  * @param  string $msg The message received
  * @throws \Exception
  */
 function onMessage(ConnectionInterface $from, $msg)
 {
     $json = json_decode($msg, true);
     Logger::debug("WS message received: {$msg}");
     switch ($json["type"]) {
         case "message":
             if ($this->clients[$from]->isAuthenticated()) {
                 if ($this->clients[$from]->isMemberOf($json["payload"]["channel"])) {
                     $this->sendMessageToChannel($json["payload"]["message"], $from, $json["payload"]["channel"]);
                     if ($this->isBridged()) {
                         $this->irc->sendMessageToChannel($json["payload"]["message"], "{$this->clients[$from]->getUser()["_id"]}!{$this->clients[$from]->getUser()["_id"]}@{$from->remoteAddress}", $json["payload"]["channel"]);
                     }
                 }
             }
             break;
         case "channel":
             if ($this->clients[$from]->isAuthenticated()) {
                 if ($json["payload"]["verb"] == "add") {
                     $reply = $json;
                     $chan = Channels::getChannel($json["payload"]["channel"]);
                     if ($chan !== null) {
                         if (!$chan["private"] && !in_array($this->clients[$from]->getUser()['_id'], $chan["banned"])) {
                             $reply["payload"]["verb"] = "add";
                             $this->clients[$from]->addChannel($json["payload"]["channel"]);
                         } elseif (Users::isRepoOwner($this->clients[$from]->getUser()["_id"], $json["payload"]["channel"])) {
                             $reply["payload"]["verb"] = "add";
                             $this->clients[$from]->addChannel($json["payload"]["channel"]);
                         } else {
                             $reply["payload"]["verb"] = "error";
                         }
                     } else {
                         $reply["payload"]["verb"] = "error";
                     }
                     $from->send(json_encode($reply));
                 } elseif ($json["payload"]["verb"] == "remove") {
                     $reply = $json;
                     if ($this->clients[$from]->isMemberOf($json["payload"]["channel"])) {
                         $reply["payload"]["verb"] = "remove";
                         $this->clients[$from]->removeChannel($json["payload"]["channel"]);
                     } else {
                         $reply["payload"]["verb"] = "error";
                     }
                     $from->send(json_encode($reply));
                 }
             }
             break;
         case "auth":
             $from->send(json_encode(["type" => "authreply", "payload" => ["done" => $this->clients[$from]->authenticate($json["payload"])]]));
             break;
         default:
             Logger::warning("Bad message got.");
             break;
     }
 }
Esempio n. 2
0
 public function handleMessage($msg)
 {
     Logger::debug("IRC message received: {$msg}");
     $msg = explode(" ", $msg);
     switch ($msg[0]) {
         case "PASS":
             $this->password = trim($msg[1]);
             break;
         case "NICK":
             foreach ($this->server->getClients() as $client) {
                 if (strtolower($client->getNick()) === $msg[1]) {
                     $this->sendNumerical("NOTICE AUTH :*** That nick is already in use..");
                     break;
                 }
             }
             $this->setNick($msg[1]);
             break;
         case "USER":
             if ($this->password) {
                 if (Users::checkLogin($msg[1], $this->password)) {
                     $this->setIdent($msg[1]);
                     $this->setRealName($msg[4]);
                     $this->sendNumerical("001 {$this->nick} :Welcome to the Internet Relay Network {$this}");
                     $this->sendNumerical("002 {$this->nick} :Your host is shogchat, running version a development build.");
                     $this->sendNumerical("003 {$this->nick} :This server was created " . @date('r'));
                     $this->sendNumerical("375 {$this->nick} :=== Welcome to ShogChat IRC Bridge ====");
                     $this->sendNumerical("372 {$this->nick} :You are connected to the IRC bridge as {$this->nick}. All of your GitHub repos have a channel, just join the channel with the name of your repo and start chatting. If you get an error saying the channel doesn't exist, you might need to reload your repos on our website.");
                     $this->sendNumerical("372 {$this->nick} :-----------------------------");
                     $this->sendNumerical("372 {$this->nick} :(ShogChat)->shogAllTheChats()");
                     $this->sendNumerical("376 {$this->nick} :End of MOTD");
                     $this->sendNumerical("NICK " . $this->nick);
                     $this->authenticated = true;
                     $this->user = Users::getUser($msg[1]);
                 } else {
                     $this->sendNumerical("464 {$this->nick} :The password you have set is incorrect.");
                     $this->close();
                 }
             } else {
                 $this->sendNumerical("464 {$this->nick} :You haven't set a password.");
                 $this->close();
             }
             break;
         case "JOIN":
             if ($this->isAuthenticated()) {
                 $chans = explode(",", $msg[1]);
                 foreach ($chans as $chan) {
                     $chan = Channels::getChannel(trim(substr($chan, 1)));
                     if ($chan != null) {
                         if (!$chan["private"]) {
                             if (!in_array($this->getUser()['_id'], $chan["banned"])) {
                                 $this->addChannel($chan["_id"]);
                                 $this->sendNumerical("332 {$this->nick} #{$chan["_id"]} :This is a public channel.");
                             } else {
                                 $this->sendNumerical("474 {$this->nick} :You are banned from this channel.");
                             }
                         } else {
                             if (Users::isRepoOwner($this->getUser()['_id'], $chan["_id"])) {
                                 $this->addChannel($chan["_id"]);
                                 $this->sendNumerical("332 {$this->nick} #{$chan["_id"]} :This is a private channel.");
                             } else {
                                 $this->sendNumerical("473 {$this->nick} :This is a private channel, you need an invite.");
                             }
                         }
                     } else {
                         $this->sendNumerical("403 {$this->nick} :That repository isn't registered with ShogChat.");
                     }
                 }
             }
             break;
         case "PRIVMSG":
             $chan = substr($msg[1], 1);
             if ($this->isMemberOf($chan)) {
                 if ($msg[2][0] == ":") {
                     $msg[2] = substr($msg[2], 1);
                     $msg[2] = implode(" ", array_slice($msg, 2));
                 }
                 $this->server->sendMessageToChannel($msg[2], $this, $chan);
                 $this->server->getChatServer()->sendMessageToChannel($msg[2], $this->ident, $chan);
             }
             break;
         case "PONG":
             //Nothing needed?
             break;
         case "QUIT":
             $this->close();
             break;
         case "PART":
             //TODO
             break;
         case "USERS":
             $this->sendNumerical("395 {$this->nick} :ShogChat can't display users in channel.");
             break;
         case "WHOIS":
             //TODO check nick
             $this->sendNumerical("311 {$this->nick} {$this->nick} {$this->ident} {$this->host} * :{$this->realName}");
             $this->sendNumerical("318 {$this->nick} {$this->nick} :End of /WHOIS list.");
             break;
         default:
             Logger::warning($msg[0] . " is an unrecognized IRC command.");
             break;
     }
 }