forked from groupsc10-zz/WBot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwbot_core.pas
354 lines (314 loc) · 9.16 KB
/
wbot_core.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
{
_ _ _ _____
| | | || | |_ _| /|
| | | || |___ ___ | | / |__
| |/\| || _ |/ _ \| | /_ /
| / \ || (_) | (_) | | | /
|__/\__||_____|\___/|_| |/
}
unit WBot_Core;
{$i wbot.inc}
interface
uses
Classes, SysUtils, StrUtils, LResources,
//WBot
WBot_Model, WBot_Form;
type
TRequestChatEvent = procedure(const ASender: TObject;
const AChats: TResponseChat) of object;
TRequestContactsEvent = procedure(const ASender: TObject;
const AContacts: TResponseContact) of object;
TRequestGroupsEvent = procedure(const ASender: TObject;
const AGroups: TResponseGroups) of object;
{ TWBot }
TWBot = class(TComponent)
private
FBrowser: Boolean;
FLowBatteryLevel: NativeInt;
FMonitorBattery: boolean;
FMonitorUnreadMsgs: boolean;
FOnConnected: TNotifyEvent;
FOnDisconnected: TNotifyEvent;
FOnError: TErrorEvent;
FOnLowBatteryLevel: TNotifyEvent;
FOnNotification: TNotificationEvent;
FOnRequestChat: TRequestChatEvent;
FOnRequestContact: TRequestContactsEvent;
FForm: TWBotForm;
FOnRequestGroups: TRequestGroupsEvent;
FVersion: string;
function GetAuthenticated: boolean;
function GetConected: boolean;
procedure SetUnreadMsgs(const AValue: boolean);
protected
property Console: TWBotForm read FForm;
procedure InternalError(const ASender: TObject; const AError: string;
const AAdditionalInformation: string);
procedure InternalNotification(const ASender: TObject;
const AAction: TActionType; const AData: string);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Connect;
procedure Disconnect(const ALogout: boolean = False);
procedure GetAllContacts;
procedure GetAllGroups;
procedure GetBatteryLevel;
procedure GetUnreadMessages;
procedure ReadMsg(const APhone: String);
procedure SendContact(const APhone, AContact: string);
procedure SendFile(const APhone, ACaption, AFileName: string);
procedure SendMsg(const APhone, AMsg: string);
public
property Authenticated: boolean read GetAuthenticated;
property Conected: boolean read GetConected;
// Events
property OnError: TErrorEvent read FOnError write FOnError;
property OnNotification: TNotificationEvent
read FOnNotification write FOnNotification;
published
property Browser: Boolean read FBrowser write FBrowser default True;
property LowBatteryLevel: NativeInt
read FLowBatteryLevel write FLowBatteryLevel default 10;
property MonitorBattery: boolean
read FMonitorBattery write FMonitorBattery default True;
property MonitorUnreadMsgs: boolean
read FMonitorUnreadMsgs write SetUnreadMsgs default True;
property Version: string read FVersion;
// Events
property OnConnected: TNotifyEvent
read FOnConnected write FOnConnected;
property OnDisconnected: TNotifyEvent
read FOnDisconnected write FOnDisconnected;
property OnLowBatteryLevel: TNotifyEvent
read FOnLowBatteryLevel write FOnLowBatteryLevel;
property OnRequestChat: TRequestChatEvent
read FOnRequestChat write FOnRequestChat;
property OnRequestContact: TRequestContactsEvent
read FOnRequestContact write FOnRequestContact;
property OnRequestGroups: TRequestGroupsEvent
read FOnRequestGroups write FOnRequestGroups;
end;
procedure Register;
implementation
uses
WBot_Const, WBot_Utils;
procedure Register;
begin
{$i wbot.lrs}
RegisterComponents('WBot', [TWBot]);
end;
{ TWBot }
function TWBot.GetAuthenticated: boolean;
begin
if (csDesigning in ComponentState) then
begin
Exit;
end;
Result := FForm.Authenticated;
end;
function TWBot.GetConected: boolean;
begin
if (csDesigning in ComponentState) then
begin
Exit;
end;
Result := FForm.Conected;
end;
procedure TWBot.SetUnreadMsgs(const AValue: boolean);
begin
if (FMonitorUnreadMsgs<>AValue) then
begin
FMonitorUnreadMsgs:=AValue;
if (not(csDesigning in ComponentState)) then
begin
FForm.MonitorUnreadMsgs:=FMonitorUnreadMsgs;
end;
end;
end;
procedure TWBot.InternalError(const ASender: TObject; const AError: string;
const AAdditionalInformation: string);
begin
if (Assigned(FOnError)) then
begin
FOnError(ASender, AError, AAdditionalInformation);
end;
end;
procedure TWBot.InternalNotification(const ASender: TObject;
const AAction: TActionType; const AData: string);
var
VResponseChat: TResponseChat;
VResponseContact: TResponseContact;
VResponseGroups: TResponseGroups;
VResponseBattery: TResponseBattery;
begin
if (Assigned(FOnNotification)) then
begin
FOnNotification(ASender, AAction, AData);
end;
case AAction of
atConnected:
begin
if (Assigned(FOnConnected)) then
begin
FOnConnected(Self);
end;
end;
atDisconnected:
begin
if (Assigned(FOnDisconnected)) then
begin
FOnDisconnected(Self);
end;
end;
atGetBatteryLevel:
begin
if (Assigned(FOnLowBatteryLevel)) then
begin
VResponseBattery := TResponseBattery.Create;
try
VResponseBattery.LoadJSON(AData);
if (StrToIntDef(VResponseBattery.Result, 0) <= FLowBatteryLevel) then
begin
FOnLowBatteryLevel(Self);
end;
finally
FreeAndNil(VResponseBattery);
end;
end;
end;
atGetAllContacts:
begin
if (Assigned(FOnRequestContact)) then
begin
VResponseContact := TResponseContact.Create;
try
VResponseContact.LoadJSON(AData);
FOnRequestContact(Self, VResponseContact);
finally
FreeAndNil(VResponseContact);
end;
end;
end;
atGetAllGroups:
begin
if (Assigned(FOnRequestGroups)) then
begin
VResponseGroups := TResponseGroups.Create;
try
VResponseGroups.LoadJSON(AData);
FOnRequestGroups(Self, VResponseGroups);
finally
FreeAndNil(VResponseGroups);
end;
end;
end;
atGetAllChats,
atGetUnreadMessages:
begin
if (Assigned(FOnRequestChat)) then
begin
VResponseChat := TResponseChat.Create;
try
VResponseChat.LoadJSON(AData);
FOnRequestChat(Self, VResponseChat);
finally
FreeAndNil(VResponseChat);
end;
end;
end;
end;
end;
constructor TWBot.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if (not(csDesigning in ComponentState)) then
begin
FForm := TWBotForm.Create(Self);
FForm.OnError := @InternalError;
FForm.OnNotification := @InternalNotification;
end;
FBrowser := True;
FLowBatteryLevel := 10;
FMonitorBattery := True;
FMonitorUnreadMsgs:= True;
FVersion := WBOT_VERSION;
end;
destructor TWBot.Destroy;
begin
inherited Destroy;
end;
procedure TWBot.Connect;
begin
FForm.Browser := FBrowser;
FForm.MonitorBattery := FMonitorBattery;
FForm.MonitorUnreadMsgs := FMonitorUnreadMsgs;
FForm.Connect;
end;
procedure TWBot.Disconnect(const ALogout: boolean);
begin
FForm.Disconnect(ALogout);
end;
procedure TWBot.GetAllContacts;
begin
FForm.GetAllContacts;
end;
procedure TWBot.GetAllGroups;
begin
FForm.GetAllGroups;
end;
procedure TWBot.GetBatteryLevel;
begin
FForm.GetBatteryLevel;
end;
procedure TWBot.GetUnreadMessages;
begin
FForm.GetUnreadMessages;
end;
procedure TWBot.ReadMsg(const APhone: String);
begin
FForm.ReadMsg(APhone);
end;
procedure TWBot.SendContact(const APhone, AContact: string);
begin
// TODO: Check phone structure
FForm.SendContact(APhone, AContact);
end;
procedure TWBot.SendFile(const APhone, ACaption, AFileName: string);
var
VStream: TStream;
VFileBase64: string;
VFileName: string;
VFileExt: string;
VMsg: string;
begin
// TODO: Check phone structure
if (not(FileExists(AFileName))) then
begin
InternalError(Self, Format(EXCEPT_FILE_NOFOUND, [AFileName]), '');
Exit;
end;
VStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
try
VFileBase64 := StreamToBase64(VStream);
finally
FreeAndNil(VStream);
end;
VFileName := ExtractFileName(AFileName);
VFileExt := ExtractFileExt(AFileName);
VMsg := 'data:application/';
if (AnsiIndexStr(VFileExt,
['.jpg', '.jpeg', '.tif', '.ico', '.bmp', '.png', '.raw']) > -1) then
begin
VMsg := 'data:image/';
end;
VMsg := VMsg + VFileExt + ';base64,' + VFileBase64;
// Send
FForm.SendMsgBase64(APhone, VMsg, VFileName, ACaption);
end;
procedure TWBot.SendMsg(const APhone, AMsg: string);
begin
// TODO: Check phone structure
FForm.SendMsg(APhone, AMsg);
end;
end.