Indy Telnet problem

Giganews Newsgroups
Subject: Indy Telnet problem
Posted by:  Solomon Grundy (nobo…@nowhere.com)
Date: Tue, 11 Jan 2005

I've been using Indy Telnet but have a curious problem
It was working and I have not modified the code within the program that I
can see, in such a way as to cause the problems I'm getting. The problem is
that the incoming text is being truncated.

My actual received (on the TMemo) text is:

- received -
^L
@@@  @@@@@@  @@@        @@@@@@  @@@  @@@
- end of received -

My expected receicved tex (which I was receiving earlier) was identical to
this (copied from a basic Telnet program already supplied with Windows)

- expected -
^L

  _  _  _  _  _  _  _    _  _    _  _  _  _
/ \ / \ / \ / \ / \ / \ / \  / \ / \  / \ / \ / \ / \
( I | s | l | a | n | d | s ) ( o | f ) ( M | y | t | h )
\_/ \_/ \_/ \_/ \_/ \_/ \_/  \_/ \_/  \_/ \_/ \_/ \_/

----------------------------------------------------------------------------
Islands of Myth is running a LIMA mudlib base, heavily modified.
----------------------------------------------------------------------------
            __________________________________________________
            |                                                  |
            |Email: m…@islandsofmyth.org                      |
            |      Currently there are 52 players playing.    |__
            |__________________________________________________|  |
              |                                                  |
              |          [name]  - Enter the game as [name]      |
              |          [guest] - Visit the game.                |
              |          [d]    - Disconnect.                    |
              |          [s]    - Status of the game.            |
              |          [w]    - Who is currently playing.      |
              |          [c]    - Create a new character.        |
              |___________________________________________________|

Please enter a name or choice from the above menu.
>
- end of expected -

I'm not sure where the problem lies. Here's my code. Can you see it?
unit TelnetU;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdTelnet, ExtCtrls, ComCtrls, Grids, ValEdit;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Edit1: TEdit;
    Server: TLabeledEdit;
    Port: TLabeledEdit;
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Button1Edit: TLabeledEdit;
    LabeledEdit1: TLabeledEdit;
    LabeledEdit2: TLabeledEdit;
    LabeledEdit3: TLabeledEdit;
    LabeledEdit5: TLabeledEdit;
    LabeledEdit6: TLabeledEdit;
    LabeledEdit7: TLabeledEdit;
    ValueListEditor1: TValueListEditor;
    IdTelnet1: TIdTelnet;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure IdTelnet1DataAvailable(Sender: TIdTelnet;
      const Buffer: String);
    procedure Memo1Change(Sender: TObject);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button4Click(Sender: TObject);
    procedure Button1EditChange(Sender: TObject);
    procedure LabeledEdit1Change(Sender: TObject);
    procedure LabeledEdit2Change(Sender: TObject);
    procedure LabeledEdit3Change(Sender: TObject);
    procedure LabeledEdit5Change(Sender: TObject);
    procedure LabeledEdit6Change(Sender: TObject);
    procedure LabeledEdit7Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private
    { Private declarations }
    procedure AutomaticActions(IncomingText: String);
    procedure SendCommand(CommandToPerform : String);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  CommandList : TStringList;
  ListPosition : Integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
IdTelnet1.Port := StrToInt(Port.Text);
IdTelnet1.Host := Server.Text;
IdTelnet1.Connect;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
IdTelnet1.disconnect;
end;

procedure TForm1.IdTelnet1DataAvailable(Sender: TIdTelnet;
  const Buffer: String);
var CleanedBuffer : String;
    Counter : Integer;
begin
CleanedBuffer := Buffer;
for Counter := 0 to 99 do
  begin
  CleanedBuffer :=
StringReplace(CleanedBuffer,'['+IntToStr(Counter)+'m','',[rfReplaceAll]);
  end;
Memo1.Lines.Add(CleanedBuffer);
// now parse for automatic actions
AutomaticActions(CleanedBuffer);
end;

procedure TForm1.Memo1Change(Sender: TObject);
begin
  while Memo1.Lines.Count > 100 do Memo1.Lines.Delete(0);
  Memo1.SelStart := Memo1.GetTextLen;
  Memo1.Perform(EM_SCROLLCARET, 0, 0);
end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if Key = VK_RETURN then
  begin
  Edit1.SelectAll;
  // send data
  SendCommand(Edit1.Text);
//add new command to the list
  CommandList.Add(Edit1.Text);
//reduce command list to 15 items max
// remove first blank
  if CommandList.Strings[0] = '' then CommandList.Delete(0);
  while CommandList.Count > 15 do
    begin
    CommandList.Delete(0);
    end;
  end;
if Key = VK_UP then
  begin
  if ListPosition > 0 then ListPosition := ListPosition -1;
  Edit1.Text := CommandList.Strings[ListPosition];
  end;
if Key = VK_DOWN then
  begin
  if ((ListPosition < 15) and (ListPosition < CommandList.Count-1) and
(CommandList.Count > 0)) then ListPosition := ListPosition +1;
  Edit1.Text := CommandList.Strings[ListPosition];
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  LoadList : TStringList;
  LoadCounter : Integer;
begin
CommandList := TStringList.Create;
ListPosition := 0;
CommandList.Add('');
LoadList := TStringList.Create;
if FileExists(ExtractFilePath(application.ExeName) + 'ProgramData.txt') then
  begin
  LoadList.LoadFromFile(ExtractFilePath(application.ExeName) +
'ProgramData.txt');
  Server.Text := LoadList.Strings[0];
  Port.Text := LoadList.Strings[1];
  Button1Edit.Text := LoadList.Strings[2];
  LabeledEdit1.Text := LoadList.Strings[3];
  LabeledEdit2.Text := LoadList.Strings[4];
  LabeledEdit3.Text := LoadList.Strings[5];
  LabeledEdit5.Text := LoadList.Strings[6];
  LabeledEdit6.Text := LoadList.Strings[7];
  LabeledEdit7.Text := LoadList.Strings[8];
  // now do buttons?
  LoadCounter := 9;
  while LoadCounter < LoadList.Count-1 do
    begin
    ValueListEditor1.InsertRow(LoadList.Strings[LoadCounter],LoadList.Strings[LoadCounter+1],True);
    LoadCounter := LoadCounter +2;
    end;
  LoadList.Free;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  LoadList : TStringList;
  LoadCounter : Integer;
begin
LoadList := TStringList.Create;
LoadList.Add(Server.Text);
LoadList.Add(Port.Text);
LoadList.Add(Button1Edit.Text);
LoadList.Add(LabeledEdit1.Text);
LoadList.Add(LabeledEdit2.Text);
LoadList.Add(LabeledEdit3.Text);
LoadList.Add(LabeledEdit5.Text);
LoadList.Add(LabeledEdit6.Text);
LoadList.Add(LabeledEdit7.Text);
// now add stuff from automatic actions box
for LoadCounter := 1 to ValueListEditor1.RowCount-1 do
  begin
  LoadList.Add(ValueListEditor1.Cells[0,LoadCounter]);
  LoadList.Add(ValueListEditor1.Cells[1,LoadCounter]);
  end;
LoadList.SaveToFile(ExtractFilePath(application.ExeName) +
'ProgramData.txt');
LoadList.Free;
CommandList.Free;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
SendCommand(Button4.Caption);
end;

procedure TForm1.Button1EditChange(Sender: TObject);
begin
Button3.Caption := Button1Edit.Text;
end;

procedure TForm1.LabeledEdit1Change(Sender: TObject);
begin
Button4.Caption := LabeledEdit1.Text;
end;

procedure TForm1.LabeledEdit2Change(Sender: TObject);
begin
Button5.Caption := LabeledEdit2.Text;
end;

procedure TForm1.LabeledEdit3Change(Sender: TObject);
begin
Button6.Caption := LabeledEdit3.Text;
end;

procedure TForm1.LabeledEdit5Change(Sender: TObject);
begin
Button7.Caption := LabeledEdit5.Text;
end;

procedure TForm1.LabeledEdit6Change(Sender: TObject);
begin
Button8.Caption := LabeledEdit6.Text;
end;

procedure TForm1.LabeledEdit7Change(Sender: TObject);
begin
Button9.Caption := LabeledEdit7.Text;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
SendCommand(Button3.Caption);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
SendCommand(Button5.Caption);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
SendCommand(Button6.Caption);
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
SendCommand(Button7.Caption);
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
SendCommand(Button8.Caption);
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
SendCommand(Button9.Caption);
end;

procedure TForm1.SendCommand(CommandToPerform : String);
var CCount : Integer;
begin
  for CCount := 1 to Length(CommandToPerform) do
    begin
    idtelnet1.SendCh(CommandToPerform[CCount]);
    end;
idTelnet1.SendCh(#13);
// sends the command to TIdTelnet

end;

procedure TForm1.AutomaticActions(IncomingText : String);
var ActionCounter : Integer;
begin
// now parse the text
// simple matches first
for ActionCounter := 1 to ValueListEditor1.RowCount-1 do
  begin
  if Pos(ValueListEditor1.Keys[ActionCounter],IncomingText) > 0 then
SendCommand(ValueListEditor1.Cells[1,ActionCounter]);
  end;
//

end;

end.

Replies