-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonitorBug.pas
535 lines (450 loc) · 12.9 KB
/
MonitorBug.pas
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
unit MonitorBug;
interface
uses Forms, WorkerThreadUnit, StupidHttp, ParamUnit, SysUtils, StrUtils,
Classes, ConfigUnit, ConvertCharset;
type
TMonitorBug = class;
TPatternBug = class;
TMonitorBugStatue = ( mbsOffline, // 离线
mbsLogining, // 正在登陆
mbsNormal, // 登陆成功
mbsParamFail, // 参数设置不正确
mbsLoginFail, // 登陆失败
mbsHttpFail); // 访问服务器失败
TBugCountChangeNotify = procedure(Sender: TObject;
PatternBug: TPatternBug; OldBugCount, NewBugCount: Integer) of object;
TBugStatueChangeNotify = procedure(Sender: TObject;
OldStatue, NewStatue: TMonitorBugStatue) of object;
{ TBugUpdateState - Bug数更新的状态 }
TBugUpdateState = (busNotUpdate, busUpdateFail, busUpdateSuccess);
{ TPatternBug - 每种Bug数获取的方式 }
TPatternBug = class(TObject)
public
function GetUpdateState: TBugUpdateState; virtual; abstract;
function GetBugCount: Integer; virtual; abstract;
function GetBugDesc: string; virtual; abstract;
function GetBugList: TParamList; virtual; abstract;
function UpdateBugInfo: Boolean; virtual; abstract;
end;
{ TPatternBugList }
TPatternBugList = class(TObject)
private
FList: TList;
function GetItems(I: Integer): TPatternBug;
public
constructor Create;
destructor Destroy; override;
procedure Add(PatternBug: TPatternBug);
procedure Clear;
function GetCount: Integer;
property Items[I: Integer]: TPatternBug read GetItems; default;
property Count: Integer read GetCount;
end;
TUnclosedBug = class(TPatternBug)
private
FMB: TMonitorBug;
FCount: Integer;
FBugList: TParamList;
FBugUpdateState: TBugUpdateState;
function UpdateBugCount(Html: string; var BugCount: Integer): Boolean;
function UpdateBugList(Html: string; var List: TParamList): Boolean;
public
constructor Create(MB: TMonitorBug);
destructor Destroy; override;
function GetUpdateState: TBugUpdateState; override;
function GetBugCount: Integer; override;
function GetBugDesc: string; override;
function GetBugList: TParamList; override;
function UpdateBugInfo: Boolean; override;
end;
{ TMonitorBug }
TMonitorBug = class(TCustomWorkerThread)
private
FIsLogin: Boolean;
FLastUpdateTime: TDateTime;
FUnclosedBugCount: Integer;
FUnresolvedBugCount: Integer;
FMonitorBugStatue: TMonitorBugStatue;
Http: TStupidHttp;
FBugChangeNotify: TBugCountChangeNotify;
FBugStatueChangeNotify: TBugStatueChangeNotify;
FVersion: string;
FPatternBugList: TPatternBugList;
procedure SetStatue(Value: TMonitorBugStatue);
protected
procedure Process; override;
procedure AfterRun; override;
function Login: Boolean;
function UpdateBugInfo: Boolean;
function Logout: Boolean;
function EncodeParam(Param: string): string;
public
constructor Create;
destructor Destroy; override;
function GetIndexPageUrl: string;
property Version: string read FVersion;
property IsLogin: Boolean read FIsLogin;
property LastUpdateTime: TDateTime read FLastUpdateTime;
property Statue: TMonitorBugStatue read FMonitorBugStatue;
property PatternBugList: TPatternBugList read FPatternBugList;
property BugChangeNotify: TBugCountChangeNotify
read FBugChangeNotify write FBugChangeNotify;
property BugStatueChangeNotify: TBugStatueChangeNotify
read FBugStatueChangeNotify write FBugStatueChangeNotify;
end;
implementation
{ TMonitorBug }
constructor TMonitorBug.Create;
begin
inherited;
FVersion := 'For BugFree2.0(RTM)';
Http := TStupidHttp.Create(FVersion);
FPatternBugList := TPatternBugList.Create;
FPatternBugList.Add(TUnclosedBug.Create(Self)); // 加入需要显示的Bug
// Bug更新时间
SleepTime := 10 * 1000;
FIsLogin := False;
FUnclosedBugCount := 0;
FUnresolvedBugCount := 0;
SetStatue(mbsOffline);
BugChangeNotify := nil;
BugStatueChangeNotify := nil;
end;
destructor TMonitorBug.Destroy;
begin
FreeAndNil(Http);
FreeAndNil(FPatternBugList);
inherited;
end;
function TMonitorBug.EncodeParam(Param: string): string;
const
UnsafeChar = '@#$%^&+=[]\;,/{}|:"<>?`';
var
I, J: Integer;
function CharIsInSet(c: Char; S: string): Integer;
begin
Result := Pos(c, S);
end;
begin
for I := 1 to Length(Param) do
begin
J := CharIsInSet(Param[I], UnsafeChar);
if J <> 0 then
Result := Result + '%25' + IntToHex(Ord(Param[i]), 2)
else
Result := Result + Param[I];
end;
end;
function TMonitorBug.Login: Boolean;
const
SuccessFlag = 'index.php';
var
Params: TParamList;
Html: string;
StartIndex: Integer;
begin
Result := False;
if Config.BugFreeUrl = '' then Exit;
if Config.UserName = '' then Exit;
if Config.UserPWD = '' then Exit;
Params := TParamList.Create;
try
Params.SetParam('${post}', 'xajax=xCheckUserLogin&xajaxr=1213254554968&xajaxargs[]=%3Cxjxquery%3E%3Cq%3ETestUserName%3D${username}%26TestUserPWD%3D${password}%26Language%3DZH_CN_UTF-8%26HttpRefer%3D%3C%2Fq%3E%3C%2Fxjxquery%3E');
Params.SetParam('${username}', EncodeParam(Config.UserName));
Params.SetParam('${password}', EncodeParam(Config.UserPWD));
Html := Http.Post(Config.BugFreeUrl + '/Login.php', Params);
StartIndex := Pos(SuccessFlag, Html);
if StartIndex <= 0 then Exit;
finally
FreeAndNil(Params);
end;
Result := True;
end;
function TMonitorBug.UpdateBugInfo: Boolean;
var
I: Integer;
Bug: TPatternBug;
OldCount: Integer;
begin
for I := 0 to FPatternBugList.Count - 1 do
begin
Bug := FPatternBugList[I];
OldCount := Bug.GetBugCount;
if Bug.UpdateBugInfo then
begin
try
if (OldCount <> Bug.GetBugCount) and Assigned(BugChangeNotify) then
BugChangeNotify(Self, Bug, OldCount, Bug.GetBugCount);
except
// nothing
end;
end;
end;
Result := True;
end;
function TMonitorBug.Logout: Boolean;
begin
Result := False;
if Config.BugFreeUrl = '' then Exit;
try
//todo Http.Get(Config.BugFreeUrl + '/Logout.php?Logout=Yes');
Result := True;
except
// nothing
end;
end;
function TMonitorBug.GetIndexPageUrl: string;
begin
Result := Config.BugFreeUrl + '/' + 'index.php';
end;
procedure TMonitorBug.Process;
begin
case FMonitorBugStatue of
mbsOffline:
begin
SetStatue(mbsLogining);
Process;
end;
mbsLogining:
begin
if (Config.BugFreeUrl = '') or (Config.UserName = '') or (Config.UserPWD = '') then
begin
SetStatue(mbsParamFail);
Exit;
end;
try
if not Login then
SetStatue(mbsLoginFail)
else begin
SetStatue(mbsNormal);
Process;
end;
except
SetStatue(mbsHttpFail);
end;
end;
mbsNormal:
begin
try
if UpdateBugInfo then
begin
FLastUpdateTime := Now;
end else
begin
Logout;
SetStatue(mbsOffline);
end;
except
SetStatue(mbsHttpFail);
end;
end;
mbsParamFail:
begin
// nothing
end;
mbsLoginFail:
begin
// nothing
end;
mbsHttpFail:
begin
SetStatue(mbsOffline);
Process;
end;
end;
end;
procedure TMonitorBug.SetStatue(Value: TMonitorBugStatue);
begin
if Value <> FMonitorBugStatue then
begin
case Value of
mbsOffline:
begin
FIsLogin := False;
end;
mbsLogining:
begin
// nothing
end;
mbsNormal:
begin
Http.SetIESessionCookie; // 设置IECookies,使得登录状态可以保持
FIsLogin := True;
end;
mbsParamFail:
begin
FIsLogin := False;
end;
mbsLoginFail:
begin
FIsLogin := False;
end;
mbsHttpFail:
begin
FIsLogin := False;
end;
end;
try
if Assigned(FBugStatueChangeNotify) then
FBugStatueChangeNotify(Self, FMonitorBugStatue, Value);
except
// nothing
end;
FMonitorBugStatue := Value;
end;
end;
procedure TMonitorBug.AfterRun;
begin
Logout;
Http.ClearIESessionCookie;
end;
{ TUnclosedBug }
constructor TUnclosedBug.Create(MB: TMonitorBug);
begin
FMB := MB;
FBugList := TParamList.Create;
FBugUpdateState := busNotUpdate;
end;
destructor TUnclosedBug.Destroy;
begin
FreeAndNil(FBugList);
inherited;
end;
function TUnclosedBug.GetBugCount: Integer;
begin
Result := FCount;
end;
function TUnclosedBug.GetBugDesc: string;
begin
Result := '未关闭Bug';
end;
function TUnclosedBug.GetBugList: TParamList;
begin
Result := FBugList;
end;
function TUnclosedBug.GetUpdateState: TBugUpdateState;
begin
Result := FBugUpdateState;
end;
function TUnclosedBug.UpdateBugInfo: Boolean;
const
UnclosedBugCountParam = 'AutoComplete=on&AndOr0=And&Field0=ProjectName&Operator0=%3D&' +
'Value0=&AndOrGroup=AND&AndOr1=And&Field1=OpenedBy&Operator1=%3D&Value1=&' +
'AndOr2=And&Field2=ModulePath&Operator2=LIKE&Value2=&AndOr3=And&' +
'Field3=AssignedTo&Operator3=%3D&Value3=${username}&AndOr4=And&' +
'Field4=BugID&Operator4=%3D&Value4=&AndOr5=And&Field5=BugTitle&' +
'Operator5=LIKE&Value5=&PostQuery=%E6%8F%90%E4%BA%A4%E6%9F%A5%E8%AF%A2%E5%86%85%E5%AE%B9&QueryType=Bug';
var
Html: string;
function GetPost(PostParam: string): string;
var
Param: TParamList;
begin
Param := TParamList.Create;
try
Param.SetParam('${post}', PostParam);
Param.SetParam('${username}', Config.UserName);
Result := FMB.Http.Post(Config.BugFreeUrl + '/BugList.php', Param);
finally
FreeAndNil(Param);
end;
end;
begin
Html := GetPost(UnclosedBugCountParam);
try
Result := UpdateBugCount(Html, FCount);
if not Result then raise Exception.Create('');
Result := UpdateBugList(Html, FBugList);
if not Result then raise Exception.Create('');
FBugUpdateState := busUpdateSuccess;
except
FBugUpdateState := busUpdateFail;
Result := False;
Exit;
end;
end;
function TUnclosedBug.UpdateBugCount(Html: string; var BugCount: Integer): Boolean;
var
StartIndex: Integer;
RecordStr: string;
begin
Result := False;
TParamList.FindSubStr(Html, '<td style="text-align:left;border:0;width:30%">'#13#10, #13#10, Html);
StartIndex := Pos('/', Html);
if StartIndex <= 0 then Exit;
StartIndex := StartIndex + 1;
RecordStr := MidStr(Html, StartIndex, MaxInt);
BugCount := StrToIntDef(RecordStr, 0);
Result := True;
end;
function TUnclosedBug.UpdateBugList(Html: string; var List: TParamList): Boolean;
const
NumFirstFlag = '<td align="center">' + #13#10 + ' <nobr>';
NumLastFlag = '</nobr>';
PriFirstFlag = '<nobr>';
PriLastFlag = '</nobr>';
UrlFirstFlag = '<a href="';
UrlLastFlag = '" title="';
TitleFirstFlag = 'target="_blank"><nobr>';
TitleLastFlag = '</nobr></a>';
var
B, E: Integer;
Url: string;
Title: string;
begin
List.Clear;
List.SetParam('>>>>>>最多前20项<<<<<<', Config.BugFreeUrl + '/BugList.php');
E := 0;
while E < Length(Html) do
begin
B := PosEx(NumFirstFlag, Html, E + 1);
if B = 0 then Break;
B := B + Length(NumFirstFlag);
E := PosEx(NumLastFlag, Html, B);
Title := '[' + MidStr(Html, B, E - B) + ']';
B := PosEx(PriFirstFlag, Html, E + 1) + Length(PriFirstFlag);
E := PosEx(PriLastFlag, Html, B);
Title := Title + '(' + MidStr(Html, B, E - B) + ')';
B := PosEx(UrlFirstFlag, Html, E + 1) + Length(UrlFirstFlag);
E := PosEx(UrlLastFlag, Html, B);
Url := Config.BugFreeUrl + '/' + MidStr(Html, B, E - B);
B := PosEx(TitleFirstFlag, Html, E + 1) + Length(TitleFirstFlag);
E := PosEx(TitleLastFlag, Html, B);
Title := Title + MidStr(Html, B, E - B);
Title := Utf8ToAnsiString(Title);
List.SetParam(Title, Url);
end;
Result := True;
end;
{ TPatternBugList }
constructor TPatternBugList.Create;
begin
FList := TList.Create;
end;
destructor TPatternBugList.Destroy;
begin
Clear;
FreeAndNil(FList);
inherited;
end;
procedure TPatternBugList.Add(PatternBug: TPatternBug);
begin
FList.Add(PatternBug);
end;
procedure TPatternBugList.Clear;
var
I: Integer;
begin
for I := 0 to FList.Count - 1 do
TPatternBug(FList[I]).Free;
FList.Clear;
end;
function TPatternBugList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TPatternBugList.GetItems(I: Integer): TPatternBug;
begin
Result := TPatternBug(FList[I]);
end;
end.