Skip to content

Commit d887f4e

Browse files
authored
Merge pull request #15 from fluisgirardi/master
Added PC Locker/Unlocker example (linux only) P.S. Workflow must be updated too later
2 parents 8cdce5d + 6bb08a6 commit d887f4e

File tree

3 files changed

+379
-0
lines changed

3 files changed

+379
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectOptions>
4+
<Version Value="12"/>
5+
<General>
6+
<Flags>
7+
<MainUnitHasCreateFormStatements Value="False"/>
8+
<MainUnitHasTitleStatement Value="False"/>
9+
<MainUnitHasScaledStatement Value="False"/>
10+
</Flags>
11+
<SessionStorage Value="InProjectDir"/>
12+
<Title Value="My Application"/>
13+
<UseAppBundle Value="False"/>
14+
<ResourceType Value="res"/>
15+
</General>
16+
<BuildModes>
17+
<Item Name="Debug" Default="True"/>
18+
<Item Name="Release">
19+
<CompilerOptions>
20+
<Version Value="11"/>
21+
<Target>
22+
<Filename Value="tgpclocker"/>
23+
</Target>
24+
<SearchPaths>
25+
<IncludeFiles Value="$(ProjOutDir)"/>
26+
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
27+
</SearchPaths>
28+
<CodeGeneration>
29+
<SmartLinkUnit Value="True"/>
30+
<Optimizations>
31+
<OptimizationLevel Value="3"/>
32+
</Optimizations>
33+
</CodeGeneration>
34+
<Linking>
35+
<Debugging>
36+
<GenerateDebugInfo Value="False"/>
37+
<DebugInfoType Value="dsDwarf3"/>
38+
</Debugging>
39+
<LinkSmart Value="True"/>
40+
</Linking>
41+
</CompilerOptions>
42+
</Item>
43+
</BuildModes>
44+
<PublishOptions>
45+
<Version Value="2"/>
46+
<UseFileFilters Value="True"/>
47+
</PublishOptions>
48+
<RunParams>
49+
<FormatVersion Value="2"/>
50+
</RunParams>
51+
<RequiredPackages>
52+
<Item>
53+
<PackageName Value="fptelegram"/>
54+
</Item>
55+
</RequiredPackages>
56+
<Units>
57+
<Unit>
58+
<Filename Value="tgpclocker.lpr"/>
59+
<IsPartOfProject Value="True"/>
60+
</Unit>
61+
</Units>
62+
</ProjectOptions>
63+
<CompilerOptions>
64+
<Version Value="11"/>
65+
<Target>
66+
<Filename Value="tgpclocker"/>
67+
</Target>
68+
<SearchPaths>
69+
<IncludeFiles Value="$(ProjOutDir)"/>
70+
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
71+
</SearchPaths>
72+
<Parsing>
73+
<SyntaxOptions>
74+
<IncludeAssertionCode Value="True"/>
75+
</SyntaxOptions>
76+
</Parsing>
77+
<CodeGeneration>
78+
<Checks>
79+
<IOChecks Value="True"/>
80+
<RangeChecks Value="True"/>
81+
<OverflowChecks Value="True"/>
82+
<StackChecks Value="True"/>
83+
</Checks>
84+
<VerifyObjMethodCallValidity Value="True"/>
85+
</CodeGeneration>
86+
<Linking>
87+
<Debugging>
88+
<DebugInfoType Value="dsDwarf3"/>
89+
<UseHeaptrc Value="True"/>
90+
<TrashVariables Value="True"/>
91+
<UseExternalDbgSyms Value="True"/>
92+
</Debugging>
93+
</Linking>
94+
</CompilerOptions>
95+
<Debugging>
96+
<Exceptions>
97+
<Item>
98+
<Name Value="EAbort"/>
99+
</Item>
100+
<Item>
101+
<Name Value="ECodetoolError"/>
102+
</Item>
103+
<Item>
104+
<Name Value="EFOpenError"/>
105+
</Item>
106+
</Exceptions>
107+
</Debugging>
108+
</CONFIG>
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
program tgpclocker;
2+
3+
{$mode objfpc}{$H+}
4+
5+
uses
6+
{$IFDEF UNIX}
7+
cthreads,
8+
{$ENDIF}
9+
Classes, SysUtils, BaseUnix, Unix, CustApp, process, tgsendertypes, tgtypes,
10+
xlib, xshm, xutil, x, ipc, Cairo, CairoXlib
11+
{ you can add units after this };
12+
13+
type
14+
15+
{ TMyApplication }
16+
17+
TMyApplication = class(TCustomApplication)
18+
protected
19+
fbot:TTelegramSender;
20+
procedure DoRun; override;
21+
procedure TGLockCmd(ASender: TObject; const ACommand: String; AMessage: TTelegramMessageObj);
22+
procedure TGUnlockCmd(ASender: TObject; const ACommand: String; AMessage: TTelegramMessageObj);
23+
procedure TGScreenshotCmd(ASender: TObject; const ACommand: String; AMessage: TTelegramMessageObj);
24+
public
25+
constructor Create(TheOwner: TComponent); override;
26+
destructor Destroy; override;
27+
end;
28+
29+
const
30+
API_TOKEN = 'BotToken'; //TODO: fill with your bot API
31+
AuthorizedSender = PC owner ID; //TODO: fill with the ID of the user allower do lock/unlock PC remotely
32+
33+
{ TMyApplication }
34+
35+
procedure TMyApplication.DoRun;
36+
var
37+
ErrorMsg: String;
38+
output:String;
39+
begin
40+
while not Terminated do begin
41+
fbot.getUpdatesEx(0,0);
42+
end;
43+
end;
44+
45+
procedure TMyApplication.TGLockCmd(ASender: TObject; const ACommand: String;
46+
AMessage: TTelegramMessageObj);
47+
var
48+
output:String;
49+
ec: Integer;
50+
begin
51+
if AMessage.From.ID = AuthorizedSender then begin
52+
ec:=ExecuteProcess('/usr/bin/cinnamon-screensaver-command', ['--lock']);
53+
fbot.sendMessage(AMessage.ChatId,'Lock command exited with code '+ec.ToString,pmDefault,false,nil,AMessage.MessageId);
54+
end;
55+
end;
56+
57+
procedure TMyApplication.TGUnlockCmd(ASender: TObject; const ACommand: String;
58+
AMessage: TTelegramMessageObj);
59+
var
60+
output:String;
61+
ec: Integer;
62+
begin
63+
if AMessage.From.ID = AuthorizedSender then begin
64+
ec:=ExecuteProcess('/usr/bin/loginctl', ['unlock-session']);
65+
fbot.sendMessage(AMessage.ChatId,'Lock command exited with code '+ec.ToString,pmDefault,false,nil,AMessage.MessageId);
66+
end;
67+
end;
68+
69+
procedure TMyApplication.TGScreenshotCmd(ASender: TObject;
70+
const ACommand: String; AMessage: TTelegramMessageObj);
71+
var
72+
display: PDisplay;
73+
root: TWindow;
74+
scr: cint;
75+
surface: Pcairo_surface_t;
76+
fn: String;
77+
begin
78+
fn:='/tmp/tgpclocker_'+GetProcessID.ToString+'.png';
79+
display := XOpenDisplay(nil);
80+
try
81+
root := DefaultRootWindow(display);
82+
scr := DefaultScreen(display);
83+
84+
surface := cairo_xlib_surface_create(display,
85+
root,
86+
DefaultVisual(display, scr),
87+
DisplayWidth(display, scr),
88+
DisplayHeight(display, scr));
89+
90+
91+
cairo_surface_write_to_png(surface, pchar(fn));
92+
cairo_surface_destroy(surface);
93+
94+
fbot.sendPhotoByFileName(AMessage.ChatId, fn, '', pmDefault, nil, AMessage.MessageId);
95+
finally
96+
XCloseDisplay(display);
97+
end;
98+
end;
99+
100+
constructor TMyApplication.Create(TheOwner: TComponent);
101+
begin
102+
inherited Create(TheOwner);
103+
fbot:=TTelegramSender.Create(API_TOKEN);
104+
fbot.Timeout:=2000;
105+
fbot.CommandHandlers['/lock'] := @TGLockCmd;
106+
fbot.CommandHandlers['/unlock'] := @TGUnlockCmd;
107+
fbot.CommandHandlers['/screenshot'] := @TGScreenshotCmd;
108+
StopOnException:=True;
109+
end;
110+
111+
destructor TMyApplication.Destroy;
112+
begin
113+
inherited Destroy;
114+
end;
115+
116+
var
117+
Application: TMyApplication;
118+
begin
119+
Application:=TMyApplication.Create(nil);
120+
Application.Title:='My Application';
121+
Application.Run;
122+
Application.Free;
123+
end.
124+
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectSession>
4+
<Version Value="12"/>
5+
<BuildModes Active="Debug"/>
6+
<Units>
7+
<Unit>
8+
<Filename Value="tgpclocker.lpr"/>
9+
<IsPartOfProject Value="True"/>
10+
<IsVisibleTab Value="True"/>
11+
<TopLine Value="90"/>
12+
<CursorPos X="21" Y="108"/>
13+
<UsageCount Value="30"/>
14+
<Loaded Value="True"/>
15+
</Unit>
16+
<Unit>
17+
<Filename Value="../../f322lfixes/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
18+
<EditorIndex Value="-1"/>
19+
<TopLine Value="2276"/>
20+
<CursorPos Y="2297"/>
21+
<UsageCount Value="15"/>
22+
</Unit>
23+
<Unit>
24+
<Filename Value="../../f322lfixes/fpcsrc/rtl/unix/unix.pp"/>
25+
<UnitName Value="Unix"/>
26+
<EditorIndex Value="-1"/>
27+
<TopLine Value="325"/>
28+
<CursorPos X="10" Y="346"/>
29+
<UsageCount Value="14"/>
30+
</Unit>
31+
<Unit>
32+
<Filename Value="../base_libs/pascalscada/src/scada/modbusserial.pas"/>
33+
<UnitName Value="ModBusSerial"/>
34+
<EditorIndex Value="-1"/>
35+
<TopLine Value="86"/>
36+
<CursorPos X="18" Y="86"/>
37+
<UsageCount Value="12"/>
38+
</Unit>
39+
<Unit>
40+
<Filename Value="../base_libs/pascalscada/src/scada/modbusdriver.pas"/>
41+
<UnitName Value="ModBusDriver"/>
42+
<EditorIndex Value="-1"/>
43+
<TopLine Value="211"/>
44+
<CursorPos X="19" Y="216"/>
45+
<UsageCount Value="12"/>
46+
</Unit>
47+
<Unit>
48+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
49+
<EditorIndex Value="1"/>
50+
<TopLine Value="626"/>
51+
<CursorPos X="63" Y="638"/>
52+
<UsageCount Value="10"/>
53+
<Loaded Value="True"/>
54+
</Unit>
55+
</Units>
56+
<JumpHistory HistoryIndex="11">
57+
<Position>
58+
<Filename Value="tgpclocker.lpr"/>
59+
<Caret Line="86" TopLine="62"/>
60+
</Position>
61+
<Position>
62+
<Filename Value="tgpclocker.lpr"/>
63+
<Caret Line="92" TopLine="62"/>
64+
</Position>
65+
<Position>
66+
<Filename Value="tgpclocker.lpr"/>
67+
<Caret Line="91" TopLine="62"/>
68+
</Position>
69+
<Position>
70+
<Filename Value="tgpclocker.lpr"/>
71+
<Caret Line="90" TopLine="62"/>
72+
</Position>
73+
<Position>
74+
<Filename Value="tgpclocker.lpr"/>
75+
<Caret Line="88" TopLine="62"/>
76+
</Position>
77+
<Position>
78+
<Filename Value="tgpclocker.lpr"/>
79+
<Caret Line="95" TopLine="62"/>
80+
</Position>
81+
<Position>
82+
<Filename Value="tgpclocker.lpr"/>
83+
<Caret Line="96" TopLine="63"/>
84+
</Position>
85+
<Position>
86+
<Filename Value="tgpclocker.lpr"/>
87+
<Caret Line="98" TopLine="65"/>
88+
</Position>
89+
<Position>
90+
<Filename Value="tgpclocker.lpr"/>
91+
<Caret Line="100" TopLine="67"/>
92+
</Position>
93+
<Position>
94+
<Filename Value="tgpclocker.lpr"/>
95+
<Caret Line="102" TopLine="69"/>
96+
</Position>
97+
<Position>
98+
<Filename Value="tgpclocker.lpr"/>
99+
<Caret Line="98" Column="60" TopLine="69"/>
100+
</Position>
101+
<Position>
102+
<Filename Value="tgpclocker.lpr"/>
103+
<Caret Line="108" Column="17" TopLine="90"/>
104+
</Position>
105+
<Position>
106+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
107+
<Caret Line="638" Column="63" TopLine="624"/>
108+
</Position>
109+
<Position>
110+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
111+
<Caret Line="754" Column="23" TopLine="728"/>
112+
</Position>
113+
<Position>
114+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
115+
<Caret Line="1793" Column="36" TopLine="1767"/>
116+
</Position>
117+
<Position>
118+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
119+
<Caret Line="1795" Column="19" TopLine="1769"/>
120+
</Position>
121+
<Position>
122+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
123+
<Caret Line="2248" Column="29" TopLine="2222"/>
124+
</Position>
125+
<Position>
126+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
127+
<Caret Line="2276" Column="29" TopLine="2250"/>
128+
</Position>
129+
<Position>
130+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
131+
<Caret Line="2397" Column="37" TopLine="2371"/>
132+
</Position>
133+
<Position>
134+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
135+
<Caret Line="2399" Column="11" TopLine="2373"/>
136+
</Position>
137+
<Position>
138+
<Filename Value="../../f323l30fix/config_lazarus/onlinepackagemanager/packages/FpTelegram/tgsendertypes.pas"/>
139+
<Caret Line="2539" Column="11" TopLine="2513"/>
140+
</Position>
141+
</JumpHistory>
142+
<RunParams>
143+
<FormatVersion Value="2"/>
144+
<Modes ActiveMode=""/>
145+
</RunParams>
146+
</ProjectSession>
147+
</CONFIG>

0 commit comments

Comments
 (0)