『スタパライフ』エージェント Ver1.10 stplBrowser.pas





   0:unit qBrowser;
   1:
   2:interface
   3:
   4:uses
   5:  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   6:  Compo, ExtCtrls,StdCtrls,Buttons,UIWebBrowser;
   7:
   8:type
   9:  TstplBrowser = class(TForm2)
  10:    TitleView1: TAWTitleView;
  11:    Splitter1: TSplitter;
  12:    HyperText1: TAWHyperText;
  13:    Panel1: TPanel;
  14:    ComBtn: TBitBtn;
  15:    ResBtn: TBitBtn;
  16:    GoBtn: TBitBtn;
  17:    UIWebBrowser1: TUIWebBrowser;
  18:    Unread1: TEdit;
  19:    Label4: TLabel;
  20:    procedure ComBtnClick(Sender: TObject);
  21:    procedure RefreshPlusButtonClick(Sender: TObject);
  22:    procedure ResBtnClick(Sender: TObject);
  23:    procedure FormCreate(Sender: TObject);
  24:    procedure TitleView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
  25:    procedure GoBtnClick(Sender: TObject);
  26:    procedure UIWebBrowser1BeforeNavigate2(Sender: TObject; const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool);
  27:    procedure UIWebBrowser1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
  28:  private
  29:    FFileName:string;
  30:    FCaption:string;
  31:    FPostPrefix:string;
  32:    FHomeURL:string;
  33:    FPostURL: string;
  34:    FComPadName:string;
  35:    FComPadArg:string;
  36:    FResPadName:string;
  37:    FResPadArg:string;
  38:    FGetName:string;
  39:    FGetArg:string;
  40:    FOptData:string;
  41:    bNavigateComplete:string;
  42:    bWaitNavigate:string;
  43:    function ExcludeReSubject(const Subject:string; var Num:Integer):string;
  44:    function MakeReSubject(const Subject:string):string;
  45:    function GetNewFileName: string;
  46:    function GenerateComFile: string;
  47:    function GenerateResFile(const Item:TTitleItem): string;
  48:  public
  49:    procedure Load(AFileName: string);
  50:  end;
  51:
  52:var
  53:  stplBrowser: TstplBrowser;
  54:
  55:implementation
  56:
  57:{$R *.DFM}
  58:
  59:{
  60: usage:  unibrowser [-C compadprog arg]  [-R repadprog arg] [-G getprog arg] [-H homeurl] [-U posturl] [-P prefix] [-D optdata] [-N] mboxfile
  61:}
  62:
  63:procedure TstplBrowser.FormCreate(Sender: TObject);
  64:var
  65:  icofile:string;
  66:  htmfile:string;
  67:  i:Integer;
  68:  s:string;
  69:begin
  70:  RegisterTitleView(TitleView1);
  71:  icofile := ChangeFileExt(Params.Items[0], '.ico');
  72:  if FileExists(icofile) then
  73:     Icon.LoadFromFile(icofile);
  74:  i := 1;
  75:  FComPadName := '';
  76:  FComPadArg := '';
  77:  FResPadName := '';
  78:  FResPadArg := '';
  79:  FCaption := '';
  80:  FHomeUrl := '';
  81:  FPostUrl := '';
  82:  FFileName := '';
  83:  FPostPrefix := 'post';
  84:  FOptData := '';
  85:  while i < Params.Count do
  86:  begin
  87:    s := Params.Items[i];
  88:    if s = '-C' then
  89:    begin
  90:      i := i + 1;
  91:      FComPadName := Params.Items[i];
  92:      i := i + 1;
  93:      FComPadArg := Params.Items[i];
  94:    end
  95:    else if s = '-R' then
  96:    begin
  97:      i := i + 1;
  98:      FResPadName := Params.Items[i];
  99:      i := i + 1;
 100:      FResPadArg := Params.Items[i];
 101:    end
 102:    else if s = '-G' then
 103:    begin
 104:      i := i + 1;
 105:      FGetName := Params.Items[i];
 106:      i := i + 1;
 107:      FGetArg := Params.Items[i];
 108:    end
 109:    else if s = '-H' then
 110:    begin
 111:      i := i + 1;
 112:      FHomeUrl := Params.Items[i];
 113:    end
 114:    else if s = '-U' then
 115:    begin
 116:      i := i + 1;
 117:      FPostUrl := Params.Items[i];
 118:    end
 119:    else if s = '-P' then
 120:    begin
 121:      i := i + 1;
 122:      FPostPrefix := Params.Items[i];
 123:    end
 124:    else if s = '-D' then
 125:    begin
 126:      i := i + 1;
 127:      FOptData := Params.Items[i];
 128:    end
 129:    else if s = '-T' then
 130:    begin
 131:      i := i + 1;
 132:      FCaption := Params.Items[i];
 133:    end
 134:    else if s = '-N' then // N means NIF
 135:    begin
 136:      HyperText1.SuppressHeaders := False;
 137:    end
 138:    else
 139:    begin
 140:      FFileName := Params.Items[i];
 141:    end;
 142:    i := i + 1;
 143:  end;
 144:  ComBtn.Enabled := FComPadName <> '';
 145:  ResBtn.Enabled := FResPadName <> '';
 146:  GoBtn.Enabled := FHomeUrl <> '';
 147:
 148:// for HTML View   by quwaji
 149:  bNavigateComplete := '';
 150:  bWaitNavigate := '';
 151://  htmfile := DataDir + '\' + ChangeFileExt(FFileName, '.htm');
 152:  htmfile := ExeDir + '\' + 'Plugin\stpl\stpl.htm';
 153:  if FileExists(htmfile) then
 154:  begin
 155:       UIWebBrowser1.Navigate(htmfile, 0, nil, nil, nil);
 156:  end
 157:  else
 158:  begin
 159:       UIWebBrowser1.Navigate('about:blank', 0, nil, nil, nil);
 160:  end;
 161:// end HTML View
 162:
 163:  if FFileName <> '' then
 164:    Load(FFileName)
 165:  else if FCaption <> '' then
 166:    Caption := FCaption;
 167:end;
 168:
 169:procedure TstplBrowser.Load(AFileName:string);
 170:begin
 171:  TitleView1.Url := AFileName;
 172:  if FCaption <> '' then
 173:    Caption := FCaption
 174:  else
 175:    Caption := TitleView1.Caption;
 176:end;
 177:
 178:function TstplBrowser.ExcludeReSubject(const Subject:string; var Num:Integer):string;
 179:var
 180:  i,n,c:Integer;
 181:begin
 182:  if StrLIComp(Subject, 'RE', 2) = 0 then
 183:  begin
 184:    if Subject[3] = '^' then
 185:    begin
 186:      n := 0;
 187:      for i := 4 to Length(Subject) do
 188:      begin
 189:        case Subject[i] of
 190:        '0': c := 0;
 191:        '1': c := 1;
 192:        '2': c := 2;
 193:        '3': c := 3;
 194:        '4': c := 4;
 195:        '5': c := 5;
 196:        '6': c := 6;
 197:        '7': c := 7;
 198:        '8': c := 8;
 199:        '9': c := 9;
 200:        else
 201:             c := -1;
 202:        end;
 203://        c := Ord(Subject[i]) - Ord('0');
 204:        if (0 <= c) and (c <= 9) then
 205:          n := n*10 + c
 206:        else
 207:        begin
 208:          Result := ExcludeReSubject(Copy(Subject, i + 1, Length(Subject) - i), Num);
 209:          Num := Num + n;
 210:          break;
 211:        end;
 212:      end;
 213:    end
 214:    else if Subject[3] = ':' then
 215:    begin
 216:      Result := ExcludeReSubject(Copy(Subject, 4, Length(Subject) - 3), Num);
 217:      Num := Num + 1;
 218:    end
 219:    else
 220:    begin
 221:      Result := Subject;
 222:    end;
 223:  end
 224:  else
 225:  begin
 226:    Result := Subject;
 227:  end;
 228:end;
 229:
 230:function TstplBrowser.MakeReSubject(const Subject:string):string;
 231:var
 232:  n:Integer;
 233:  s:string;
 234:begin
 235:  n := 0;
 236:  s := ExcludeReSubject(Subject, n);
 237:  if n = 0 then
 238:    Result := 'Re:' + s
 239:  else
 240:    Result := Format('Re^%d:%s',[n+1,s]);
 241:end;
 242:
 243:
 244:function TstplBrowser.GetNewFileName: string;
 245:var
 246:  Num: Integer;
 247:  Tmp: string;
 248:begin
 249:  if not DirectoryExists(PostDir) then CreateDir(PostDir);
 250:  Result:='';
 251:  for Num:=1 to 9999 do begin
 252:    Tmp:=Format('%s\%s%.04d.mbo',[PostDir,FPostPrefix,Num]);
 253:    if not FileExists(Tmp) then begin
 254:      Result:=Tmp;
 255:      Break;
 256:    end;
 257:  end;
 258:end;
 259:
 260:function TstplBrowser.GenerateComFile: string;
 261:var
 262:  TmpLines: TStringList;
 263:begin
 264:  Result:=GetNewFilename;
 265:  if Length(Result)>0 then begin
 266:    TmpLines:=TStringList.Create;
 267:    if FOptData <> '' then
 268:      TmpLines.Add('X-Optional-Data: ' + FOptData);
 269:    if FPostURL <> '' then
 270:       TmpLines.Add('X-Post-URL: '+FPostURL);
 271:    TmpLines.Add('X-Logfile: ' + FFileName);
 272:    TmpLines.Add('');
 273:    // 保存
 274:    TmpLines.SaveToFile(Result);
 275:    TmpLines.Free;
 276:  end;
 277:end;
 278:
 279:function TstplBrowser.GenerateResFile(const Item:TTitleItem): string;
 280:var
 281:  TmpLines: TStringList;
 282:  Idx: Integer;
 283:  Line:string;
 284:begin
 285:  Result:=GetNewFilename;
 286:  if Length(Result)>0 then begin
 287:    TmpLines:=TStringList.Create;
 288:    TmpLines.Text:=Item.Text;
 289:    // 元のヘッダを削除する(mbox & NIFTY 用)
 290:    while TmpLines.Count>0 do begin
 291:      Line := TmpLines.Strings[0];
 292:      if Length(Line)=0 then begin
 293:        TmpLines.Delete(0);
 294:        Break;
 295:      end;
 296:      TmpLines.Delete(0);
 297:    end;
 298:    // 本文だけになったところで引用符をがーッとつける
 299:    for Idx:=TmpLines.Count-1 downto 0 do
 300:      TmpLines.Strings[Idx]:='>'+TmpLines.Strings[Idx];
 301:    // 新しいヘッダを先頭に差し込む
 302:    Idx := 0;
 303:    TmpLines.Insert(Idx,'Subject: ' + MakeReSubject(Item.Subject));
 304:    Idx := Idx + 1;
 305:    if Item.Number > 0 then
 306:    begin
 307:      TmpLines.Insert(Idx,'X-Refer-To: '+IntToStr(Item.Number));
 308:      Idx := Idx + 1;
 309:    end;
 310:    if Item.MessageID <> '' then
 311:    begin
 312:      TmpLines.Insert(Idx,'References: ' + Item.MessageID);
 313:      Idx := Idx + 1;
 314:    end;
 315:    if FOptData <> '' then
 316:    begin
 317:      TmpLines.Insert(Idx,'X-Optional-Data: ' + FOptData);
 318:      Idx := Idx + 1;
 319:    end;
 320:    if FPostURL <> '' then
 321:    begin
 322:       TmpLines.Insert(Idx,'X-Post-URL: ' + FPostURL);
 323:       Idx := Idx + 1;
 324:    end;
 325:    TmpLines.Insert(Idx, 'X-Logfile: ' + FFileName);
 326:    Idx := Idx + 1;
 327:    TmpLines.Insert(Idx, '');
 328:    // Item.Number
 329:    // Item.MessageID
 330:    // Item.Subject
 331:    // Item.SenderAddress
 332:    // Item.SenderName
 333:    // Item.Date  -> FormatDateTime('yyyy/mm/dd HH:MM:SS', Item.Date)
 334:{
 335:    Idx := Idx + 1;
 336:    TmpLines.Insert(Idx, Format('%s wrote at %s', [Item.SenderName, FormatDateTime('yyyy/mm/dd HH:MM:SS', Item.Date)]));
 337:}
 338:    // 保存
 339:    TmpLines.SaveToFile(Result);
 340:    TmpLines.Free;
 341:  end;
 342:end;
 343:
 344:procedure TstplBrowser.ComBtnClick(Sender: TObject);
 345:begin
 346:  SpawnL(P_NOWAIT, FComPadName, FComPadArg + ' "' + GenerateComFile + '"');
 347:end;
 348:
 349:procedure TstplBrowser.ResBtnClick(Sender: TObject);
 350:var
 351:  Item: TAWTitleItem;
 352:begin
 353:  Item:=TitleView1.Selected;
 354:  if Assigned(Item) then
 355:    SpawnL(P_NOWAIT, FResPadName, FResPadArg + ' "' + GenerateResFile(Item) + '"')
 356:  else
 357:    SpawnL(P_NOWAIT, FComPadName, FComPadArg + ' "' + GenerateComFile + '"');
 358:end;
 359:
 360:procedure TstplBrowser.RefreshPlusButtonClick(Sender: TObject);
 361:var
 362:  Tmp: Integer;
 363:begin
 364:  SpawnL(P_WAIT,FGetName, FGetArg);
 365:  Tmp:=TitleView1.ItemIndex;
 366:  TitleView1.ItemIndex:=-1;
 367:  TitleView1.Url:='';
 368:  TitleView1.Url:=FFileName;
 369:  TitleView1.ItemIndex:=Tmp;
 370:end;
 371:
 372:procedure TstplBrowser.TitleView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
 373:var
 374:   Item:TTitleItem;
 375:   TmpLines: TStringList;
 376:   Line:string;
 377:// modify-start shitara ▼▼▼イメージ表示を追加▼▼▼
 378:   ImageFile:String;
 379:// modify-end   shitara ▲▲▲イメージ表示を追加▲▲▲
 380:begin
 381:// modify-start shitara ▼▼▼イメージ表示を追加▼▼▼
 382:   ImageFile:='';
 383:// modify-end   shitara ▲▲▲イメージ表示を追加▲▲▲
 384:     Item := TitleView1.Selected;
 385:     if Assigned(Item) then
 386:     begin
 387:// for HTML View   by quwaji
 388:        if bNavigateComplete <> '' then
 389:        begin
 390:            TmpLines:=TStringList.Create;
 391:            TmpLines.Text:=Item.Text;
 392:            // 元のヘッダを削除する(mbox & NIFTY 用)
 393:            while TmpLines.Count>0 do begin
 394:              Line := TmpLines.Strings[0];
 395:// modify-start shitara ▼▼▼イメージ表示を追加▼▼▼
 396:              if StrLIComp(Line,'X-ImageFile: ',13)=0 then begin
 397:                ImageFile:=Copy(Line,14,Length(Line)-12);
 398:              end;
 399:// modify-end   shitara ▲▲▲イメージ表示を追加▲▲▲
 400:              if Length(Line)=0 then begin
 401:                TmpLines.Delete(0);
 402:                Break;
 403:              end;
 404:              TmpLines.Delete(0);
 405:            end;
 406:             UIWebBrowser1.document.all('SUBJ').innerHTML := Item.Subject;
 407:             UIWebBrowser1.document.all('POSTDATE').innerHTML := FormatDateTime('yyyy/mm/dd HH:MM', Item.Date);
 408:             UIWebBrowser1.document.all('SENDER').innerHTML := Item.SenderName;
 409:// modify-start shitara ▼▼▼テキスト表示をプレーンテキストに変更・イメージ表示を追加▼▼▼
 410://             UIWebBrowser1.document.all('MSGBODY').innerHTML := TmpLines.Text;
 411:             if Length(ImageFile)>0 then begin
 412:               UIWebBrowser1.document.all('MSGBODY').innerHTML := '<img src="' + DataDir + '\stpl\' + ImageFile + '"><br><pre>'+TmpLines.Text+'</pre>';
 413:             end else begin
 414:               UIWebBrowser1.document.all('MSGBODY').innerHTML := '<pre>'+TmpLines.Text+'</pre>';
 415:             end;
 416:// modify-end   shitara ▲▲▲テキスト表示をプレーンテキストに変更・イメージ表示を追加▲▲▲
 417:             bWaitNavigate := '';
 418:            TmpLines.Free;
 419:        end
 420:        else
 421:        begin
 422:             bWaitNavigate := 'たのむよ';
 423:        end;
 424:// end HTML View
 425:     end;
 426:     Unread1.Text := Format('%d/%d', [TitleView1.UnreadCount, TitleView1.Items.Count]);
 427:
 428:end;
 429:
 430:procedure TstplBrowser.GoBtnClick(Sender: TObject);
 431:begin
 432:     OpenBrowser(FHomeUrl);
 433:end;
 434:procedure TstplBrowser.UIWebBrowser1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
 435:var
 436:   Item:TTitleItem;
 437:// modify-start shitara ▼▼▼初期表示メッセージのテキスト部編集・イメージ表示を追加▼▼▼
 438:   TmpLines: TStringList;
 439:   Line:String;
 440:   ImageFile:String;
 441:// modify-end   shitara ▲▲▲初期表示メッセージのテキスト部編集・イメージ表示を追加▲▲▲
 442:begin
 443:// modify-start shitara ▼▼▼イメージ表示を追加▼▼▼
 444:   ImageFile:='';
 445:// modify-end   shitara ▲▲▲イメージ表示を追加▲▲▲
 446:     bNavigateComplete := 'おっけ〜';
 447:     if bWaitNavigate <> '' then
 448:     begin
 449:          Item := TitleView1.Selected;
 450:// modify-start shitara ▼▼▼初期表示メッセージのテキスト部編集▼▼▼
 451:          TmpLines:=TStringList.Create;
 452:          TmpLines.Text:=Item.Text;
 453:          // 元のヘッダを削除する(mbox & NIFTY 用)
 454:          while TmpLines.Count>0 do begin
 455:            Line := TmpLines.Strings[0];
 456:            if StrLIComp(Line,'X-ImageFile: ',13)=0 then begin
 457:              ImageFile:=Copy(Line,14,Length(Line)-12);
 458:            end;
 459:            if Length(Line)=0 then begin
 460:              TmpLines.Delete(0);
 461:              Break;
 462:            end;
 463:            TmpLines.Delete(0);
 464:          end;
 465:// modify-end   shitara ▲▲▲初期表示メッセージのテキスト部編集▲▲▲
 466:          if Assigned(Item) then
 467:          begin
 468:             UIWebBrowser1.document.all('SUBJ').innerHTML := Item.Subject;
 469:             UIWebBrowser1.document.all('POSTDATE').innerHTML := FormatDateTime('yyyy/mm/dd HH:MM', Item.Date);
 470:             UIWebBrowser1.document.all('SENDER').innerHTML := Item.SenderName;
 471:// modify-start shitara ▼▼▼テキスト表示をプレーンテキストに変更・イメージ表示を追加▼▼▼
 472://             UIWebBrowser1.document.all('MSGBODY').innerHTML := TmpLines.Text;
 473:             if Length(ImageFile)>0 then begin
 474:               UIWebBrowser1.document.all('MSGBODY').innerHTML := '<img src="' + DataDir + '\stpl\' + ImageFile + '"><br><pre>'+TmpLines.Text+'</pre>';
 475:             end else begin
 476:               UIWebBrowser1.document.all('MSGBODY').innerHTML := '<pre>'+TmpLines.Text+'</pre>';
 477:             end;
 478:// modify-end   shitara ▲▲▲テキスト表示をプレーンテキストに変更・イメージ表示を追加▲▲▲
 479:          end;
 480:          bWaitNavigate := '';
 481:     end;
 482:
 483:end;