unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Ping, TFlatHintUnit, StdCtrls, TFlatMemoUnit, TFlatEditUnit,
  TFlatButtonUnit, ExtCtrls, TFlatPanelUnit, Winsock, BUFormAnimation;

type
  TForm3 = class(TForm)
    FlatPanel1: TFlatPanel;
    FlatButton1: TFlatButton;
    FlatButton2: TFlatButton;
    FlatPanel2: TFlatPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TFlatEdit;
    Edit2: TFlatEdit;
    Edit3: TFlatEdit;
    Memo1: TFlatMemo;
    btnResolvehost: TFlatButton;
    btnPing: TFlatButton;
    btnResolveuin: TFlatButton;
    btnClear: TFlatButton;
    FlatHint1: TFlatHint;
    Ping1: TPing;
    BUFormAnimation3: TBUFormAnimation;
    procedure btnClearClick(Sender: TObject);
    procedure btnResolvehostClick(Sender: TObject);
    procedure Ping1Display(Sender, Icmp: TObject; Msg: String);
    procedure Ping1DnsLookupDone(Sender: TObject; Error: Word);
    procedure Ping1EchoReply(Sender, Icmp: TObject; Error: Integer);
    procedure Ping1EchoRequest(Sender, Icmp: TObject);
    procedure btnPingClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnResolveuinClick(Sender: TObject);
    procedure FlatPanel1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FlatButton2Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FlatButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.DFM}

const
 ICQMAPI_DLL = 'ICQMAPI.dll';

type
 BPSICQAPI_User = ^BSICQAPI_User;
 BSICQAPI_User = record
   m_iUIN: Integer; { the user's ICQ #. }
   m_hFloatWindow: HWND; { the handle of the "Float" window if the user is floating. }
   m_iIP: Integer; { the user's IP address. }
   m_szNickname: array[0..19] of Char; { the user's nickname. }
   m_szFirstName: array[0..19] of Char; { the user's first name. }
   m_szLastName: array[0..19] of Char; { the user's last name. }
   m_szEmail: array[0..99] of Char; { the user's email address. }
   m_szCity: array[0..99] of Char; { the user's city. }
   m_szState: array[0..99] of Char; { the user's state. }
   m_iCountry: Integer; { the user's country code. }
   m_szCountryName: array[0..99] of Char; { the user's country name. }
   m_szHomePage: array[0..99] of Char; { the user's homepage. }
   m_iAge: Integer; { the user's age. }
   m_szPhone: array[0..19] of Char; { the user's phone. }
   m_bGender: Byte; { the user's gender. The codes are : 0 - Not Specified,  1 - Female,  2 - Male. 
}
   m_iHomeZip: Integer; { the user's zip code. }
   { Version 1.0001 only }
   m_iStateFlags: Integer; { State Flags }
 end;

function ICQAPICall_SetLicenseKey(pszName, pszPassword, pszLicense: PChar): BOOL; stdcall;
 external ICQMAPI_DLL name 'ICQAPICall_SetLicenseKey';

function ICQAPICall_GetVersion(var iVersion: Integer): BOOL; stdcall;
 external ICQMAPI_DLL name 'ICQAPICall_GetVersion';

function ICQAPICall_GetFullUserData(var pUser: BSICQAPI_User; iVersion: Integer): BOOL; stdcall;
 external ICQMAPI_DLL name 'ICQAPICall_GetFullUserData';

procedure ICQAPIUtil_FreeUser(pUser: BPSICQAPI_User); stdcall;
 external ICQMAPI_DLL name 'ICQAPIUtil_FreeUser';

var
 ICQLoaded: boolean = False;
 ICQ_Version: Integer;

 function HostToIP(Name: string; var Ip: string): Boolean; 
var 
  wsdata : TWSAData;
  hostName : array [0..255] of char; 
  hostEnt : PHostEnt; 
  addr : PChar; 
begin 
  WSAStartup ($0101, wsdata); 
  try 
    gethostname (hostName, sizeof (hostName)); 
    StrPCopy(hostName, Name); 
    hostEnt := gethostbyname (hostName); 
    if Assigned (hostEnt) then 
      if Assigned (hostEnt^.h_addr_list) then begin 
        addr := hostEnt^.h_addr_list^; 
        if Assigned (addr) then begin 
          IP := Format ('%d.%d.%d.%d', [byte (addr [0]), 
          byte (addr [1]), byte (addr [2]), byte (addr [3])]); 
          Result := True; 
        end 
        else 
          Result := False; 
      end 
      else 
        Result := False 
    else begin 
      Result := False; 
    end; 
  finally 
    WSACleanup; 
  end 
end;




procedure TForm3.btnClearClick(Sender: TObject);
begin
Memo1.Clear;
end;

procedure TForm3.btnResolvehostClick(Sender: TObject);
var 
IP: string; 
begin 
if HostToIp(Edit1.Text, IP) then Memo1.Lines.Add ('Hosts ip address is' + IP);
end;

procedure TForm3.Ping1Display(Sender, Icmp: TObject; Msg: String);
begin
Memo1.Lines.Add(Msg);
end;

procedure TForm3.Ping1DnsLookupDone(Sender: TObject; Error: Word);
begin
if Error <> 0 then begin
        Memo1.Lines.Add('Unknown Host ''' + Edit2.Text + '''');
        Exit;
    end;

    Memo1.Lines.Add('Host ''' + Edit2.Text + ''' is ' + Ping1.DnsResult);
    Ping1.Address := Ping1.DnsResult;
    Ping1.Ping;
end;

procedure TForm3.Ping1EchoReply(Sender, Icmp: TObject; Error: Integer);
begin
if Error = 0 then
        Memo1.Lines.Add('Cannot ping host (' + Ping1.HostIP + ') : ' +
                              Ping1.ErrorString)
    else
        Memo1.Lines.Add('Received ' + IntToStr(Ping1.Reply.DataSize) +
                              ' bytes from ' + Ping1.HostIP +
                              ' in ' + IntToStr(Ping1.Reply.RTT) + ' msecs');
end;

procedure TForm3.Ping1EchoRequest(Sender, Icmp: TObject);
begin
Memo1.Lines.Add('Sending ' + IntToStr(Ping1.Size) + ' bytes to ' +
                          Ping1.HostName + ' (' + Ping1.HostIP + ')');
end;

procedure TForm3.btnPingClick(Sender: TObject);
begin
Memo1.Lines.Add('Resolving host ''' + Edit2.Text + '''');
Ping1.DnsLookup(Edit2.Text);
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
ICQLoaded := (FindWindow(NIL,'ICQMsgAPI Window') <> 0) and
   ICQAPICall_SetLicenseKey('neo','sheeptown','18AD67AAF5AAC1BA');
 if not ICQLoaded then
   Memo1.Lines.Add('ERROR: ICQ not loaded or no license') else
   ICQAPICall_GetVersion(ICQ_Version);

Memo1.Clear;
end;

procedure TForm3.btnResolveuinClick(Sender: TObject);
var
 ICQ_User: BSICQAPI_User;
 IP: string;
begin
 if not ICQLoaded then Exit;
 FillChar(ICQ_User, SizeOf(BSICQAPI_User), 0);
 ICQ_User.m_iUIN := StrToInt(Edit3.Text);
 if ICQAPICall_GetFullUserData(ICQ_User, ICQ_Version) then
   begin
     IP := StrPas(inet_ntoa(TInAddr(ICQ_User.m_iIP))); // uses the winsock.dll
     Memo1.Lines.Add('IP for UIN ' + IntToStr(ICQ_User.m_iUIN) + ' - ' + IP);
     ICQAPIUtil_FreeUser(@ICQ_User);
   end;

end;


procedure TForm3.FlatPanel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 ReleaseCapture;
 Perform(WM_NCLBUTTONDOWN,HTCAPTION,0);

end;

procedure TForm3.FlatButton2Click(Sender: TObject);
begin
Form3.WindowState:=wsMinimized;
end;

procedure TForm3.FormActivate(Sender: TObject);
begin
BUFormAnimation3.AppearRectangle;
end;

procedure TForm3.FlatButton1Click(Sender: TObject);
begin
Close;
end;

end.
