OSDN Git Service

更新が無かったときに、BBSをリロードしなくした。
[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;
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         BoardURLComboBox: TComboBox;
26         Label13: TLabel;
27         EditIgnoreListsButton: TButton;
28         Label2: TLabel;
29                 procedure UpdateButtonClick(Sender: TObject);
30                 procedure StopButtonClick(Sender: TObject);
31                 procedure CloseButtonClick(Sender: TObject);
32                 procedure FormCreate(Sender: TObject);
33         procedure EditIgnoreListsButtonClick(Sender: TObject);
34         procedure FormClose(Sender: TObject; var Action: TCloseAction);
35         private
36                 { Private \90é\8c¾ }
37                 IgnoreLists : TStringList;
38                 FAbort: Boolean;
39                 function BoardDownload(const URL: String): TNewBoardItem;
40                 function BoardLoardFromFile(const FilePath: String): String;
41                 function UpdateURL(s: string): boolean;
42                 procedure SetIgnoreCategory(b: boolean);
43                 procedure EditIgnoreList(Sender: TObject);
44                 procedure UpdateIgnoreList(Sender: TObject);
45         public
46                 { Public \90é\8c¾ }
47         end;
48
49 var
50         NewBoardDialog: TNewBoardDialog;
51
52
53 implementation
54
55 uses Giko, IdHeaderList, MojuUtils, GikoDataModule;
56
57 {$R *.dfm}
58
59 procedure TNewBoardDialog.UpdateButtonClick(Sender: TObject);
60 var
61         Item: TNewBoardItem;
62         URL : String;
63         protocol, host, path, document, port, bookmark: String;
64 begin
65         try
66                 GikoSys.Setting.BoardURLSelected := BoardURLComboBox.ItemIndex + 1;
67                 FAbort := False;
68                 UpdateButton.Enabled := False;
69                 StopButton.Enabled := True;
70                 CloseButton.Enabled := False;
71                 EditIgnoreListsButton.Enabled := False;
72                 URL := BoardURLComboBox.Text;
73                 GikoSys.ParseURI(URL, protocol, host, path, document, port, bookmark);
74                 Writeln(protocol);
75                 if (protocol = '') then begin
76                         Item.FContent := BoardLoardFromFile(URL);
77                 end else if (AnsiPos('http', protocol) > 0) then begin
78                         Item := BoardDownload(URL);
79                 end;
80                 StopButton.Enabled := False;
81                 if FAbort then
82                         Exit;
83                 if Item.FContent <> '' then begin
84                         GikoDM.TabsSaveAction.Tag := 1;
85                         GikoDM.TabsOpenAction.Tag := 1;
86                         GikoDM.TabsSaveAction.Execute;
87                         if (UpdateURL(Item.FContent)) then begin
88                                 GikoForm.ReloadBBS;
89                         end;
90                         GikoDM.TabsSaveAction.Tag := 0;
91                         GikoDM.TabsOpenAction.Tag := 0;
92                 end else
93                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ª\8e¸\94s\82µ\82Ü\82µ\82½[' + IntToStr(Item.FResponseCode) + ']');
94         finally
95                 UpdateButton.Enabled := True;
96                 StopButton.Enabled := False;
97                 CloseButton.Enabled := True;
98                 EditIgnoreListsButton.Enabled := True;
99         end;
100 end;
101
102 procedure TNewBoardDialog.StopButtonClick(Sender: TObject);
103 begin
104         FAbort := True;
105         Indy.DisconnectSocket;
106 end;
107
108 procedure TNewBoardDialog.CloseButtonClick(Sender: TObject);
109 begin
110         Close;
111 end;
112
113 function TNewBoardDialog.BoardDownload(const URL: String): TNewBoardItem;
114 var
115         Stream: TMemoryStream;
116         s: string;
117         i: Integer;
118 begin
119         MessageMemo.Clear;
120         Indy.Request.Clear;
121         Indy.RecvBufferSize := Gikosys.Setting.RecvBufferSize;
122         Indy.ProxyParams.BasicAuthentication := False;
123         if GikoSys.Setting.ReadProxy then begin
124                 if GikoSys.Setting.ProxyProtocol then
125                         Indy.ProtocolVersion := pv1_1
126                 else
127                         Indy.ProtocolVersion := pv1_0;
128                 Indy.ProxyParams.ProxyServer := GikoSys.Setting.ReadProxyAddress;
129                 Indy.ProxyParams.ProxyPort := GikoSys.Setting.ReadProxyPort;
130                 Indy.ProxyParams.ProxyUsername := GikoSys.Setting.ReadProxyUserID;
131                 Indy.ProxyParams.ProxyPassword := GikoSys.Setting.ReadProxyPassword;
132                 if GikoSys.Setting.ReadProxyUserID <> '' then
133                         Indy.ProxyParams.BasicAuthentication := True;
134         end else begin
135                 if GikoSys.Setting.Protocol then
136                         Indy.ProtocolVersion := pv1_1
137                 else
138                         Indy.ProtocolVersion := pv1_0;
139                 Indy.ProxyParams.ProxyServer := '';
140                 Indy.ProxyParams.ProxyPort := 80;
141                 Indy.ProxyParams.ProxyUsername := '';
142                 Indy.ProxyParams.ProxyPassword := '';
143         end;
144         //URL := GikoSys.Setting.BoardURL2ch;
145         //URL := BoardURLComboBox.Text;
146         Indy.Request.UserAgent := GikoSys.GetUserAgent;
147         Indy.Request.Referer := '';
148         Indy.Request.AcceptEncoding := 'gzip';
149
150         Indy.Request.CacheControl := 'no-cache';
151         Indy.Request.CustomHeaders.Add('Pragma: no-cache');
152
153 //      s := '';
154         Stream := TMemoryStream.Create;
155         try
156                 try
157                         MessageMemo.Lines.Add('\88È\89º\82Ì\8fê\8f\8a\82©\82ç\8eæ\93¾\82µ\82Ü\82·');
158                         //MessageMemo.Lines.Add(GikoSys.Setting.BoardURL2ch);
159                         MessageMemo.Lines.Add(URL);
160                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ð\8aJ\8en\82µ\82Ü\82·');
161                         IdAntiFreeze.Active := True;
162                         try
163                                 Indy.Get(URL, Stream);
164                         finally
165                                 IdAntiFreeze.Active := False;
166                         end;
167                         Result.FContent := GikoSys.GzipDecompress(Stream, Indy.Response.ContentEncoding);
168                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ª\8a®\97¹\82µ\82Ü\82µ\82½');
169                 except
170                         on E: EIdConnectException do begin
171                                 MessageMemo.Lines.Add('');
172                                 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¢');
173                                 MessageMemo.Lines.Add('FW\82ð\93ü\82ê\82Ä\82¢\82é\90l\82Í\90Ý\92è\82ð\8am\94F\82µ\82Ä\82­\82¾\82³\82¢');
174                                 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·');
175                                 MessageMemo.Lines.Add('Message: ' + E.Message);
176                         end;
177                         on E: Exception do begin
178                                 if FAbort then
179                                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ð\92\86\92f\82µ\82Ü\82µ\82½')
180                                 else begin
181                                         MessageMemo.Lines.Add('\83_\83E\83\93\83\8d\81[\83h\82ª\8e¸\94s\82µ\82Ü\82µ\82½');
182                                         MessageMemo.Lines.Add('ResponseCode: ' + IntToStr(Indy.ResponseCode));
183                                         MessageMemo.Lines.Add('Message: ' + E.Message);
184                                         MessageMemo.Lines.Add('------------------------');
185                                         for i := 0 to Indy.Response.RawHeaders.Count - 1 do begin
186                                                 s := Indy.Response.RawHeaders.Names[i];
187                                                 s := s + ': ' + Indy.Response.RawHeaders.Values[s];
188                                                 MessageMemo.Lines.Add(s);
189                                         end;
190                                         MessageMemo.Lines.Add('------------------------');
191                                 end;
192                         end;
193                 end;
194                 Result.FResponseCode := Indy.ResponseCode;
195         finally
196                 Stream.Free;
197         end;
198 end;
199
200 function TNewBoardDialog.UpdateURL(s: string): boolean;
201 var
202         i: Integer;
203         idx: Integer;
204         idx1: Integer;
205         idx2: Integer;
206         tmp: string;
207         URL: string;
208         Title: string;
209         cate: string;
210         Board: TBoard;
211         Change: Boolean;
212         Ignore: Boolean;
213         ini: TMemIniFile;
214         oldURLs : TStringList;
215         newURLs : TStringList;
216 begin
217         Change := False;
218         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\83`\83F\83b\83N\82ð\8aJ\8en\82µ\82Ü\82·');
219         MessageMemo.Lines.Add('');
220         s := CustomStringReplace(s, '<B>', '<b>', true);
221         s := CustomStringReplace(s, '<BR>', '<br>', true);
222         s := CustomStringReplace(s, '</B>', '</b>', true);
223         s := CustomStringReplace(s, '<A HREF', '<a href', true);
224         s := CustomStringReplace(s, '</A', '</a', true);
225         cate := '';
226
227         oldURLs := TStringList.Create;
228         newURLs := TStringList.Create;
229
230         try
231
232                 GikoSys.ForceDirectoriesEx(GikoSys.GetConfigDir);
233                 ini := TMemIniFile.Create(GikoSys.GetBoardFileName);
234                 try
235                         //
236                         //\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
237
238                         ini.Clear;
239
240                         while True do begin
241                                 idx1 := AnsiPos('<b>', s);
242                                 idx2 := AnsiPos('<a', s);
243                                 if (idx1 = 0) and (idx2 = 0) then Break;
244
245                                 if idx1 < idx2 then begin
246                                         //<br>
247                                         idx := AnsiPos('</b>', s);
248                                         if idx = 0 then begin
249                                                 s := Copy(s, idx1 + 4, Length(s));
250                                                 continue;
251                                         end;
252                                         tmp := Copy(s, idx1, (idx - idx1) + 4);
253                                         tmp := CustomStringReplace(tmp, '<b>', '');
254                                         tmp := CustomStringReplace(tmp, '</b>', '');
255                                         Ignore := false;
256                                         for i := 0 to IgnoreLists.Count - 1 do begin
257                                                 if tmp = Trim(IgnoreLists[i]) then begin
258                                                         cate := '';
259                                                         s := Copy(s, idx + 5, Length(s));
260                                                         Ignore := True;
261                                                         break;
262                                                 end;
263                                         end;
264                                         if Ignore then
265                                                 Continue;
266                                         {
267                                         if (tmp = '\82¨\82·\82·\82ß') or
268                                                  (tmp = '\93Á\95Ê\8aé\89æ') or
269                                                  (tmp = '\82Ü\82¿\82a\82a\82r') or
270                                                  (tmp = '\83`\83\83\83b\83g') or
271                                                  (tmp = '\82¨\8aG\82©\82«') or
272                                                  (tmp = '\89^\89c\88Ä\93à') or
273                                                  (tmp = '\83c\81[\83\8b\97Þ') or
274                                                  (tmp = '\91¼\82Ì\83T\83C\83g') then begin
275                                                 cate := '';
276                                                 s := Copy(s, idx + 5, Length(s));
277                                                 Continue;
278                                         end;
279                                         }
280                                         s := Copy(s, idx + 5, Length(s));
281                                         cate := tmp;
282                                 end else begin
283                                         //<a href=
284                                         if cate = '' then begin
285                                                 s := Copy(s, idx2 + 2, Length(s));
286                                         end else begin
287                                                 idx := AnsiPos('</a>', s);
288                                                 tmp := Copy(s, idx2, (idx - idx2) + 4);
289                                                 tmp := CustomStringReplace(tmp, '<a href=', '');
290                                                 tmp := CustomStringReplace(tmp, '</a>', '');
291                         tmp := CustomStringReplace(tmp, 'TARGET=_blank', '');
292                                                 i := AnsiPos('>', tmp);
293                                                 if i <> 0 then begin
294                                                         URL := Copy(tmp, 1, i - 1);
295                                                         Title := Copy(tmp, i + 1, Length(tmp));
296                             Board := BBSs[ 0 ].FindBoardFromTitle(Title);
297                             if Board = nil then begin
298                                 MessageMemo.Lines.Add('\90V\94Â\92Ç\89Á\81u' + Title + '(' + URL + ')\81v');
299                                 ini.WriteString(cate, Title, URL);
300                                 Change := True;
301                             end else begin
302                                 if Board.URL <> URL then begin
303                                         MessageMemo.Lines.Add('URL\95Ï\8dX\81u' + Board.Title + '(' + URL +')\81v');
304                                     ini.WriteString(cate, Title, URL);
305                                     oldURLs.Add(Board.URL);
306                                     newURLs.Add(URL);
307                                     Change := True;
308                                 end else begin
309                                         ini.WriteString(cate, Title, URL);
310                                 end;
311                             end;
312                                                 end else begin
313                                                         s := Copy(s, idx2 + 2, Length(s));
314                                                         Continue;
315                                                 end;
316                                                 s := Copy(s, idx + 5, Length(s));
317                                         end;
318                                 end;
319                         end;
320                 finally
321                         if Change then
322                                 ini.UpdateFile;
323                         ini.Free;
324                 end;
325                 MessageMemo.Lines.Add('');
326             if Change then begin
327             GikoForm.FavoritesURLReplace(oldURLs, newURLs);
328             GikoForm.RoundListURLReplace(oldURLs, newURLs);
329             GikoForm.TabFileURLReplace(oldURLs, newURLs);
330                         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\83`\83F\83b\83N\82ª\8a®\97¹\82µ\82Ü\82µ\82½');
331                         MessageMemo.Lines.Add('\81u\95Â\82\82é\81v\83{\83^\83\93\82ð\89\9f\82µ\82Ä\82­\82¾\82³\82¢');
332                 end else
333                         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\82Í \82 \82è\82Ü\82¹\82ñ\82Å\82µ\82½');
334     finally
335         oldURLs.Free;
336         newURLs.Free;
337         end;
338         Result := Change;
339 end;
340
341 procedure TNewBoardDialog.FormCreate(Sender: TObject);
342 begin
343         StopButton.Enabled := False;
344         BoardURLComboBox.Clear;
345         BoardURLComboBox.Items.AddStrings(GikoSys.Setting.BoardURLs);
346         try
347                 BoardURLComboBox.ItemIndex := GikoSys.Setting.BoardURLSelected - 1;
348         except
349                 BoardURLComboBox.ItemIndex := 0;
350         end;
351         SetIgnoreCategory(false);
352 end;
353 //\94Â\8dX\90V\82Ì\8f\9c\8aO\83J\83e\83S\83\8a\83\8a\83X\83g\82Ì\93o\98^
354 {['\82¨\82·\82·\82ß', '\93Á\95Ê\8aé\89æ', '\82Ü\82¿\82a\82a\82r', '\83`\83\83\83b\83g', '\82¨\8aG\82©\82«', '\89^\89c\88Ä\93à', '\83c\81[\83\8b\97Þ', '\91¼\82Ì\83T\83C\83g']}
355 procedure TNewBoardDialog.SetIgnoreCategory(b: boolean);
356 begin
357         IgnoreLists := TStringList.Create;
358         if not( FileExists(GikoSys.Setting.GetIgnoreFileName) ) or ( b )then begin
359                 IgnoreLists.Add('\82¨\82·\82·\82ß');
360                 IgnoreLists.Add('\93Á\95Ê\8aé\89æ');
361                 IgnoreLists.Add('\82Ü\82¿\82a\82a\82r');
362                 IgnoreLists.Add('\83`\83\83\83b\83g');
363                 IgnoreLists.Add('\82¨\8aG\82©\82«');
364                 IgnoreLists.Add('\89^\89c\88Ä\93à');
365                 IgnoreLists.Add('\83c\81[\83\8b\97Þ');
366                 IgnoreLists.Add('\91¼\82Ì\83T\83C\83g');
367         end else begin
368                 try
369                         IgnoreLists.LoadFromFile(GikoSys.Setting.GetIgnoreFileName);
370                 except
371                         IgnoreLists.Free;
372                         SetIgnoreCategory(true);
373                 end;
374         end;
375 end;
376
377 procedure TNewBoardDialog.EditIgnoreListsButtonClick(Sender: TObject);
378 begin
379         EditIgnoreList(Sender);
380         EditIgnoreListsButton.OnClick := UpdateIgnoreList;
381 end;
382 procedure TNewBoardDialog.EditIgnoreList(Sender: TObject);
383 var
384         i: Integer;
385 begin
386         EditIgnoreListsButton.Caption := '\8f\9c\8aO\83J\83e\83S\83\8a\81[\8dX\90V';
387         Label2.Caption := '\8ae\82P\8ds\82É\83J\83e\83S\83\8a\96¼\82ð\8bL\93ü\82µ\82Ä\82­\82¾\82³\82¢\81B\81i\89ü\8ds\82ÍCtrl+Enter\81j';
388         UpdateButton.Enabled := false;
389         //MessageMemo.ReadOnly := false;
390         MessageMemo.Clear;
391         for i := 0 to IgnoreLists.Count - 1 do
392                 MessageMemo.Lines.Add(IgnoreLists[i]);
393 end;
394 procedure TNewBoardDialog.UpdateIgnoreList(Sender: TObject);
395 var
396         i: Integer;
397 begin
398         Label2.Caption := '';
399     UpdateButton.Enabled := true;
400         EditIgnoreListsButton.Caption := '\8f\9c\8aO\83J\83e\83S\83\8a\81[\95Ò\8fW';
401         IgnoreLists.Clear;
402         for i := 0 to MessageMemo.Lines.Count - 1 do
403                 IgnoreLists.Add(MessageMemo.Lines[i]);
404         IgnoreLists.SaveToFile(GikoSys.Setting.GetIgnoreFileName);
405         IgnoreLists.Free;
406         SetIgnoreCategory(false);
407         //MessageMemo.ReadOnly := true;
408         MessageMemo.Clear;
409         EditIgnoreListsButton.OnClick := EditIgnoreListsButtonClick;
410 end;
411
412 procedure TNewBoardDialog.FormClose(Sender: TObject;
413   var Action: TCloseAction);
414 begin
415         IgnoreLists.Free;
416 end;
417 //! \83\8d\81[\83J\83\8b\83t\83@\83C\83\8b\82ð\83\8d\81[\83h\82·\82é
418 function TNewBoardDialog.BoardLoardFromFile(const FilePath: String): String;
419 var
420         html : TStringList;
421 begin
422         Result := '';
423         // \83t\83@\83C\83\8b\82ª\91\8dÝ\82µ\82Ä\82¢\82é\82©\83`\83F\83b\83N
424         if (FileExists(FilePath)) then begin
425                 html := TStringList.Create();
426                 try
427                         html.LoadFromFile(FilePath);
428                         Result := html.Text;
429                 finally
430                         html.Free;
431                 end;
432         end;
433 end;
434 end.