/* for webOS, add

        function require(lib) { return IMPORTS.require(lib); }
        
   This is a simple gopher client for Node.js (0.2.3 and up). Why? Because
   everything is Node.js. And gopher protocol should be everywhere.

   Okay, for you disbelievers, here's another good reason. The gopher protocol
   as defined in RFC 1436 is approximately the world's most trivial protocol.
   You transmit a single string, terminated by \r\n, and you get back data.
   The server closes the connection. So, for people looking for a simple way
   to construct arbitrary protocols in Node.js, you can start with this.

   This does not construct or display gopher menus, and it does not handle
   item types. It just gets you the data, given a port, host, and an
   arbitrary selector (which can be "" for the root menu). To use, save this
   file somewhere as gopher.js and then reference it in your application. Here
   is a very simple example that grabs the main menu from this server,
   gopher.floodgap.com:

var sys = require('sys');
var gopher = require('./gopher');
var client = new gopher.gopherClient(70, "gopher.floodgap.com", "");
client.addListener('gopherReply', function(data) {
        sys.puts(data);
        process.exit(0);
});

   Save it as, say, gophertest.js, and run it with

   % node gophertest.js

   You're welcome. Version 1.0.

This is released under the 3-clause BSD license.

Copyright (c) 2011, Cameron Kaiser <gopher@floodgap.com>.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither Floodgap nor Cameron Kaiser may be used to promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

var sys = require('sys');
var net = require('net');
var EventEmitter = require('events').EventEmitter;

function gopherClient(port, host, sel) {
        var self = this;
        self.buf = '';
        self.connected = false;
        
        self.timeout = setTimeout(function() {
                self.emit("gopherTimeout");
        }, 25000);
        
        self.conn = net.createConnection(port, host);
        self.conn.addListener('data', function(data) {
                self.buf += data;
                clearTimeout(self.timeout);
                self.timeout = setTimeout(function() {
                        self.emit("gopherTimeout");
                }, 25000);
        });                
        self.conn.addListener('connect', function(data) {
                self.connected = true;
                self.conn.write(sel+"\r\n");
        });
        self.conn.addListener('end', function() {
                self.connected = false;
                self.emit("gopherReply", self.buf);
        });
}

sys.inherits(gopherClient, EventEmitter);
exports.gopherClient = gopherClient;