parent
452a5ba97c
commit
77ba61c8bc
|
@ -77,19 +77,56 @@ class Irc implements \PHPCI\Plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
$sock = fsockopen($this->server, $this->port);
|
$sock = fsockopen($this->server, $this->port);
|
||||||
fputs($sock, 'USER ' . $this->nick . ' phptesting.org ' . $this->nick . ' :' . $this->nick . "\r\n");
|
stream_set_timeout($sock, 1);
|
||||||
fputs($sock, 'NICK ' . $this->nick . "\r\n");
|
|
||||||
fputs($sock, 'JOIN ' . $this->room . "\r\n");
|
|
||||||
fputs($sock, 'PRIVMSG ' . $this->room . ' :' . $msg . "\r\n");
|
|
||||||
|
|
||||||
while (fgets($sock)) {
|
$connectCommands = array(
|
||||||
// We don't need to do anything,
|
'USER ' . $this->nick . ' 0 * :' . $this->nick,
|
||||||
// but the IRC server doesn't appear to post the message
|
'NICK ' . $this->nick,
|
||||||
// unless we wait for responses.
|
);
|
||||||
}
|
$this->executeIrcCommands($sock, $connectCommands);
|
||||||
|
$this->executeIrcCommand($sock, 'JOIN ' . $this->room);
|
||||||
|
$this->executeIrcCommand($sock, 'PRIVMSG ' . $this->room . ' :' . $msg);
|
||||||
|
|
||||||
fclose($sock);
|
fclose($sock);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param resource $socket
|
||||||
|
* @param array $commands
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function executeIrcCommands($socket, array $commands)
|
||||||
|
{
|
||||||
|
foreach ($commands as $command) {
|
||||||
|
fputs($socket, $command . "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
$pingBack = false;
|
||||||
|
|
||||||
|
// almost all servers expect pingback!
|
||||||
|
while ($response = fgets($socket)) {
|
||||||
|
$matches = array();
|
||||||
|
if (preg_match('/^PING \\:([A-Z0-9]+)/', $response, $matches)) {
|
||||||
|
$pingBack = $matches[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($pingBack) {
|
||||||
|
$command = 'PONG :' . $pingBack . "\n";
|
||||||
|
fputs($socket, $command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param resource $socket
|
||||||
|
* @param string $command
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function executeIrcCommand($socket, $command)
|
||||||
|
{
|
||||||
|
return $this->executeIrcCommands($socket, array($command));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue