OSDN Git Service

・ExternalBoardManager が大きくなったので分割。
[gikonavigoeson/gikonavi.git] / ExternalBoardPlugInMain.pas
1 unit ExternalBoardPlugInMain;
2
3 interface
4
5 uses
6         Windows, Classes, SysUtils,
7         IdHTTP, IdComponent, IdGlobal, IdException,
8         AbonUnit;
9
10 type
11
12         // \83_\83E\83\93\83\8d\81[\83h\82ª\90¬\8c÷\82µ\82½\82©\82Ç\82¤\82©
13         TDownloadState = (dsWait, dsWork, dsComplete, dsDiffComplete, dsNotModify, dsAbort, dsError);
14
15         // *************************************************************************
16         // \83v\83\89\83O\83C\83\93\82Ì\83o\81[\83W\83\87\83\93
17         // *************************************************************************
18         TOnVersionInfo = procedure(
19                 var outAgent            : PChar;        // \83o\81[\83W\83\87\83\93\82ð\88ê\90Ø\8aÜ\82Ü\82È\82¢\8f\83\90\88\82È\96¼\8fÌ
20                 var outMajor            : DWORD;        // \83\81\83W\83\83\81[\83o\81[\83W\83\87\83\93
21                 var outMinor            : DWORD;        // \83}\83C\83i\81[\83o\81[\83W\83\87\83\93
22                 var outRelease  : PChar;        // \83\8a\83\8a\81[\83X\92i\8aK\96¼
23                 var outRevision : DWORD         // \83\8a\83r\83W\83\87\83\93\83i\83\93\83o\81[
24         ); stdcall;
25
26         // *************************************************************************
27         // \8ew\92è\82µ\82½ URL \82ð\82±\82Ì\83v\83\89\83O\83C\83\93\82Å\8eó\82¯\95t\82¯\82é\82©\82Ç\82¤\82©
28         // *************************************************************************
29         TOnAcceptURL = function(
30                 inURL : PChar                                           // \94»\92f\82ð\8bÂ\82¢\82Å\82¢\82é URL
31         ): Boolean; stdcall;                    // \8eó\82¯\95t\82¯\82é\82È\82ç True
32
33         // *************************************************************************
34         // \83X\83\8c\88ê\97\97\82Ì URL \82©\82ç\83X\83\8c\83b\83h\82Ì URL \82ð\93±\82«\8fo\82·
35         // *************************************************************************
36         TOnListURL2ThreadURL = function(
37                 inListURL               : PChar;        // \83X\83\8c\88ê\97\97\82ð\8e¦\82µ\82½ URL
38                 inFileName      : PChar         // \83X\83\8c\83b\83h\83t\83@\83C\83\8b\96¼
39         ) : PChar; stdcall;                     // \83X\83\8c\83b\83h\82Ì URL
40
41 function CreateResultString(
42         resultStr : string
43 ) : PChar; stdcall;
44
45 procedure DisposeResultString(
46         resultStr : PChar
47 ); stdcall;
48
49 implementation
50
51 uses ExternalBoardManager, ExternalThreadItem, GikoSystem, BoardGroup;
52
53 // *************************************************************************
54 // \96ß\82è\92l\82ª PChar \82Å\82 \82é API \82Ì\83\81\83\82\83\8a\82ð\8am\95Û\82·\82é
55 // *************************************************************************
56 function CreateResultString(
57         resultStr : string
58 ) : PChar; stdcall;
59 var
60         tmp : PChar;
61 begin
62
63         tmp := PChar( resultStr );
64         GetMem( Result, Length( tmp ) + 1 );
65         Move( tmp^, Result^, Length( tmp ) + 1 );
66
67 end;
68
69 // *************************************************************************
70 // \96ß\82è\92l\82ª PChar \82Å\82 \82é API \82Ì\83\81\83\82\83\8a\82ð\8aJ\95ú\82·\82é
71 // *************************************************************************
72 procedure DisposeResultString(
73         resultStr : PChar
74 ); stdcall;
75 begin
76
77         FreeMem( resultStr );
78
79 end;
80
81 // *************************************************************************
82 // \83v\83\8d\83O\83\89\83\80\96{\91Ì\82Ì\83o\81[\83W\83\87\83\93\82ð\8eæ\93¾\82·\82é
83 // *************************************************************************
84 procedure VersionInfo(
85         var outAgent            : PChar;        // \83o\81[\83W\83\87\83\93\82ð\88ê\90Ø\8aÜ\82Ü\82È\82¢\8f\83\90\88\82È\96¼\8fÌ
86         var outMajor            : DWORD;        // \83\81\83W\83\83\81[\83o\81[\83W\83\87\83\93
87         var outMinor            : DWORD;        // \83}\83C\83i\81[\83o\81[\83W\83\87\83\93
88         var outRelease  : PChar;        // \83\8a\83\8a\81[\83X\92i\8aK\96¼
89         var outRevision : DWORD         // \83\8a\83r\83W\83\87\83\93\83i\83\93\83o\81[
90 ); stdcall;
91 begin
92
93         {$IFDEF DEBUG}
94         Writeln('ExternalBoardManager.VersionInfo');
95         {$ENDIF}
96         outAgent                := CreateResultString( PChar( APP_NAME ) );
97         outMajor                := 1;
98         outMinor                := 0;
99         outRelease      := CreateResultString( PChar( BETA_VERSION_NAME_E ) );
100         outRevision     := BETA_VERSION;
101
102 end;
103
104 // *************************************************************************
105 // \8ew\92è\82µ\82½ URL \82ð\83_\83E\83\93\83\8d\81[\83h\82µ\82Ä\95Ô\82·
106 // *************************************************************************
107 function InternalDownload(
108         inURL                                                   : PChar;        // \83_\83E\83\93\83\8d\81[\83h\82·\82é URL
109         var ioModified          : Double;       // \8dÅ\8cã\82É\8eæ\93¾\82µ\82½\93ú\8e\9e
110         var outResultData       : PChar;        // \83_\83E\83\93\83\8d\81[\83h\82³\82ê\82½\95\8e\9a\97ñ
111         inRangeStart                    : DWORD;        // \8aJ\8en\88Ê\92u
112         inRangeEnd                              : DWORD         // \8fI\97¹\88Ê\92u
113 ) : Longint; stdcall;                                   // \83\8c\83X\83|\83\93\83X\83R\81[\83h
114 var
115         httpSocket                              : TIdHTTP;
116         resStream                                       : TMemoryStream;
117         content                                         : string;
118 begin
119
120         {$IFDEF DEBUG}
121         Writeln('ExternalBoardManager.InternalDownload');
122         {$ENDIF}
123         Result := -1;
124
125         httpSocket := TIdHTTP.Create( nil );
126         try
127                 // \83C\83x\83\93\83g\82Ì\90Ý\92è
128                 if Assigned( OnWork ) then
129                         httpSocket.OnWork                               := OnWork;
130                 if Assigned( OnWorkBegin ) then
131                         httpSocket.OnWorkBegin  := OnWorkBegin;
132                 if Assigned( OnWorkEnd ) then
133                         httpSocket.OnWorkEnd            := OnWorkEnd;
134
135                 // \92Ê\90M\82É\8aÖ\82·\82é\90Ý\92è
136                 httpSocket.Request.CustomHeaders.Clear;
137                 httpSocket.Response.Clear;
138                 httpSocket.Request.Clear;
139
140                 httpSocket.Request.UserAgent    := GikoSys.GetUserAgent;
141                 httpSocket.RecvBufferSize                       := Gikosys.Setting.RecvBufferSize;
142                 httpSocket.ProxyParams.BasicAuthentication := False;
143                 {$IFDEF DEBUG}
144                 Writeln('------------------------------------------------------------');
145                 {$ENDIF}
146                 //httpSocket.AllowCookies := False;
147                 if GikoSys.Setting.ReadProxy then begin
148                         if GikoSys.Setting.ProxyProtocol then
149                                 httpSocket.ProtocolVersion := pv1_1
150                         else
151                                 httpSocket.ProtocolVersion := pv1_0;
152                         httpSocket.ProxyParams.ProxyServer              := GikoSys.Setting.ReadProxyAddress;
153                         httpSocket.ProxyParams.ProxyPort                        := GikoSys.Setting.ReadProxyPort;
154                         httpSocket.ProxyParams.ProxyUsername    := GikoSys.Setting.ReadProxyUserID;
155                         httpSocket.ProxyParams.ProxyPassword    := GikoSys.Setting.ReadProxyPassword;
156                         if GikoSys.Setting.ReadProxyUserID <> '' then
157                                 httpSocket.ProxyParams.BasicAuthentication := True;
158                         {$IFDEF DEBUG}
159                         Writeln('\83v\83\8d\83L\83V\90Ý\92è\82 \82è');
160                         Writeln('\83z\83X\83g: ' + GikoSys.Setting.ReadProxyAddress);
161                         Writeln('\83|\81[\83g: ' + IntToStr( GikoSys.Setting.ReadProxyPort ));
162                         {$ENDIF}
163                 end else begin
164                         if GikoSys.Setting.Protocol then
165                                 httpSocket.ProtocolVersion := pv1_1
166                         else
167                                 httpSocket.ProtocolVersion := pv1_0;
168                         httpSocket.ProxyParams.ProxyServer              := '';
169                         httpSocket.ProxyParams.ProxyPort                        := 80;
170                         httpSocket.ProxyParams.ProxyUsername    := '';
171                         httpSocket.ProxyParams.ProxyPassword    := '';
172                         {$IFDEF DEBUG}
173                         Writeln('\83v\83\8d\83L\83V\90Ý\92è\82È\82µ');
174                         {$ENDIF}
175                 end;
176
177                 httpSocket.Request.ContentRangeStart    := inRangeStart;
178                 httpSocket.Request.ContentRangeEnd              := inRangeEnd;
179                 if (ioModified <> 0) and (ioModified <> ZERO_DATE) then
180                         httpSocket.Request.LastModified := ioModified - OffsetFromUTC;
181                 if inRangeStart = 0 then
182                         httpSocket.Request.AcceptEncoding := 'gzip'
183                 else
184                         httpSocket.Request.AcceptEncoding := '';
185                 httpSocket.Request.Accept := 'text/html';
186
187                 resStream := TMemoryStream.Create;
188                 try
189                         try
190                                 resStream.Clear;
191
192                                 {$IFDEF DEBUG}
193                                 Writeln('URL: ' + inURL);
194                                 {$ENDIF}
195                                 httpSocket.Get( inURL, resStream );
196                                 {$IFDEF DEBUG}
197                                 Writeln('\8eæ\93¾\82Å\97á\8aO\82È\82µ');
198                                 {$ENDIF}
199
200                                 content                 := GikoSys.GzipDecompress( resStream, httpSocket.Response.ContentEncoding );
201                                 ioModified      := httpSocket.Response.LastModified;
202
203                                 Result := httpSocket.ResponseCode;
204                                 if (Length( content ) = 0) and (Result = 206) then
205                                         Result := 304;
206
207                                 outResultData := CreateResultString( content );
208                         except
209                                 on E: EIdSocketError do begin
210                                         ioModified              := ZERO_DATE;
211                                         outResultData   := nil;
212                                 end;
213                                 on E: EIdConnectException do begin
214                                         ioModified              := ZERO_DATE;
215                                         outResultData   := nil;
216                                         //Item.ErrText  := E.Message;
217                                 end;
218                                 on E: Exception do begin
219                                         {$IFDEF DEBUG}
220                                         Writeln('\8eæ\93¾\82Å\97á\8aO\82 \82è');
221                                         Writeln('E.Message: ' + E.Message);
222                                         {$ENDIF}
223                                         ioModified              := ZERO_DATE;
224                                         outResultData   := nil;
225                                         Result                          := httpSocket.ResponseCode;
226                                         //Item.ErrText  := E.Message;
227                                 end;
228                         end;
229                 finally
230                         resStream.Free;
231                 end;
232
233         finally
234                 httpSocket.Free;
235         end;
236
237 end;
238
239 // *************************************************************************
240 // \82Q\82¿\82á\82ñ\82Ë\82é\82Ì dat \8c`\8e®\82ð\83\8d\81[\83J\83\8b\82 \82Ú\81`\82ñ\82É\92Ê\82·
241 // *************************************************************************
242 function InternalAbon(
243         inDatText : PChar                               // \96¼\91O<>\83\81\81[\83\8b<>\93ú\95tID<>\96{\95¶<>[\89ü\8ds] \82Å\8d\\90¬\82³\82ê\82½\83e\83L\83X\83g
244 ) : PChar; stdcall;                             // \82 \82Ú\81`\82ñ\8dÏ\82Ý\82Ì dat \8c`\8e®\83e\83L\83X\83g
245 var
246         datList : TStringList;
247 begin
248
249         {$IFDEF DEBUG}
250         Writeln('ExternalBoardManager.InternalAbon');
251         {$ENDIF}
252         datList := TStringList.Create;
253         datList.Text := string( inDatText );
254         //GikoSys.FAbon.IndividualAbon( datList, ChangeFileExt(FileName,'.NG'));
255         GikoSys.FAbon.Execute( datList );
256         GikoSys.FSelectResFilter.Execute( datList );
257
258         Result := CreateResultString( datList.Text );
259
260 end;
261
262 // *************************************************************************
263 // \82Q\82¿\82á\82ñ\82Ë\82é\82Ì dat \8c`\8e® 1 \8ds\82ð HTML \82É\95Ï\8a·\82·\82é
264 // *************************************************************************
265 function InternalDat2HTML(
266         inDatRes                : PChar;                        // \96¼\91O<>\83\81\81[\83\8b<>\93ú\95tID<>\96{\95¶<> \82Å\8d\\90¬\82³\82ê\82½\83e\83L\83X\83g
267         inResNo                 : DWORD;                        // \83\8c\83X\94Ô\8d\86
268         inIsNew                 : Boolean                       // \90V\92\85\83\8c\83X\82È\82ç True
269 ) : PChar; stdcall;                             // \90®\8c`\82³\82ê\82½ HTML
270 var
271         board                           : TBoard;
272         threadItem      : TThreadItem;
273 begin
274
275         {$IFDEF DEBUG}
276         Writeln('ExternalBoardManager.InternalDat2HTML');
277         {$ENDIF}
278         // \83_\83~\81[\82Ì\83N\83\89\83X
279         board                           := TBoard.Create;
280         threadItem      := TThreadItem.Create;
281
282         try
283                 threadItem.ParentBoard := Board;
284
285                 Result := ThreadItemDat2HTML( DWORD( threadItem ), inDatRes, inResNo, inIsNew );
286         except
287                 Result := nil;
288         end;
289
290 end;
291
292 exports
293         CreateResultString,
294         DisposeResultString,
295         VersionInfo,
296         InternalDownload,
297         InternalAbon,
298         InternalDat2HTML;
299
300 end.