<?php

/*
 * Copyright 2012 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Implements the base IRC protocol so servers don't kick you off.
 *
 * @group irc
 */
final class PhabricatorIRCProtocolHandler extends PhabricatorIRCHandler {

  public function receiveMessage(PhabricatorIRCMessage $message) {
    switch ($message->getCommand()) {
      case '376': // End of MOTD
        $join = $this->getConfig('join');
        if (!$join) {
          throw new Exception("Not configured to join any channels!");
        }
        foreach ($join as $channel) {
          $this->write('JOIN', $channel);
        }
        break;
      case 'PING':
        $this->write('PONG', $message->getRawData());
        break;
    }
  }

}

<?php

/*
 * Copyright 2012 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Implements the base IRC protocol so servers don't kick you off.
 *
 * @group irc
 */
final class PhabricatorIRCProtocolHandler extends PhabricatorIRCHandler {

  public function receiveMessage(PhabricatorIRCMessage $message) {
    switch ($message->getCommand()) {
      case '422': // Error - no MOTD
      case '376': // End of MOTD
        $join = $this->getConfig('join');
        if (!$join) {
          throw new Exception("Not configured to join any channels!");
        }
        foreach ($join as $channel) {
          $this->write('JOIN', $channel);
        }
        break;
      case 'PING':
        $this->write('PONG', $message->getRawData());
        break;
    }
  }

}

Make IRC Bot connect on both successful end of MOTD (376) and non-successful MOTD (422)
Make IRC Bot connect on both successful end of MOTD (376) and non-successful MOTD (422)

Summary: http://www.networksorcery.com/enp/protocol/irc.htm

Test Plan: augment code with an additional debug line (phlog('hi');) so I can see my case was trigged and it will fall through. setup an ill-configured IRC server with ngircd. Configure an ircbot to connect to said ill-configured IRC server. verify ircbot connected to channel. verify in irc bot logs that debug line was invoked.

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T1452

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