Skype API wrapper class (PHP) を試してみた (2)

試しに、Bot を動かしてみました。

% cd example
% php skype_bot.php
PHP Fatal error:  Uncaught exception 'Exception' with message 'plugin file not found [Skype/Bot/Plugin/Log.php]' in /usr/share/pear/Skype/Bot.php:54
Stack trace:
#0 /home/yoya/src/php/php-skype-0.1.0/example/skype_bot.php(14): Skype_Bot->loadPlugin('log', Array)
#1 {main}
  thrown in /usr/share/pear/Skype/Bot.php on line 54


えーっと…

51 if (class_exists($klass) == false) {
52         $path = sprintf("Skype/Bot/Plugin/%s.php", ucfirst(strtolower($plugin_id)));
53         if (is_readable($path) == false) {
54                 throw new Exception(sprintf("plugin file not found [%s]", $path));
55         }
56         require_once($path);
57 }

あー。Skype.php と同じディレクトリ上で実行する前提なのね。
この前提はよくないので、とりあえず事前チェックをやめて
require_once で失敗したら FAULT が発生するに任せてみます。

というわけで、is_readable のブロックをごっそりコメントアウトと…

% php skype_bot.php
invoke: NAME skype_bot

動いt…
(1分後)
きゃーーっ (´Д`;)

% php skype_bot.php
invoke: NAME skype_bot
PHP Warning:  dbusconnection::sendwithreplyandblock(): dbus_connection_send_with_reply_and_block() failed (Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.) in /usr/share/pear/Skype.php on line 106
PHP Fatal error:  Uncaught exception 'Exception' with message 'dbus_connection_send_with_reply_and_block() failed' in /usr/share/pear/Skype.php:108
Stack trace:
#0 /usr/share/pear/Skype.php(85): Skype->invoke('NAME skype_bot', -1)
#1 /usr/share/pear/Skype/Bot.php(89): Skype->connect()
#2 /usr/share/pear/Skype/Bot.php(32): Skype_Bot->_startup()
#3 /home/yoya/src/php/php-skype-0.1.0/example/skype_bot.php(16): Skype_Bot->run()
#4 {main}
  thrown in /usr/share/pear/Skype.php on line 108

試しに、"skype_bot" を "skype_api_client" に変えたら

% php skype_bot.php
invoke: NAME skype_api_client
reply:  OK
invoke: PROTOCOL 7
reply:  PROTOCOL 7
	<色々沢山>

あ…
NAME の右には(自己申告の)アプリケーション名を入れますが、
新規のアプリケーション名で Skype に接続すると、Skype 側で

他のプログラムが Skype の仕様を希望しています
プログラム "skype_bot" が Skype への接続を試みています。
許可しますか?
[ ] この選択を記憶する
                               [  はい  ] [  いいえ  ]

といったプロンプトが出るので、これに OK しないと接続失敗します。(当たり前)

というわけで、[ はい ]を押したら解決しました。

debug

とりあえず動きがあるので一度、debug 出力を止めようかと思い、
第二引数の debug フラグを false にしました。

$bot = new Skype_Bot("skype_bot", false);

でも、今まで通り debug っぽいメッセージが沢山出てきます。
なんでだろうと、コードを追ってみると

class Skype_Bot {
    public function __construct($id, $debug = false) {
        $this->debug = $debug;
        $this->skype = new Skype($id, Skype::default_protocol, $debug);
    }

ふむ。

class Skype {
    public function __construct($id, $protocol = self::default_protocol,
                                $debug = false) {
        $this->id = $id;
        $this->protocol = intval($protocol);
        $this->debug = $debug;

ふむふむ。

    public function invoke($s) {
	<略>
        $this->_debug("invoke: %s\n", $s);

ふむふむふむ。

    protected function _debug($format) {
        $args = func_get_args();
        array_shift($args);
        vprintf($format, $args);
    }

… (゜Д゜) !

    protected function _debug($format) {
        if (! $this->debug) {
             return ;
         }
        $args = func_get_args();
        array_shift($args);
        vprintf($format, $args);

↑こんな感じに書き換えとこっ。(実際は行数を変えないように汚い書き方してるけど)

今日の成果

% php skype_bot.php
exception 'Exception' with message 'unsupported property [DIALOG_PARTNER]' in /usr/share/pear/Skype/Object.php:68
Stack trace:
#0 /usr/share/pear/Skype.php(226): Skype_Object->set('DIALOG_PARTNER', 'yoya_test')
#1 /usr/share/pear/Skype.php(139): Skype->handleChat('#yoya_test/$y...')
#2 [internal function]: Skype->callback(Object(dbusmessage))
#3 /usr/share/pear/Skype.php(126): dbusconnection->poll(86400)
#4 /usr/share/pear/Skype/Bot.php(36): Skype->poll(86400)
#5 /home/yoya/lang/php/skype_bot.php(16): Skype_Bot->run()
#6 {main}

(⊃д⊂)

でも、/var/tmp にはファイルが出来ていて、ログが残ってはいます。

2008/09/17 02:51:52     [SAID]  よや@test(yoya_test)     テスト

知らないメッセージも(利用できるかは別として)とりあえず受け取れるようにはして、
更に、そのメッセージの対処コードを追加すると良さそうですが、
もう眠いので、今日はここまでで… Zzz