if (!$this->token) {
throw new \Exception('unable to find access token');
}
+ if ($this->token->hasExpired()) {
+ $this->token->refresh();
+ }
$this->connector = new Connector();
$this->connect();
$this->handlePrivMsg($msg);
return;
}
+ if ($msg->isWhisper()) {
+ $this->handleWhisper($msg);
+ return;
+ }
if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
$this->logger->notice('login failed, refreshing access token');
$this->token->refresh();
$this->ready = true;
return;
}
+ if ($msg->command == 'GLOBALUSERSTATE') {
+ // receive own user metadata
+ $this->handleUserState($msg);
+ return;
+ }
}
public function getMessageChannel(IRCMessage $msg) {
public function handlePrivMsg(IRCMessage $msg) {
}
+ public function handleUserState(IRCMessage $msg) {
+ if (isset($msg->tags['user-id'])) {
+ $this->user_id = $msg->tags['user-id'];
+ }
+ }
+
+ public function handleWhisper(IRCMessage $msg) {
+ }
+
public function login() {
$this->ws->send('PASS oauth:'.$this->token->access);
$this->ws->send('NICK '.$this->nick);
if (!$this->ready) return;
if (time() - $this->last_contact < 60) return;
try {
- $this->sendIRCMessage(IRCMessage::ping());
+ $this->sendIRCMessage(IRCMessage::ping($this->nick));
} catch (\Exception $e) {
}
});
$this->last_contact = time();
}
+ public function sendWhisper($to, $msg) {
+ $this->logger->info('sending whisper to '.$to.': '.$msg);
+ try {
+ $response = $this->token->request()->post('/whispers?from_user_id='.$this->user_id.'&to_user_id='.$to, [
+ 'message' => $msg,
+ ]);
+ if (!$response->successful()) {
+ $this->logger->error('sending whisper to '.$to.': '.$response->status());
+ }
+ } catch (\Exception $e) {
+ $this->logger->error('sending whisper to '.$to.': '.$e->getMessage());
+ }
+ }
+
protected function listenCommands() {
$this->getLoop()->addPeriodicTimer(1, function () {
private $nick;
private $token;
+ private $user_id = '';
private $connector;
private $ws;