Advertisement
HEX0x29A

API temp-mail.ru

May 30th, 2014
1,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.66 KB | None | 0 0
  1. unit uTempMailRuApi;
  2. (*
  3. API temp-mail.ru
  4. Author: HEX0x29A
  5. Date  : 31.05.2014
  6. Notes :
  7. *Requere Delphi XE+
  8. *)
  9. interface
  10.  
  11. uses
  12.   Windows, SysUtils, WinInet, DBXJSON;
  13.  
  14. type
  15.   TMail       = packed record
  16.     mail_id        : string;
  17.     mail_address_id: string;
  18.     mail_from      : string;
  19.     mail_subject   : string;
  20.     mail_preview   : string;
  21.     mail_text_only : string;
  22.     mail_text      : string;
  23.     mail_html      : string;
  24.     mail_timestamp : TDateTime;
  25.   end;
  26.   TMailList   = array of TMail;
  27.   TDomainList = array of string;
  28.   TTempMailRu = class
  29.   private
  30.     fDomainList: TDomainList;
  31.     fEmail     : string;
  32.     fMD5       : string;
  33.     function GetRandomString(const StrLen: BYTE = 10): string;
  34.     function GetDomains(): Boolean;
  35.   public
  36.     constructor Create;
  37.     destructor  Destroy; override;
  38.     function GetRandomEmail(): string;
  39.     function GetMails(var MailList: TMailList; const Email: string = ''): Boolean;
  40.   end;
  41.  
  42.   function UnixDateTimeToDelphiDateTime(UnixDateTime: longint): TDateTime;
  43.   function MD5Hash(const Text: AnsiString): AnsiString;
  44.  
  45. implementation
  46.  
  47. { TTempMailRu }
  48.  
  49. const
  50.   USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080401';
  51.   ADVAPI32   = 'advapi32.dll';
  52.  
  53. type
  54.   PMD5Context = ^TMD5Context;
  55.   TMD5Context = packed record
  56.     Internals : array[0..21] of DWORD;
  57.     MD5Digest : array[0..15] of BYTE;
  58.   end;
  59.  
  60. procedure MD5Init(Context: PMD5Context);
  61.  stdcall; external ADVAPI32;
  62. procedure MD5Final(Context: PMD5Context);
  63.  stdcall; external ADVAPI32;
  64. procedure MD5Update(Context: PMD5Context; const Buffer: PAnsiChar; BufferSize: DWORD);
  65.  stdcall; external ADVAPI32;
  66.  
  67.  
  68. function MD5Hash(const Text: AnsiString): AnsiString;
  69. var
  70.   Context: TMD5Context;
  71.   Digit  : BYTE;
  72. begin
  73.   MD5Init(PMD5Context(@Context));
  74.   MD5Update(PMD5Context(@Context), PAnsiChar(text), Length(text));
  75.   MD5Final(PMD5Context(@Context));
  76.   Result := '';
  77.   for Digit in Context.MD5Digest do
  78.     Result := Result + IntToHex(Digit, 2);
  79. end;
  80.  
  81. function HttpGet(const URL: AnsiString): AnsiString;
  82. const
  83.   BufferSize = 1024;
  84. var
  85.   hSession  : HInternet;
  86.   hURL      : HInternet;
  87.   Buffer    : array[0..Pred(BufferSize)] of AnsiChar;
  88.   BufferLen : DWORD;
  89.   dwReserved: DWORD;
  90.   respCode  : DWORD;
  91. begin
  92.   Result := '';
  93.   hSession := InternetOpenA(PAnsiChar(USER_AGENT), INTERNET_OPEN_TYPE_PRECONFIG,  nil, nil, 0);
  94.   try
  95.     hURL := InternetOpenURLA(hSession, PAnsiChar(URL), nil, 0, 0, 0);
  96.     BufferLen := SizeOf(respCode);
  97.     dwReserved := 0;
  98.     if (HttpQueryInfo(hURL, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
  99.                       @respCode, BufferLen, dwReserved)) then
  100.       if respCode = 200 then
  101.       try
  102.         repeat
  103.           ZeroMemory(@Buffer, SizeOf(Buffer));
  104.           InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
  105.           Result := Result + StrPas(Buffer);
  106.         until BufferLen = 0;
  107.       finally
  108.         InternetCloseHandle(hURL);
  109.       end else
  110.         InternetCloseHandle(hURL);
  111.   finally
  112.     InternetCloseHandle(hSession);
  113.   end;
  114. end;
  115.  
  116. function UnixDateTimeToDelphiDateTime(UnixDateTime: longint): TDateTime;
  117. const
  118.   SecsPerDay = 24 * 60 * 60;
  119. begin
  120.   Result := EncodeDate(1970, 1, 1) + (UnixDateTime / SecsPerDay);
  121. end;
  122.  
  123. constructor TTempMailRu.Create;
  124. begin
  125.   Randomize;
  126. end;
  127.  
  128. destructor TTempMailRu.Destroy;
  129. begin
  130.   Finalize(fDomainList);
  131.   inherited;
  132. end;
  133.  
  134. function TTempMailRu.GetMails(var MailList: TMailList;
  135.   const Email: string): Boolean;
  136. var
  137.   i, j  : integer;
  138.   buffer: string;
  139.   ja    : TJSONArray;
  140.   jo    : TJSONObject;
  141.   jp    : TJSONPair;
  142.   jv    : TJSONValue;
  143. begin
  144.   Result := False;
  145.   SetLength(MailList, 0);
  146.   if Email = '' then
  147.   begin
  148.     if fEmail = '' then
  149.       Exit
  150.     else
  151.       buffer := fMD5;
  152.   end else
  153.     buffer := MD5Hash(Email);
  154.   Buffer := HttpGet('http://api.temp-mail.ru/request/mail/id/' + buffer + '/format/json/');
  155.   if buffer = '' then
  156.     Exit;
  157.   ja := TJSONObject.ParseJSONValue(buffer) as TJSONArray;
  158.   if Assigned(ja) then
  159.   try
  160.     SetLength(MailList, ja.Size);
  161.     if ja.Size > 0 then
  162.     begin
  163.       for i := 0 to ja.Size -1 do
  164.       begin
  165.         if ja.Get(i) is TJSONObject then
  166.         begin
  167.           jo := ja.Get(i) as TJSONObject;
  168.           if jo.Size > 0 then
  169.           begin
  170.             Result := (jo.Size = 9);
  171.             for j := 0 to jo.Size - 1 do
  172.             begin
  173.               jv := jo.Get(i).JsonValue;
  174.               if jv is TJSONAncestor then
  175.               begin
  176.                 if jo.Get(j) is TJSONPair then
  177.                 begin
  178.                   jp := TJSONPair(jo.Get(j));
  179.                   with MailList[i] do
  180.                   case j of
  181.                     0: mail_id         := jp.JsonValue.Value;
  182.                     1: mail_address_id := jp.JsonValue.Value;
  183.                     2: mail_from       := jp.JsonValue.Value;
  184.                     3: mail_subject    := jp.JsonValue.Value;
  185.                     4: mail_preview    := jp.JsonValue.Value;
  186.                     5: mail_text_only  := jp.JsonValue.Value;
  187.                     6: mail_text       := jp.JsonValue.Value;
  188.                     7: mail_html       := jp.JsonValue.Value;
  189.                     8: mail_timestamp  := UnixDateTimeToDelphiDateTime(StrToIntDef(jp.JsonValue.Value, 0));
  190.                   end;  
  191.                 end          
  192.               end;
  193.             end;
  194.           end;
  195.         end;  
  196.       end;
  197.     end;
  198.   finally
  199.     ja.Free;
  200.   end;
  201. end;
  202.  
  203. function TTempMailRu.GetDomains(): Boolean;
  204. var
  205.   i     : integer;
  206.   buffer: string;
  207.   ja    : TJSONArray;
  208. begin
  209.   try
  210.     buffer := HttpGet('http://api.temp-mail.ru/request/domains/format/json/');
  211.     if Trim(buffer) = '' then
  212.       buffer := '';
  213.   except
  214.     buffer := '';
  215.   end;
  216.   try
  217.     ja := TJSONObject.ParseJSONValue(buffer) as TJSONArray;
  218.     if Assigned(ja) then
  219.     begin
  220.       SetLength(fDomainList, ja.Size);
  221.       if ja.Size > 0 then
  222.       begin
  223.         for i := 0 to ja.Size -1 do
  224.         begin
  225.           fDomainList[i] := ja.Get(i).Value;
  226.         end;
  227.       end;
  228.     end;
  229.   except end;
  230. end;
  231.  
  232. function TTempMailRu.GetRandomEmail: string;
  233. begin
  234.   if Length(fDomainList) = 0 then
  235.     GetDomains();
  236.   fEmail := (GetRandomString(Random(10) + 5)) + fDomainList[Random(Length(fDomainList))];
  237.   fMD5   := MD5Hash(fEmail);
  238.   Result := fEmail;
  239. end;
  240.  
  241. function TTempMailRu.GetRandomString(const StrLen: BYTE = 10): string;
  242. var
  243.   c: byte;
  244.   i: byte;
  245. begin
  246.   Result := '';
  247.   for i := 0 to Pred(StrLen) do
  248.   begin
  249.     c := Random(36);
  250.     case c of
  251.       0..9 : Result := Result + chr(c + 48);
  252.       else
  253.         Result := Result + chr((c - 10) + 97);
  254.     end;
  255.   end;
  256. end;
  257.  
  258. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement