Skip to content

Commit

Permalink
Merge branch 'feature/runtime-prompt'
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed Mar 11, 2021
2 parents 21bf8a8 + 059fdfc commit 2691b9e
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SSLScrobbler v0.24
# SSLScrobbler v0.25

[![Testing sslscrobbler](https://github.com/ben-xo/sslscrobbler/actions/workflows/testing.yml/badge.svg)](https://github.com/ben-xo/sslscrobbler/actions/workflows/testing.yml)

Expand Down
8 changes: 8 additions & 0 deletions SSL/CLIPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ public function parseOption($arg, array &$argv);
* @param SSLPluggable $sslpluggable
*/
public function addPluginsTo(SSLPluggable $sslpluggable);

/**
* Interactive arg setting. Modified $argv in place.
*
* @param array $argv
*/
public function addPrompts(array &$argv);

}
36 changes: 36 additions & 0 deletions SSL/Factories/OsascriptPromptFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @author Ben XO (me@ben-xo.com)
* @copyright Copyright (c) 2021 Ben XO
* @license MIT License (http://www.opensource.org/licenses/mit-license.html)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

class OsascriptPromptFactory extends PromptFactory
{
/**
* @return Prompt
*/
public function newPrompt()
{
return new OsascriptPrompt();
}
}
36 changes: 36 additions & 0 deletions SSL/Factories/PromptFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @author Ben XO (me@ben-xo.com)
* @copyright Copyright (c) 2021 Ben XO
* @license MIT License (http://www.opensource.org/licenses/mit-license.html)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

class PromptFactory implements Factory
{
/**
* @return Prompt
*/
public function newPrompt()
{
return new StdIOPrompt();
}
}
22 changes: 22 additions & 0 deletions SSL/HistoryReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ protected function parseOptions(array $argv)
continue;
}

if($arg == '--prompt')
{
$this->addPrompts($argv);
continue;
}

if($arg == '--prompt-osascript')
{
Inject::map('PromptFactory', new OsascriptPromptFactory());
$this->addPrompts($argv);
continue;
}

if($arg == '--debug-help')
{
$this->help = true;
Expand Down Expand Up @@ -578,4 +591,13 @@ protected function getMostRecentFile($from_dir, $type)
if($fp) return $fp;
throw new RuntimeException("No $type file found in $from_dir");
}

protected function addPrompts(array &$argv)
{
foreach($this->cli_plugins as $plugin)
{
/* @var $plugin CLIPlugin */
$plugin->addPrompts($argv);
}
}
}
7 changes: 6 additions & 1 deletion SSL/Plugins/DB/CLIDBPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ public function parseOption($arg, array &$argv)

return false;
}


public function addPrompts(array &$argv)
{
// not worth it
}

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
Expand Down
7 changes: 6 additions & 1 deletion SSL/Plugins/IrcCat/CLIIrcCatPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ public function parseOption($arg, array &$argv)

return false;
}


public function addPrompts(array &$argv)
{
// not worth it
}

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
Expand Down
11 changes: 9 additions & 2 deletions SSL/Plugins/JsonServer/CLIJsonServerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ public function parseOption($arg, array &$argv)
$this->plugins[] = $this->newJsonServerPlugin($this->config, $port);
return true;
}

return false;
}


public function addPrompts(array &$argv)
{
$port = 8080;
echo "Json Server: now playing info will be available at http://localhost:$port/nowplaying.json\n";
$argv[] = '-J';
$argv[] = $port;
}

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
Expand Down
37 changes: 36 additions & 1 deletion SSL/Plugins/Last.fm/CLILastfmPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,42 @@ public function parseOption($arg, array &$argv)

return false;
}


public function addPrompts(array &$argv)
{
$ui = new UI();
$lastfm_session_files = glob('lastfm-*.txt');
if($lastfm_session_files)
{
$lastfm_session_file = $lastfm_session_files[0];
preg_match('/lastfm-([^.]+)\.txt/', $lastfm_session_file, $matches);
if(isset($matches[1])) {
$lastfm_name = $matches[1];
while(true) {
$answer = strtolower($ui->readline("Last.fm: do you want to scrobble to www.last.fm/user/$lastfm_name? [Y/n] "));
if ($answer == 'y' || $answer == '') {
$argv[] = '-L';
$argv[] = $lastfm_name;
return;
} elseif($answer == 'n') {
$answer = strtolower($ui->readline("Last.fm: do you want to log out from www.last.fm/user/$lastfm_name? [y/N] "));
if ($answer == 'y') {
unlink($lastfm_session_file);
}
break;
}
}
}
}

$lastfm_name = $ui->readline("Last.fm: type your Last.fm user name (empty to skip): ");
if ($lastfm_name) {
$argv[] = '-L';
$argv[] = $lastfm_name;
return;
}
}

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
Expand Down
35 changes: 35 additions & 0 deletions SSL/Plugins/Twitter/CLITwitterPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ public function parseOption($arg, array &$argv)

return false;
}

public function addPrompts(array &$argv)
{
$ui = new UI();
$twitter_session_files = glob('twitter-*.txt');
if($twitter_session_files)
{
$twitter_session_file = $twitter_session_files[0];
preg_match('/twitter-([^.]+)\.txt/', $twitter_session_file, $matches);
if(isset($matches[1])) {
$twitter_name = $matches[1];
while(true) {
$answer = strtolower($ui->readline("Twitter: do you want to tweet to @$twitter_name? [Y/n] "));
if ($answer == 'y' || $answer == '') {
$argv[] = '-T';
$argv[] = $twitter_name;
return;
} elseif($answer == 'n') {
$answer = strtolower($ui->readline("Twitter: do you want to log out from @$twitter_name? [y/N] "));
if ($answer == 'y') {
unlink($twitter_session_file);
}
break;
}
}
}
}

$twitter_name = $ui->readline("Twitter: type your Twitter name (empty to skip): @");
if ($twitter_name) {
$argv[] = '-T';
$argv[] = $twitter_name;
return;
}
}

public function addPluginsTo(SSLPluggable $sslpluggable)
{
Expand Down
42 changes: 42 additions & 0 deletions SSL/Prompts/OsascriptPrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @author Ben XO (me@ben-xo.com)
* @copyright Copyright (c) 2010 Ben XO
* @license MIT License (http://www.opensource.org/licenses/mit-license.html)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

class OsascriptPrompt implements Prompt
{
/**
* @return string
*/
public function readline($prompt_text)
{

$prompt_text = escapeshellarg($prompt_text);
$command = "osascript -e 'try\ntell app \"SystemUIServer\"\n"
. "set answer to text returned of (display dialog \"'$prompt_text'\" default answer \"\")\n"
. "end\nend\nactivate app (path to frontmost application as text)\nanswer'";

return trim(shell_exec($command));
}
}
33 changes: 33 additions & 0 deletions SSL/Prompts/Prompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* @author Ben XO (me@ben-xo.com)
* @copyright Copyright (c) 2010 Ben XO
* @license MIT License (http://www.opensource.org/licenses/mit-license.html)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

interface Prompt
{
/**
* @return string
*/
public function readline($prompt_text);
}
49 changes: 49 additions & 0 deletions SSL/Prompts/StdIOPrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* @author Ben XO (me@ben-xo.com)
* @copyright Copyright (c) 2010 Ben XO
* @license MIT License (http://www.opensource.org/licenses/mit-license.html)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

class StdIOPrompt implements Prompt
{
/**
* @return string
*/
public function readline($prompt_text)
{
echo $prompt_text;

// would be easier to do this with readline(), but some people don't have the extension installed.
if(($fp = fopen("php://stdin", 'r')) !== false)
{
$input = trim(fgets($fp));
fclose($fp);
}
else
{
throw new RuntimeException('Failed to open stdin');
}

return $input;
}
}
Loading

0 comments on commit 2691b9e

Please sign in to comment.