Including CLI commands in HTML?

zarqon

Well-known member
Here's an idea that could be awesome. There might also be problems with it that I'm not aware of.

Idea: the ability to include CLI commands in links.

Simple example:
<a href="urlforbookstore.php" cli="equip acc3 pirate fledges">[bookstore]</a>

Or:
<a href="urlforbookstore.php?cli=equip+acc3+pirate+fledges">[bookstore]</a>

(The second one seems like it would be easier to implement, but a little harder to use since everything has to be urlencoded.)

Then, when the relay browser parses links, mafia first executes the CLI code before visiting the url.

I think this has the potential to significantly streamline the browsing (and relay scripting) process.

Avoiding browser timeouts might be an issue for lengthy chunks of CLI, but in that case it should probably just be a standalone script.

Any thoughts?
 

Bale

Minion
[quote author=zarqon link=topic=2159.msg11124#msg11124 date=1232058568]
Any thoughts?
[/quote]

Just a question: Why? What does this do that is better than just writing a script containing more than one line? Is this so that you can make favorite places links for firefox? Click the browser button and solve the quest?
 

zarqon

Well-known member
It would reduce clicking (even more than mafia's handy minilinks) without taking you out of the browser. There are a lot of click-routines in regular browser play; this would enable you to pop out on the end of that routine, while retaining the actually-playing-the-game feel of relay browsing that some prefer.

My example wasn't that great for demonstrating that. Here is a better one:

Replacing <a href="linktoimpassablechasm"> with <a href="urlforunlockingchasm.php?cli=checkpoint;outfit swash;acquire+dictionary;untinker dictionary;outfit+checkpoint">.

It's useful for actions you want to prepare before visiting certain links, without needing to write a separate relay script for each of those links. So relay overrides can be concentrated in the jumping-off points, the pages where the click-routines begin, rather than all the individual pages that need pre-action.

In the above example, assuming mafia didn't already add [untinker] and [chasm] links to the page, we would have to add those ourselves, overriding both town_right.php and store.php (or whatever Barrrtleby's url is). This way, we only alter mountains.php, AND we only need to click once, AND it's transparent.

And even though we are adding CLI commands, it's just as flexible as ASH since the inclusion of CLI commands is performed by an ASH script. In the example above, the outfit change could easily be omitted if you were already wearing pirate fledges.

So that's why I think it would be useful.
 

jasonharper

Developer
Your first example isn't workable: the browser would simply ignore that "cli" attribute, mafia would never see it when the link was clicked.

The second example could be made workable, with a little concern for security (you don't want anyone to be able to put a link in a kmail, clan ad, or display case that would cause your copy of KoLmafia to perform arbitrary commands!). There would need to be a specification for what happens if the command doesn't execute successfully: error page? follow the link anyway?
 

Bale

Minion
[quote author=jasonharper link=topic=2159.msg11130#msg11130 date=1232065925]
The second example could be made workable, with a little concern for security (you don't want anyone to be able to put a link in a kmail, clan ad, or display case that would cause your copy of KoLmafia to perform arbitrary commands!). There would need to be a specification for what happens if the command doesn't execute successfully: error page? follow the link anyway?
[/quote]

Good point about security. I wouldn't want to send someone all my meat! This might be dangerous to implement.

An error page would be best if it doesn't execute successfully. Just some text that says there was an error and list the CLI that didn't execute.
 

zarqon

Well-known member
Yeah, that is a good point. How to prevent abuse? Some thoughts:

1) only execute/include CLI-empowered links on pages that were generated via override (pages with an existing filename.ash in the relay folder). A safeguard, but not security.
2) a more thorough measure is to disallow "dangerous" commands in CLI-empowered links, such as send, mallsell, set, ash, alias, and all php urls. I would imagine the list would be pretty small.

That said, I don't think the danger is very great. People should always be careful about the actual address of any link in a kmail or clan ad, and most browsers show you the address in the status bar or some such before clicking. Even without protections, I don't think mafia would be responsible if someone were the victim of a malicious link (especially since if it were in a kmail, the link would actually be fully written out).

As far as what happens with a CLI error, it should be the same as any other script -- everything stops. Don't load the page, and no error page is necessary, since mafia will be in a red error state (I'm assuming that mafia will still display errors to the CLI as normal). However, using the "try" command gives scripters the option to load the page despite errors. The burden of functionality here should be on the scripters.

-----
EDIT: 3) Okay, just thought of this one and I like it best: CLI-empowered links must begin with (or somehow otherwise include) your pwd hash. A container function string gen_cli_link(string url, string clicommands) which includes your hash in whatever format is best (beginning of cli string, separate url variable, whatever) and urlencodes the clicommands string would be nice but unnecessary.
 

Miser

New member
It's possible to execute CLI commands from the relay browser, but it's not exactly perfect.

Having a link like this:

Code:
<a href="/KoLmafia/sideCommand?cmd=win+game&pwd=[hash]">

will execute the command in the CLI, but the browser window (or the frame) will change to the char pane. See my charpane.php override for an example.
Using executeCommand instead of sideCommand will instead blank out the frame.

You could try using Javascript to get around that:

Code:
<a href="javascript:top.mainpane.location.href='/KoLmafia/executeCommand?cmd=outfit+elite+guard&pwd=[hash]';function timeout(){top.mainpane.location.href='store.php?whichstore=g'};window.setTimeout(timeout,3000);void(0);">

for example ought to work, but it's very clumsy, and Javascript doesn't know how long it will take mafia to execute the commands, so you'll just have to assume something. Maybe someone finds a better way, but I think it wouldn't be too difficult to extend this functionality in mafia to allow to do what you want.

-------
EDIT:

After messing around with Javascript's event handlers a bit, I found a way to make this work better, at least as a temporary solution. Here's a little ash script that will create such a link:

Code:
string cli_link(string command, string target, string text){
	string result = "<a href=\"javascript:top.mainpane.location.href='/KoLmafia/executeCommand?cmd=" + command;
	result = result + "&pwd=" + my_hash() + "';top.mainpane.document.body.setAttribute('onUnload','window.location.href = \\'";
	result = result + target + "\\';');void(0);\">" + text + "</a>";
	return result;
}

e.g. cli_link("outfit+elite+guard","store.php?whichstore=g","Laboratory")
 
Top