This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tim's profile picture

Webmessage (0.21)

Started by tim, 14 February 2012 - 11:27 PM
tim #1
Posted 15 February 2012 - 12:27 AM
The API can send and receive messages via a php script on the web.

New features:
  • Uses URL encoding (0.21)
  • Added tUtil api (0.21)
  • Broadcast system using channels (0.2)
  • Web script now supports multiple databases/servers (0.2)
  • Web script now supports password (0.2)
Usage:
com.send(receiverid, message) where the receiver id is the computers id.
com.number() this wil get the number of messages waiting, if none it returns 0 (only the ones sent to this id) (returns int)
com.get() this will get a single message (returns string)
com.announce(channel) call this method with a channel string such as "channel1" and this computer will be added to the mailing list for "channel1"
com.hide(channel) used as above but to remove from a channel
com.onchannels() returns the channels that the computer is registered to
com.broadcast(channel, message) used to broadcast a message channel being something like "channel1" then just a message string

Usage for tUtil:
tUtil.replaceAll(text, bytefind, bytereplace)
tUtil.toBinary(byte)
tUtil.toHex(byte)
tUtil.urlencode(input)

Installation instructions:
Webserver
(1)Copy the contents of web to a server accessible to your minecraft client (ssp) or server (smp)
(2) Make sure that the directory is writable.

Extra: (All configurations at the top of webmessage.php)
Change databases directory
(3) In the web script replace $prefix = ""; with something like $prefix = "db/"; to place them into the folder db
(4) Make sure this directory is writable

Enable multi-server
(5) Change $multiserver = false; to $multiserver = true;
(6) Perform client config (below)

Enable password
(7) Change $password = ""; to $password = "yourpasswordhere";
(8) Perform client config (below)

Minecraft
(1)Place the contents of the api folder in mods/ComputerCraft/lua/rom/apis
(2)Edit the first configurable option in com to point to your webmessage.php

Enable multi-server
(3) Uncomment the local server = "server1" line in com
(4) Change server1 to anything that is only alphanumeric

Enable password
(5) Uncomment the local password = "pass1" line
(6) Change pass1 to your alphanumeric password

Using my server:
I have setup a webmessage.php script
These are the config options the client needs.
local url = "http://users.aber.ac.uk/tis4/computercraft/index.php"
local server = "server1"
Replace server1 with any alphanumeric string.

Notes:
  • The included PHP script creates an sqlite database, so your webserver will need to support that.
  • This is obviously not very secure as this application doesn't really require it (who wants to hack your in-game computers?)
  • Feel free to improve and redistribute
  • Remember I'm new to LUA
Planned features:
Encryption
Passworded script (web)
Broadcast

Examples:
Receive all

if com.number()>0 then
while com.number()>0 do
  print(com.get())
end
else
print("no messages")
end
Send "hello there" to computer with id 13

message="hello there"
print(com.send("13", message))
Add to channel mailing list "channel1"

com.announce("channel1")
Remove from channel "channel1"

com.hide("channel1")
Broadcast to all announced computers in a channel "channel1"

message="hello channel1"
com.broadcast("channel1", message)
Check which channels you're in

print(com.onchannels())

Using StrUtil for encrypted messages:
Send an encrypted message to ID13

key="1234"
message="secret hello"
message=StrUtil.encrypt(message, key)
com.send("13", message)
Receive an encrypted message

key="1234"
message=com.get()
message=StrUtil.decrypt(message, key)
print(message)

Files are attached but alternative download here: http://users.aber.ac.../webmessage.zip
Espen #2
Posted 15 February 2012 - 01:34 AM
Very nice work tim!
Didn't try it out yet, but from the code it looks pretty tidy and structured.
I'm looking forward to your planned features, keep up the good work! :)/>/>

EDIT:
This line here…
if($recid==""||$message=="") die("Error, no id/message");
… means that as soon as either recid OR message is not an empty string, then it will continue.
But this way it is possible to send a message without a receiver.
Now it could be that you did this intentionally in order to implement some kind of broadcast message?
Because then one wouldn't send to a specific address and it would then make sense to not have recid as a necessary parameter.
I'm just wondering, because you didn't seem to have a getBroadcast() or similar in your lua code, but only a get() for a specific computerID.
Is that a planned feature as well?
Edited on 15 February 2012 - 12:49 AM
tim #3
Posted 15 February 2012 - 12:04 PM
Very nice work tim!
Didn't try it out yet, but from the code it looks pretty tidy and structured.
I'm looking forward to your planned features, keep up the good work! :)/>/>

EDIT:
This line here…
if($recid==""||$message=="") die("Error, no id/message");
… means that as soon as either recid OR message is not an empty string, then it will continue.
But this way it is possible to send a message without a receiver.
Now it could be that you did this intentionally in order to implement some kind of broadcast message?
Because then one wouldn't send to a specific address and it would then make sense to not have recid as a necessary parameter.
I'm just wondering, because you didn't seem to have a getBroadcast() or similar in your lua code, but only a get() for a specific computerID.
Is that a planned feature as well?

I don't think you're right about it continuing, I see it as if recid or message is empty then die.
However it did make me notice I didn't check if recid was an int.
Espen #4
Posted 15 February 2012 - 12:12 PM
Very nice work tim!
Didn't try it out yet, but from the code it looks pretty tidy and structured.
I'm looking forward to your planned features, keep up the good work! :P/>/>

EDIT:
This line here…
if($recid==""||$message=="") die("Error, no id/message");
… means that as soon as either recid OR message is not an empty string, then it will continue.
But this way it is possible to send a message without a receiver.
Now it could be that you did this intentionally in order to implement some kind of broadcast message?
Because then one wouldn't send to a specific address and it would then make sense to not have recid as a necessary parameter.
I'm just wondering, because you didn't seem to have a getBroadcast() or similar in your lua code, but only a get() for a specific computerID.
Is that a planned feature as well?

I don't think you're right about it continuing, I see it as if recid or message is empty then die.
However it did make me notice I didn't check if recid was an int.
*Sigh* *slapping myself in the face (…figuratively)*
Yeah, your're right, I derped up there. I mistook the || for 'and', thus for my brain yesterday it looked like:
if($recid=="" and $message=="") die("Error, no id/message");
Sorry for that, my bad. :)/>/>
tim #5
Posted 15 February 2012 - 07:26 PM
Updated, and as far as my plans went, finished. Excluding the inevitable bug fixes.
Espen #6
Posted 15 February 2012 - 08:41 PM
I really like your work, kudos!
As soon as I've got some more time on my hands, I'm definitely gonna dive into this. Playing around with this is gonna be a lot of fun!
Also have to start coding in PHP again, ideas are floating around my brain… so many interesting things and so little time. :D/>/>
tim #7
Posted 15 February 2012 - 09:00 PM
Thanks Espen :D/>/>
FuzzyPurp #8
Posted 16 February 2012 - 05:21 AM
Nice work Tim, i'm grabbing this and trying it out now.