<?php
# Stream Wrapper for ITSNO instant message plumbing service!
# written by Adam Smith (adam@adamsmith.as)
#
# $f = fopen("itsno://screenname")
# fwrite($f, "hey!");
# fclose($f); // actually sends the message here
#
# Reading, Seeking, Stat'ing don't make any sense here, don't try them.
#
# Have a nice day.

class ItsnoStream {
	
	var $dest;
	var $msg;

	function stream_open ($path, $mode, $options, &$opened_path) {
		$parts = parse_url($path);
		$this->dest = $parts['host'];
		$this->msg = "ITSNO\n$this->dest\n";
		$opened_path = "$proto://$dest/";

		if($mode == "w" || $mode == "x") {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function stream_close () {
		if( $msg[-1] != '\n') { $msg .= '\n'; }

		$udp = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

		$result = socket_sendto(
			$udp,
			$this->msg,
			strlen($this->msg),
			0x100,
			"lglan.net",
			17520
		);

		if ($result !== false) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function stream_read ($count) {
		return FALSE;
	}

	function stream_write($data) {
		$this->msg .= $data;
		return strlen($data);
	}

	function stream_eof () {
		return FALSE; // there is no end
	}

	function stream_tell () {
		return 0; // you have no location
	}

	function stream_seek ($offset, $whence) {
		return FALSE; // you cant go anywhere
	}

	function stream_flush () {
		return TRUE; // its already cached
	}

	function stream_stat () {
		return FALSE; // no peeking!
	}


} // ItsnoStream


stream_wrapper_register("itsno", "ItsnoStream") or die("Failed to register protocol");

?>
