1 { Dolib
\82ð
\83R
\83s
\81[
\82µ
\82ÄBE
\83\8d\83O
\83C
\83\93\83Z
\83b
\83V
\83\87\83\93\8aÇ
\97\9d\82ð
\8dì
\90¬ }
9 Windows, SysUtils, WinInet, YofUtils, Y_TextConverter;
12 TBelibSession = class(TObject)
19 property MDMD: string read FMDMD write FMDMD;
20 property DMDM: string read FDMDM write FDMDM;
21 property ErrorCode: Integer read FErrorCode write FErrorCode;
22 property ErrorString: string read FErrorString write FErrorString;
25 TBelib = class(TObject)
27 FSession : TBelibSession;
32 FProxyAddress: string;
34 function GetMDMD : string;
35 function GetDMDM : string;
36 function GetErrorCode: integer;
37 function GetErrorMsg: string;
38 procedure MakeError(Session: TBelibSession; Error: DWORD);
39 procedure BELIB_LOGIN(Proxy: string; Port: Integer; ID: string; Pass: string);
42 destructor Destroy; override;
43 function Connect: boolean;
44 function Disconnect: boolean;
45 property ProxyAddress: string read FProxyAddress write FProxyAddress;
46 property ProxyPort: integer read FProxyPort write FProxyPort;
47 property UserName: string read FUserName write FUserName;
48 property Password: string read FPassword write FPassword;
49 property ClientUA: string read FClientUA write FClientUA;
50 property Connected: boolean read FConnected;
51 property MDMD: string read GetMDMD;
52 property DMDM: string read GetDMDM;
53 property ErrorCode: integer read GetErrorCode;
54 property ErrorMsg: string read GetErrorMsg;
59 BELIB_LOGIN_UA = 'BELIB/1.00';
60 BELIB_LOGIN_HOST = 'be.2ch.net';
61 BELIB_LOGIN_URL = '/test/login.php';
62 BELIB_2CH_UA = 'X-2ch-UA:';
63 BELIB_ENOMEM_STRING = '
\83\81\83\82\83\8a\82ª
\91«
\82è
\82Ü
\82¹
\82ñ
\81B';
64 BELIB_LOGIN_ERROR = 'ERROR:';
69 constructor TBelib.Create;
75 destructor TBelib.Destroy;
82 function TBelib.Connect: boolean;
85 if not Connected then begin
86 BELIB_LOGIN(FProxyAddress, FProxyPort, FUserName, FPassword);
88 if (Length(MDMD)=0) and (Length(DMDM)=0) then begin
91 end else if ErrorCode <> 0 then begin
96 // Result := (ErrorCode = 0);
101 function TBelib.Disconnect: boolean;
104 if FSession <> nil then
105 FreeAndNil(FSession);
109 function TBelib.GetMDMD : string;
112 Result := FSession.FMDMD
117 function TBelib.GetDMDM : string;
120 Result := FSession.FDMDM
126 function TBelib.GetErrorMsg: string;
129 Result := FSession.FErrorString
131 Result := 'Error:
\83\81\81[
\83\8b\83A
\83h
\83\8c\83X
\82©
\83p
\83X
\83\8f\81[
\83h
\82ª
\90³
\82µ
\82
\82 \82è
\82Ü
\82¹
\82ñ
\81B';
134 function TBelib.GetErrorCode: integer;
137 Result := FSession.ErrorCode
142 procedure TBelib.MakeError(Session: TBelibSession; Error: DWORD);
144 Buf: array[0..4096] of Char;
146 Session.ErrorCode := Error;
147 if Error = ERROR_NOT_ENOUGH_MEMORY then
148 Session.ErrorString := BELIB_ENOMEM_STRING
150 FillChar(Buf, SizeOf(Buf), #0);
151 FormatMessage({FORMAT_MESSAGE_ALLOCATE_BUFFER or}
152 FORMAT_MESSAGE_IGNORE_INSERTS or
153 FORMAT_MESSAGE_FROM_SYSTEM or
154 FORMAT_MESSAGE_FROM_HMODULE,
155 Pointer(GetModuleHandle('wininet')), Error,
156 (((Word(SUBLANG_DEFAULT)) shl 10) or Word(LANG_NEUTRAL)), //Delphi
\82ÉMAKELANGID
\83}
\83N
\83\8d\82ª
\96³
\82©
\82Á
\82½
\82Ì
\81B(
\81L¥
\83Ö¥`)¼®ÎÞ°Ý
157 // MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
158 Buf, SizeOf(Buf), nil);
159 Session.ErrorString := Buf;
163 {DOLIB
\82ð
\8eQ
\8dl
\82É
\82µ
\82Ä
\82Ü
\82·
165 procedure TBelib.BELIB_LOGIN(Proxy: string; Port: Integer; ID: string; Pass: string);
170 ProxyHostPort: string;
171 Buf: array[0..4096] of Char;
179 FSession := TBelibSession.Create;
181 if Proxy <> '' then begin
182 ProxyHostPort := Format('%s:%d', [Proxy, Port]);
183 hSession := InternetOpen(BELIB_LOGIN_UA, INTERNET_OPEN_TYPE_PROXY, PChar(ProxyHostPort), '', 0);
185 hSession := InternetOpen(BELIB_LOGIN_UA, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
188 if not Assigned(hSession) then
189 MakeError(FSession, GetLastError())
191 hConnect := InternetConnect(hSession, BELIB_LOGIN_HOST,
192 INTERNET_DEFAULT_HTTP_PORT, nil, nil,
193 INTERNET_SERVICE_HTTP, 0, 0);
194 if not Assigned(hConnect) then
195 MakeError(FSession, GetLastError())
197 hRequest := HttpOpenRequest(hConnect, 'POST', BELIB_LOGIN_URL,
199 INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or
200 INTERNET_FLAG_NO_UI, 0);
201 if not Assigned(hRequest) then
202 MakeError(FSession, GetLastError())
204 UserInfo := Format('m=%s&p=%s&submit=%s', [HttpEncode(ID), HttpEncode(Pass), HttpEncode(SJIStoEUC('
\93o
\98^'))]);
205 Header := 'Content-Type: application/x-www-form-urlencoded'#13#10;
206 UserAgent := Format('%s %s', [BELIB_2CH_UA, ClientUA]) + #13#10;
207 Header := Header + UserAgent;
208 if not HttpSendRequest(hRequest, PChar(Header), DWORD(-1), PChar(UserInfo), Length(UserInfo)) then
209 MakeError(FSession, GetLastError())
211 if not InternetReadFile(hRequest, @Buf, SizeOf(Buf), cb) then
212 MakeError(FSession, GetLastError())
213 else if (Pos('cookie', Buf) = 0) or (Pos('"DMDM=', Buf) = 0)
214 or (Pos('"MDMD=', Buf) = 0) then
215 MakeError(FSession, ERROR_INVALID_DATA)
218 body := EUCtoSJIS(body); // 2byte
\95¶
\8e\9a\82Ì
\95\94\95ª
\82Í
\8eg
\82í
\82È
\82¢
\82¯
\82Ç
\94O
\82Ì
\82½
\82ß
219 FSession.FDMDM := Copy(body, Pos('"DMDM=', body) + 6, Length(body));
220 FSession.FDMDM := Copy(FSession.FDMDM, 1, Pos(';', FSession.FDMDM) - 1);
221 FSession.FMDMD := Copy(body, Pos('"MDMD=', body) + 6, Length(body));
222 FSession.FMDMD := Copy(FSession.FMDMD, 1, Pos(';', FSession.FMDMD) - 1);
225 InternetCloseHandle(hRequest);
227 InternetCloseHandle(hConnect);
229 InternetCloseHandle(hSession);