/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is ChatZilla
 *
 * The Initial Developer of the Original Code is
 * Max Maischein
 * Portions created by Max Maischein are Copyright (C) 2004 Max Maischein.
 *
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU Public License (the "GPL"), in which case the
 * provisions of the GPL are applicable instead of those above.
 * If you wish to allow use of your version of this file only
 * under the terms of the GPL and not to allow others to use your
 * version of this file under the MPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL.  If you do not delete
 * the provisions above, a recipient may use your version of this
 * file under either the MPL or the GPL.
 *
 * Contributor(s):
 *  Max Maischein, <chatzilla - at - corion dot net>, original author
 */

const __id = "telnet-plugin";
const __maj_version = 1;
const __min_version = 20040322;
const __description = "Adds the /telnet command.";

//const TELNET_INITIAL = -1;
//const TELNET_CONNECTING = 1;
//const TELNET_DISCONNECTED = 2;
//const TELNET_CONNECTED = 3;
//const TELNET_CONNECT_FAILED = 4;

function initPlugin(glob)
{
    /* This function will be called when chatzilla first loads the plugin.
     * We initialize some of the plugin object's properties, define a new
     * command and some new prefs, and print out a message.
     */
    plugin.id = __id;
    plugin.major = __maj_version;
    plugin.minor = __min_version;
    plugin.version = __maj_version + "." + __min_version
    plugin.description = __description;

    var base = "http://www.corion.net/js/chatzilla/telnet/";

    /* a global pref will be created for each item in the pref array.
     * By adding these motif.* preferences, we're creating shortcuts
     * that can be used with the /motif command.
     */
    // No preferences (yet)
    //plugin.prefary =
    //    [/*    pref name                default value             */
    //     ["motif.default-faces", base + "output-default-faces.css"],
    //     ["motif.dark-faces",    base + "output-dark-faces.css"],
    //     ["motif.light-faces",   base + "output-light-faces.css"]
    //    ];

    //client.prefManager.addPrefs(plugin.prefary);

    /* A command will be created for each item in here.
     * The first position contains the command name, then the function to
     * be associated with this command (if this is a string instead of a
     * function, an alias will be created.)  The the flags.  If null is
     * passed, the default flags will be used.  The final column is the
     * parameter list, which is used to parse the command line and initialize
     * the |e| object.
     *
     * The following flags are available:
     *
     *   CMD_CONSOLE   The command is available from the console.
     *
     *   CMD_NEED_NET  The command requires a network.  Your command
     *                 will be *guaranteed* to have a valid e.network object
     *                 when it is invoked, regardless of your parameter
     *                 list.
     *
     *   CMD_NEED_SRV  The command requires a *connected* server.
     *
     *  CMD_NEED_CHAN  The command requires a channel.
     *
     *  CMD_NEED_USER  The command requires a user.
     *
     *    CMD_NO_HELP  Tells the command manager not to complain about commands
     *                 without help text.
     *
     * The default flags are CMD_CONSOLE | CMD_NO_HELP.
     */
    plugin.cmdary =
        [ /* comand name     function               flags    usage*/
          ["telnet",          cmdTelnet, CMD_CONSOLE, "<host> [<port>]","Opens a telnet sesssion to host:port"],
        ];
    client.commandManager.defineCommands(plugin.cmdary);

    /*
     * Now we add our new aliases into the motifs submenu.  We're going
     * to need this utility function to help create the checkedif attributes.
     */
    display(__id + " loaded");
    // return "loaded";
    return;
}

function disablePlugin(status)
{
    display("disabling " + __id);
    // How to remove the command?
}

function enablePlugin(status)
{
    display("enabling " + __id);
}

function cmdTelnet(e) {
  // fake-open a telnet session by sending ourselves a faked DCC
  // chat message with the host
  var host = e.host;
  if (!e.host) {
    e.parseerror = "Need a hostname";
    return false
  };

  if (!e.port) {
    e.port = 23;
  };

  var me = client.dcc.addUser({ properNick: "telnet", nick: "telnet" }, "127.0.0.1");
	var u = client.dcc.addUser({ properNick: host, nick: host, parent: { me: me } }, host);
	display("Connecting to "+host+":"+e.port);
  var c = client.dcc.addChat(u, e.port);
  c.state = DCC_STATE_REQUESTED_IN;
  c.accept();
  c.display("Connected to "+host+":"+e.port);
  // make the telnet tab the active tab:
  setCurrentObject(c);

  return 1;
}


