예제 #1
0
 /**
  * 発言
  * @param string $topic Skypeチャットトピック名
  * @param string $message 送信メッセージ
  */
 public static function send($topic, $message)
 {
     // エラーフラグの初期化
     $err_flg = false;
     // スタンドアロンで使われているかを判定
     if (!defined('MESSAGE_SERVER_ADDRESS') or !defined('MESSAGE_SERVER_PORT')) {
         // パッケージのconfigを見に行くためSkypeBotの初期化(相対パスで指定)
         require_once realpath(dirname(__FILE__)) . '/../../Init.php';
         Skype_Bot_Init::init();
         $config = (require PLUGIN_CONFIG_DIR . 'Messageserver.config.php');
         if (isset($config['address'])) {
             $server = $config['address'];
         } else {
             $server = '127.0.0.1';
             // 設定が無い場合は自分自身を見る
         }
         $port = $config['port'];
     } else {
         $server = MESSAGE_SERVER_ADDRESS;
         $port = MESSAGE_SERVER_PORT;
     }
     // 対象チャット名を取得
     if (!isset($topic) or '' == $topic) {
         fputs(STDERR, self::USAGE);
         exit(1);
     }
     // 発言内容を取得
     if (!isset($message) or '' == $message) {
         fputs(STDERR, self::USAGE);
         exit(1);
     } else {
         $message = strtr($message, array('\\n' => "\n"));
     }
     try {
         // 新しいソケットを作成する
         $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
         if (!is_resource($sock)) {
             throw new Exception("Socket create error.");
         }
         // 接続先アドレスと接続する
         if (!@socket_connect($sock, $server, $port)) {
             throw new Exception("Socket connect error.");
         }
         // ソケットに書き込み
         $out = sprintf("SENDMESSAGE %s %s\n", base64_encode($topic), base64_encode($message));
         if (false === socket_write($sock, $out)) {
             throw new Exception("Socket write error.");
         }
         // 結果を取得
         if (!socket_recv($sock, $buf, 3, MSG_WAITALL)) {
             throw new Exception("Socket recieve error.");
         } else {
             echo $buf . "\n";
         }
     } catch (Exception $e) {
         fputs(STDERR, sprintf("%s (%s)\n", $e->getMessage(), socket_strerror(socket_last_error())));
         $err_flg = true;
     }
     // ソケットを閉じる
     if (is_resource($sock)) {
         socket_close($sock);
     }
     // エラーの場合はここで終了
     if ($err_flg) {
         exit(1);
     }
 }
예제 #2
0
 /**
  * コンストラクタ
  */
 public function __construct()
 {
     // SkypeBotの初期化(相対パスで指定)
     require_once 'Init.php';
     Skype_Bot_Init::init();
 }