pick_resource(); $this->jid="$id_name@$id_server/{$this->resource}"; $this->serverid=$id_server; $this->password=$password; $this->username=$username; $this->server=$server; $this->port=$port; $this->resource=$resource; } function disconnect() { fwrite($this->sock,''); fclose($this->sock); } function message($recipient,$body) { $body=htmlspecialchars($body); fwrite($this->sock," $body "); } function reply($received,$body) { //var_export($received);print "\n"; $attr=$received->attributes(); $body=htmlspecialchars($body); fwrite($this->sock," $body "); } function log($received) { if(is_null($this->logfile)) return; if(is_null($this->logfilep)) $this->logfilep=fopen($this->logfile,'a'); $ts=strftime("%Y-%m-%dT%H:%M:%S%z"); $attr=$received->attributes(); $body=strtr($received->body,array('\\'=>'\\\\',"\n"=>'\n')); fwrite($this->logfilep,"$ts <{$attr->from} -> {$attr->to}> $body\n"); } function presence($status=null,$show='chat') { if(is_null($status)) $status=$this->pick_status(); fwrite($this->sock," $show $status 1 "); echo fread($this->sock,$this->bufsize),"\n\n"; $this->last_status=$status; $this->last_show=$show; } function restate() { $this->presence($this->last_status,$this->last_show); $this->last_restate=time(); } function conditional_restate($gap) { if(time()-$this->last_restate>$gap) $this->restate(); } function subscribe($jid) { fwrite($this->sock,""); echo fread($this->sock,$this->bufsize),"\n\n"; } function confirm_subscription($received) { $attr=$received->attribuites(); if($received->getName()!=="presence") return; if($attr->type!=="subscribe") return; fwrite($this->sock,""); } function gateway_setup($gateway,$username,$password) { // transport register $username=htmlspecialchars($username); $password=htmlspecialchars($password); fwrite($this->sock," $username $password "); echo fread($this->sock,$this->bufsize),"\n\n"; // transport subscribe $this->subscribe($gateway); $this->restate(); } function logout($service) { $this->unavailable($service); } function unavailable($jid) { fwrite($this->sock,""); } function available($jid) { fwrite($this->sock,""); } function roster() { fwrite($this->sock," "); try { $elem=new SimpleXMLElement(fread($this->sock,$this->bufsize)); } catch(Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } return $elem; } function pick_resource() { return "jabber-php:".substr(md5(rand()),10,6); } function connect() { $this->upsince=time(); $this->sock=stream_socket_client( "sslv3://{$this->server}:{$this->port}", $errno, $errstr, 30 ); stream_set_blocking($this->sock,1); stream_set_timeout($this->sock,1200); if (!$this->sock) die("Unable to connect: $errstr ($errno)"); $username=htmlspecialchars($this->username); $password=htmlspecialchars($this->password); $resource=htmlspecialchars($this->resource); fwrite($this->sock," "); echo fread($this->sock,$this->bufsize),"\n\n"; // auth fwrite($this->sock," $username "); echo fread($this->sock,$this->bufsize),"\n\n"; echo fread($this->sock,$this->bufsize),"\n\n"; //$digest=sha1($this->password); fwrite($this->sock," $username $password $resource "); echo fread($this->sock,$this->bufsize),"\n\n"; $this->presence($this->last_show,$this->last_status); $this->roster(); } function pick_status() { if(is_string($this->status)) return $this->status; return $status[rand(0,count($status)-1)]; } function add_status($status) { if(is_array($status)){ foreach($status as $s) $this->add_status($s); return; } if(is_null($this->status)) $this->status=$status; elseif(is_string($this->status)) $this->status=array($this->status,$status); else $this->status[]=$status; } function avatar_upload($filename) { /* // avatar upload $avatar=file_get_contents('avatar.jpg'); $avatar_digest=sha1($avatar); $avatar_base64=base64_encode($avatar); $avatar_imageinfo=getimagesize('avatar.jpg'); $avatar_mime=$avatar_imageinfo['mime']; fwrite($this->sock," $avatar_mime $avatar_base64 "); echo fread($this->sock,$this->bufsize),"\n\n"; // avatar announce fwrite($this->sock," $avatar_digest "); echo fread($this->sock,$this->bufsize),"\n\n"; */ } function uptime() { if(@!$this->upsince) $this->upsince=time()-540366; $uptime=time()-$this->upsince; $uptime=array( 'day' => intval($uptime/86400), 'hour' => intval($uptime%86400/3600), 'min' => intval($uptime%86400%3600/60), 'sec' => intval($uptime%86400%3600%60) ); $ret=''; foreach($uptime as $k=>$v){ if($v>0){ $ret.=", $v $k"; if ($v>1) $ret.="s"; } } $ret=substr($ret,2); $ret.="\n\nUp since: ".strftime('%d %b %Y, %H:%M:%S'); return $ret; } function timed_out() { $info = stream_get_meta_data($this->sock); return $info['timed_out']; } }