triggers: handle rpc commands

move the text query parser used in web console in the wallet rpc class
This commit is contained in:
Tanguy Pruvot 2016-12-24 01:09:38 +01:00
parent 3801d3d539
commit 8a034edf29
5 changed files with 100 additions and 53 deletions

View file

@ -77,6 +77,16 @@ function NotifyCheckRules()
$message .= "Field: {$field}\n";
$message .= "Value: {$value} at ".strftime("%Y-%m-%d %T %z", $time)."\n";
// replace some possible vars in message (description)
$message = str_replace('$X', $value, $message);
$message = str_replace('$F', $field, $message);
$message = str_replace('$T', $conditiontype, $message);
$message = str_replace('$V', $conditionvalue, $message);
$message = str_replace('$N', $coin->name, $message);
$message = str_replace('$SYM', $coin->symbol, $message);
$message = str_replace('$S2', $coin->symbol2, $message);
$message = str_replace('$A', $coin->master_wallet, $message);
$dest = YAAMP_ADMIN_EMAIL;
if (!empty($rule->notifycmd) && strstr($notifycmd, "@")) {
$dest = $rule->notifycmd;
@ -87,6 +97,27 @@ function NotifyCheckRules()
debuglog("notify: unable to send mail to {$dest}!");
break;
case 'rpc':
$command = $rule->notifycmd;
// replace some possible vars in user command
$command = str_replace('$X', $value, $command);
$command = str_replace('$F', $field, $command);
$command = str_replace('$T', $conditiontype, $command);
$command = str_replace('$V', $conditionvalue, $command);
$command = str_replace('$N', $coin->name, $command);
$command = str_replace('$SYM', $coin->symbol, $command);
$command = str_replace('$S2', $coin->symbol2, $command);
$command = str_replace('$A', $coin->master_wallet, $command);
$remote = new WalletRPC($coin);
$res = $remote->execute($command);
if ($res === false)
debuglog("notify: rpc error {$coin->symbol} {$command}");
break;
case 'system':
$command = $rule->notifycmd;
@ -99,6 +130,7 @@ function NotifyCheckRules()
$command = str_replace('$N', $coin->name, $command);
$command = str_replace('$SYM', $coin->symbol, $command);
$command = str_replace('$S2', $coin->symbol2, $command);
$command = str_replace('$A', $coin->master_wallet, $command);
$res = system($command);
if ($res === false)

View file

@ -324,4 +324,63 @@ class WalletRPC {
$this->rpc->$prop = $value;
}
function execute($query)
{
$result = '';
if (!empty($query)) try {
$params = explode(' ', trim($query));
$command = array_shift($params);
$p = array();
foreach ($params as $param) {
if ($param === 'true' || $param === 'false') {
$param = $param === 'true' ? true : false;
}
else if (strpos($param, '0x') === 0)
$param = "$param"; // eth hex crap
else
$param = (is_numeric($param)) ? 0 + $param : trim($param,'"');
$p[] = $param;
}
switch (count($params)) {
case 0:
$result = $this->$command();
break;
case 1:
$result = $this->$command($p[0]);
break;
case 2:
$result = $this->$command($p[0], $p[1]);
break;
case 3:
$result = $this->$command($p[0], $p[1], $p[2]);
break;
case 4:
$result = $this->$command($p[0], $p[1], $p[2], $p[3]);
break;
case 5:
$result = $this->$command($p[0], $p[1], $p[2], $p[3], $p[4]);
break;
case 6:
$result = $this->$command($p[0], $p[1], $p[2], $p[3], $p[4], $p[5]);
break;
case 7:
$result = $this->$command($p[0], $p[1], $p[2], $p[3], $p[4], $p[5], $p[6]);
break;
case 8:
$result = $this->$command($p[0], $p[1], $p[2], $p[3], $p[4], $p[5], $p[6], $p[7]);
break;
default:
$result = 'error: too much parameters';
}
} catch (Exception $e) {
$result = false;
}
return $result;
}
}

View file

@ -118,57 +118,11 @@ div.terminal { color: silver; background: black; min-height: 180px; margin-left:
end;
$result = '';
if (!empty($query)) try {
$params = explode(' ', trim($query));
$command = array_shift($params);
$p = array();
foreach ($params as $param) {
if ($param === 'true' || $param === 'false') {
$param = $param === 'true' ? true : false;
}
else if (strpos($param, '0x') === 0)
$param = "$param"; // eth hex crap
else
$param = (is_numeric($param)) ? 0 + $param : trim($param,'"');
$p[] = $param;
if (!empty($query)) {
$result = $remote->execute($query);
if ($result === false) {
$result = $remote->error;
}
switch (count($params)) {
case 0:
$result = $remote->$command();
break;
case 1:
$result = $remote->$command($p[0]);
break;
case 2:
$result = $remote->$command($p[0], $p[1]);
break;
case 3:
$result = $remote->$command($p[0], $p[1], $p[2]);
break;
case 4:
$result = $remote->$command($p[0], $p[1], $p[2], $p[3]);
break;
case 5:
$result = $remote->$command($p[0], $p[1], $p[2], $p[3], $p[4]);
break;
case 6:
$result = $remote->$command($p[0], $p[1], $p[2], $p[3], $p[4], $p[5]);
break;
case 7:
$result = $remote->$command($p[0], $p[1], $p[2], $p[3], $p[4], $p[5], $p[6]);
break;
case 8:
$result = $remote->$command($p[0], $p[1], $p[2], $p[3], $p[4], $p[5], $p[6], $p[7]);
break;
default:
$result = 'error: too much parameters';
}
} catch (Exception $e) {
$result = $remote->error;
}
if (!empty($remote->error) && $remote->error != $result) {

View file

@ -93,9 +93,10 @@ echo <<<end
<label for="notifytype">Type</label>
<select id="notifytype" name="notifytype">
<option value="email">Email</option>
<option value="rpc">RPC command</option>
<option value="system">System command</option>
</select><br/><br/>
<input type="text" name="conditiontype" class="main-text-input" placeholder="Condition" style="width: 190px; margin-right: 4px;">
<input type="text" name="conditiontype" class="main-text-input" placeholder="Condition like 'balance >'" style="width: 190px; margin-right: 4px;">
<input type="text" name="conditionvalue" class="main-text-input" placeholder="Value" style="width: 100px; margin-right: 4px;">
<input type="submit" value="Add rule" class="main-submit-button" ><br/>
<input type="text" name="notifycmd" class="main-text-input" placeholder="Email or Command (optional)" style="width: 400px; margin-right: 4px; margin-top: 8px;"><br/>
@ -113,6 +114,7 @@ echo <<<end
<li>\$SYM coin symbol</li>
<li>\$S2 coin symbol2</li>
<li>\$N coin name</li>
<li>\$A wallet address</li>
</ul>
</div>

View file

@ -276,8 +276,8 @@ if (!empty($list_extra)) {
</tr>
<tr class="ssrow">
<td colspan="3" style="font-size: .9em; padding-bottom: 8px;">
Some wallets (LYB) have a problem and don't confirm a transaction in the requested time.<br/>
Please be honest and continue mining to handle these extra transactions sent to you.<br/>
Some wallets (UFO,LYB) have a problem and don't always confirm a transaction in the requested time.<br/>
<!-- Please be honest and continue mining to handle these extra transactions sent to you. --><br/>
</th>
</tr>
<tr class="ssrow">