summaryrefslogtreecommitdiff
path: root/unit1.pas
blob: 04f0f52659eab15e15830a8a6e8c84b8ca3e2233 (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
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, popimapunit, gitUpdateUnit;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    emailsDaIcon: TImage;
    nichtsDaIcon: TImage;
    Memo1: TMemo;
    TrayIcon1: TTrayIcon;
    procedure Button1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TrayIcon1Click(Sender: TObject);
    procedure TrayIcon1DblClick(Sender: TObject);
    procedure TrayIcon1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { private declarations }
    lb: tMouseButton;
  public
    { public declarations }
    pc:             tPopImapClient;
    encryptionUser: string;
    procedure neueNachrichten(sender: tObject);
  end;

var
  Form1: TForm1;

implementation

uses myStringListUnit, lowLevelUnit, process, deencrypt;

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  sl:    tMyStringList;
  s,ga:  string;
  found: boolean;
begin
  __ausgabenMaske:=1;

  if not fileexists(extractfilepath(application.exename)+'optionen.konf') then
    raise exception.create('Kann Optionendatei ''' + extractfilepath(application.exename) + 'optionen.konf' + ''' nicht finden!');

  sl:=tMyStringList.create;
  sl.loadFromFile(extractfilepath(application.exename)+'optionen.konf');

  s:=sl.grepFirst('^aufploppen\s*=');
  erstesArgument(s,'=');
  if uppercase(s)='JA' then
    checkBox1.checked:=true
  else if uppercase(s)='NEIN' then
    checkBox1.checked:=false
  else if s<>'' then
    raise exception.create(''''+s+''' ist kein gültiger Wert für ''aufploppen'' (ja|nein)!');

  s:=sl.grepFirst('^Ton\s+geben\s*=');
  erstesArgument(s,'=');
  if uppercase(s)='JA' then
    checkBox2.checked:=true
  else if uppercase(s)='NEIN' then
    checkBox2.checked:=false
  else if s<>'' then
    raise exception.create(''''+s+''' ist kein gültiger Wert für ''Ton geben'' (ja|nein)!');

  s:=sl.grepFirst('^Encryption-User\s*=');
  if erstesArgument(s,'=')<>'' then
    encryptionUser:=s
  else
    raise exception.create('Kein Encryption-User in '''+extractfilepath(application.exename)+'optionen.konf'' angegeben!');

  pc:=tPopImapClient.create;
  s:=sl.grepFirst('^Timeout\s*=');
  if erstesArgument(s,'=')<>'' then
    pc.timeout:=strToFloat(s);
  pc.neueNachrichten:=@neueNachrichten;
  s:=sl.grepFirst('^ssh-User\s*=');
  if erstesArgument(s,'=')<>'' then
    pc.sshUser:=s;
  s:=sl.grepFirst('^Host\s*=');
  if erstesArgument(s,'=')<>'' then
    pc.host:=s
  else
    raise exception.create('Kein Host in '''+extractfilepath(application.exename)+'optionen.konf'' angegeben!');
  s:=sl.grepFirst('^User\s*=');
  if erstesArgument(s,'=')<>'' then
    pc.user:=s
  else
    raise exception.create('Kein User in '''+extractfilepath(application.exename)+'optionen.konf'' angegeben!');
  s:=sl.grepFirst('^Pass\s*=');
  if erstesArgument(s,'=')<>'' then
    pc.pass:=decrypt(s)
  else
    raise exception.create('Kein Pass in '''+extractfilepath(application.exename)+'optionen.konf'' angegeben!');
  found:=false;
  s:=sl.grepFirst('^Port\s*=');
  if erstesArgument(s,'=')<>'' then begin
    pc.port:=strToInt(s);
    found:=true;
  end;
  s:=sl.grepFirst('^Protocol\s*=');
  erstesArgument(s,'=');
  if s='pop' then begin
    pc.mailProtocol:=mpPop;
    if not found then
      pc.port:=995;
  end
  else if s='imap' then begin
    pc.mailProtocol:=mpImap;
    if not found then
      pc.port:=993;
  end
  else begin
    if not found then
      raise exception.create('Kein Port und kein Protocol in '''+extractfilepath(application.exename)+'optionen.konf'' angegeben!');
    if pc.port=993 then
      pc.mailProtocol:=mpImap
    else if pc.port=995 then
      pc.mailProtocol:=mpPop
    else
      raise exception.create('Ungültiges/unbekanntes Mail-Protokoll '''+s+'''!');
  end;

  sl.grep('^gültiger\s+Adressat\s*=');
  if sl.count>0 then begin
    ga:='';
    sl.rewind;
    while sl.readln(s) do begin
      erstesArgument(s,'=');
      ga:=ga+'|'+s;
    end;
    delete(ga,1,1);
  end;
  pc.gueltigerAdressat:='('+ga+')';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  pc.free;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  pc.threadAnhalten;
  pc.aufThreadWarten;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
var
  sl: tMyStringList;
begin
  if not assigned(pc) then
    exit;
  sl:=tMyStringList.create;

  if checkBox1.checked then
    sl.add('aufploppen = JA')
  else
    sl.add('aufploppen = NEIN');

  if checkBox2.checked then
    sl.add('Ton geben =  JA')
  else
    sl.add('Ton geben =  NEIN');

  sl.addWithLineBreaks('gültiger Adressat = '+umbrechen(copy(pc.gueltigerAdressat,2,length(pc.gueltigerAdressat)-2),'|',true,'gültiger Adressat = '));

  sl.add('Encryption-User = ' + encryptionUser);
  sl.add('Timeout = ' + floatToStr(pc.timeOut));
  if pc.sshUser<>'' then
    sl.add('ssh-User = ' + pc.sshUser);
  sl.add('Host = ' + pc.host);
  sl.add('User = ' + pc.user);
  sl.add('Pass = ' + encrypt(pc.pass, encryptionUser));
  sl.add('Port = ' + intToStr(pc.port));
  case pc.mailProtocol of
    mpPop:
      sl.add('Protocol = pop');
    mpImap:
      sl.add('Protocol = imap');
    else
      raise exception.create('unbekanntes Mail-Protokoll!');
  end{of Case};

  sl.saveToFile(extractfilepath(application.exename)+'optionen.konf');
  sl.free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  pc.loeschen;
  form1.visible:=false;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  button1.top:=form1.clientHeight-button1.height;
  checkBox1.top:=button1.top + (button1.height-checkBox1.height) div 2;
  checkBox2.top:=button1.top + (button1.height-checkBox2.height) div 2;
  button1.left:=(form1.clientWidth - button1.width - checkBox1.width - checkBox2.width) div 6;
  checkBox1.left:=button1.left+button1.width + (form1.clientWidth - button1.width - checkBox1.width - checkBox2.width) div 3;
  checkBox2.left:=checkBox1.left+checkBox1.width + (form1.clientWidth - button1.width - checkBox1.width - checkBox2.width) div 3;
  memo1.width:=form1.clientWidth-memo1.left;
  memo1.height:=button1.top-memo1.top;
end;

const
  __erstesMal: boolean = true;
  __letzterTon: extended = 0;

procedure TForm1.FormShow(Sender: TObject);
begin
  if __erstesMal then begin
    __erstesMal:=false;
    form1.visible:=false;
  end;
  if form1.visible then begin
    form1.top:=screen.monitors[screen.monitorCount-1].top  +  (screen.monitors[screen.monitorCount-1].height-form1.height-24) div 2;
    form1.left:=screen.monitors[screen.monitorCount-1].left + (screen.monitors[screen.monitorCount-1].width-form1.width) div 2;
  end;
end;

procedure TForm1.TrayIcon1Click(Sender: TObject);
begin
  form1.visible:=not form1.visible;
end;

procedure TForm1.TrayIcon1DblClick(Sender: TObject);
begin
  if (lb=mbRight) or
     ((lb=mbLeft) and
      (messageDlg('Email-Tool beenden?',mtConfirmation,[mbYes,mbNo],0)=mrYes)) then
    form1.close;
end;

procedure TForm1.TrayIcon1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  lb:=button;
end;

procedure tForm1.neueNachrichten(sender: tObject);
var
  i,ung,mitMarkeDa: longint;
  argumente:        array of string;
  ausgabe:          string;
begin
  mitMarkeDa:=0;
  memo1.lines.clear;
  ung:=0;
  for i:=pc.nAnz-1 downto 0 do
    case pc.marke(i) of
      mvGueltig: begin
        inc(mitMarkeDa);
        memo1.lines.add('! '+pc.zeit(i)+' '+pc.von(i)+': '+pc.betreff(i));
      end;
      mvUngueltig: begin
        memo1.lines.add('( '+pc.zeit(i)+' '+pc.von(i)+': '+pc.betreff(i)+' )');
        inc(ung);
      end;
      mvKeine:
        memo1.lines.add('  '+pc.zeit(i)+' '+pc.von(i)+': '+pc.betreff(i));
    end{of case};
  if pc.nAnz<=ung then begin
    trayIcon1.icon:=nichtsDaIcon.picture.icon;
    trayIcon1.hint:='keine Post';
  end
  else begin
    trayIcon1.icon:=emailsDaIcon.picture.icon;
    trayIcon1.hint:=inttostr(pc.nAnz-ung)+' neue Nachrichten';
    if mitMarkeDa>0 then begin
      trayIcon1.hint:=trayIcon1.hint+', davon '+inttostr(mitMarkeDa)+' mit Marke';
      if checkBox1.checked then
        form1.visible:=true;
      if checkBox2.checked and ((now-__letzterTon > 1/24/60/3) or (now<__letzterTon)) then begin
        __letzterTon:=now;
        setlength(argumente,1);
        argumente[0]:=extractFilePath(application.exename)+'gotmail.wav';
        ausgabe:='';
        runCommand('play',argumente,ausgabe);
        setlength(argumente,0);
      end;
    end;
  end;
  if ung>0 then
    trayIcon1.hint:=trayIcon1.hint+' (und noch '+inttostr(ung)+' mit ungültiger Marke)';
  trayIcon1.visible:=true;
  form1.icon:=trayIcon1.icon;
  form1.caption:=trayIcon1.hint;
end;

end.