summaryrefslogtreecommitdiff
path: root/plugins/HistoryPlusPlus/PassForm.pas
blob: 6b923b3b686466ebf891b6e64b147e8aea6ff450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
(*
    History++ plugin for Miranda IM: the free IM client for Microsoft* Windows*

    Copyright (C) 2006-2009 theMIROn, 2003-2006 Art Fedorov.
    History+ parts (C) 2001 Christian Kastner

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*)

unit PassForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, Menus, ExtCtrls, StdCtrls,
  m_api;

type
  TfmPass = class(TForm)
    Image1: TImage;
    rbProtAll: TRadioButton;
    rbProtSel: TRadioButton;
    lvCList: TListView;
    bnPass: TButton;
    laPassState: TLabel;
    Bevel1: TBevel;
    bnCancel: TButton;
    bnOK: TButton;
    PopupMenu1: TPopupMenu;
    Refresh1: TMenuItem;
    Label1: TLabel;
    procedure bnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure rbProtSelClick(Sender: TObject);
    procedure bnPassClick(Sender: TObject);
    procedure bnOKClick(Sender: TObject);
    procedure Refresh1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    PassMode: Byte;
    Password: AnsiString;
    FLastContact: THandle;
    procedure FillList;
    procedure UpdatePassword;
    procedure SetlastContact(const Value: THandle);
    procedure TranslateForm;
  public
    property LastContact: THandle read FLastContact write SetLastContact;
    { Public declarations }
  end;

var
  fmPass: TfmPass;

const
  PASSMODE_PROTNONE   = 0; // no protection, not used
  PASSMODE_PROTALL    = 1; // protect all contacts
  PASSMODE_PROTSEL    = 2; // protect ONLY selected contacts
  PASSMODE_PROTNOTSEL = 3; // protect ALL, except selected contacts (not used)

function ReadPassModeFromDB: Byte;
function GetPassMode: Byte;
function GetPassword: AnsiString;
function IsPasswordBlank(const Password: AnsiString): Boolean;
function IsUserProtected(hContact: THandle): Boolean;
function CheckPassword(const Pass: AnsiString): Boolean;

procedure RunPassForm;

implementation

uses
  PassNewForm, hpp_options, hpp_services, PassCheckForm,
  Checksum, hpp_global, hpp_contacts, hpp_database, hpp_forms;

{$R *.DFM}

procedure RunPassForm;
begin
  if Assigned(PassFm) then
  begin
    PassFm.Show;
    exit;
  end;
  if Assigned(PassCheckFm) then
  begin
    PassCheckFm.Show;
    exit;
  end;
  if IsPasswordBlank(GetPassword) then
  begin
    if not Assigned(PassFm) then
    begin
      PassFm := TfmPass.Create(nil);
    end;
    PassFm.Show;
  end
  else
  begin
    PassCheckFm := TfmPassCheck.Create(nil);
    PassCheckFm.Show;
  end;
end;

function CheckPassword(const Pass: AnsiString): Boolean;
begin
  Result := (DigToBase(HashString(Pass)) = GetPassword);
end;

function IsUserProtected(hContact: THandle): Boolean;
begin
  Result := False;
  case GetPassMode of
    PASSMODE_PROTNONE:   Result := False;
    PASSMODE_PROTALL:    Result := True;
    PASSMODE_PROTSEL:    Result := (DBGetContactSettingByte(hContact, hppDBName, 'PasswordProtect', 0) = 1);
    PASSMODE_PROTNOTSEL: Result := (DBGetContactSettingByte(hContact, hppDBName, 'PasswordProtect', 1) = 1);
  end;
  if IsPasswordBlank(GetPassword) then;
end;

function IsPasswordBlank(const Password: AnsiString): Boolean;
begin
  Result := (Password = DigToBase(HashString('')));
end;

function GetPassword: AnsiString;
begin
  Result := GetDBStr(hppDBName,'Password',DigToBase(HashString('')));
end;

function ReadPassModeFromDB: Byte;
begin
  Result := GetDBByte(hppDBName,'PasswordMode',PASSMODE_PROTALL);
end;

function GetPassMode: Byte;
begin
  Result := ReadPassModeFromDB;
  if IsPasswordBlank(GetPassword) then
    Result := PASSMODE_PROTNONE;
end;

procedure TfmPass.bnCancelClick(Sender: TObject);
begin
  close;
end;

procedure AddContact(var lvCList:TListView; Contact: THandle);
var
  li: TListItem;
  Capt: String;
begin
  li := lvCList.Items.Add;
  if Contact = 0 then
  begin
    Capt := TranslateW('System History') + ' (' + GetContactDisplayName(Contact, 'ICQ') + ')';
  end
  else
    Capt := GetContactDisplayName(Contact);
  li.Caption := Capt;
  li.Data := Pointer(Contact);
  li.Checked := DBGetContactSettingByte(Contact, hppDBName, 'PasswordProtect', 0) = 1;
end;

procedure TfmPass.FillList;
var
  hCont: THandle;
begin
  lvCList.Items.BeginUpdate;
  try
    lvCList.Items.Clear;
    hCont := db_find_first();
    while hCont <> 0 do
    begin
      AddContact(lvCList,hCont);
      hCont := db_find_next(hCont);
    end;
    AddContact(lvCList,0);
    lvCList.SortType := stNone;
    lvCList.SortType := stText;
  finally
    lvCList.Items.EndUpdate;
  end;
end;

procedure TfmPass.FormCreate(Sender: TObject);
begin
  DesktopFont := True;
  MakeFontsParent(Self);
  TranslateForm;
  FillList;
  PassMode := ReadPassModeFromDB;
  if not(PassMode in [PASSMODE_PROTALL, PASSMODE_PROTSEL]) then
    PassMode := PASSMODE_PROTALL;
  Password := GetPassword;

  if PassMode = PASSMODE_PROTSEL then
    rbProtSel.Checked := True
  else
    rbProtAll.Checked := True;
  rbProtSelClick(Self);
  UpdatePassword;
  Image1.Picture.Icon.Handle := CopyIcon(hppIntIcons[0].Handle);
end;

procedure TfmPass.rbProtSelClick(Sender: TObject);
begin
  if rbProtSel.Checked then
    PassMode := PASSMODE_PROTSEL
  else if rbProtAll.Checked then
    PassMode := PASSMODE_PROTALL;

  if rbProtSel.Checked then
  begin
    lvCList.Enabled := True;
    lvCList.Color   := clWindow;
  end
  else
  begin
    lvCList.Enabled := False;
    lvCList.Color   := clInactiveBorder;
  end;
end;

procedure TfmPass.bnPassClick(Sender: TObject);
begin
  with TfmPassNew.Create(Self) do
  begin
    if ShowModal = mrOK then
    begin
      Password := DigToBase(HashString(AnsiString(edPass.Text)));
      UpdatePassword;
    end;
    Free;
  end;
end;

procedure TfmPass.UpdatePassword;
begin
  if Password = DigToBase(HashString('')) then
  begin
    // password not set
    laPassState.Font.Style := laPassState.Font.Style + [fsBold];
    laPassState.Caption    := TranslateW('Password not set');
  end
  else
  begin
    // password set
    laPassState.ParentFont := True;
    laPassState.Caption    := TranslateW('Password set');
  end;
end;

procedure TfmPass.bnOKClick(Sender: TObject);
var
  i: Integer;
  li: TListItem;
begin
  WriteDBByte(hppDBName,'PasswordMode',PassMode);
  WriteDBStr(hppDBName, 'Password', Password);
  if PassMode = PASSMODE_PROTSEL then
  begin
    for i := 0 to lvCList.Items.Count - 1 do
    begin
      li := lvCList.Items[i];
      if li.Checked then
        DBWriteContactSettingByte(THANDLE(li.Data), hppDBName, 'PasswordProtect', 1)
      else
        DBDeleteContactSetting(THANDLE(li.Data), hppDBName, 'PasswordProtect');
    end;
  end;

  close;
end;

procedure TfmPass.SetlastContact(const Value: THandle);
begin
  FLastContact := Value;
end;

procedure TfmPass.Refresh1Click(Sender: TObject);
begin
  FillList;
end;

procedure TfmPass.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfmPass.FormDestroy(Sender: TObject);
begin
  try
    PassFm := nil;
  except
  end;
end;

procedure TfmPass.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
  Mask: Integer;
begin
  with Sender as TWinControl do
  begin
    if Perform(CM_CHILDKEY, Key, LPARAM(Sender)) <> 0 then
      Exit;
    Mask := 0;
    case Key of
      VK_TAB:
        Mask := DLGC_WANTTAB;
      VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN:
        // added to change radio buttons from keyboard
        // however, we have to disable it when lvCList is focused
        if not lvCList.Focused then Mask := DLGC_WANTARROWS;
      VK_RETURN, VK_EXECUTE, VK_ESCAPE, VK_CANCEL:
        Mask := DLGC_WANTALLKEYS;
    end;
    if (Mask <> 0) and
       (Perform(CM_WANTSPECIALKEY, Key, 0) = 0) and
       (Perform(WM_GETDLGCODE, 0, 0) and Mask = 0) and
       (Perform(CM_DIALOGKEY, Key, 0) <> 0)  then
      exit;
  end;
end;

procedure TfmPass.TranslateForm;
begin
  Caption           := TranslateUnicodeString(Caption);
  Label1.Caption    := TranslateUnicodeString(Label1.Caption);
  rbProtAll.Caption := TranslateUnicodeString(rbProtAll.Caption);
  rbProtSel.Caption := TranslateUnicodeString(rbProtSel.Caption);
  bnPass.Caption    := TranslateUnicodeString(bnPass.Caption);
  bnOK.Caption      := TranslateUnicodeString(bnOK.Caption);
  bnCancel.Caption  := TranslateUnicodeString(bnCancel.Caption);
  Refresh1.Caption  := TranslateUnicodeString(Refresh1.Caption);
end;

end.