@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge IRCProtocolHandler into IRCAdapter

Summary:
Clearly silly to have a separate handler for this. I also made most of the protocol stuff direct writes so we don't need to ship them through handlers, and made the adapter ignore message it does not understand by default instead of sending them to IRC, and added PASTE "support".

We could still let handlers react to these messages by emitting them all as 'RAWIRC' or similar, but there's currently no need for that so I didn't bother.

Also fix an issue in D4924 with nickpass.

Test Plan: Had bot join IRC, talked to it.

Reviewers: indiefan

Reviewed By: indiefan

CC: aran

Differential Revision: https://secure.phabricator.com/D4925

+67 -86
+62 -55
src/infrastructure/daemon/bot/adapter/PhabricatorIRCProtocolAdapter.php
··· 41 41 } 42 42 43 43 $this->socket = $socket; 44 - $this->writeMessage( 45 - id(new PhabricatorBotMessage()) 46 - ->setCommand('USER') 47 - ->setBody("{$user} 0 * :{$user}")); 44 + $this->write("USER {$user} 0 * :{$user}"); 48 45 if ($pass) { 49 - $this->writeMessage( 50 - id(new PhabricatorBotMessage()) 51 - ->setCommand('PASS') 52 - ->setBody("{$pass}")); 46 + $this->write("PASS {$pass}"); 53 47 } 54 - 55 - $this->writeMessage( 56 - id(new PhabricatorBotMessage()) 57 - ->setCommand('NICK') 58 - ->setBody("{$nick}")); 48 + $this->write("NICK {$nick}"); 59 49 } 60 50 61 51 public function getNextMessages($poll_frequency) { ··· 111 101 } while (strlen($this->writeBuffer)); 112 102 } 113 103 114 - while ($m = $this->processReadBuffer()) { 115 - $messages[] = $m; 104 + while (($m = $this->processReadBuffer()) !== false) { 105 + if ($m !== null) { 106 + $messages[] = $m; 107 + } 116 108 } 117 109 118 110 return $messages; 119 111 } 120 112 121 113 private function write($message) { 122 - $this->writeBuffer .= $message; 114 + $this->writeBuffer .= $message."\r\n"; 123 115 return $this; 124 116 } 125 117 126 118 public function writeMessage(PhabricatorBotMessage $message) { 127 - $irc_command = $this->getIRCCommand($message->getCommand()); 128 119 switch ($message->getCommand()) { 129 - case 'MESSAGE': 130 - $data = $irc_command.' '. 131 - $message->getTarget()->getName().' :'. 132 - $message->getBody()."\r\n"; 133 - break; 134 - default: 135 - $data = $irc_command.' '. 136 - $message->getBody()."\r\n"; 137 - break; 120 + case 'MESSAGE': 121 + case 'PASTE': 122 + $name = $message->getTarget()->getName(); 123 + $body = $message->getBody(); 124 + $this->write("PRIVMSG {$name} :{$body}"); 125 + return true; 126 + default: 127 + return false; 138 128 } 139 - 140 - return $this->write($data); 141 129 } 142 130 143 131 private function processReadBuffer() { ··· 161 149 throw new Exception("Unexpected message from server: {$message}"); 162 150 } 163 151 152 + if ($this->handleIRCProtocol($matches)) { 153 + return null; 154 + } 155 + 164 156 $command = $this->getBotCommand($matches['command']); 165 157 list($target, $body) = $this->parseMessageData($command, $matches['data']); 166 158 ··· 180 172 return $bot_message; 181 173 } 182 174 175 + private function handleIRCProtocol(array $matches) { 176 + $data = $matches['data']; 177 + switch ($matches['command']) { 178 + case '422': // Error - no MOTD 179 + case '376': // End of MOTD 180 + $nickpass = $this->getConfig('nickpass'); 181 + if ($nickpass) { 182 + $this->write("PRIVMSG nickserv :IDENTIFY {$nickpass}"); 183 + } 184 + $join = $this->getConfig('join'); 185 + if (!$join) { 186 + throw new Exception("Not configured to join any channels!"); 187 + } 188 + foreach ($join as $channel) { 189 + $this->write("JOIN {$channel}"); 190 + } 191 + return true; 192 + case 'PING': 193 + $this->write("PONG {$data}"); 194 + return true; 195 + } 196 + 197 + return false; 198 + } 199 + 183 200 private function getBotCommand($irc_command) { 184 201 if (isset(self::$commandTranslations[$irc_command])) { 185 202 return self::$commandTranslations[$irc_command]; ··· 189 206 return $irc_command; 190 207 } 191 208 192 - private function getIRCCommand($original_bot_command) { 193 - foreach (self::$commandTranslations as $irc_command=>$bot_command) { 194 - if ($bot_command === $original_bot_command) { 195 - return $irc_command; 196 - } 197 - } 198 - 199 - return $original_bot_command; 200 - } 201 - 202 209 private function parseMessageData($command, $data) { 203 210 switch ($command) { 204 - case 'MESSAGE': 205 - $matches = null; 206 - if (preg_match('/^(\S+)\s+:?(.*)$/', $data, $matches)) { 211 + case 'MESSAGE': 212 + $matches = null; 213 + if (preg_match('/^(\S+)\s+:?(.*)$/', $data, $matches)) { 207 214 208 - $target_name = $matches[1]; 209 - if (strncmp($target_name, '#', 1) === 0) { 210 - $target = id(new PhabricatorBotChannel()) 211 - ->setName($target_name); 212 - } else { 213 - $target = id(new PhabricatorBotUser()) 214 - ->setName($target_name); 215 + $target_name = $matches[1]; 216 + if (strncmp($target_name, '#', 1) === 0) { 217 + $target = id(new PhabricatorBotChannel()) 218 + ->setName($target_name); 219 + } else { 220 + $target = id(new PhabricatorBotUser()) 221 + ->setName($target_name); 222 + } 223 + 224 + return array( 225 + $target, 226 + rtrim($matches[2], "\r\n")); 215 227 } 216 - 217 - return array( 218 - $target, 219 - rtrim($matches[2], "\r\n")); 220 - } 221 - break; 228 + break; 222 229 } 223 230 224 231 // By default we assume there is no target, only a body ··· 228 235 } 229 236 230 237 public function __destruct() { 231 - $this->write("QUIT Goodbye.\r\n"); 238 + $this->write("QUIT Goodbye."); 232 239 fclose($this->socket); 233 240 } 234 241 }
+5 -31
src/infrastructure/daemon/bot/handler/PhabricatorIRCProtocolHandler.php
··· 1 1 <?php 2 2 3 3 /** 4 - * Implements the base IRC protocol so servers don't kick you off. 5 - * 6 - * @group irc 4 + * @deprecated 7 5 */ 8 6 final class PhabricatorIRCProtocolHandler extends PhabricatorBotHandler { 9 7 10 8 public function receiveMessage(PhabricatorBotMessage $message) { 11 - switch ($message->getCommand()) { 12 - case '422': // Error - no MOTD 13 - case '376': // End of MOTD 14 - $nickpass = $this->getConfig('nickpass'); 15 - if ($nickpass) { 16 - $this->writeMessage( 17 - id(new PhabricatorBotMessage()) 18 - ->setCommand('MESSAGE') 19 - ->setTarget('nickserv') 20 - ->setBody("IDENTIFY {$nickpass}")); 21 - } 22 - $join = $this->getConfig('join'); 23 - if (!$join) { 24 - throw new Exception("Not configured to join any channels!"); 25 - } 26 - foreach ($join as $channel) { 27 - $this->writeMessage( 28 - id(new PhabricatorBotMessage()) 29 - ->setCommand('JOIN') 30 - ->setBody($channel)); 31 - } 32 - break; 33 - case 'PING': 34 - $this->writeMessage( 35 - id(new PhabricatorBotMessage()) 36 - ->setCommand('PONG') 37 - ->setBody($message->getBody())); 38 - break; 9 + static $warned; 10 + if (!$warned) { 11 + $warned = true; 12 + phlog("The PhabricatorIRCProtocolHandler has been deprecated."); 39 13 } 40 14 } 41 15