Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit uOnlineSimRu;
- (*
- Class for OnlineSim.Ru
- Date : 04.04.2014
- Author: HEX0x29A
- Notes :
- *Requere Delphi XE+
- *)
- interface
- uses
- Windows, SysUtils, Classes, ExtCtrls, IdHttp, IdAntiFreezeBase, IdAntiFreeze,
- RegularExpressions;
- const
- USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080401';
- type
- TSms = packed record
- author : string;
- content: string;
- end;
- TSmsList = array of TSms;
- TPhoneNumbers = array of string;
- TOnNewSmsCallback = function (SmsList: TSmsList): Boolean; //if Result=False then StopWaiting
- TSmsWaitTimer = class(TTimer)
- private
- OnNewMessage : TOnNewSmsCallback;
- end;
- TOnlineSimRu = class
- private
- AHttp : TIdHttp;
- AParams : TStringList;
- ANumber : string;
- APhoneNumbers : TPhoneNumbers;
- ASmsList : TSmsList;
- ASmsWaitTimer : TSmsWaitTimer;
- function GetProxyHost: string;
- function GetProxyPort: integer;
- function GetUserAgent: string;
- procedure SetProxyHost(const Value: string);
- procedure SetProxyPort(const Value: integer);
- procedure SetUserAgent(const Value: string);
- function GetCurrentNumber(const Buffer: string): string;
- function GetNumbersFromBuffer(var PhoneNumbers: TPhoneNumbers;
- const Buffer: string): Boolean;
- function GetSmsListFromBuffer(var SmsList: TSmsList;
- const Buffer: string): Boolean;
- procedure SetNumber(const Value: string);
- procedure WaitNewSmsOnTimer(Sender: TObject);
- procedure StopWaiting;
- public
- constructor Create;
- destructor Destroy; override;
- function GetNumbers(var PhoneNumbers: TPhoneNumbers): Boolean;
- function GetSmsList(var SmsList: TSmsList): Boolean;
- function WaitNewSms(OnNewSmsCallback: TOnNewSmsCallback; const Timeout: integer = 15000): Boolean;
- property UserAgent: string read GetUserAgent write SetUserAgent;
- property ProxyHost: string read GetProxyHost write SetProxyHost;
- property ProxyPort: integer read GetProxyPort write SetProxyPort;
- property Number : string read ANumber write SetNumber;
- end;
- procedure ProcessMessages;
- implementation
- { TOnlineSimRu }
- constructor TOnlineSimRu.Create;
- begin
- if GAntiFreeze = nil then begin
- GAntiFreeze := TIdAntiFreeze.Create(nil);
- GAntiFreeze.ApplicationHasPriority := False;
- end;
- Randomize;
- AHttp := TIdHttp.Create(nil);
- AHttp.Request.UserAgent := USER_AGENT;
- AHttp.AllowCookies := True;
- AHttp.HandleRedirects := True;
- AParams := TStringList.Create;
- ASmsWaitTimer := TSmsWaitTimer.Create(nil);
- ASmsWaitTimer.Enabled := False;
- ASmsWaitTimer.OnTimer := WaitNewSmsOnTimer;
- end;
- destructor TOnlineSimRu.Destroy;
- begin
- ASmsWaitTimer.Free;
- AParams.Free;
- AHttp.Free;
- Finalize(APhoneNumbers);
- Finalize(ASmsList);
- inherited;
- end;
- function TOnlineSimRu.GetCurrentNumber(const Buffer: string): string;
- var
- mc : TMatchCollection;
- begin
- mc := TRegEx.Matches(Buffer, '<\/div>(\+\d+)');
- if (mc.Count <> 0) then
- begin
- Result := mc.Item[0].Groups.Item[1].Value;
- end else
- Result := '';
- end;
- function TOnlineSimRu.GetNumbers(var PhoneNumbers: TPhoneNumbers): Boolean;
- var
- buffer: string;
- begin
- try
- buffer := AHttp.Get('http://onlinesim.ru/');
- except end;
- Result := GetNumbersFromBuffer(PhoneNumbers, buffer);
- GetNumbersFromBuffer(APhoneNumbers, buffer);
- ANumber := GetCurrentNumber(buffer);
- end;
- function TOnlineSimRu.GetNumbersFromBuffer(var PhoneNumbers: TPhoneNumbers;
- const Buffer: string): Boolean;
- var
- i : integer;
- mc : TMatchCollection;
- begin
- mc := TRegEx.Matches(Buffer, '<option>(\+\d+)');
- SetLength(PhoneNumbers, mc.Count);
- if (mc.Count <> 0) then
- begin
- for i := 0 to Pred(mc.Count) do
- PhoneNumbers[i] := mc.Item[i].Groups.Item[1].Value;
- Result := True;
- end else
- Result := False;
- end;
- function TOnlineSimRu.GetProxyHost: string;
- begin
- Result := AHttp.ProxyParams.ProxyServer;
- end;
- function TOnlineSimRu.GetProxyPort: integer;
- begin
- Result := AHttp.ProxyParams.ProxyPort;
- end;
- function TOnlineSimRu.GetSmsList(var SmsList: TSmsList): Boolean;
- var
- buffer: string;
- begin
- try
- buffer := AHttp.Get('http://onlinesim.ru/enter_number.php?number=' +
- ANumber + '&_=' + IntToStr(Random(MaxInt)));
- except end;
- Result := GetSmsListFromBuffer(SmsList, buffer);
- end;
- function TOnlineSimRu.GetSmsListFromBuffer(var SmsList: TSmsList;
- const Buffer: string): Boolean;
- var
- i : integer;
- mc : TMatchCollection;
- begin
- mc := TRegEx.Matches(Buffer, '\''>([^<]*)[^=]+[^>]+>([^<]*)');
- SetLength(SmsList, mc.Count);
- if (mc.Count <> 0) then
- begin
- for i := 0 to Pred(mc.Count) do
- with SmsList[i] do
- begin
- author := mc.Item[i].Groups.Item[1].Value;
- content := mc.Item[i].Groups.Item[2].Value;
- end;
- Result := True;
- end else
- Result := False;
- end;
- function TOnlineSimRu.GetUserAgent: string;
- begin
- Result := AHttp.Request.UserAgent;
- end;
- procedure TOnlineSimRu.SetNumber(const Value: string);
- var
- i : integer;
- buffer : string;
- begin
- ANumber := '';
- if (Length(APhoneNumbers) <> 0)and(Trim(Value) <> '') then
- begin
- for i := Low(APhoneNumbers) to High(APhoneNumbers) do
- begin
- if APhoneNumbers[i] = Trim(Value) then
- begin
- ANumber := Trim(Value);
- try
- with AParams do
- begin
- Clear;
- Add('innumber=' + ANumber);
- Add('submit=ok');
- end;
- buffer := AHttp.Post('http://onlinesim.ru/', AParams);
- except end;
- Break;
- end;
- end;
- end;
- end;
- procedure TOnlineSimRu.SetProxyHost(const Value: string);
- begin
- AHttp.ProxyParams.ProxyServer := Value;
- end;
- procedure TOnlineSimRu.SetProxyPort(const Value: integer);
- begin
- AHttp.ProxyParams.ProxyPort := Value;
- end;
- procedure TOnlineSimRu.SetUserAgent(const Value: string);
- begin
- AHttp.Request.UserAgent := Value;
- end;
- procedure TOnlineSimRu.StopWaiting;
- begin
- ASmsWaitTimer.Enabled := False;
- end;
- function TOnlineSimRu.WaitNewSms(OnNewSmsCallback: TOnNewSmsCallback;
- const Timeout: integer): Boolean;
- begin
- Result := ((Timeout >= 1000)and(@OnNewSmsCallback <> nil)and(GetSmsList(ASmsList)));
- if Result then
- begin
- with ASmsWaitTimer do
- begin
- Interval := Timeout;
- OnNewMessage := OnNewSmsCallback;
- Enabled := True;
- end;
- end;
- end;
- procedure TOnlineSimRu.WaitNewSmsOnTimer(Sender: TObject);
- var
- NewFound : Boolean;
- i : integer;
- NewSmsList: TSmsList;
- begin
- if GetSmsList(NewSmsList) then
- begin
- NewFound := False;
- if Length(NewSmsList) = Length(ASmsList) then
- begin
- for i := Low(ASmsList) to High(ASmsList) do
- begin
- if (ASmsList[i].author <> NewSmsList[i].author)or(ASmsList[i].content <> NewSmsList[i].content) then
- begin
- NewFound := True;
- Break;
- end;
- end;
- end else
- if Length(NewSmsList) <> 0 then
- NewFound := True;
- if NewFound then
- begin
- SetLength(ASmsList, Length(NewSmsList));
- for i := Low(ASmsList) to High(ASmsList) do
- with ASmsList[i] do
- begin
- author := NewSmsList[i].author;
- content := NewSmsList[i].content;
- end;
- if not ASmsWaitTimer.OnNewMessage(NewSmsList) then
- begin
- ASmsWaitTimer.Enabled := False;
- end;
- end;
- end;
- ProcessMessages;
- end;
- procedure ProcessMessages;
- var
- Msg: TMsg;
- function ProcessMsg(var Msg: TMsg): Boolean;
- const
- WM_QUIT = $0012;
- begin
- Result := False;
- if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
- begin
- Result := True;
- if Msg.Message <> WM_QUIT then
- begin
- TranslateMessage(Msg);
- DispatchMessage(Msg);
- end
- else
- DispatchMessage(Msg);
- end;
- end;
- begin
- while ProcessMsg(Msg) do;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement