Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Automatically uploading info to a website?

Automatically uploading info to a website?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  

Posted by Areadien   USA  (47 posts)  Bio
Date Tue 07 Aug 2018 02:34 AM (UTC)
Message
Is there any way to set up a trigger to copy info to a website form? I'd like to be able to, say, complete a quest in my game and then have this website automatically keep a backup of my questing.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 07 Aug 2018 05:34 AM (UTC)
Message

It’s possible, but I don’t think it would be particularly easy.

The format for sending messages to a HTTP server is well-documented, but if you are not familiar with it, it could be fairly daunting. Plus, you presumably need to authenticate yourself to the site. Normally when you do that in a web browser it sends a cookie which, when returned for the next transaction, identifies you as the logged-in user.

If you tried to automate all that you would have to somehow log in, save the cookie, retrieve it when you finished a quest, and send that along with the other information.

Probably what would be easier would be to just save your quest completions to a text file (possibly with markup if your blog/site supports that) and then periodically (eg. each day) upload that to your site.


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #2 on Tue 07 Aug 2018 06:06 PM (UTC)

Amended on Tue 07 Aug 2018 06:09 PM (UTC) by Areadien

Message
Well, this would be a form I would write, so it wouldn't need a cookie, I don't think, since I'm using it just for myself. The form would look something like this:

<form action ="post">
	<input name = "character" type = "text" value = "" /> <input name = "questNum" type = "text" value = "" /> <input name = "time" type = "text" value = "" /> 
</form>
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 07 Aug 2018 08:19 PM (UTC)

Amended on Tue 07 Aug 2018 08:25 PM (UTC) by Nick Gammon

Message
I see. Well, see my post here: http://www.gammon.com.au/forum/?id=12942

In particular the graphic about the format of a HTTP message:



You could use the LuaSocket library, some description here:

https://www.gammon.com.au/forum/?id=4935

Also look here for more recent instructions:

http://www.gammon.com.au/forum/?id=8319&reply=3#reply3

Based on the graphic above, you just need to substitute the path to the processing page, omit the cookie line, change the post parameters, and then connect to the web server, send that message, and disconnect.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #4 on Wed 08 Aug 2018 01:55 AM (UTC)

Amended on Wed 08 Aug 2018 01:56 AM (UTC) by Areadien

Message
OK, so I got it to work in cmd, but how would I parse a form from a lua plugin? Here's a copy of what I got in cmd.

Microsoft Windows [Version 10.0.17134.191]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Windows\System32>cd c:\luasocket

c:\luasocket>lua
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> dofile("lua.lua")
> http = require "http"
> a, b, c = http.request("http://localhost/mm/abby.php")
> print(a, b, c)
Hello World
        200     table: 00A3F930
>
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #5 on Wed 08 Aug 2018 04:37 AM (UTC)
Message
You don't need to parse anything. Just as an analogy, imagine this conversation:


Me: What is your name?
You: My name is Areadien


The point is that PHP forms don't really care if you previously saw the form or not, as each interaction with the server is independent of the other. Thus you could say:


You: My name is Areadien


You can do this without even seeing the question. Thus, you can "fill in" a form, without seeing it. You just send the appropriate data fields (as in the example above: "action=save").

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #6 on Wed 08 Aug 2018 03:27 PM (UTC)
Message
OK, but how would I code that in Lua?
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #7 on Wed 08 Aug 2018 09:08 PM (UTC)

Amended on Wed 08 Aug 2018 09:12 PM (UTC) by Nick Gammon

Message
OK then. I just tested this with MUSHclient. For testing I have a local web server set up, amongst other things it does date calculations. The form in question has two fields named "StartDate" and "Range".

The page URL is: http://10.0.0.2/hms/dates.php

(You won't be able to access that, of course, as it is a private IP address).

That is in this line:


url = "http://10.0.0.2/hms/dates.php",


You can see in the code below the form being "filled in" here:


request_body = fixrequest { StartDate = '1 Jan 2018', Range = '20' }


The URL encoding should handle things like multiple lines, spaces inside a field, and so on.



http = require "socket.http" 

-- converts characters that are not alphanumeric
-- into the form %xx (for use in cookies, forms etc.)
function urlencode (s)
 return (string.gsub (s, "%W", 
        function (str)
          return string.format ("%%%02X", string.byte (str))
        end  ))
end -- function urlencode

-- turn a table of POST values into a url-encoded string
function fixrequest (req)
  local t = { }
  for k, v in pairs (req) do
    table.insert (t, urlencode(k) .. '=' .. urlencode(v))
  end -- for
  return table.concat (t, "&")
end -- fixrequest 

request_body = fixrequest { StartDate = '1 Jan 2018', Range = '20' }
response_body = {}

socket.http.request {
                    url = "http://10.0.0.2/hms/dates.php",
                    method = "POST",
                    headers = {
                              ["Content-Length"] = string.len(request_body),
                              ["Content-Type"] =  "application/x-www-form-urlencoded"
                              },   -- end of headers
                    source = ltn12.source.string(request_body),
                    sink = ltn12.sink.table(response_body)
                    }  -- end of request table

require "tprint"
tprint (response_body)


Effectively this is "posting" the form with the given URL, with the specified fields in "request_body" filled in with the specified values, and then getting the response. If your main aim is for the form to add to a database you could ignore the response, or maybe just check that you haven't got an error message.




If you aren't sure of the field names you can "view page source" in your web browser and check out what is used in the form. For example, in the above case:


<form METHOD="post" ACTION=/hms/dates.php>
Starting date: <input type=text Name="StartDate" size=20 maxlength=20 value="" autofocus>
+/- days or end date: <input type=text Name="Range" size=20 maxlength=20 value="">
<input Type=submit Name=Submit Value="Calculate">
</form>

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #8 on Mon 13 Aug 2018 02:47 PM (UTC)

Amended on Mon 13 Aug 2018 02:49 PM (UTC) by Areadien

Message
How would I call all that from a trigger? Also, can I put the part of that that isn't a function into a function? I want to be able to have the trigger call, say, questUpload() right when I turn in the quest. I would find out the quest number from the alias I type (ex: quest complete 3500) and then have the trigger call upon the quest master saying I successfully completed the quest.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #9 on Mon 13 Aug 2018 08:33 PM (UTC)
Message
If you are using a script file, just put the functions into it, and the part that isn't a function into one of your own, eg.


function questUpload (name, line, wildcards)

-- put stuff here

end -- questUpload


Leave out the last two lines (the tprint stuff), of course, because that is just for debugging. You will need to change the URL and the names of the fields that are to be populated.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #10 on Tue 14 Aug 2018 03:44 AM (UTC)
Message
What would name, line, and wildcards refer to?
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #11 on Tue 14 Aug 2018 04:30 AM (UTC)
Message
If you call a script from a trigger, they are the standard arguments supplied to that script. The name of the trigger, the matching line, and wildcard values. If you don't care about them, just ignore them. Or, in Lua at least, don't even mention them, as Lua functions can ignore extraneous arguments.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #12 on Wed 15 Aug 2018 09:53 PM (UTC)
Message
OK, so I tried this with an alias, and it didn't work. Here's my alias.

<aliases>
	<alias enabled="y" match="^upload$" regexp="y" send_to="12" sequence="100">
		<send>testUpload()</send>
	</alias>
</aliases>


Here's the functions:

function urlencode (s)
	return string.gsub(s, "%W", string.format("%%%02X", string.byte(str)))
end -- function urlencode

function fixrequest(req)
	local t = {}
	for k, v in pairs (req) do
		table.insert(t, urlencode(k) .. '=' .. urlencode(v))
	end -- for
	return table.concat(t, "&")
end -- fixrequest 

function testUpload()
	http = require "socket.http" 
	request_body = fixrequest {character = "Aberdeen", qnum = 43, time = 1534372981}
	response_body = {}
	socket.http.request {
    	url = "http://localhost/mm/abby.php",
		method = "POST",
		headers = {
			["Content-Length"] = string.len(request_body),
			["Content-Type"] =  "application/x-www-form-urlencoded"
		},   -- end of headers
		source = ltn12.source.string(request_body),
		sink = ltn12.sink.table(response_body)
	}  -- end of request table
	cNote("done")
end --function testUpload


And here's my form.

<form action = "" method = "post">
	<input name = "character" type = "text" value = "" /> <input name = "qnum" type = "text" value = "" /> <input name = "completed" type = "text" value = "" />
	<input type = "submit" value = "Submit" />
</form>
Top

Posted by Areadien   USA  (47 posts)  Bio
Date Reply #13 on Wed 15 Aug 2018 10:29 PM (UTC)

Amended on Wed 15 Aug 2018 10:31 PM (UTC) by Nick Gammon

Message
Actually, I thought I should give you my entire php file and the associated table.

<?php
print_r($_POST);
if(!empty($_POST))
{
    $connect = @mysqli_connect('localhost', 'mm_i', 'password', 'mm');
    if($connect === false)
    {
        bn('<p>');
        bn('<span>Error: Unable to connect to MySQL on line 6 in mm/abby.php: ' . mysqli_connect_error() . '</span>');
        bn('</p>');
        bn('<p>');
        exit;
    }
    else
    {
        $result = mysqli_query($connect, "INSERT INTO myquests VALUES(NULL, '{$_POST['character']}', {$_POST['qnum']}, {$_POST['completed']});")
            or die("Could not execute sql query <b>" . htmlentities($query) . "</b> on line $line in mm/abby.php: " . mysqli_error($connect));
        mysqli_close($connect) or die("Could not close server connection on line " . __LINE__ . " in mm/abby.php because: " . mysqli_error());
    }
}?>
<form action = "" method = "post">'
    <input name = "character" type = "text" value = "" /> <input name = "qnum" type = "text" value = "" /> <input name = "completed" type = "text" value = "" /><br />
    <input type = "submit" value = "Submit" />
</form>


And then here's the table:
CREATE TABLE IF NOT EXISTS `myquests` (
  `entry` int(15) NOT NULL AUTO_INCREMENT,
  `charName` varchar(12) NOT NULL,
  `qnum` int(5) NOT NULL,
  `completion` int(10) NOT NULL,
  PRIMARY KEY (`entry`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #14 on Wed 15 Aug 2018 10:38 PM (UTC)
Message
When you say "it didn't work", did you get an error message like this:


Immediate execution
[string "Script file"]:799: bad argument #1 to 'byte' (string expected, got nil)
stack traceback:
        [C]: in function 'byte'
        [string "Script file"]:799: in function 'urlencode'
        [string "Script file"]:805: in function 'fixrequest'
        [string "Script file"]:812: in function 'testUpload'
        [string "Alias: "]:1: in main chunk
Error context in script:
 795 : 
 796 : 
 797 : 
 798 : function urlencode (s)
 799*:  return string.gsub(s, "%W", string.format("%%%02X", string.byte(str)))
 800 : end -- function urlencode
 801 : 
 802 : function fixrequest(req)
 803 :  local t = {}

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


60,083 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.