#!/usr/bin/env expect

# $Id: remote-shell.expect,v 1.2 2004/12/11 19:58:36 cbiere Exp $

# Copyright 2004 by Thomas Schuerger (thomas@schuerger.com)
#
# Usage: remote-shell [port]
#
# This Expect script tries to connect to the remote shell
# port of a locally running GTKG instance and tries to log in
# using the ~/.gtk-gnutella/auth_cookie file. If successful,
# it will switch to interactive mode to let the user use
# GTKG's shell.
#
# The default port number used is 6346.
#
# Expect can be found here: http://expect.nist.gov

set fd [open "~/.gtk-gnutella/auth_cookie" "r"]
gets $fd cookie
close $fd

set port "[lrange $argv 0 0]"

if {$port == ""} {
  set port 6346
}

# set timeout to 3 seconds
set timeout 3

# use telnet to connect to GTKG's port
spawn telnet 127.0.0.1 $port

expect {
  timeout {send_user "Unable to connect to gtk-gnutella on port $port.\n";exit}
  "Connection refused" {send_user "Unable to connect to gtk-gnutella on port $port.\n";exit}
  "Connected to " {}
}

# send authentication cookie
send "HELO $cookie\n"

expect {
  "100 " {}
  "400 " {exit}
  timeout {send_user "gtk-gnutella not available on port $port.\n";exit}
} 

# connection was successful, switch to interactive mode
interact
