| 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 ?辿?転 } |
| 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 ?辿?転 } |
| 47 |
class procedure InitHTTPClient(client : TIdHTTP); |
| 48 |
end; |
| 49 |
|
| 50 |
var |
| 51 |
NewBoardDialog: TNewBoardDialog; |
| 52 |
|
| 53 |
|
| 54 |
implementation |
| 55 |
|
| 56 |
uses Giko, IdHeaderList, MojuUtils, GikoDataModule; |
| 57 |
|
| 58 |
{$R *.dfm} |
| 59 |
|
| 60 |
procedure TNewBoardDialog.UpdateButtonClick(Sender: TObject); |
| 61 |
var |
| 62 |
Item: TNewBoardItem; |
| 63 |
URL : String; |
| 64 |
protocol, host, path, document, port, bookmark: String; |
| 65 |
TabURLs: TStringList; |
| 66 |
begin |
| 67 |
try |
| 68 |
MessageMemo.Clear; |
| 69 |
GikoSys.Setting.BoardURLSelected := BoardURLComboBox.ItemIndex + 1; |
| 70 |
FAbort := False; |
| 71 |
UpdateButton.Enabled := False; |
| 72 |
StopButton.Enabled := True; |
| 73 |
CloseButton.Enabled := False; |
| 74 |
EditIgnoreListsButton.Enabled := False; |
| 75 |
URL := BoardURLComboBox.Text; |
| 76 |
GikoSys.ParseURI(URL, protocol, host, path, document, port, bookmark); |
| 77 |
if (protocol = '') then begin |
| 78 |
Item.FContent := BoardLoardFromFile(URL); |
| 79 |
end else if (AnsiPos('http', protocol) > 0) then begin |
| 80 |
Item := BoardDownload(URL); |
| 81 |
end; |
| 82 |
StopButton.Enabled := False; |
| 83 |
if FAbort then |
| 84 |
Exit; |
| 85 |
if Item.FContent <> '' then begin |
| 86 |
TabURLs := TStringList.Create; |
| 87 |
try |
| 88 |
GikoDM.GetTabURLs(TabURLs); |
| 89 |
if (UpdateURL(Item.FContent)) then begin |
| 90 |
GikoForm.ReloadBBS; |
| 91 |
end; |
| 92 |
GikoDM.OpenURLs(TabURLs); |
| 93 |
finally |
| 94 |
TabURLs.Free; |
| 95 |
end; |
| 96 |
end else |
| 97 |
MessageMemo.Lines.Add('?_?E?????[?h???存?s?直???直??[' + IntToStr(Item.FResponseCode) + ']'); |
| 98 |
finally |
| 99 |
UpdateButton.Enabled := True; |
| 100 |
StopButton.Enabled := False; |
| 101 |
CloseButton.Enabled := True; |
| 102 |
EditIgnoreListsButton.Enabled := True; |
| 103 |
end; |
| 104 |
end; |
| 105 |
|
| 106 |
procedure TNewBoardDialog.StopButtonClick(Sender: TObject); |
| 107 |
begin |
| 108 |
FAbort := True; |
| 109 |
Indy.DisconnectSocket; |
| 110 |
end; |
| 111 |
|
| 112 |
procedure TNewBoardDialog.CloseButtonClick(Sender: TObject); |
| 113 |
begin |
| 114 |
Close; |
| 115 |
end; |
| 116 |
|
| 117 |
function TNewBoardDialog.BoardDownload(const URL: String): TNewBoardItem; |
| 118 |
var |
| 119 |
Stream: TMemoryStream; |
| 120 |
s: string; |
| 121 |
i: Integer; |
| 122 |
begin |
| 123 |
InitHTTPClient( Indy ); |
| 124 |
|
| 125 |
Indy.Request.UserAgent := GikoSys.GetUserAgent; |
| 126 |
Indy.Request.Referer := ''; |
| 127 |
Indy.Request.AcceptEncoding := 'gzip'; |
| 128 |
|
| 129 |
Indy.Request.CacheControl := 'no-cache'; |
| 130 |
Indy.Request.CustomHeaders.Add('Pragma: no-cache'); |
| 131 |
|
| 132 |
// s := ''; |
| 133 |
Stream := TMemoryStream.Create; |
| 134 |
try |
| 135 |
try |
| 136 |
MessageMemo.Lines.Add('?????????????巽???転?直????'); |
| 137 |
//MessageMemo.Lines.Add(GikoSys.Setting.BoardURL2ch); |
| 138 |
MessageMemo.Lines.Add(URL); |
| 139 |
MessageMemo.Lines.Add('?_?E?????[?h???J?n?直????'); |
| 140 |
IdAntiFreeze.Active := True; |
| 141 |
try |
| 142 |
Indy.Get(URL, Stream); |
| 143 |
finally |
| 144 |
IdAntiFreeze.Active := False; |
| 145 |
end; |
| 146 |
Result.FContent := GikoSys.GzipDecompress(Stream, Indy.Response.ContentEncoding); |
| 147 |
MessageMemo.Lines.Add('?_?E?????[?h???貼?邸?直???直??'); |
| 148 |
except |
| 149 |
on E: EIdConnectException do begin |
| 150 |
MessageMemo.Lines.Add(''); |
| 151 |
MessageMemo.Lines.Add('???????存?s?直???直?? ???端?但?v???L?V?AFW???坦???????????足?転????'); |
| 152 |
MessageMemo.Lines.Add('FW???端???????辿?l?????????m?F?直???足?転????'); |
| 153 |
MessageMemo.Lines.Add('NEC??PC????????PC GATE???鼎?????直?????辿???\?鼎???????長??'); |
| 154 |
MessageMemo.Lines.Add('Message: ' + E.Message); |
| 155 |
end; |
| 156 |
on E: Exception do begin |
| 157 |
if FAbort then |
| 158 |
MessageMemo.Lines.Add('?_?E?????[?h?????f?直???直??') |
| 159 |
else begin |
| 160 |
MessageMemo.Lines.Add('?_?E?????[?h???存?s?直???直??'); |
| 161 |
MessageMemo.Lines.Add('ResponseCode: ' + IntToStr(Indy.ResponseCode)); |
| 162 |
MessageMemo.Lines.Add('Message: ' + E.Message); |
| 163 |
MessageMemo.Lines.Add('------------------------'); |
| 164 |
for i := 0 to Indy.Response.RawHeaders.Count - 1 do begin |
| 165 |
s := Indy.Response.RawHeaders.Names[i]; |
| 166 |
s := s + ': ' + Indy.Response.RawHeaders.Values[s]; |
| 167 |
MessageMemo.Lines.Add(s); |
| 168 |
end; |
| 169 |
MessageMemo.Lines.Add('------------------------'); |
| 170 |
end; |
| 171 |
end; |
| 172 |
end; |
| 173 |
Result.FResponseCode := Indy.ResponseCode; |
| 174 |
finally |
| 175 |
Stream.Free; |
| 176 |
end; |
| 177 |
end; |
| 178 |
|
| 179 |
function TNewBoardDialog.UpdateURL(s: string): boolean; |
| 180 |
var |
| 181 |
i: 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('?V???A??URL???X?`?F?b?N???J?n?直????'); |
| 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 |
//?鱈???I?v?V???????I???????????辿???????N???A |
| 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 = '?即??????') or |
| 247 |
(tmp = '?????辿??') or |
| 248 |
(tmp = '???甜?a?a?r') or |
| 249 |
(tmp = '?`???b?g') or |
| 250 |
(tmp = '?即?G???鼎') or |
| 251 |
(tmp = '?^?c????') or |
| 252 |
(tmp = '?c?[???鄭') or |
| 253 |
(tmp = '?添???T?C?g') 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 := Trim(Copy(tmp, 1, i - 1)); |
| 274 |
Title := Copy(tmp, i + 1, Length(tmp)); |
| 275 |
// BBSs???坦???担 |
| 276 |
if Length(BBSs) = 0 then begin |
| 277 |
Board := nil; |
| 278 |
end else begin |
| 279 |
Board := BBSs[ 0 ].FindBoardFromTitle(Title); |
| 280 |
end; |
| 281 |
if Board = nil then begin |
| 282 |
MessageMemo.Lines.Add('?V???????u' + Title + '(' + URL + ')?v'); |
| 283 |
ini.WriteString(cate, Title, URL); |
| 284 |
Change := True; |
| 285 |
end else begin |
| 286 |
if Board.URL <> URL then begin |
| 287 |
MessageMemo.Lines.Add('URL???X?u' + Board.Title + '(' + URL +')?v'); |
| 288 |
ini.WriteString(cate, Title, URL); |
| 289 |
oldURLs.Add(Board.URL); |
| 290 |
newURLs.Add(URL); |
| 291 |
Change := True; |
| 292 |
end else begin |
| 293 |
ini.WriteString(cate, Title, URL); |
| 294 |
end; |
| 295 |
end; |
| 296 |
end else begin |
| 297 |
s := Copy(s, idx2 + 2, Length(s)); |
| 298 |
Continue; |
| 299 |
end; |
| 300 |
s := Copy(s, idx + 5, Length(s)); |
| 301 |
end; |
| 302 |
end; |
| 303 |
end; |
| 304 |
finally |
| 305 |
if Change then |
| 306 |
ini.UpdateFile; |
| 307 |
ini.Free; |
| 308 |
end; |
| 309 |
MessageMemo.Lines.Add(''); |
| 310 |
if Change then begin |
| 311 |
GikoForm.FavoritesURLReplace(oldURLs, newURLs); |
| 312 |
GikoForm.RoundListURLReplace(oldURLs, newURLs); |
| 313 |
GikoForm.TabFileURLReplace(oldURLs, newURLs); |
| 314 |
MessageMemo.Lines.Add('?V???A??URL???X?`?F?b?N???貼?邸?直???直??'); |
| 315 |
MessageMemo.Lines.Add('?u?????辿?v?{?^???????直???足?転????'); |
| 316 |
end else |
| 317 |
MessageMemo.Lines.Add('?V???A??URL???X?? ???????邸???長?直??'); |
| 318 |
finally |
| 319 |
oldURLs.Free; |
| 320 |
newURLs.Free; |
| 321 |
end; |
| 322 |
Result := Change; |
| 323 |
end; |
| 324 |
|
| 325 |
procedure TNewBoardDialog.FormCreate(Sender: TObject); |
| 326 |
begin |
| 327 |
StopButton.Enabled := False; |
| 328 |
BoardURLComboBox.Clear; |
| 329 |
BoardURLComboBox.Items.AddStrings(GikoSys.Setting.BoardURLs); |
| 330 |
try |
| 331 |
BoardURLComboBox.ItemIndex := GikoSys.Setting.BoardURLSelected - 1; |
| 332 |
except |
| 333 |
BoardURLComboBox.ItemIndex := 0; |
| 334 |
end; |
| 335 |
SetIgnoreCategory(false); |
| 336 |
end; |
| 337 |
//???X?V?????O?J?e?S?????X?g???o?^ |
| 338 |
{['?即??????', '?????辿??', '???甜?a?a?r', '?`???b?g', '?即?G???鼎', '?^?c????', '?c?[???鄭', '?添???T?C?g']} |
| 339 |
procedure TNewBoardDialog.SetIgnoreCategory(b: boolean); |
| 340 |
begin |
| 341 |
IgnoreLists := TStringList.Create; |
| 342 |
if not( FileExists(GikoSys.Setting.GetIgnoreFileName) ) or ( b )then begin |
| 343 |
IgnoreLists.Add('?即??????'); |
| 344 |
IgnoreLists.Add('?????辿??'); |
| 345 |
IgnoreLists.Add('???甜?a?a?r'); |
| 346 |
IgnoreLists.Add('?`???b?g'); |
| 347 |
IgnoreLists.Add('?即?G???鼎'); |
| 348 |
IgnoreLists.Add('?^?c????'); |
| 349 |
IgnoreLists.Add('?c?[???鄭'); |
| 350 |
IgnoreLists.Add('?添???T?C?g'); |
| 351 |
end else begin |
| 352 |
try |
| 353 |
IgnoreLists.LoadFromFile(GikoSys.Setting.GetIgnoreFileName); |
| 354 |
except |
| 355 |
IgnoreLists.Free; |
| 356 |
SetIgnoreCategory(true); |
| 357 |
end; |
| 358 |
end; |
| 359 |
end; |
| 360 |
|
| 361 |
procedure TNewBoardDialog.EditIgnoreListsButtonClick(Sender: TObject); |
| 362 |
begin |
| 363 |
EditIgnoreList(Sender); |
| 364 |
EditIgnoreListsButton.OnClick := UpdateIgnoreList; |
| 365 |
end; |
| 366 |
procedure TNewBoardDialog.EditIgnoreList(Sender: TObject); |
| 367 |
var |
| 368 |
i: Integer; |
| 369 |
begin |
| 370 |
EditIgnoreListsButton.Caption := '???O?J?e?S???[?X?V'; |
| 371 |
Label2.Caption := '?e?P?s???J?e?S???添???L?端?直???足?転?????B?i?端?s??Ctrl+Enter?j'; |
| 372 |
UpdateButton.Enabled := false; |
| 373 |
//MessageMemo.ReadOnly := false; |
| 374 |
MessageMemo.Clear; |
| 375 |
for i := 0 to IgnoreLists.Count - 1 do |
| 376 |
MessageMemo.Lines.Add(IgnoreLists[i]); |
| 377 |
end; |
| 378 |
procedure TNewBoardDialog.UpdateIgnoreList(Sender: TObject); |
| 379 |
var |
| 380 |
i: Integer; |
| 381 |
begin |
| 382 |
Label2.Caption := ''; |
| 383 |
UpdateButton.Enabled := true; |
| 384 |
EditIgnoreListsButton.Caption := '???O?J?e?S???[???W'; |
| 385 |
IgnoreLists.Clear; |
| 386 |
for i := 0 to MessageMemo.Lines.Count - 1 do |
| 387 |
IgnoreLists.Add(MessageMemo.Lines[i]); |
| 388 |
IgnoreLists.SaveToFile(GikoSys.Setting.GetIgnoreFileName); |
| 389 |
IgnoreLists.Free; |
| 390 |
SetIgnoreCategory(false); |
| 391 |
//MessageMemo.ReadOnly := true; |
| 392 |
MessageMemo.Clear; |
| 393 |
EditIgnoreListsButton.OnClick := EditIgnoreListsButtonClick; |
| 394 |
end; |
| 395 |
|
| 396 |
procedure TNewBoardDialog.FormClose(Sender: TObject; |
| 397 |
var Action: TCloseAction); |
| 398 |
begin |
| 399 |
IgnoreLists.Free; |
| 400 |
end; |
| 401 |
//! ???[?J???t?@?C???????[?h???辿 |
| 402 |
function TNewBoardDialog.BoardLoardFromFile(const FilePath: String): String; |
| 403 |
var |
| 404 |
html : TStringList; |
| 405 |
begin |
| 406 |
Result := ''; |
| 407 |
// ?t?@?C?????????直?????辿???`?F?b?N |
| 408 |
if (FileExists(FilePath)) then begin |
| 409 |
html := TStringList.Create(); |
| 410 |
try |
| 411 |
html.LoadFromFile(FilePath); |
| 412 |
Result := html.Text; |
| 413 |
finally |
| 414 |
html.Free; |
| 415 |
end; |
| 416 |
end; |
| 417 |
end; |
| 418 |
|
| 419 |
class procedure TNewBoardDialog.InitHTTPClient(client : TIdHTTP); |
| 420 |
begin |
| 421 |
client.Request.Clear; |
| 422 |
client.Request.CustomHeaders.Clear; |
| 423 |
client.RecvBufferSize := Gikosys.Setting.RecvBufferSize; |
| 424 |
client.ProxyParams.BasicAuthentication := False; |
| 425 |
if GikoSys.Setting.ReadProxy then begin |
| 426 |
if GikoSys.Setting.ProxyProtocol then |
| 427 |
client.ProtocolVersion := pv1_1 |
| 428 |
else |
| 429 |
client.ProtocolVersion := pv1_0; |
| 430 |
client.ProxyParams.ProxyServer := GikoSys.Setting.ReadProxyAddress; |
| 431 |
client.ProxyParams.ProxyPort := GikoSys.Setting.ReadProxyPort; |
| 432 |
client.ProxyParams.ProxyUsername := GikoSys.Setting.ReadProxyUserID; |
| 433 |
client.ProxyParams.ProxyPassword := GikoSys.Setting.ReadProxyPassword; |
| 434 |
if GikoSys.Setting.ReadProxyUserID <> '' then |
| 435 |
client.ProxyParams.BasicAuthentication := True; |
| 436 |
end else begin |
| 437 |
if GikoSys.Setting.Protocol then |
| 438 |
client.ProtocolVersion := pv1_1 |
| 439 |
else |
| 440 |
client.ProtocolVersion := pv1_0; |
| 441 |
client.ProxyParams.ProxyServer := ''; |
| 442 |
client.ProxyParams.ProxyPort := 80; |
| 443 |
client.ProxyParams.ProxyUsername := ''; |
| 444 |
client.ProxyParams.ProxyPassword := ''; |
| 445 |
end; |
| 446 |
end; |
| 447 |
end. |