+ public function handleIRCMessage(IRCMessage $msg) {
+ if ($msg->isPrivMsg()) {
+ $this->handlePrivMsg($msg);
+ return;
+ }
+ if ($msg->isPing()) {
+ $this->sendIRCMessage($msg->makePong());
+ return;
+ }
+ if ($msg->isNotice() && $msg->getText() == 'Login authentication failed') {
+ $this->logger->notice('login failed, refreshing access token');
+ $this->token->refresh();
+ $this->login();
+ return;
+ }
+ if ($msg->command == '001') {
+ // successful login
+ $this->joinChannels();
+ return;
+ }
+ }
+
+ public function handlePrivMsg(IRCMessage $msg) {
+ $target = $msg->getPrivMsgTarget();
+ if ($target[0] != '#') return;
+ $text = $msg->getText();
+ if ($text[0] != '!') return;
+ $channel = Channel::firstWhere('twitch_chat', '=', $target);
+ if (!$channel) return;
+ $this->logger->info('got command '.$text.' on channel '.$channel->title);
+ }
+
+ public function login() {
+ $this->ws->send('PASS oauth:'.$this->token->access);
+ $this->ws->send('NICK localhorsttv');
+ }
+
+ public function joinChannels() {
+ $this->logger->info('joining channels');
+ $channels = Channel::where('twitch_chat', '!=', '')->get();
+ $names = [];
+ foreach ($channels as $channel) {
+ $names[] = $channel->twitch_chat;
+ }
+ $chunks = array_chunk($names, 10);
+ foreach ($chunks as $chunk) {
+ $this->sendIRCMessage(IRCMessage::join($chunk));
+ }
+ }
+
+ public function sendIRCMessage(IRCMessage $msg) {
+ $irc_message = $msg->encode();
+ $this->logger->debug('sending IRC message '.$irc_message);
+ $this->ws->send($irc_message);
+ }
+
+