Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit uTempMailRuApi;
- (*
- API temp-mail.ru
- Author: HEX0x29A
- Date : 31.05.2014
- Notes :
- *Requere Delphi XE+
- *)
- interface
- uses
- Windows, SysUtils, WinInet, DBXJSON;
- type
- TMail = packed record
- mail_id : string;
- mail_address_id: string;
- mail_from : string;
- mail_subject : string;
- mail_preview : string;
- mail_text_only : string;
- mail_text : string;
- mail_html : string;
- mail_timestamp : TDateTime;
- end;
- TMailList = array of TMail;
- TDomainList = array of string;
- TTempMailRu = class
- private
- fDomainList: TDomainList;
- fEmail : string;
- fMD5 : string;
- function GetRandomString(const StrLen: BYTE = 10): string;
- function GetDomains(): Boolean;
- public
- constructor Create;
- destructor Destroy; override;
- function GetRandomEmail(): string;
- function GetMails(var MailList: TMailList; const Email: string = ''): Boolean;
- end;
- function UnixDateTimeToDelphiDateTime(UnixDateTime: longint): TDateTime;
- function MD5Hash(const Text: AnsiString): AnsiString;
- implementation
- { TTempMailRu }
- const
- USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080401';
- ADVAPI32 = 'advapi32.dll';
- type
- PMD5Context = ^TMD5Context;
- TMD5Context = packed record
- Internals : array[0..21] of DWORD;
- MD5Digest : array[0..15] of BYTE;
- end;
- procedure MD5Init(Context: PMD5Context);
- stdcall; external ADVAPI32;
- procedure MD5Final(Context: PMD5Context);
- stdcall; external ADVAPI32;
- procedure MD5Update(Context: PMD5Context; const Buffer: PAnsiChar; BufferSize: DWORD);
- stdcall; external ADVAPI32;
- function MD5Hash(const Text: AnsiString): AnsiString;
- var
- Context: TMD5Context;
- Digit : BYTE;
- begin
- MD5Init(PMD5Context(@Context));
- MD5Update(PMD5Context(@Context), PAnsiChar(text), Length(text));
- MD5Final(PMD5Context(@Context));
- Result := '';
- for Digit in Context.MD5Digest do
- Result := Result + IntToHex(Digit, 2);
- end;
- function HttpGet(const URL: AnsiString): AnsiString;
- const
- BufferSize = 1024;
- var
- hSession : HInternet;
- hURL : HInternet;
- Buffer : array[0..Pred(BufferSize)] of AnsiChar;
- BufferLen : DWORD;
- dwReserved: DWORD;
- respCode : DWORD;
- begin
- Result := '';
- hSession := InternetOpenA(PAnsiChar(USER_AGENT), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
- try
- hURL := InternetOpenURLA(hSession, PAnsiChar(URL), nil, 0, 0, 0);
- BufferLen := SizeOf(respCode);
- dwReserved := 0;
- if (HttpQueryInfo(hURL, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
- @respCode, BufferLen, dwReserved)) then
- if respCode = 200 then
- try
- repeat
- ZeroMemory(@Buffer, SizeOf(Buffer));
- InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
- Result := Result + StrPas(Buffer);
- until BufferLen = 0;
- finally
- InternetCloseHandle(hURL);
- end else
- InternetCloseHandle(hURL);
- finally
- InternetCloseHandle(hSession);
- end;
- end;
- function UnixDateTimeToDelphiDateTime(UnixDateTime: longint): TDateTime;
- const
- SecsPerDay = 24 * 60 * 60;
- begin
- Result := EncodeDate(1970, 1, 1) + (UnixDateTime / SecsPerDay);
- end;
- constructor TTempMailRu.Create;
- begin
- Randomize;
- end;
- destructor TTempMailRu.Destroy;
- begin
- Finalize(fDomainList);
- inherited;
- end;
- function TTempMailRu.GetMails(var MailList: TMailList;
- const Email: string): Boolean;
- var
- i, j : integer;
- buffer: string;
- ja : TJSONArray;
- jo : TJSONObject;
- jp : TJSONPair;
- jv : TJSONValue;
- begin
- Result := False;
- SetLength(MailList, 0);
- if Email = '' then
- begin
- if fEmail = '' then
- Exit
- else
- buffer := fMD5;
- end else
- buffer := MD5Hash(Email);
- Buffer := HttpGet('http://api.temp-mail.ru/request/mail/id/' + buffer + '/format/json/');
- if buffer = '' then
- Exit;
- ja := TJSONObject.ParseJSONValue(buffer) as TJSONArray;
- if Assigned(ja) then
- try
- SetLength(MailList, ja.Size);
- if ja.Size > 0 then
- begin
- for i := 0 to ja.Size -1 do
- begin
- if ja.Get(i) is TJSONObject then
- begin
- jo := ja.Get(i) as TJSONObject;
- if jo.Size > 0 then
- begin
- Result := (jo.Size = 9);
- for j := 0 to jo.Size - 1 do
- begin
- jv := jo.Get(i).JsonValue;
- if jv is TJSONAncestor then
- begin
- if jo.Get(j) is TJSONPair then
- begin
- jp := TJSONPair(jo.Get(j));
- with MailList[i] do
- case j of
- 0: mail_id := jp.JsonValue.Value;
- 1: mail_address_id := jp.JsonValue.Value;
- 2: mail_from := jp.JsonValue.Value;
- 3: mail_subject := jp.JsonValue.Value;
- 4: mail_preview := jp.JsonValue.Value;
- 5: mail_text_only := jp.JsonValue.Value;
- 6: mail_text := jp.JsonValue.Value;
- 7: mail_html := jp.JsonValue.Value;
- 8: mail_timestamp := UnixDateTimeToDelphiDateTime(StrToIntDef(jp.JsonValue.Value, 0));
- end;
- end
- end;
- end;
- end;
- end;
- end;
- end;
- finally
- ja.Free;
- end;
- end;
- function TTempMailRu.GetDomains(): Boolean;
- var
- i : integer;
- buffer: string;
- ja : TJSONArray;
- begin
- try
- buffer := HttpGet('http://api.temp-mail.ru/request/domains/format/json/');
- if Trim(buffer) = '' then
- buffer := '';
- except
- buffer := '';
- end;
- try
- ja := TJSONObject.ParseJSONValue(buffer) as TJSONArray;
- if Assigned(ja) then
- begin
- SetLength(fDomainList, ja.Size);
- if ja.Size > 0 then
- begin
- for i := 0 to ja.Size -1 do
- begin
- fDomainList[i] := ja.Get(i).Value;
- end;
- end;
- end;
- except end;
- end;
- function TTempMailRu.GetRandomEmail: string;
- begin
- if Length(fDomainList) = 0 then
- GetDomains();
- fEmail := (GetRandomString(Random(10) + 5)) + fDomainList[Random(Length(fDomainList))];
- fMD5 := MD5Hash(fEmail);
- Result := fEmail;
- end;
- function TTempMailRu.GetRandomString(const StrLen: BYTE = 10): string;
- var
- c: byte;
- i: byte;
- begin
- Result := '';
- for i := 0 to Pred(StrLen) do
- begin
- c := Random(36);
- case c of
- 0..9 : Result := Result + chr(c + 48);
- else
- Result := Result + chr((c - 10) + 97);
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement