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     n: Integer;
177         tmp: string;
178         URL: string;
179         Title: string;
180         cate: string;
181         Board: TBoard;
182         Change: Boolean;
183         ini: TMemIniFile;
184     oldURLs : TStringList;
185     newURLs : TStringList;
186 begin
187         Change := False;
188         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\83`\83F\83b\83N\82ð\8aJ\8en\82µ\82Ü\82·');
189         MessageMemo.Lines.Add('');
190         s := CustomStringReplace(s, '<B>', '<b>', true);
191         s := CustomStringReplace(s, '<BR>', '<br>', true);
192         s := CustomStringReplace(s, '</B>', '</b>', true);
193         s := CustomStringReplace(s, '<A HREF', '<a href', true);
194         s := CustomStringReplace(s, '</A', '</a', true);
195         cate := '';
196
197     oldURLs := TStringList.Create;
198     newURLs := TStringList.Create;
199
200         try
201
202                 GikoSys.ForceDirectoriesEx(GikoSys.GetConfigDir);
203                 ini := TMemIniFile.Create(GikoSys.GetBoardFileName);
204                 try
205                         //
206                         //\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
207
208                         ini.Clear;
209
210                         while True do begin
211                                 idx1 := AnsiPos('<b>', s);
212                                 idx2 := AnsiPos('<a', s);
213                                 if (idx1 = 0) and (idx2 = 0) then Break;
214
215                                 if idx1 < idx2 then begin
216                                         //<br>
217                                         idx := AnsiPos('</b>', s);
218                                         if idx = 0 then begin
219                                                 s := Copy(s, idx1 + 4, Length(s));
220                                                 continue;
221                                         end;
222                                         tmp := Copy(s, idx1, (idx - idx1) + 4);
223                                         tmp := CustomStringReplace(tmp, '<b>', '');
224                                         tmp := CustomStringReplace(tmp, '</b>', '');
225
226                                         if (tmp = '\82¨\82·\82·\82ß') or
227                                                  (tmp = '\93Á\95Ê\8aé\89æ') or
228                                                  (tmp = '\82Ü\82¿\82a\82a\82r') or
229                                                  (tmp = '\83`\83\83\83b\83g') or
230                                                  (tmp = '\82¨\8aG\82©\82«') or
231                                                  (tmp = '\89^\89c\88Ä\93à') or
232                                                  (tmp = '\83c\81[\83\8b\97Þ') or
233                                                  (tmp = '\91¼\82Ì\83T\83C\83g') then begin
234                                                 cate := '';
235                                                 s := Copy(s, idx + 5, Length(s));
236                                                 Continue;
237                                         end;
238                                         s := Copy(s, idx + 5, Length(s));
239                                         cate := tmp;
240                                 end else begin
241                                         //<a href=
242                                         if cate = '' then begin
243                                                 s := Copy(s, idx2 + 2, Length(s));
244                                         end else begin
245                                                 idx := AnsiPos('</a>', s);
246                                                 tmp := Copy(s, idx2, (idx - idx2) + 4);
247                                                 tmp := CustomStringReplace(tmp, '<a href=', '');
248                                                 tmp := CustomStringReplace(tmp, '</a>', '');
249                         tmp := CustomStringReplace(tmp, 'TARGET=_blank', '');
250                                                 i := AnsiPos('>', tmp);
251                                                 if i <> 0 then begin
252                                                         URL := Copy(tmp, 1, i - 1);
253                                                         Title := Copy(tmp, i + 1, Length(tmp));
254                             Board := BBSs[ 0 ].FindBoardFromTitle(Title);
255                             if Board = nil then begin
256                                 MessageMemo.Lines.Add('\90V\94Â\92Ç\89Á\81u' + Title + '(' + URL + ')\81v');
257                                 ini.WriteString(cate, Title, URL);
258                                 Change := True;
259                             end else begin
260                                 if Board.URL <> URL then begin
261                                         MessageMemo.Lines.Add('URL\95Ï\8dX\81u' + Board.Title + '(' + URL +')\81v');
262                                     ini.WriteString(cate, Title, URL);
263                                     oldURLs.Add(Board.URL);
264                                     newURLs.Add(URL);
265                                     Change := True;
266                                 end else begin
267                                         ini.WriteString(cate, Title, URL);
268                                 end;
269                             end;
270                                                 end else begin
271                                                         s := Copy(s, idx2 + 2, Length(s));
272                                                         Continue;
273                                                 end;
274                                                 s := Copy(s, idx + 5, Length(s));
275                                         end;
276                                 end;
277                         end;
278                 finally
279                         if Change then
280                                 ini.UpdateFile;
281                         ini.Free;
282                 end;
283                 MessageMemo.Lines.Add('');
284             if Change then begin
285             GikoForm.FavoritesURLReplace(oldURLs, newURLs);
286                         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\83`\83F\83b\83N\82ª\8a®\97¹\82µ\82Ü\82µ\82½');
287                         MessageMemo.Lines.Add('\81u\95Â\82\82é\81v\83{\83^\83\93\82ð\89\9f\82µ\82Ä\82­\82¾\82³\82¢');
288                 end else
289                         MessageMemo.Lines.Add('\90V\94Â\81A\94ÂURL\95Ï\8dX\82Í \82 \82è\82Ü\82¹\82ñ\82Å\82µ\82½');
290     finally
291         oldURLs.Free;
292         newURLs.Free;
293     end;
294 end;
295
296 procedure TNewBoardDialog.FormCreate(Sender: TObject);
297 begin
298         StopButton.Enabled := False;
299     BoardURLComboBox.Clear;
300     BoardURLComboBox.Items.AddStrings(GikoSys.Setting.BoardURLs);
301     try
302         BoardURLComboBox.ItemIndex := GikoSys.Setting.BoardURLSelected - 1;
303     except
304         BoardURLComboBox.ItemIndex := 0;
305     end;
306 end;
307
308 end.