Advertisement
HEX0x29A

uOnlineSimRu (Class for OnlineSim.Ru)

Apr 3rd, 2014
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.06 KB | None | 0 0
  1. unit uOnlineSimRu;
  2. (*
  3. Class for OnlineSim.Ru
  4. Date  : 04.04.2014
  5. Author: HEX0x29A
  6. Notes :
  7. *Requere Delphi XE+
  8. *)
  9. interface
  10.  
  11. uses
  12.   Windows, SysUtils, Classes, ExtCtrls, IdHttp, IdAntiFreezeBase, IdAntiFreeze,
  13.   RegularExpressions;
  14.  
  15. const
  16.   USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080401';
  17.  
  18. type
  19.   TSms = packed record
  20.     author : string;
  21.     content: string;
  22.   end;
  23.   TSmsList = array of TSms;
  24.   TPhoneNumbers = array of string;
  25.   TOnNewSmsCallback = function (SmsList: TSmsList): Boolean; //if Result=False then StopWaiting
  26.   TSmsWaitTimer = class(TTimer)
  27.   private
  28.     OnNewMessage : TOnNewSmsCallback;
  29.   end;
  30.   TOnlineSimRu = class
  31.   private
  32.     AHttp         : TIdHttp;
  33.     AParams       : TStringList;
  34.     ANumber       : string;
  35.     APhoneNumbers : TPhoneNumbers;
  36.     ASmsList      : TSmsList;
  37.     ASmsWaitTimer : TSmsWaitTimer;
  38.     function  GetProxyHost: string;
  39.     function  GetProxyPort: integer;
  40.     function  GetUserAgent: string;
  41.     procedure SetProxyHost(const Value: string);
  42.     procedure SetProxyPort(const Value: integer);
  43.     procedure SetUserAgent(const Value: string);
  44.     function  GetCurrentNumber(const Buffer: string): string;
  45.     function  GetNumbersFromBuffer(var   PhoneNumbers: TPhoneNumbers;
  46.                                    const Buffer: string): Boolean;
  47.     function  GetSmsListFromBuffer(var   SmsList: TSmsList;
  48.                                    const Buffer: string): Boolean;
  49.     procedure SetNumber(const Value: string);
  50.     procedure WaitNewSmsOnTimer(Sender: TObject);
  51.     procedure StopWaiting;
  52.   public
  53.     constructor Create;
  54.     destructor  Destroy; override;
  55.     function    GetNumbers(var PhoneNumbers: TPhoneNumbers): Boolean;
  56.     function    GetSmsList(var SmsList: TSmsList): Boolean;
  57.     function    WaitNewSms(OnNewSmsCallback: TOnNewSmsCallback; const Timeout: integer = 15000): Boolean;
  58.     property UserAgent: string  read GetUserAgent write SetUserAgent;
  59.     property ProxyHost: string  read GetProxyHost write SetProxyHost;
  60.     property ProxyPort: integer read GetProxyPort write SetProxyPort;
  61.     property Number   : string  read ANumber      write SetNumber;
  62.   end;
  63.  
  64.   procedure ProcessMessages;
  65.  
  66. implementation
  67.  
  68. { TOnlineSimRu }
  69.  
  70. constructor TOnlineSimRu.Create;
  71. begin
  72.   if GAntiFreeze = nil then begin
  73.     GAntiFreeze := TIdAntiFreeze.Create(nil);
  74.     GAntiFreeze.ApplicationHasPriority := False;
  75.   end;
  76.   Randomize;
  77.   AHttp                   := TIdHttp.Create(nil);
  78.   AHttp.Request.UserAgent := USER_AGENT;
  79.   AHttp.AllowCookies      := True;
  80.   AHttp.HandleRedirects   := True;
  81.   AParams                 := TStringList.Create;
  82.   ASmsWaitTimer           := TSmsWaitTimer.Create(nil);
  83.   ASmsWaitTimer.Enabled   := False;
  84.   ASmsWaitTimer.OnTimer   := WaitNewSmsOnTimer;
  85. end;
  86.  
  87. destructor TOnlineSimRu.Destroy;
  88. begin
  89.   ASmsWaitTimer.Free;
  90.   AParams.Free;
  91.   AHttp.Free;
  92.   Finalize(APhoneNumbers);
  93.   Finalize(ASmsList);
  94.   inherited;
  95. end;
  96.  
  97. function TOnlineSimRu.GetCurrentNumber(const Buffer: string): string;
  98. var
  99.   mc : TMatchCollection;
  100. begin
  101.   mc := TRegEx.Matches(Buffer, '<\/div>(\+\d+)');
  102.   if (mc.Count <> 0) then
  103.   begin
  104.     Result := mc.Item[0].Groups.Item[1].Value;
  105.   end else
  106.     Result := '';
  107. end;
  108.  
  109. function TOnlineSimRu.GetNumbers(var PhoneNumbers: TPhoneNumbers): Boolean;
  110. var
  111.   buffer: string;
  112. begin
  113.   try
  114.     buffer := AHttp.Get('http://onlinesim.ru/');
  115.   except end;
  116.   Result := GetNumbersFromBuffer(PhoneNumbers, buffer);
  117.   GetNumbersFromBuffer(APhoneNumbers, buffer);
  118.   ANumber := GetCurrentNumber(buffer);
  119. end;
  120.  
  121. function TOnlineSimRu.GetNumbersFromBuffer(var PhoneNumbers: TPhoneNumbers;
  122.   const Buffer: string): Boolean;
  123. var
  124.   i  : integer;
  125.   mc : TMatchCollection;
  126. begin
  127.   mc := TRegEx.Matches(Buffer, '<option>(\+\d+)');
  128.   SetLength(PhoneNumbers, mc.Count);
  129.   if (mc.Count <> 0) then
  130.   begin
  131.     for i := 0 to Pred(mc.Count) do
  132.       PhoneNumbers[i] := mc.Item[i].Groups.Item[1].Value;
  133.     Result := True;
  134.   end else
  135.     Result := False;
  136. end;
  137.  
  138. function TOnlineSimRu.GetProxyHost: string;
  139. begin
  140.   Result := AHttp.ProxyParams.ProxyServer;
  141. end;
  142.  
  143. function TOnlineSimRu.GetProxyPort: integer;
  144. begin
  145.   Result := AHttp.ProxyParams.ProxyPort;
  146. end;
  147.  
  148. function TOnlineSimRu.GetSmsList(var SmsList: TSmsList): Boolean;
  149. var
  150.   buffer: string;
  151. begin
  152.   try
  153.     buffer := AHttp.Get('http://onlinesim.ru/enter_number.php?number=' +
  154.                         ANumber + '&_=' + IntToStr(Random(MaxInt)));
  155.   except end;
  156.   Result := GetSmsListFromBuffer(SmsList, buffer);
  157. end;
  158.  
  159. function TOnlineSimRu.GetSmsListFromBuffer(var SmsList: TSmsList;
  160.   const Buffer: string): Boolean;
  161. var
  162.   i  : integer;
  163.   mc : TMatchCollection;
  164. begin
  165.   mc := TRegEx.Matches(Buffer, '\''>([^<]*)[^=]+[^>]+>([^<]*)');
  166.   SetLength(SmsList, mc.Count);
  167.   if (mc.Count <> 0) then
  168.   begin
  169.     for i := 0 to Pred(mc.Count) do
  170.     with SmsList[i] do
  171.     begin
  172.       author  := mc.Item[i].Groups.Item[1].Value;
  173.       content := mc.Item[i].Groups.Item[2].Value;
  174.     end;
  175.     Result := True;
  176.   end else
  177.     Result := False;
  178. end;
  179.  
  180. function TOnlineSimRu.GetUserAgent: string;
  181. begin
  182.   Result := AHttp.Request.UserAgent;
  183. end;
  184.  
  185. procedure TOnlineSimRu.SetNumber(const Value: string);
  186. var
  187.   i      : integer;
  188.   buffer : string;
  189. begin
  190.   ANumber := '';
  191.   if (Length(APhoneNumbers) <> 0)and(Trim(Value) <> '') then
  192.   begin
  193.     for i := Low(APhoneNumbers) to High(APhoneNumbers) do
  194.     begin
  195.       if APhoneNumbers[i] = Trim(Value) then
  196.       begin
  197.         ANumber := Trim(Value);
  198.         try
  199.           with AParams do
  200.           begin
  201.             Clear;
  202.             Add('innumber=' + ANumber);
  203.             Add('submit=ok');
  204.           end;
  205.           buffer := AHttp.Post('http://onlinesim.ru/', AParams);
  206.         except end;
  207.         Break;
  208.       end;
  209.     end;
  210.   end;
  211. end;
  212.  
  213. procedure TOnlineSimRu.SetProxyHost(const Value: string);
  214. begin
  215.   AHttp.ProxyParams.ProxyServer := Value;
  216. end;
  217.  
  218. procedure TOnlineSimRu.SetProxyPort(const Value: integer);
  219. begin
  220.   AHttp.ProxyParams.ProxyPort := Value;
  221. end;
  222.  
  223. procedure TOnlineSimRu.SetUserAgent(const Value: string);
  224. begin
  225.   AHttp.Request.UserAgent := Value;
  226. end;
  227.  
  228. procedure TOnlineSimRu.StopWaiting;
  229. begin
  230.   ASmsWaitTimer.Enabled := False;
  231. end;
  232.  
  233. function TOnlineSimRu.WaitNewSms(OnNewSmsCallback: TOnNewSmsCallback;
  234.   const Timeout: integer): Boolean;
  235. begin
  236.   Result := ((Timeout >= 1000)and(@OnNewSmsCallback <> nil)and(GetSmsList(ASmsList)));
  237.   if Result then
  238.   begin
  239.     with ASmsWaitTimer do
  240.     begin
  241.       Interval     := Timeout;
  242.       OnNewMessage := OnNewSmsCallback;
  243.       Enabled      := True;
  244.     end;
  245.   end;
  246. end;
  247.  
  248. procedure TOnlineSimRu.WaitNewSmsOnTimer(Sender: TObject);
  249. var
  250.   NewFound  : Boolean;
  251.   i         : integer;
  252.   NewSmsList: TSmsList;
  253. begin
  254.   if GetSmsList(NewSmsList) then
  255.   begin
  256.     NewFound := False;
  257.     if Length(NewSmsList) = Length(ASmsList) then
  258.     begin
  259.       for i := Low(ASmsList) to High(ASmsList) do
  260.       begin
  261.         if (ASmsList[i].author <> NewSmsList[i].author)or(ASmsList[i].content <> NewSmsList[i].content) then
  262.         begin
  263.           NewFound := True;
  264.           Break;
  265.         end;
  266.       end;
  267.     end else
  268.       if Length(NewSmsList) <> 0 then
  269.         NewFound := True;
  270.     if NewFound then
  271.     begin
  272.       SetLength(ASmsList, Length(NewSmsList));
  273.       for i := Low(ASmsList) to High(ASmsList) do
  274.       with ASmsList[i] do
  275.       begin
  276.         author  := NewSmsList[i].author;
  277.         content := NewSmsList[i].content;
  278.       end;
  279.       if not ASmsWaitTimer.OnNewMessage(NewSmsList) then
  280.       begin  
  281.         ASmsWaitTimer.Enabled := False;
  282.       end;
  283.     end;
  284.   end;
  285.   ProcessMessages;
  286. end;
  287.  
  288. procedure ProcessMessages;
  289. var
  290.   Msg: TMsg;
  291. function ProcessMsg(var Msg: TMsg): Boolean;
  292. const
  293.   WM_QUIT = $0012;
  294. begin
  295.   Result := False;
  296.   if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
  297.   begin
  298.     Result := True;
  299.     if Msg.Message <> WM_QUIT then
  300.     begin
  301.       TranslateMessage(Msg);
  302.       DispatchMessage(Msg);
  303.     end
  304.     else
  305.       DispatchMessage(Msg);
  306.   end;
  307. end;
  308. begin
  309.   while ProcessMsg(Msg) do;
  310. end;
  311.  
  312. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement