OSDN Git Service

未使用変数を削除
[gikonavigoeson/gikonavi.git] / NewBoard.pas
1 unit NewBoard;
2
3 interface
4
5 uses
6         Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7         Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
8         IdTCPConnection, IdTCPClient, IdHTTP, IDException, StdCtrls, IniFiles,
9         GikoSystem, BoardGroup, MojuUtils;
10
11 type
12         TNewBoardItem = record
13                 FResponseCode: Integer;
14                 FContent: string;
15         end;
16
17         TNewBoardDialog = class(TForm)
18                 Label1: TLabel;
19                 MessageMemo: TMemo;
20                 UpdateButton: TButton;
21     CloseButton: TButton;
22                 Indy: TIdHTTP;
23                 IdAntiFreeze: TIdAntiFreeze;
24                 StopButton: TButton;
25                 Label2: TLabel;
26     BoardURLComboBox: TComboBox;
27     Label13: TLabel;
28                 procedure UpdateButtonClick(Sender: TObject);
29                 procedure StopButtonClick(Sender: TObject);
30                 procedure CloseButtonClick(Sender: TObject);
31                 procedure FormCreate(Sender: TObject);
32         private
33                 { Private \90é\8c¾ }
34                 FAbort: Boolean;
35                 function BoardDownload: TNewBoardItem;
36                 procedure UpdateURL(s: string);
37         public
38                 { Public \90é\8c¾ }
39         end;
40
41 var
42         NewBoardDialog: TNewBoardDialog;
43
44 implementation
45
46 uses Giko, IdHeaderList;
47
48 {$R *.dfm}
49
50 procedure TNewBoardDialog.UpdateButtonClick(Sender: TObject);
51 var
52         Item: TNewBoardItem;
53 begin
54         try
55         GikoSys.Setting.BoardURLSelected := BoardURLComboBox.ItemIndex + 1;
56                 FAbort := False;
57                 UpdateButton.Enabled := False;
58                 StopButton.Enabled := True;
59                 CloseButton.Enabled := False;
60                 Item := BoardDownload;
61                 StopButton.Enabled := False;
62                 if FAbort then
63                         Exit;
64                 if Item.FContent <> '' then begin
65                         UpdateURL(Item.FContent);
66                         GikoForm.ReloadBBS;
67                 end else
68                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ª\8e¸\94s\82µ\82Ü\82µ\82½[' + IntToStr(Item.FResponseCode) + ']');
69         finally
70                 UpdateButton.Enabled := True;
71                 StopButton.Enabled := False;
72                 CloseButton.Enabled := True;
73         end;
74 end;
75
76 procedure TNewBoardDialog.StopButtonClick(Sender: TObject);
77 begin
78         FAbort := True;
79         Indy.DisconnectSocket;
80 end;
81
82 procedure TNewBoardDialog.CloseButtonClick(Sender: TObject);
83 begin
84         Close;
85 end;
86
87 function TNewBoardDialog.BoardDownload: TNewBoardItem;
88 var
89         URL: string;
90         Stream: TMemoryStream;
91         s: string;
92         i: Integer;
93 begin
94         MessageMemo.Clear;
95         Indy.Request.Clear;
96         Indy.RecvBufferSize := Gikosys.Setting.RecvBufferSize;
97         Indy.ProxyParams.BasicAuthentication := False;
98         if GikoSys.Setting.ReadProxy then begin
99                 if GikoSys.Setting.ProxyProtocol then
100                         Indy.ProtocolVersion := pv1_1
101                 else
102                         Indy.ProtocolVersion := pv1_0;
103                 Indy.ProxyParams.ProxyServer := GikoSys.Setting.ReadProxyAddress;
104                 Indy.ProxyParams.ProxyPort := GikoSys.Setting.ReadProxyPort;
105                 Indy.ProxyParams.ProxyUsername := GikoSys.Setting.ReadProxyUserID;
106                 Indy.ProxyParams.ProxyPassword := GikoSys.Setting.ReadProxyPassword;
107                 if GikoSys.Setting.ReadProxyUserID <> '' then
108                         Indy.ProxyParams.BasicAuthentication := True;
109         end else begin
110                 if GikoSys.Setting.Protocol then
111                         Indy.ProtocolVersion := pv1_1
112                 else
113                         Indy.ProtocolVersion := pv1_0;
114                 Indy.ProxyParams.ProxyServer := '';
115                 Indy.ProxyParams.ProxyPort := 80;
116                 Indy.ProxyParams.ProxyUsername := '';
117                 Indy.ProxyParams.ProxyPassword := '';
118         end;
119         //URL := GikoSys.Setting.BoardURL2ch;
120     URL := BoardURLComboBox.Text;
121         Indy.Request.UserAgent := GikoSys.GetUserAgent;
122         Indy.Request.Referer := '';
123         Indy.Request.AcceptEncoding := 'gzip';
124
125         Indy.Request.CacheControl := 'no-cache';
126         Indy.Request.CustomHeaders.Add('Pragma: no-cache');
127
128 //      s := '';
129         Stream := TMemoryStream.Create;
130         try
131                 try
132                         MessageMemo.Lines.Add('\88È\89º\82Ì\8fê\8f\8a\82©\82ç\8eæ\93¾\82µ\82Ü\82·');
133                         //MessageMemo.Lines.Add(GikoSys.Setting.BoardURL2ch);
134             MessageMemo.Lines.Add(URL);
135                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ð\8aJ\8en\82µ\82Ü\82·');
136                         Indy.Get(URL, Stream);
137                         Result.FContent := GikoSys.GzipDecompress(Stream, Indy.Response.ContentEncoding);
138                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ª\8a®\97¹\82µ\82Ü\82µ\82½');
139                 except
140                         on E: EIdConnectException do begin
141                                 MessageMemo.Lines.Add('');
142                                 MessageMemo.Lines.Add('\90Ú\91±\82ª\8e¸\94s\82µ\82Ü\82µ\82½ \89ñ\90ü\82â\83v\83\8d\83L\83V\81AFW\82Ì\8fó\91Ô\82ð\92²\82×\82Ä\82­\82¾\82³\82¢');
143                                 MessageMemo.Lines.Add('FW\82ð\93ü\82ê\82Ä\82¢\82é\90l\82Í\90Ý\92è\82ð\8am\94F\82µ\82Ä\82­\82¾\82³\82¢');
144                                 MessageMemo.Lines.Add('NEC\82ÌPC\82Ì\8fê\8d\87\82ÍPC GATE\82ª\88«\82³\82ð\82µ\82Ä\82¢\82é\89Â\94\\90«\82ª\8d\82\82¢\82Å\82·');
145                                 MessageMemo.Lines.Add('Message: ' + E.Message);
146                         end;
147                         on E: Exception do begin
148                                 if FAbort then
149                                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ð\92\86\92f\82µ\82Ü\82µ\82½')
150                                 else begin
151                                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ª\8e¸\94s\82µ\82Ü\82µ\82½');
152                                         MessageMemo.Lines.Add('ResponseCode: ' + IntToStr(Indy.ResponseCode));
153                                         MessageMemo.Lines.Add('Message: ' + E.Message);
154                                         MessageMemo.Lines.Add('------------------------');
155                                         for i := 0 to Indy.Response.RawHeaders.Count - 1 do begin
156                                                 s := Indy.Response.RawHeaders.Names[i];
157                                                 s := s + ': ' + Indy.Response.RawHeaders.Values[s];
158                                                 MessageMemo.Lines.Add(s);
159                                         end;
160                                         MessageMemo.Lines.Add('------------------------');
161                                 end;
162                         end;
163                 end;
164                 Result.FResponseCode := Indy.ResponseCode;
165         finally
166                 Stream.Free;
167         end;
168 end;
169
170 procedure TNewBoardDialog.UpdateURL(s: string);
171 var
172         i: Integer;
173         idx: Integer;
174         idx1: Integer;
175         idx2: Integer;
176         tmp: string;
177         URL: string;
178         Title: string;
179         cate: string;
180         Board: TBoard;
181         Change: Boolean;
182         ini: TMemIniFile;
183     oldURLs : TStringList;
184     newURLs : TStringList;
185 begin
186         Change := False;
187         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\83`\83F\83b\83N\82ð\8aJ\8en\82µ\82Ü\82·');
188         MessageMemo.Lines.Add('');
189         s := CustomStringReplace(s, '<B>', '<b>', true);
190         s := CustomStringReplace(s, '<BR>', '<br>', true);
191         s := CustomStringReplace(s, '</B>', '</b>', true);
192         s := CustomStringReplace(s, '<A HREF', '<a href', true);
193         s := CustomStringReplace(s, '</A', '</a', true);
194         cate := '';
195
196     oldURLs := TStringList.Create;
197     newURLs := TStringList.Create;
198
199         try
200
201                 GikoSys.ForceDirectoriesEx(GikoSys.GetConfigDir);
202                 ini := TMemIniFile.Create(GikoSys.GetBoardFileName);
203                 try
204                         //
205                         //\8dí\8f\9c\83I\83v\83V\83\87\83\93\82ª\91I\91ð\82³\82ê\82Ä\82¢\82é\8fê\8d\87\82Í\83N\83\8a\83A
206
207                         ini.Clear;
208
209                         while True do begin
210                                 idx1 := AnsiPos('<b>', s);
211                                 idx2 := AnsiPos('<a', s);
212                                 if (idx1 = 0) and (idx2 = 0) then Break;
213
214                                 if idx1 < idx2 then begin
215                                         //<br>
216                                         idx := AnsiPos('</b>', s);
217                                         if idx = 0 then begin
218                                                 s := Copy(s, idx1 + 4, Length(s));
219                                                 continue;
220                                         end;
221                                         tmp := Copy(s, idx1, (idx - idx1) + 4);
222                                         tmp := CustomStringReplace(tmp, '<b>', '');
223                                         tmp := CustomStringReplace(tmp, '</b>', '');
224
225                                         if (tmp = '\82¨\82·\82·\82ß') or
226                                                  (tmp = '\93Á\95Ê\8aé\89æ') or
227                                                  (tmp = '\82Ü\82¿\82a\82a\82r') or
228                                                  (tmp = '\83`\83\83\83b\83g') or
229                                                  (tmp = '\82¨\8aG\82©\82«') or
230                                                  (tmp = '\89^\89c\88Ä\93à') or
231                                                  (tmp = '\83c\81[\83\8b\97Þ') or
232                                                  (tmp = '\91¼\82Ì\83T\83C\83g') then begin
233                                                 cate := '';
234                                                 s := Copy(s, idx + 5, Length(s));
235                                                 Continue;
236                                         end;
237                                         s := Copy(s, idx + 5, Length(s));
238                                         cate := tmp;
239                                 end else begin
240                                         //<a href=
241                                         if cate = '' then begin
242                                                 s := Copy(s, idx2 + 2, Length(s));
243                                         end else begin
244                                                 idx := AnsiPos('</a>', s);
245                                                 tmp := Copy(s, idx2, (idx - idx2) + 4);
246                                                 tmp := CustomStringReplace(tmp, '<a href=', '');
247                                                 tmp := CustomStringReplace(tmp, '</a>', '');
248                         tmp := CustomStringReplace(tmp, 'TARGET=_blank', '');
249                                                 i := AnsiPos('>', tmp);
250                                                 if i <> 0 then begin
251                                                         URL := Copy(tmp, 1, i - 1);
252                                                         Title := Copy(tmp, i + 1, Length(tmp));
253                             Board := BBSs[ 0 ].FindBoardFromTitle(Title);
254                             if Board = nil then begin
255                                 MessageMemo.Lines.Add('\90V\94Â\92Ç\89Á\81u' + Title + '(' + URL + ')\81v');
256                                 ini.WriteString(cate, Title, URL);
257                                 Change := True;
258                             end else begin
259                                 if Board.URL <> URL then begin
260                                         MessageMemo.Lines.Add('URL\95Ï\8dX\81u' + Board.Title + '(' + URL +')\81v');
261                                     ini.WriteString(cate, Title, URL);
262                                     oldURLs.Add(Board.URL);
263                                     newURLs.Add(URL);
264                                     Change := True;
265                                 end else begin
266                                         ini.WriteString(cate, Title, URL);
267                                 end;
268                             end;
269                                                 end else begin
270                                                         s := Copy(s, idx2 + 2, Length(s));
271                                                         Continue;
272                                                 end;
273                                                 s := Copy(s, idx + 5, Length(s));
274                                         end;
275                                 end;
276                         end;
277                 finally
278                         if Change then
279                                 ini.UpdateFile;
280                         ini.Free;
281                 end;
282                 MessageMemo.Lines.Add('');
283             if Change then begin
284             GikoForm.FavoritesURLReplace(oldURLs, newURLs);
285                         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\83`\83F\83b\83N\82ª\8a®\97¹\82µ\82Ü\82µ\82½');
286                         MessageMemo.Lines.Add('\81u\95Â\82\82é\81v\83{\83^\83\93\82ð\89\9f\82µ\82Ä\82­\82¾\82³\82¢');
287                 end else
288                         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\82Í \82 \82è\82Ü\82¹\82ñ\82Å\82µ\82½');
289     finally
290         oldURLs.Free;
291         newURLs.Free;
292     end;
293 end;
294
295 procedure TNewBoardDialog.FormCreate(Sender: TObject);
296 begin
297         StopButton.Enabled := False;
298     BoardURLComboBox.Clear;
299     BoardURLComboBox.Items.AddStrings(GikoSys.Setting.BoardURLs);
300     try
301         BoardURLComboBox.ItemIndex := GikoSys.Setting.BoardURLSelected - 1;
302     except
303         BoardURLComboBox.ItemIndex := 0;
304     end;
305 end;
306
307 end.