-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPlugin.pm
578 lines (457 loc) · 18.3 KB
/
Plugin.pm
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# ===============================================================================================================
# SrvrPowerCtrl - a plugin for SqueezeCenter 7.3.x / Squeezebox Server 7.4.x
# Allows shutdown/restart/suspend/hibernation of your Squeezebox Server
# hardware via SBS's web interface, your Squeezebox's IR remote
# or via a SBC / Touch / SqueezePlay.
#
# Version 20160501.151506
#
# Copyright (C) 2008, 2009 Gordon Harris
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2, which should be included with this software.
#
# Based on Adrian C's (aka tenwiseman) Server Power Control plugin:
# http://adrianonline.wordpress.com/
# http://forums.slimdevices.com/showthread.php?t=32674
# Copyright AdrianOnline.Net Feb 2007. Initially modeled on Max Spicer's Max2.pm
#
# Also includes code from PeterW's AllQuiet plugin: http://www.tux.org/~peterw/slim/slim7/AllQuiet/
# AllQuiet copyright (c) 2007 by Peter Watkins ([email protected])
#
#
# See a duscussion of this plugin at http://forums.slimdevices.com/showthread.php?t=48521
#
# ===============================================================================================================
#
# Plugin.pm -- main routines for SrvrPowerCtrl
#
package Plugins::SrvrPowerCtrl::Plugin;
use base qw(Slim::Plugin::Base);
use strict;
use Slim::Utils::Misc;
use File::Spec::Functions qw(:ALL);
use File::Basename;
use FindBin qw($Bin);
#use Slim::Utils::Log;
#use Slim::Utils::Prefs;
use Slim::Utils::Strings qw(string);
use Slim::Player::Client;
#Global Variables..
use Plugins::SrvrPowerCtrl::Settings;
use vars qw(%g);
*g = \%Plugins::SrvrPowerCtrl::Settings::g;
use Plugins::SrvrPowerCtrl::Alarms;
use Plugins::SrvrPowerCtrl::AltServer;
use Plugins::SrvrPowerCtrl::Block;
use Plugins::SrvrPowerCtrl::CLI;
use Plugins::SrvrPowerCtrl::Help;
use Plugins::SrvrPowerCtrl::Jive;
use Plugins::SrvrPowerCtrl::Menu;
use Plugins::SrvrPowerCtrl::SleepButton;
use Plugins::SrvrPowerCtrl::Util;
use Plugins::SrvrPowerCtrl::Watchdog;
use Plugins::SrvrPowerCtrl::WebUI;
#use Plugins::SrvrPowerCtrl::bQueue;
#use Plugins::SrvrPowerCtrl::Stats;
my $apiVersion = $g{nAppVersion};
#public $apiVersion;
# --------------------------------------
# Called by the server on plugin startup
sub initPlugin {
my ($class) = @_;
#Initialize global vars, prefs, log, etc..
Plugins::SrvrPowerCtrl::Settings->new($class);
$class->SUPER::initPlugin();
# Register our CLI command
Slim::Control::Request::addDispatch(['srvrpowerctrl','_action','_message', '_switchclient', '_fromjive'], [0, 0, 0, \&Plugins::SrvrPowerCtrl::CLI::pluginCLI]);
# Protect against CSRF attack
#for SBS 7.4 and later..
#if ( $g{nSCVersion} >= 7.4 ) {
#compareVersions Returns: 1 if $left > $right, 0 if $left == $right, -1 if $left < $right
if (Slim::Utils::Versions::compareVersions($::VERSION , '7.4') >= 0) {
Slim::Web::HTTP::CSRF->protectCommand('srvrpowerctrl');
Slim::Web::HTTP::CSRF->protectCommand('jivesrvrpowerctrlmenu');
} else {
Slim::Web::HTTP::protectCommand('srvrpowerctrl');
Slim::Web::HTTP::protectCommand('jivesrvrpowerctrlmenu');
}
# Hook the sleep button on the IR remote..
if ($g{prefs}->bHookSleepButton) {
Plugins::SrvrPowerCtrl::SleepButton::HookSleepButton($g{prefs}->bHookSleepButton);
}
# Prep the various watchdogs...
Plugins::SrvrPowerCtrl::Watchdog::ActivateWatchdogs();
#Initialize our action items..
Plugins::SrvrPowerCtrl::Menu::initActionItems();
# Add the Jive menu
Plugins::SrvrPowerCtrl::Jive::addJiveMenu($class);
# Tack our menu onto the SC WebUI home page Extras menu..
Plugins::SrvrPowerCtrl::WebUI->new($class);
Plugins::SrvrPowerCtrl::WebUI::ActivateWebUI($g{prefs}->bInclude_WebInterface);
# Setup page handlers for the help pages..
Plugins::SrvrPowerCtrl::Help->new($class);
# Start the watchdogs' timer
Plugins::SrvrPowerCtrl::Watchdog::ActivateSPCWatchdog(1, -1);
# Pull back any players from SqueezeNetwork that we want..
if ($g{prefs}->bOnWakeupFetchPlayers) {
$g{tPendingPullFromASTimer} = Slim::Utils::Timers::setTimer(undef, time() + $g{prefs}->nOnWakeupFetchPlayersDelay, \&Plugins::SrvrPowerCtrl::AltServer::PullFromAltServer, );
}
# Execute the .autoexec item (if any)..
my $item = Plugins::SrvrPowerCtrl::Menu::findActionItemByMenuText('.autoexec');
if (defined($item)) {
$g{tPendingActionTimer} = Slim::Utils::Timers::setTimer( undef, time() + ($g{prefs}->nOnWakeupFetchPlayersDelay + 30), \&performAction, ( $item ) );
if ($g{tPendingActionTimer}) {
#$g{log}->is_debug && $g{log}->debug("Timer # $g{tPendingActionTimer} created. Action " . $item->{action} . " is pending in $nWatchdogActionDelay seconds..");
$item->{exeTime} = time() + ($g{prefs}->nOnWakeupFetchPlayersDelay + 15);
$g{hPendingAction} = $item;
$g{log}->is_debug && $g{log}->debug("Pending Action: " . Data::Dump::dump($g{hPendingAction}) );
}
}
$g{log}->is_debug && $g{log}->debug( "$class done initializing.");
}
sub shutdownPlugin {
my ($class) = @_;
# Are we here becuase of an external event?
if ( ! $g{hPreviousAction} || $g{hPreviousAction}->{exeTime} < time() - $g{prefs}->{nAltServerPostPushDelay}) {
#Plugin is shutting down because of some external cause...so take these actions..
$g{log}->is_debug && $g{log}->debug( "$class externally initiated shutdown.");
#Set rtc wake alarm
if ( Plugins::SrvrPowerCtrl::Alarms::ShouldSetRTCWakeup() ) {
Plugins::SrvrPowerCtrl::Alarms::SetRTCWakeup();
}
#Turn off players...
if ( $g{prefs}->bPowerOffPlayers ) {
Plugins::SrvrPowerCtrl::Util::PowerOffPlayer();
}
#Send players to SqueezeNetwork..
if ( $g{prefs}->{bAltServerPushOnXShutdown} ) {
$g{log}->is_debug && $g{log}->debug( "Attempting to push players to $g{prefs}->{szAltServerName}..");
Plugins::SrvrPowerCtrl::AltServer::PushToAltServer();
}
#Perform the xCmd:
if ( $g{prefs}->{szOnXShutdown_cmd} ) {
Plugins::SrvrPowerCtrl::Util::SystemExecCmd(undef, $g{prefs}->{szOnXShutdown_cmd} );
}
} else {
# Nope, we're here because of one of our actions..
$g{log}->is_debug && $g{log}->debug( "$class self initiated " . Data::Dump::dump( $g{hPreviousAction} ) );
}
# Remove our jive menu..
#Plugins::SrvrPowerCtrl::Jive::removeJiveMenu();
# Kill the watchdogs' timer
Plugins::SrvrPowerCtrl::Watchdog::ActivateSPCWatchdog(0);
}
sub getIcon {
my ( $class, $url ) = @_;
return Plugins::SrvrPowerCtrl::Plugin->_pluginDataFor('icon');
}
# ------------------------------------------------------------------
# This is called by the server when the user presses the right arrow
# on the Extras menu and this plugin is selected as a mode.
sub setMode {
my ($class, $client, $method) = @_;
#$g{log}->is_debug && $g{log}->debug("class == " . $class . "; client == " . ( eval {$client->name()} || 'no client' ) . "; method == " . $method );
my @lines;
if ($method eq 'pop') {
# Pop the current mode off the mode stack and restore the previous one
Slim::Buttons::Common::popMode($client);
return;
}
# list of actions for INPUT.List
# menu items for the above node..
foreach my $item (@{$g{aActions}}) {
if ($item->{menuindex} ge 0) {
push (@lines, $item->{'menutext'});
}
}
# INPUT.List takes several parameters, which are passed to it via a hash reference.
my %params = (
stringHeader => 1,
header => 'PLUGIN_SRVRPOWERCTRL_MODULE_NAME',
listRef => \@lines,
onChange => \&Plugins::SrvrPowerCtrl::Menu::doInputListChange,
callback => \&Plugins::SrvrPowerCtrl::Menu::doInputListCallback,
overlayRef => sub { return ( undef, $client->symbols('rightarrow') ); },
);
# start the new mode....
$g{log}->is_debug && $g{log}->debug($client->name() . " method: " . $method . " starting new mode..");
Slim::Buttons::Common::pushModeLeft($client, 'INPUT.List', \%params);
}
sub GetPendingActionMessage {
my $action = shift;
my $message = "";
#OK, we're not blocked..maybe there's a pending action...
if ($g{tPendingActionTimer}) {
$g{log}->is_debug && $g{log}->debug("Timer # $g{tPendingActionTimer} already pending. Action " . $g{hPendingAction}->{action} . " ignored..");
$message = sprintf( string('PLUGIN_SRVRPOWERCTRL_PENDINGACTION_MSG'), ucfirst(lc($action)), ucfirst(lc($g{hPendingAction}->{action})) );
}
return $message;
}
sub prepareAction {
my ($client, $item, $nocancelmsg, $fromJive) = @_;
my $nDeferTime = 0;
my $timeoutmsg;
my $message;
my $jivemessage;
#Don't prepare an action if one is already pending..
if ($g{tPendingActionTimer}) {
$g{log}->is_debug && $g{log}->debug("Timer # $g{tPendingActionTimer} already pending. Action " . $item->{action} . " ignored..");
return 0;
}
if ($item->{checkblock}) {
#If a action block has been set...display the message and do nothing..
if ( Plugins::SrvrPowerCtrl::Block::IsBlocked() ) {
if ($client) {
if (! ($client->deviceid eq 7 || $client->deviceid eq 9) ) {
Slim::Buttons::Common::popModeRight($client);
}
}
$g{log}->is_debug && $g{log}->debug("Trying to warn about block condition..");
Plugins::SrvrPowerCtrl::Block::DispBlockedMessage($client, $item->{'action'}, 3);
return 0;
}
}
#Auto-add sleep defer time...
$nDeferTime = Plugins::SrvrPowerCtrl::SleepButton::GetSleepTime();
#if we are sleep-playing..
if ($nDeferTime > 0) {
$g{log}->is_debug && $g{log}->debug("Deferring action " . $item->{action} . " for $nDeferTime while sleep-playing..");
$item->{isSleepDefered} = 1;
#Activate the sleep monitor
Plugins::SrvrPowerCtrl::Watchdog::ActivateSleepRequestMonitor(1);
}
$nDeferTime += $item->{cancelwait};
#warn all the clients..
if ($nDeferTime > 59) {
#display the timeout in minutes..
$timeoutmsg = sprintf(string('PLUGIN_SRVRPOWERCTRL_SLEEP_TIMEOUT_MSG'), ($nDeferTime/60 + 0.5));
} else {
#display the timeout in seconds..
$timeoutmsg = sprintf(string('PLUGIN_SRVRPOWERCTRL_TIMEOUT_MSG'), $nDeferTime);
}
$message = $item->{'message'} . ' ' . $timeoutmsg;
$jivemessage = $message;
#warn all clients except this one..
Plugins::SrvrPowerCtrl::Util::DisplayPlayerMessage (undef, $message, $nDeferTime, $client);
#warn this client..
if ($client) {
#$message = $item->{'message'};
#display the 'how to cancel' message..
if (!$nocancelmsg) {
if ($fromJive) {
$jivemessage = $jivemessage . '...' . (Plugins::SrvrPowerCtrl::Util::ClientAttribute($client, 'model') eq 'controller' ? string('PLUGIN_SRVRPOWERCTRL_CANCELJIVE_MSG') : string('PLUGIN_SRVRPOWERCTRL_CANCELTOUCH_MSG'));
} else {
$message = $message . '...' . string('PLUGIN_SRVRPOWERCTRL_CANCEL_MSG');
}
}
Plugins::SrvrPowerCtrl::Util::DisplayPlayerMessage ($client, $message, $nDeferTime, undef, $jivemessage);
}
#set a timer to perform the action...
$g{tPendingActionTimer} = Slim::Utils::Timers::setTimer( $client, time() + $nDeferTime, \&performAction, ( $item ) );
if ($g{tPendingActionTimer}) {
$g{log}->is_debug && $g{log}->debug("Timer # $g{tPendingActionTimer} created. Action " . $item->{action} . " is pending in $nDeferTime seconds..");
$item->{exeTime} = time() + $nDeferTime;
$g{hPendingAction} = $item;
$g{log}->is_debug && $g{log}->debug("Pending Action: " . Data::Dump::dump($g{hPendingAction}) );
return 1;
}
return 0;
}
sub resecheduleAction {
my ($client, $item) = @_;
my $nDeferTime = 0;
#Auto-add sleep defer time...
$nDeferTime = Plugins::SrvrPowerCtrl::SleepButton::GetSleepTime();
#if we are sleep-playing..
if ($nDeferTime > 0) {
$g{log}->is_debug && $g{log}->debug("Deferring action " . $item->{action} . " for $nDeferTime while sleep-playing..");
$item->{isSleepDefered} = 1;
#Activate the sleep monitor
Plugins::SrvrPowerCtrl::Watchdog::ActivateSleepRequestMonitor(1);
}
$nDeferTime += $item->{cancelwait};
$g{log}->is_debug && $g{log}->debug("Killing timer # $g{tPendingActionTimer}..");
Slim::Utils::Timers::killSpecific($g{tPendingActionTimer});
$g{tPendingActionTimer} = 0;
#set a timer to perform the action...
$g{tPendingActionTimer} = Slim::Utils::Timers::setTimer( $client, time() + $nDeferTime, \&performAction, ( $item ) );
if ($g{tPendingActionTimer}) {
$g{log}->is_debug && $g{log}->debug("Timer # $g{tPendingActionTimer} rescheduled. Action " . $item->{action} . " is pending in $nDeferTime seconds..");
$item->{exeTime} = time() + $nDeferTime;
$g{hPendingAction} = $item;
$g{log}->is_debug && $g{log}->debug("Pending Action: " . Data::Dump::dump($g{hPendingAction}) );
return 1;
}
return 0;
}
# -------------------------------------------------------------------------
# This is the sub which executes the shutdown/restart/suspend/etc. commands
sub performAction {
my ($client, $item) = @_;
my $nPushed = 0;
my $nDelay = 0;
#Power Off all clients unless we're going to SN..
if ( $item->{poweroffplayers} ) {
Plugins::SrvrPowerCtrl::Util::PowerOffPlayer();
} elsif ( Plugins::SrvrPowerCtrl::Util::IsValidClient($client) ) {
#Force the player to the home page. Makes resuming from suspend/hibernation less messy. Suggested by Wirrunna.
$client->execute(['button', 'home']);
}
# point of no return, block further updates to display..
Plugins::SrvrPowerCtrl::Util::DisplayPlayerMessage(undef, $item->{message}, $item->{messagetime}, undef, undef, 1); #blocking message..
#Switch the calling player to SqueezeNetwork..
if ($item->{'push2as'}) {
($nPushed, $nDelay) = Plugins::SrvrPowerCtrl::AltServer::PushToAltServer($client);
}
#cue up our action command to execute in the near future..
$g{tPendingActionTimer} = Plugins::SrvrPowerCtrl::Util::ScheduleCommand($client, $item, $nDelay);
return 1;
}
sub cancelAction {
my ($client, $item) = @_;
my $message;
if (!$g{tPendingActionTimer}) {
return 0;
}
$g{log}->is_debug && $g{log}->debug("Killing timer # $g{tPendingActionTimer}..");
Slim::Utils::Timers::killSpecific($g{tPendingActionTimer});
$g{tPendingActionTimer} = 0;
#Action is no longer pending..
if (defined($item)) {
$item->{isSleepDefered} = 0;
}
$g{hPendingAction} = { };
#desplay the cancel message on all players..
$message = string('PLUGIN_SRVRPOWERCTRL_CANCELED_MSG');
Plugins::SrvrPowerCtrl::Util::DisplayPlayerMessage (undef, $message, 3);
$g{log}->is_debug && $g{log}->debug( "$message");
return 1;
}
sub blockAction {
my ($client, $set, $caller, $reason) = @_;
my $blockcode;
my $element;
my $num;
my $nCurrentBlocks;
my $nBlockCount = 0;
my $numremain = 0;
if (!defined($set)) {
$g{log}->is_debug && $g{log}->debug("blockAction: bad param!");
return 0;
}
#$nCurrentBlocks = (defined(@{$g{aBlockAction}}) ? (@{$g{aBlockAction}}) : 0);
$nCurrentBlocks = (@{$g{aBlockAction}});
$g{log}->is_debug && $g{log}->debug("Begin: there are $nCurrentBlocks blockactions set.");
if ($set eq 'set') {
if ( $caller eq 'viacli' && $reason eq 'blockfile') {
Plugins::SrvrPowerCtrl::Block::CreateBlockFile();
$blockcode = Plugins::SrvrPowerCtrl::Block::GetBlockFileName();
return $blockcode;
}
$blockcode = int(rand(999999)) + 1;
$g{log}->is_debug && $g{log}->debug("Pushing blockAction caller: $caller, reason: $reason, blockcode: $blockcode");
push ( @{$g{aBlockAction}},
{
client => $client,
caller => $caller,
reason => $reason,
blockcode => $blockcode,
} );
#re-initialize the menu..
Plugins::SrvrPowerCtrl::Menu::initActionItems();
return $blockcode;
} elsif ($set eq 'count' ) {
$nBlockCount = 0;
if (Plugins::SrvrPowerCtrl::Block::BlockFileExists()) {
$nBlockCount++;
}
#count the valid blocks..
foreach my $element (@{$g{aBlockAction}}) {
#skip empty block elements..
if ( defined($element->{'caller'}) ) {
$nBlockCount++;
}
}
return $nBlockCount;
} elsif ($set eq 'clear' ) {
$nBlockCount = 0;
my $bBlockfileExists = Plugins::SrvrPowerCtrl::Block::BlockFileExists();
if ($bBlockfileExists) {
$nBlockCount++;
}
#count the valid blocks..
foreach my $element (@{$g{aBlockAction}}) {
#skip empty block elements..
if ( defined($element->{'caller'}) ) {
$nBlockCount++;
}
}
if ( $caller eq 'viacli' && $reason eq 'blockfile' && $bBlockfileExists) {
if ( Plugins::SrvrPowerCtrl::Block::DeleteBlockFile() ) {
return --$nBlockCount;
} else {
return -1;
}
}
$num = 0;
foreach my $element (@{$g{aBlockAction}}) {
#skip empty block elements..
if ( defined($element->{'caller'}) ) {
#Don't worry about blockcode security if the caller is viacli or softblock..
if ( $caller =~ m/^viacli$/i && ($element->{'caller'} eq $caller) ||
$caller =~ m/^softblock$/i && ($element->{'caller'} eq $caller)) {
$g{log}->is_debug && $g{log}->debug("Deleting blockAction[$num] caller: $caller, reason: $reason");
#delete $g{aBlockAction}[$num];
splice @{$g{aBlockAction}}, $num, 1;
#re-initialize the menu..
Plugins::SrvrPowerCtrl::Menu::initActionItems();
return --$nBlockCount;
} elsif ( ($element->{'caller'} eq $caller) && ($element->{'blockcode'} eq $reason) ) {
$g{log}->is_debug && $g{log}->debug("Deleting blockAction[$num] caller: $caller, reason: $reason, blockcode: $element->{'blockcode'}");
#delete $g{aBlockAction}[$num];
splice @{$g{aBlockAction}}, $num, 1;
#re-initialize the menu..
Plugins::SrvrPowerCtrl::Menu::initActionItems();
return --$nBlockCount;
}
}
$num++;
}
}
$g{log}->is_debug && $g{log}->debug("End: there are " . (@{$g{aBlockAction}}) . " blockactions set.");
$g{log}->is_debug && $g{log}->debug("blockAction $set: no action taken!..");
return -1;
}
# ----------------------------
# thanks to mavit for this..
our %menuFunctions = (
# normal up/right/left/down navigation:
# all we offer is a way to exit this mode
'left' => sub {
my $client = shift;
my $button = shift;
my $id = $client->id();
Slim::Buttons::Common::popModeRight($client);
},
# and the function that does the work
'ourSleepHoldButtonHandler' => sub {
my $client = shift;
&Plugins::SrvrPowerCtrl::SleepButton::ourSleepHoldButtonHandler($client);
},
);
# ---------------------------------------------------
# This is called by the server to work out what to do
# when the user presses buttons on the remote.
sub getFunctions {
return \%menuFunctions;
}
# ----------------------------------------------------------------------------------------
# Return the language independent string for the plugin name. This will be passed through
# the strings function to get the appropriate language version.
sub getDisplayName {
return 'PLUGIN_SRVRPOWERCTRL_MODULE_NAME';
}
1;
__END__