OSDN Git Service

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