rendered paste body<?phperror_reporting(E_ALL);# Gtk::io_add_watch() test program# see glib documentation : # http://developer.gnome.org/doc/API/2.0/glib/glib-IO-Channels.html# perl example http://www.koders.com/perl/fidBEE46D78030D9667BC205FBA6D3FD0304435C2F7.aspx# http://www.linuxjournal.com/node/8545/print (tutorial)# $conditions = (Gtk::IO_IN, Gtk::IO_OUT, IO_PRI, IO_ERR, IO_HUP, IO_NVAL);# $id = io_add_watch($stream_rsrc, $condition, $callback);# $id = io_add_watch($stream_rsrc, $condition, $callback, $priority);class GtkIO{ protected $gio_ids; protected $fd; protected $hostname; protected $port; protected $errno; # fsocketopen error status protected $errstr; function __construct(){ $this->gio_ids = array(); } function connect($hostname, $port){ $this->hostname = $hostname; $this->port = $port; $this->fd = fsockopen($hostname, $port, $this->errno, $this->errstr); if(!$this->fd) trigger_error("GtkIO::connect : connexion error : {$this->errstr} ({$this->errno})"); else{ $this->io_setup(); } return $this->fd; } function io_setup(){ $this->gio_ids[Gtk::IO_IN] = Gtk::io_add_watch($this->fd, Gtk::IO_IN, array($this, 'io_in')); $this->gio_ids[Gtk::IO_OUT] = Gtk::io_add_watch($this->fd, Gtk::IO_OUT, array($this, 'io_out')); $this->gio_ids[Gtk::IO_HUP] = Gtk::io_add_watch($this->fd, Gtk::IO_HUP, array($this, 'io_hup')); $this->gio_ids[Gtk::IO_PRI] = Gtk::io_add_watch($this->fd, Gtk::IO_PRI, array($this, 'io_pri')); $this->gio_ids[Gtk::IO_ERR] = Gtk::io_add_watch($this->fd, Gtk::IO_ERR, array($this, 'io_err')); $this->gio_ids[Gtk::IO_NVAL] = Gtk::io_add_watch($this->fd, Gtk::IO_NVAL, array($this, 'ion_val')); } function write($data){ return fwrite($this->fd, $data, strlen($data)); } function read($size=2048){ return fread($this->fd, $size); } function eof(){ return foef($this->fd); } function io_in($gio_channel, $conditions){ echo "GtkIO::io_in()\n"; } function io_out($gio_channel, $conditions){ echo "GtkIO::io_out()\n"; } function io_hup($gio_channel, $conditions){ echo "GtkIO::io_hup()\n"; } function io_pri($gio_channel, $conditions){ echo "GtkIO::io_pri()\n"; } function io_err($gio_channel, $conditions){ echo "GtkIO::io_err()\n"; }}define('GTK_IRC_CONNECTED', 1);define('GTK_IRC_DISCONNECTED', 2);class IrcProtocol extends GtkIO{ protected $nick; protected $user; protected $channels; protected $timeout_id; protected $irc_status; function __construct(){ $this->timeout = 1000; $this->timeout_id = Gtk::timeout_add($this->timeout, array($this, 'timeout')); $this->irc_status = GTK_IRC_DISCONNECTED; } function connect($hostname, $port, $nick, $user, $channels){ $status = parent::connect($hostname, $port); if($status === false) die("can't connect to irc server"); $this->irc_status = GTK_IRC_CONNECTED; $this->nick = $nick; $this->user = $user; # initial channels to join; $this->channels = $channels; $this->nick($nick); $this->user($user); $this->join($channels); } function send($str){ echo "Irc::send($str)\n"; $this->write($str . "\n\r"); } function timeout(){ static $count = 0; echo "timeout ...\n"; $count++; # debug - for gtk_io function monitor if($count > 30){ $this->send_chan('#php-gtk', "I'm going out; bye ..."); $this->disconnect(); return; } if($this->irc_status == GTK_IRC_CONNECTED && $this->fd != null) $this->timeout_id = Gtk::timeout_add($this->timeout, array($this, 'timeout')); $this->send_chan('#php-gtk', "hello ; I'm php-gtk-irc bot"); } function io_in($gio_channel, $conditions){ $data = $this->read(); echo "$data\n"; } function io_hup($gio_channel, $conditions){ echo "irc : disconnected\n"; $this->fd = null; Gtk::main_quit(); } function disconnect(){ $this->send('QUIT'); $this->irc_status = GTK_IRC_DISCONNECTED; Gtk::main_quit(); } function send_chan($chan, $msg){ $this->send("PRIVMSG $chan :$msg"); } function nick($nick){ $this->send("NICK $nick"); } function user($user){ $this->send("USER $user $user $user localhost"); } function join($channels){ foreach($channels as $chan) $this->send("JOIN $chan"); }}$irc = new IrcProtocol();$irc->connect($server = 'localhost', $port = '26667', $nick='php-irc-bot-mq', $user='Marc', $channels=array('#php-gtk'));Gtk::main();?>