-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
119 lines (96 loc) · 2.96 KB
/
main.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
program KBot6;
{$linklib m}
{$linklib pthread}
uses
{$ifdef unix}cthreads,{$endif} fpjson, sysutils, libcurl, dateutils, fileinfo,
baseunix, ssockets,
Net, Utils, VKAPI, LongpollChat, Database, LuaPlugins, PythonPlugins;
var
response: TResponse;
lpInfo, lpResponse: TJSONObject;
msg: TJSONObject;
lpUpdate: TJSONEnum;
fileVerInfo: TFileVersionInfo;
{$R *.res}
//procedure Write_Str(Len : Longint;var f : Text;const s : String); iocheck; external name 'FPC_WRITE_TEXT_SHORTSTR';
procedure sigHandler(sig: Longint); cdecl;
begin
case sig of
SIGTERM, SIGQUIT, SIGINT:
begin
//Write_Str(1, output, #10#13);
writeln();
halt;
end;
end;
end;
procedure updateLPInfo();
begin
lpInfo := TJSONObject(callVkApi('groups.getLongPollServer', ['group_id', config['group_id'].AsString]));
logWrite('New longpoll info received');
end;
begin
fpsignal(SIGINT, @sigHandler);
fpsignal(SIGTERM, @sigHandler);
fpsignal(SIGQUIT, @sigHandler);
{$if declared(UseHeapTrace)}deleteFile('heap.trc'); setHeapTraceOutput('heap.trc');{$endif}
logThreadId := 0;
botStartTime := dateTimeToUnix(Now());
fileVerInfo := TFileVersionInfo.Create(nil);
FileVerInfo.ReadFileInfo;
logWrite(format('KBot6 v.%s by Augmeneco (Lanode, Cha14ka).', [fileVerInfo.VersionStrings.Values['FileVersion']]));
FileVerInfo.Free;
logWrite('Initilizing...');
if not directoryExists('./plugins/') then
createDir('./plugins/');
luaLoadPlugins();
pythonLoadPlugins();
updateLPInfo();
logWrite('KBot6 ready to work');
while true do
begin
try
response := get(format('%s?act=a_check&key=%s&ts=%s&wait=25',
[lpInfo['server'].asString,
lpInfo['key'].asString,
lpInfo['ts'].asString]),
10000);
except
on e: ESocketError do
begin
logWrite('Longpoll socket timeout!');
updateLPInfo();
continue;
end;
end;
if response.code <> 200 then
begin
logWrite('Longpoll HTTP response isn''t 200 (OK)!');
updateLPInfo();
continue;
end;
lpResponse := TJSONObject(getJSON(response.text));
//logWrite('Longpoll recieved!');
if lpResponse.indexOfName('failed') <> -1 then
begin
logWrite('Longpoll JSON response is "failed"!');
updateLPInfo();
continue;
end;
lpInfo.strings['ts'] := lpResponse['ts'].asString;
for lpUpdate in lpResponse['updates'] do
begin
if TJSONObject(lpUpdate.value).strings['type'] = 'message_new' then
begin
msg := TJSONObject(lpUpdate.value).objects['object'];
try
//raise Exception.create('AAA KERNEL PANIC!!!');
commandsHandler(msg)
except
on e: Exception do
logWrite('Error occurred while message processing: '+E.ToString(), TLogType.logError);
end;
end; {$if declared(UseHeapTrace)}dumpHeap();{$endif}
end;
end;
end.