|
| 1 | +unit IRCLogBot.Replay; |
| 2 | + |
| 3 | +{$mode ObjFPC}{$H+} |
| 4 | + |
| 5 | +interface |
| 6 | + |
| 7 | +uses |
| 8 | + Classes |
| 9 | +, SysUtils |
| 10 | +, SyncObjs |
| 11 | +, IdIRC |
| 12 | + |
| 13 | +; |
| 14 | + |
| 15 | +type |
| 16 | +{ TReplayThread } |
| 17 | + TReplayThread = class(TThread) |
| 18 | + private |
| 19 | + FCriticalSection: TCriticalSection; |
| 20 | + FIRC: TIdIRC; |
| 21 | + FTarget: String; |
| 22 | + FLines: TStringList; |
| 23 | + protected |
| 24 | + procedure Execute; override; |
| 25 | + public |
| 26 | + constructor Create(AIRC: TIdIRC; const ATarget: String; |
| 27 | + const ALines: TStringList); |
| 28 | + destructor Destroy; override; |
| 29 | + published |
| 30 | + end; |
| 31 | + |
| 32 | +implementation |
| 33 | + |
| 34 | +uses |
| 35 | + IRCLogBot.Common |
| 36 | +; |
| 37 | + |
| 38 | +{ TReplayThread } |
| 39 | + |
| 40 | +procedure TReplayThread.Execute; |
| 41 | +var |
| 42 | + line: String; |
| 43 | + index: Integer = 0; |
| 44 | +begin |
| 45 | + if not FIRC.Connected then |
| 46 | + begin |
| 47 | + debug('Exiting replay thread due not being connected.'); |
| 48 | + exit; |
| 49 | + end; |
| 50 | + try |
| 51 | + FIRC.Say(FTarget, '!! --> To avoid triggering flooding, for each 5 lines, I will pause for 5 seconds <-- !!'); |
| 52 | + FIRC.Say(FTarget, Format('*** Here are the last %d lines ***', [FLines.Count])); |
| 53 | + for line in FLines do |
| 54 | + begin |
| 55 | + if (Terminated) or (not FIRC.Connected) then |
| 56 | + begin |
| 57 | + debug('Exiting replay thread due to termination or not being connected.'); |
| 58 | + exit; |
| 59 | + end; |
| 60 | + debug('Sending #%d: "%s"', [index, line]); |
| 61 | + Inc(index); |
| 62 | + FIRC.Say(FTarget, line); |
| 63 | + if (index mod 5) = 0 then |
| 64 | + begin |
| 65 | + debug('Pausing'); |
| 66 | + Sleep(5000); |
| 67 | + end; |
| 68 | + end; |
| 69 | + FIRC.Say(FTarget, Format('*** End of the last %d lines ***', [FLines.Count])); |
| 70 | + finally |
| 71 | + FLines.Free; |
| 72 | + end; |
| 73 | +end; |
| 74 | + |
| 75 | +constructor TReplayThread.Create(AIRC: TIdIRC; const ATarget: String; |
| 76 | + const ALines: TStringList); |
| 77 | +begin |
| 78 | + inherited Create(True); |
| 79 | + FCriticalSection:= TCriticalSection.Create; |
| 80 | + FIRC:= AIRC; |
| 81 | + FTarget:= ATarget; |
| 82 | + FLines:= TStringList.Create; |
| 83 | + FLines.Text := ALines.Text; |
| 84 | + FreeOnTerminate:= True; |
| 85 | + Start; |
| 86 | +end; |
| 87 | + |
| 88 | +destructor TReplayThread.Destroy; |
| 89 | +begin |
| 90 | + FCriticalSection.Free; |
| 91 | + inherited Destroy; |
| 92 | +end; |
| 93 | + |
| 94 | +end. |
| 95 | + |
0 commit comments