unit main_form; { This file is part of AMIProxyPal. AMIProxyPal is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Foobar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Foobar. If not, see . } {$mode objfpc}{$H+} interface uses Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, blcksock , StdCtrls, ExtCtrls; type TForm1 = class( TForm) btnConnect: TButton; btnDisconnect: TButton; btnSendRequest: TButton; ckEventsOn: TCheckBox; ckRestrict: TCheckBox; ePort: TEdit; eChannel: TEdit; eUsername: TEdit; eSecret: TEdit; eHost: TEdit; Label1: TLabel; Label2: TLabel; Password: TLabel; memResponses: TMemo; memRequest: TMemo; Panel1: TPanel; Password1: TLabel; lblRestrictChannel: TLabel; Splitter1: TSplitter; Timer1: TTimer; procedure btnConnectClick( Sender: TObject) ; procedure btnDisconnectClick( Sender: TObject) ; procedure btnSendRequestClick( Sender: TObject) ; procedure FormClose( Sender: TObject;var CloseAction: TCloseAction) ; procedure FormCreate( Sender: TObject) ; procedure FormShow( Sender: TObject) ; procedure Timer1Timer( Sender: TObject) ; private FSocket: TTCPBlockSocket; slOutgoing: TStringList; procedure UpdateConnectState(AConnected: boolean); public { public declarations } end; var Form1: TForm1; implementation { TForm1 } procedure TForm1.btnConnectClick( Sender: TObject) ; var sSend, sResponse: string; sClientID, sChannel: string; begin if FSocket = nil then FSocket := TTCPBlockSocket.Create; FSocket.SetTimeout(5000); FSocket.GetSins; FSocket.Connect(EHost.Text, ePort.Text); if FSocket.LastError <> 0 then begin ShowMessage(FSocket.LastErrorDesc); exit; end; sClientID := BoolToStr(ckRestrict.Checked, true); sChannel := eChannel.Text; FSocket.SendString('Action: Login' + #13#10 + 'Username: ' + eUsername.text + #13#10 + 'Secret: ' + eSecret.Text + #13#10 + 'Events: On' + #13#10 + 'ByClientID: ' + sClientID + #13#10+ 'RestrictChannel: ' + sChannel + #13#10#13#10); sResponse := FSocket.RecvTerminated(3000, #13#10#13#10); if POS('Success', sResponse) = 0 then begin FSocket.CloseSocket; FreeAndNil(FSocket); UpdateConnectState(false); end else begin UpdateConnectState(true); Timer1.Enabled := true; end; end; procedure TForm1.btnDisconnectClick( Sender: TObject) ; begin Timer1.Enabled := false; FSocket.CloseSocket; FreeAndNil(FSocket); UpdateConnectState(false); end; procedure TForm1.btnSendRequestClick( Sender: TObject) ; begin if memRequest.Text <>'' then begin slOutgoing.Add(Trim(memRequest.Text)); memRequest.Clear; end; end; procedure TForm1.FormClose( Sender: TObject;var CloseAction: TCloseAction) ; begin slOutgoing.free; CloseAction := cafree; end; procedure TForm1.FormCreate( Sender: TObject) ; begin slOutgoing := TStringList.create; end; procedure TForm1.FormShow( Sender: TObject) ; begin UpdateConnectState(false); end; procedure TForm1.Timer1Timer( Sender: TObject) ; var s: string; iError, icounter: integer; begin Timer1.Enabled := false; try s := FSocket.RecvTerminated(50, #13#10#13#10); iError := FSocket.LastError; if (iError <> 0) and (iError <> 10060) then begin FSocket.CloseSocket; FreeAndNil(FSocket); UpdateConnectState(false); exit; end; if s <> '' then begin s := StringReplace(s, #10, #13#10, [rfReplaceAll]); memResponses.Append(LineEnding + s + LineEnding); end; for iCounter := slOutgoing.Count -1 downto 0 do begin FSocket.SendString(slOutgoing[iCounter] + #13#10#13#10); slOutgoing.Delete(iCounter); end; finally if (iError = 0) or (iError = 10060) then Timer1.Enabled := true; end; Application.ProcessMessages; end; procedure TForm1.UpdateConnectState( AConnected: boolean) ; begin if Aconnected then begin btnConnect.Enabled := false; btnDisconnect.Enabled := true; btnSendRequest.Enabled := true; end else begin btnConnect.Enabled := true; btnDisconnect.Enabled := false; btnSendRequest.Enabled := false; end; end; initialization {$I main_form.lrs} end.