/** * Parse read string into a message object * * Parses IRC message, whiich conform to RFC 1459 into a IRC message object * for further handling. * * @param TwIRCd\Logger $logger * @param string $string * @return TwIRCd\Irc\Message */ public static function parseClientString(\TwIRCd\Logger $logger, $string) { if (preg_match('(^ (?: :(?P<host>\\S+) \\s* )? (?P<command> [A-Za-z]+ | \\d{3} ) (?P<params> (?:\\s+ [^:\\s]\\S* )* ) (?: \\s+: (?P<text>.*) )? \\s*$)Sx', $string, $match)) { $message = new static(); // Split up client host if (preg_match('(^(?P<nick>\\S*)!(?P<ident>\\S*)@(?P<host>\\S*))S', $match['host'], $host)) { $message->nick = $host['nick']; $message->ident = $host['ident']; $message->host = $host['host']; } else { $msg['server'] = $match['host']; } $message->command = strtoupper($match['command']); $match['params'] = trim($match['params']); $message->params = empty($match['params']) ? array() : preg_split('(\\s+)', $match['params']); // The "text" should be considered as just another parameter if (isset($match['text'])) { $message->params[] = $match['text']; } return $message; } else { $logger->log(E_ERROR, "Could not parse: {$string}"); return null; } }
/** * Disconnect client, after it sent a quit message. * * @param User $user * @param Message $message * @return void */ protected function disconnectUser(User $user, Message $message) { foreach ($this->users as $nr => $client) { if ($client === $user) { $this->logger->log(E_NOTICE, "Disconnecting client {$user->nick}."); $this->sendServerMessage($user, 'QUIT :' . $message->params[0] ?: 'Client exited'); socket_close($user->connection); unset($this->users[$nr]); } } }
/** * Somebody has been kicked * * If an kick has been sent in the &twitter channel, this means an * unfollowing request issued by the user. * * @param Irc\User $user * @param Irc\Message $message * @return void */ public function removeFromGroup(Irc\User $user, Irc\Message $message) { $groups = $user->configuration->getGroups(); $channel = $message->params[0]; if ($channel === '&twitter' || !isset($groups[$channel])) { return; } if (($key = array_search($friend = $message->params[1], $groups[$channel])) !== false) { unset($groups[$channel][$key]); $user->configuration->setGroup($channel, $groups[$channel]); $this->logger->log(E_NOTICE, "Removed {$friend} from group {$channel}."); $this->ircServer->send($user, ":{$friend}!{$friend}@twitter.com PART {$channel} :Removed"); } }
/** * Receive updates * * Receive updates from searches, direct messages and the timeline. Only * returns something, if the queue offers something to process. Can be * called at any rate and ensures itself, that the services are not called * too often. * * Returns an array of message objects. * * @return array */ public function getUpdates() { $current = time(); foreach ($this->queue as &$entry) { if ($entry['scheduled'] < $current) { $result = array(); try { $result = call_user_func_array(array($this, $entry['type']), $entry['parameters']); } catch (ConnectionException $e) { // Ignore these errors, they most likely mean connection // failures, which are just too common with twitter, to // report them to the user. $this->logger->log(E_NOTICE, 'Connection failure: ' . $e->getMessage()); } catch (\Exception $e) { // Log all other errors $this->logger->log(E_ERROR, 'An error occured: ' . $e->getMessage()); } $entry['scheduled'] = $current + $this->queueFactor * $this->updateTimes[$entry['type']]; $this->logger->log(E_NOTICE, "Rescheduled item {$entry['type']} at " . date('r', $entry['scheduled']) . '.'); return $result; } } return array(); }