如何使Delphi TS3 Serverquery IdTelnet作为控制台应用程序运行?

时间:2019-01-11 20:41:59

标签: delphi console-application telnet indy10 delphi-10.2-tokyo

我需要在控制台模式下运行Delphi应用程序,以便可以在Linux服务器上作为Wine模拟应用程序在VPS上运行它,以便它作为服务器查询通过telnet与Teamspeak服务器通信。它需要保持持续的联系,并将玩家从一个渠道转移到另一个渠道,因此被动使用PHP是不可行的。我知道TS3机器人已经存在,但是在Delphi中没有为Teamspeak 3进行编程的。该程序在Windows应用程序中可以完美运行,而只是在控制台版本中挂起。

我在数据模块中有indy设置IdTelnet1.ThreadedEvent:= true,这似乎仅能有所帮助。我在猜测是否需要与该线程交谈,但不确定如何。

我尝试过这种方式,但是程序只是挂起了

program TS3bot;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Unit1 in 'Unit1.pas' {DataModule1: TDataModule};

begin
  try
    DataModule1 := TDataModule1.Create(nil);
    try
      { TODO -oUser -cConsole Main : Insert code here }
      DataModule1.IdTelnet1.Connect;
      DataModule1.IdTelnet1.TelnetThread.Start;
      repeat
        //
      until (DataModule1.IdTelnet1.Connected = false);
      DataModule1.IdTelnet1.TelnetThread.Stop;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    writeln('Program ended.');
    DataModule1.Free;
  end;
end.

UNIT1

unit Unit1;

interface

uses
  System.SysUtils, System.Classes, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdTelnet, IdGlobal;

type
  TDataModule1 = class(TDataModule)
    IdTelnet1: TIdTelnet;
    procedure IdTelnet1Connected(Sender: TObject);
    procedure IdTelnet1DataAvailable(Sender: TIdTelnet; const Buffer: TIdBytes);
    procedure IdTelnet1Disconnected(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
    procedure processCommand(Command : string);
    procedure processCommands;
    procedure InterpetBuffer(Buffer: string);
  public
    { Public declarations }
  end;

Const
  Elements = (3); //(Elements - 1)
  ListOfOnConnectCommands : array [0..Elements] of string =
  ('login serverquery password',
  'use 1',
  'clientupdate client_nickname=NickNameServer',
  'servernotifyregister event=server');

var
  DataModule1: TDataModule1;
  BufferNumber: integer = 0;
  CommandSent : boolean = false;
  CommandOK : boolean = false;
  CommandNumber : integer = 0;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

procedure pSplitIT(BreakString, BaseString: string; StringList: TStrings);
var
  EndOfCurrentString: byte;
begin
  StringList.Clear;

  repeat
    EndOfCurrentString := Pos(BreakString, BaseString);

    if EndOfCurrentString = 0 then
      StringList.add(BaseString)
    else
      StringList.add(Copy(BaseString, 1, EndOfCurrentString - 1));
    BaseString := Copy(BaseString, EndOfCurrentString + length(BreakString), length(BaseString) - EndOfCurrentString);

  until EndOfCurrentString = 0;
end;

procedure TDataModule1.processCommand(Command : string);
begin
  writeln('processCommand: ' + Command);
  IdTelnet1.SendString(Command);
  IdTelnet1.SendCh(#10);
  IdTelnet1.SendCh(#13);
end;

procedure TDataModule1.processCommands;
var
  MyString: string;
begin
  if CommandNumber <= Elements then
  begin
    MyString := ListOfOnConnectCommands[CommandNumber];
    writeln('processCommands: ' + MyString);
    IdTelnet1.SendString(MyString);
    IdTelnet1.SendCh(#10);
    IdTelnet1.SendCh(#13);
    inc(CommandNumber);
    //exit;
  end;
end;

procedure TDataModule1.InterpetBuffer(Buffer: string);
var
  MyTstringlist: Tstringlist;
  MyBuffer: string;
  I: integer;
  clid: integer;
  member, legionnaire, enteredGuestChannel: boolean;
begin
  enteredGuestChannel := false;
  member := false;
  legionnaire := false;

  inc(BufferNumber);
  writeln('---------------------------------------------------------');
  writeln('IdTelnet1DataAvailable BufferNumber: ' + BufferNumber.ToString);
  writeln('---------------------------------------------------------');

  if Pos('notifycliententerview',Buffer)>0 then
    begin
      writeln('----------------------');
      writeln('EXIT notifycliententerview:');
      writeln(Buffer);

      MyTstringlist := Tstringlist.Create;
      MyBuffer := Buffer;

      pSplitIT(' ',MyBuffer,MyTstringlist);
      writeln('COUNT: ' + MyTstringlist.Count.ToString);
      for I := 0 to MyTstringlist.Count - 1 do
      begin
        writeln(MyTstringlist.Strings[I]);
        if MyTstringlist.Strings[I] = 'ctid=45' then
        begin
          // client entered GUESTS CHANNEL, see if we can move them.
          enteredGuestChannel := true;
        end;
        if Pos('client_servergroups=',MyTstringlist.Strings[I]) > 0 then
        begin
          if Pos('18',MyTstringlist.Strings[I]) > 0 then
          begin
            member := true;
          end;
          if Pos('19',MyTstringlist.Strings[I]) > 0 then
          begin
            legionnaire := true;
          end;
          if Pos('28',MyTstringlist.Strings[I]) > 0 then
          begin
            legionnaire := true;
          end;
        end;
        //clid
        if Pos('clid=',MyTstringlist.Strings[I]) > 0 then
        begin
          clid := StrToInt(copy(MyTstringlist.Strings[I],6,high(MyTstringlist.Strings[I])));
        end;

      end;

      writeln('----------------------');

      MyTstringlist.Free;

      if ( (enteredGuestChannel = true)
        and (member = true) ) then
        begin
          processCommand('clientmove clid=' + clid.ToString + ' cid=47');
        end
      else if ( (enteredGuestChannel = true)
        and (legionnaire = true) ) then
        begin
          processCommand('clientmove clid=' + clid.ToString + ' cid=47');
        end;

      exit;
    end;

  // Create Returns in terminal
  if Pos(#10#13,Buffer)>0 then
    begin
      MyTstringlist := Tstringlist.Create;
      pSplitIT(#10#13,Buffer,MyTstringlist);
      writeln('COUNT: ' + MyTstringlist.Count.ToString);
      for I := 0 to MyTstringlist.Count - 1 do
      begin
        writeln(MyTstringlist.Strings[I]);
      end;
      MyTstringlist.Free;
    end
    else
    begin
      writeln(Buffer);
    end;

  if Pos('error id=0 msg=ok',Buffer)>0 then
    begin
      processCommands;
    end;

  writeln('');
end;

procedure TDataModule1.DataModuleDestroy(Sender: TObject);
begin
  processCommand('logout');
  processCommand('quit');
  IdTelnet1.Disconnect(true);
end;

procedure TDataModule1.IdTelnet1Connected(Sender: TObject);
begin
  writeln('IdTelnet1Connected');
//  sleep(5000);
  //writeln('processCommands:');
//  processCommands;
end;

procedure TDataModule1.IdTelnet1DataAvailable(Sender: TIdTelnet;
  const Buffer: TIdBytes);
begin
  InterpetBuffer(bytestostring(Buffer));
end;

procedure TDataModule1.IdTelnet1Disconnected(Sender: TObject);
begin
  writeln('IdTelnet1Disconnected');
end;

end.

这是我来自TForm的原始代码:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdTelnet, IdGlobal, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    IdTelnet1: TIdTelnet;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo2: TMemo;
    Timer1: TTimer;
    Button4: TButton;
    IdSchedulerOfThreadDefault1: TIdSchedulerOfThreadDefault;
    procedure Button1Click(Sender: TObject);
    procedure IdTelnet1Disconnected(Sender: TObject);
    procedure IdTelnet1DataAvailable(Sender: TIdTelnet; const Buffer: TIdBytes);
    procedure Button3Click(Sender: TObject);
    procedure IdTelnet1Status(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: string);
    procedure IdTelnet1Connected(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure InterpetBuffer(buffer: string);
    procedure processCommands;
    procedure processCommand(Command : string);
  public
    { Public declarations }
  end;

Const
  Elements = (3); //(Elements - 1)
  ListOfOnConnectCommands : array [0..Elements] of string =
  ('login severquery password',
  'use 1',
  'clientupdate client_nickname=NickNameServer',
  'servernotifyregister event=server');

var
  Form1: TForm1;
  BufferNumber: integer = 0;
  CommandSent : boolean = false;
  CommandOK : boolean = false;
  CommandNumber : integer = 0;

implementation

{$R *.dfm}


procedure pSplitIT(BreakString, BaseString: string; StringList: TStrings);
var
  EndOfCurrentString: byte;
begin
  StringList.Clear;

  repeat
    EndOfCurrentString := Pos(BreakString, BaseString);

    if EndOfCurrentString = 0 then
      StringList.add(BaseString)
    else
      StringList.add(Copy(BaseString, 1, EndOfCurrentString - 1));
    BaseString := Copy(BaseString, EndOfCurrentString + length(BreakString), length(BaseString) - EndOfCurrentString);

  until EndOfCurrentString = 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  BufferNumber := 0;
  IdTelnet1.Connect;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  MyString: string;
  I: integer;
begin
  MyString := edit1.Text;
  for I := Low(MyString) to High(MyString) do
  begin
    IdTelnet1.SendCh(MyString[I]);
  end;
  IdTelnet1.SendCh(#10);
  IdTelnet1.SendCh(#13);
  edit1.Text := '';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  processCommand('logout');
  processCommand('quit');
  IdTelnet1.Disconnect(true);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  processCommands;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  processCommand('logout');
  processCommand('quit');

  IdTelnet1.Disconnect(true);
end;

procedure TForm1.IdTelnet1Connected(Sender: TObject);
begin
  memo1.Lines.Add('IdTelnet1Connected');
  // Wait 1 second after connected to send commands.
  Timer1.Enabled := true;
end;

procedure TForm1.InterpetBuffer(Buffer: string);
var
  MyTstringlist: Tstringlist;
  MyBuffer: string;
  I: integer;
  //ctid: integer;
  clid: integer;
  member, legionnaire, enteredGuestChannel: boolean;
begin
  inc(BufferNumber);
  memo1.Lines.Add('---------------------------------------------------------');
  memo1.Lines.Add('IdTelnet1DataAvailable BufferNumber: ' + BufferNumber.ToString);
  memo1.Lines.Add('---------------------------------------------------------');

  OutputDebugString(PChar('IdTelnet1DataAvailable BufferNumber: ' + BufferNumber.ToString));

  if Pos('notifycliententerview',Buffer)>0 then
    begin
      memo1.Lines.Add('----------------------');
      memo1.Lines.Add('EXIT notifycliententerview:');
      memo1.Lines.Add(Buffer);

      MyTstringlist := Tstringlist.Create;
      MyBuffer := Buffer;

      pSplitIT(' ',MyBuffer,MyTstringlist);
      memo1.Lines.Add('COUNT: ' + MyTstringlist.Count.ToString);
      for I := 0 to MyTstringlist.Count - 1 do
      begin
        memo1.Lines.Add(MyTstringlist.Strings[I]);
        if MyTstringlist.Strings[I] = 'ctid=45' then
        begin
          // client entered GUESTS CHANNEL, see if we can move them.
          enteredGuestChannel := true;
        end;
        if Pos('client_servergroups=',MyTstringlist.Strings[I]) > 0 then
        begin
          if Pos('18',MyTstringlist.Strings[I]) > 0 then
          begin
            member := true;
          end;
          if Pos('19',MyTstringlist.Strings[I]) > 0 then
          begin
            legionnaire := true;
          end;
          if Pos('28',MyTstringlist.Strings[I]) > 0 then
          begin
            legionnaire := true;
          end;
        end;
        //clid
        if Pos('clid=',MyTstringlist.Strings[I]) > 0 then
        begin
          clid := StrToInt(copy(MyTstringlist.Strings[I],6,high(MyTstringlist.Strings[I])));
        end;

      end;

      memo1.Lines.Add('----------------------');

      MyTstringlist.Free;

      if ( (enteredGuestChannel = true)
        and (member = true) ) then
        begin
          processCommand('clientmove clid=' + clid.ToString + ' cid=47');
        end
      else if ( (enteredGuestChannel = true)
        and (legionnaire = true) ) then
        begin
          processCommand('clientmove clid=' + clid.ToString + ' cid=47');
        end;

      exit;
    end;

  // Create Returns in terminal
  if Pos(#10#13,Buffer)>0 then
    begin
      MyTstringlist := Tstringlist.Create;
      pSplitIT(#10#13,Buffer,MyTstringlist);
      memo1.Lines.Add('COUNT: ' + MyTstringlist.Count.ToString);
      for I := 0 to MyTstringlist.Count - 1 do
      begin
        memo1.Lines.Add(MyTstringlist.Strings[I]);
      end;
      MyTstringlist.Free;
    end
    else
    begin
      memo1.Lines.Add(Buffer);
    end;

  if Pos('error id=0 msg=ok',Buffer)>0 then
    begin
      processCommands;
    end;

  memo1.Lines.Add('');
end;

procedure TForm1.processCommand(Command : string);
begin
  memo2.Lines.Add('processCommand: ' + Command);
  IdTelnet1.SendString(Command);
  IdTelnet1.SendCh(#10);
  IdTelnet1.SendCh(#13);
end;

procedure TForm1.processCommands;
var
  MyString: string;
begin
  if CommandNumber <= Elements then
  begin
    MyString := ListOfOnConnectCommands[CommandNumber];
    memo2.Lines.Add('processCommands: ' + MyString);
    IdTelnet1.SendString(MyString);
    IdTelnet1.SendCh(#10);
    IdTelnet1.SendCh(#13);
    inc(CommandNumber);
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if IdTelnet1.Connected = false then
  begin
    memo2.Lines.Add('NOT CONNECTED YET...WAITING TO SEND COMMANDS.');
    exit;
  end;

  processCommands;
  Timer1.Enabled := false;
end;

procedure TForm1.IdTelnet1DataAvailable(Sender: TIdTelnet;
  const Buffer: TIdBytes);
begin
  InterpetBuffer(bytestostring(Buffer));
end;

procedure TForm1.IdTelnet1Disconnected(Sender: TObject);
begin
  memo1.Lines.Add('IdTelnet1Disconnected');
end;

procedure TForm1.IdTelnet1Status(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: string);
begin
  memo1.Lines.Add('AStatusText: ' + AStatusText);
end;

end.

我希望程序可以像本身的GUI Windows版本一样在控制台中运行,但是我不确定如何使用indy idTelnet进行此操作。我主要将其转换为我所知以及在Internet上可以找到的东西(不多)。我需要以某种方式找出导致它挂起的原因,而不是处理telnet消息吗?

1 个答案:

答案 0 :(得分:0)

好的,我做了很多研究,找到了一些代码来帮助我使用控制台应用程序。

我知道雷米·勒博(Remy Lebeau)说过Teamspeak并不使用telnet协议,但是我使用了它,并且效果很好。

功劳必须归功于此网页上的Tony Caduto:http://www.44342.com/delphi-f1279-t5081-p1.htm遵循链接并转到页面底部。搜索Tony Caduto的最新回复。

此代码现在有效!

现在我可以通过WINE在运行Linux的VPS上运行它了!我只是没有钱购买可以在Linux上运行的更昂贵版本的Delphi。我将等到他们为像我这样的小型开发人员发布一个版本,使我们能够在Linux上运行delphi应用程序。在那之前,WINE就是这样。

edit:确保您将IdTelnet1.TelnetThread.Loop:= true;在IdTelnet1.Connect之后;否则会出错。

代码如下:

program TS3bot;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Unit1 in 'Unit1.pas' {DataModule1: TDataModule};

begin
  try
    DataModule1 := TDataModule1.Create(nil);
    with DataModule1 do
    try
      { TODO -oUser -cConsole Main : Insert code here }
      IdTelnet1.ThreadedEvent := true;
      IdTelnet1.Connect;
      IdTelnet1.TelnetThread.Loop := true;
      while (IdTelnet1.Connected = true) do
      begin
        Sleep(60);
      end;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    writeln('Program ended.');
    freeandnil(DataModule1);
  end;
end.

这是另一部分,以防万一它发生变化:

unit Unit1;

interface

uses
  System.SysUtils, System.Classes, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdTelnet, IdGlobal;

type
  TDataModule1 = class(TDataModule)
    IdTelnet1: TIdTelnet;
    procedure IdTelnet1Connected(Sender: TObject);
    procedure IdTelnet1DataAvailable(Sender: TIdTelnet; const Buffer: TIdBytes);
    procedure IdTelnet1Disconnected(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
    procedure processCommand(Command : string);
    procedure processCommands;
    procedure InterpetBuffer(Buffer: string);
  public
    { Public declarations }
  end;

Const
  Elements = (3); //(Elements - 1)
  ListOfOnConnectCommands : array [0..Elements] of string =
  ('login serverquery mypassword',
  'use 1',
  'clientupdate client_nickname=ServerNickName',
  'servernotifyregister event=server');

var
  DataModule1: TDataModule1;
  BufferNumber: integer = 0;
  CommandSent : boolean = false;
  CommandOK : boolean = false;
  CommandNumber : integer = 0;

implementation

{%CLASSGROUP 'System.Classes.TPersistent'}

{$R *.dfm}

procedure pSplitIT(BreakString, BaseString: string; StringList: TStrings);
var
  EndOfCurrentString: byte;
begin
  StringList.Clear;

  repeat
    EndOfCurrentString := Pos(BreakString, BaseString);

    if EndOfCurrentString = 0 then
      StringList.add(BaseString)
    else
      StringList.add(Copy(BaseString, 1, EndOfCurrentString - 1));
    BaseString := Copy(BaseString, EndOfCurrentString + length(BreakString), length(BaseString) - EndOfCurrentString);

  until EndOfCurrentString = 0;
end;

procedure TDataModule1.processCommand(Command : string);
begin
  writeln('processCommand: ' + Command);
  IdTelnet1.SendString(Command);
  IdTelnet1.SendCh(#10);
  IdTelnet1.SendCh(#13);
end;

procedure TDataModule1.processCommands;
var
  MyString: string;
begin
  if CommandNumber <= Elements then
  begin
    MyString := ListOfOnConnectCommands[CommandNumber];
    writeln('processCommands: ' + MyString);
    IdTelnet1.SendString(MyString);
    IdTelnet1.SendCh(#10);
    IdTelnet1.SendCh(#13);
    inc(CommandNumber);
    //exit;
  end;
end;

procedure TDataModule1.InterpetBuffer(Buffer: string);
var
  MyTstringlist: Tstringlist;
  MyBuffer: string;
  I: integer;
  clid: integer;
  member, legionnaire, enteredGuestChannel: boolean;
begin
  enteredGuestChannel := false;
  member := false;
  legionnaire := false;

  inc(BufferNumber);
  writeln('---------------------------------------------------------');
  writeln('IdTelnet1DataAvailable BufferNumber: ' + BufferNumber.ToString);
  writeln('---------------------------------------------------------');

  if Pos('notifycliententerview',Buffer)>0 then
    begin
      writeln('----------------------');
      writeln('EXIT notifycliententerview:');
      writeln(Buffer);

      MyTstringlist := Tstringlist.Create;
      MyBuffer := Buffer;

      pSplitIT(' ',MyBuffer,MyTstringlist);
      writeln('COUNT: ' + MyTstringlist.Count.ToString);
      for I := 0 to MyTstringlist.Count - 1 do
      begin
        writeln(MyTstringlist.Strings[I]);
        if MyTstringlist.Strings[I] = 'ctid=45' then
        begin
          // client entered GUESTS CHANNEL, see if we can move them.
          enteredGuestChannel := true;
        end;
        if Pos('client_servergroups=',MyTstringlist.Strings[I]) > 0 then
        begin
          if Pos('18',MyTstringlist.Strings[I]) > 0 then
          begin
            member := true;
          end;
          if Pos('19',MyTstringlist.Strings[I]) > 0 then
          begin
            legionnaire := true;
          end;
          if Pos('28',MyTstringlist.Strings[I]) > 0 then
          begin
            legionnaire := true;
          end;
        end;
        //clid
        if Pos('clid=',MyTstringlist.Strings[I]) > 0 then
        begin
          clid := StrToInt(copy(MyTstringlist.Strings[I],6,high(MyTstringlist.Strings[I])));
        end;

      end;

      writeln('----------------------');

      MyTstringlist.Free;

      if ( (enteredGuestChannel = true)
        and (member = true) ) then
        begin
          processCommand('clientmove clid=' + clid.ToString + ' cid=47');
        end
      else if ( (enteredGuestChannel = true)
        and (legionnaire = true) ) then
        begin
          processCommand('clientmove clid=' + clid.ToString + ' cid=47');
        end;

      exit;
    end;

  // Create Returns in terminal
  if Pos(#10#13,Buffer)>0 then
    begin
      MyTstringlist := Tstringlist.Create;
      pSplitIT(#10#13,Buffer,MyTstringlist);
      writeln('COUNT: ' + MyTstringlist.Count.ToString);
      for I := 0 to MyTstringlist.Count - 1 do
      begin
        writeln(MyTstringlist.Strings[I]);
      end;
      MyTstringlist.Free;
    end
    else
    begin
      writeln(Buffer);
    end;

  if Pos('error id=0 msg=ok',Buffer)>0 then
    begin
      processCommands;
    end;

  writeln('');
end;

procedure TDataModule1.DataModuleDestroy(Sender: TObject);
begin
  processCommand('logout');
  processCommand('quit');
  IdTelnet1.Disconnect(true);
end;

procedure TDataModule1.IdTelnet1Connected(Sender: TObject);
begin
  writeln('IdTelnet1Connected');
  sleep(5000);
  writeln('processCommands:');
  processCommands;
end;

procedure TDataModule1.IdTelnet1DataAvailable(Sender: TIdTelnet;
  const Buffer: TIdBytes);
begin
  InterpetBuffer(bytestostring(Buffer));
end;

procedure TDataModule1.IdTelnet1Disconnected(Sender: TObject);
begin
  writeln('IdTelnet1Disconnected');
end;

end.