-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtgdaemon.pas
244 lines (213 loc) · 5.75 KB
/
tgdaemon.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
(*
Tg framework, FCL HTTP Daemon Broker
Copyright (C) 2014 Silvio Clecio
See the file LICENSE.txt, included in this distribution,
for details about the copyright.
This library 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.
*)
unit tgdaemon;
{$mode objfpc}{$H+}
interface
uses
{$IFDEF MSWINDOWS}
ServiceManager,
{$ENDIF}
DaemonApp, Classes, SysUtils, IniFiles, tgsendertypes, tgtypes, process;
type
{ TTgShellDaemon }
TTgShellDaemon = class(TDaemon)
private
FBot: TTelegramSender;
FChatID: Int64;
FConf: TMemIniFile;
FProc: TProcess;
FTerminated: Boolean;
procedure BotReceiveUpdate({%H-}ASender: TObject; AnUpdate: TTelegramUpdateObj);
procedure BotReceiveMessage({%H-}ASender: TObject; AMessage: TTelegramMessageObj);
procedure BotLogMessage({%H-}ASender: TObject; EventType: TEventType; const Msg: String);
procedure OutputStd;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Install: Boolean; override;
function Execute: Boolean; override;
function Stop: Boolean; override;
function Uninstall: Boolean; override;
end;
{ TTgShellMapper }
TTgShellMapper = class(TDaemonMapper)
private
published
constructor Create(AOwner: TComponent); override;
end;
var
TgShellDaemon: TTgShellDaemon;
TgShellMapper: TTgShellMapper;
implementation
uses
{$IFDEF MSWINDOWS}windows{$ENDIF}
;
var
AppDir: String;
{ TTgShellMapper }
constructor TTgShellMapper.Create(AOwner: TComponent);
var
DmnDef: TDaemonDef;
begin
inherited CreateNew(AOwner);
DmnDef:=DaemonDefs.Add as TDaemonDef;
DmnDef.DaemonClassName:='TTgShellDaemon';
DmnDef.DisplayName:='Telegram shell bot';
DmnDef.Name:='TgShellDaemon';
end;
{ TTgShellDaemon }
procedure TTgShellDaemon.BotReceiveUpdate(ASender: TObject;
AnUpdate: TTelegramUpdateObj);
begin
Logger.Log(etDebug, 'Update received: '+AnUpdate.AsString);
end;
procedure TTgShellDaemon.BotReceiveMessage(ASender: TObject;
AMessage: TTelegramMessageObj);
var
ExitCode: integer;
InputString: String;
begin
InputString:=AMessage.Text;
if InputString=EmptyStr then
Exit;
InputString+=LineEnding;
if FProc.Running then
begin
OutputStd;
FProc.Input.Write(InputString[1], Length(InputString));
sleep(100);
OutputStd;
end;
end;
procedure TTgShellDaemon.BotLogMessage(ASender: TObject; EventType: TEventType;
const Msg: String);
begin
Logger.Log(EventType, Msg);
end;
procedure TTgShellDaemon.OutputStd;
var
CharBuffer: array [0..511] of char;
ReadCount: integer;
OutputString: String;
begin
OutputString:=EmptyStr;
// read stdout and write to our stdout
while FProc.Output.NumBytesAvailable > 0 do
begin
ReadCount := Min(512, FProc.Output.NumBytesAvailable); //Read up to buffer, not more
FProc.Output.Read(CharBuffer{%H-}, ReadCount);
OutputString+=Copy(CharBuffer, 0, ReadCount);
Write(StdOut, OutputString);
end;
if OutputString<>EmptyStr then
FBot.sendMessage(OutputString);
// read stderr and write to our stderr
{ while FProc.Stderr.NumBytesAvailable > 0 do
begin
ReadCount := Min(512, FProc.Stderr.NumBytesAvailable); //Read up to buffer, not more
FProc.Stderr.Read(CharBuffer, ReadCount);
FBot.sendMessage(Copy(CharBuffer, 0, ReadCount));
// Write(StdErr, Copy(CharBuffer, 0, ReadCount));
end; }
end;
constructor TTgShellDaemon.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FConf:=TMemIniFile.Create(AppDir+'telegram.ini');
TelegramAPI_URL:=FConf.ReadString('API', 'Endpoint', TelegramAPI_URL); // For Russian specific case
FBot:=TTelegramSender.Create(FConf.ReadString('Bot', 'Token', EmptyStr));
if FBot.Token=EmptyStr then
begin
Logger.Error('Please, specify bot token in telegram.ini!');
Exit;
end;
FChatID:=FConf.ReadInt64('Chat', 'ID', 0);
if FChatID=0 then
begin
Logger.Error('Please, specify chat ID in telegram.ini! See readme.md');
Exit;
end;
FBot.OnReceiveUpdate:=@BotReceiveUpdate;
FBot.OnReceiveMessage:=@BotReceiveMessage;
FBot.OnLogMessage:=@BotLogMessage;
{$IFDEF MSWINDOWS}
SetConsoleOutputCP(CP_UTF8);{$ENDIF}
FProc:=TProcess.Create(nil);
FProc.Options := [poUsePipes, poStderrToOutPut];
FProc.Executable:={$IFDEF MSWINDOWS}'cmd'{$ELSE}'sh'{$ENDIF};
FProc.Execute;
sleep(100);
OutputStd;
FTerminated:=False;
end;
destructor TTgShellDaemon.Destroy;
begin
FreeAndNil(FProc);
FreeAndNil(FConf);
FreeAndNil(FBot);
inherited Destroy;
end;
function TTgShellDaemon.Install: Boolean;
{$IFDEF MSWINDOWS}
var
VSM: TServiceManager;
{$ENDIF}
begin
Result := inherited Install;
{$IFDEF MSWINDOWS}
VSM := TServiceManager.Create(nil);
try
VSM.Connect;
if VSM.Connected then
VSM.StartService('TelegramShellBot', nil);
VSM.Disconnect;
finally
VSM.Free;
end;
{$ENDIF}
WriteLn('Service installed.');
WriteLn('Some help from terminal');
end;
function TTgShellDaemon.Uninstall: Boolean;
{$IFDEF MSWINDOWS}
var
VSM: TServiceManager;
{$ENDIF}
begin
Result := inherited Uninstall;
{$IFDEF MSWINDOWS}
VSM := TServiceManager.Create(nil);
try
VSM.Connect;
if VSM.Connected then
VSM.StopService('TelegramShellBot', True);
VSM.Disconnect;
finally
VSM.Free;
end;
{$ENDIF}
WriteLn('Service uninstalled.');
end;
function TTgShellDaemon.Execute: Boolean;
begin
while not FTerminated do
FBot.getUpdatesEx(0, 10);
Result:=True;
end;
function TTgShellDaemon.Stop: Boolean;
begin
Result:=inherited Stop;
FTerminated:=True;
end;
initialization
AppDir:=IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0)));
RegisterDaemonClass(TTgShellDaemon);
RegisterDaemonMapper(TTgShellMapper);
end.