Difference between revisions of "User:Molideus"
Line 1: | Line 1: | ||
{{TOC right}} | {{TOC right}} | ||
− | {{#description2:Mudlet API documentation for functions | + | {{#description2:Mudlet API documentation for functions for managing networking.}} |
− | = | + | = Networking Functions = |
− | + | A collection of functions for managing networking. | |
− | == | + | ==connectToServer== |
− | ; | + | ;connectToServer(host, port, [save]) |
− | : | + | : Connects to a given game. |
− | : | + | ;Parameters: |
− | + | * ''host:'' | |
− | + | : Server domain or IP address. | |
− | : | + | * ''port:'' |
− | + | : Servers port. | |
− | + | * ''save:'' | |
− | + | : (optional, boolean) if provided, saves the new connection parameters in the profile so they'll be used next time you open it. | |
− | |||
− | * '' | ||
− | * '' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {{note}} ''save'' is available in Mudlet 3.2+. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | connectToServer("midnightsun2.org", 3000) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | -- save to disk so these parameters are used next time when opening the profile | |
− | + | connectToServer("midnightsun2.org", 3000, true) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==disconnect== | |
− | + | ;disconnect() | |
− | == | + | : Disconnects you from the game right away. Note that this will ''not'' properly log you out of the game - use an ingame command for that. Such commands vary, but typically QUIT will work. |
− | |||
− | ; | ||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | : See also: [[Manual:Networking_Functions#reconnect|reconnect()]] | ||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | disconnect() | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==downloadFile== |
− | ; | + | ;downloadFile(saveto, url) |
− | : | + | : Downloads the resource at the given url into the saveto location on disk. This does not pause the script until the file is downloaded - instead, it lets it continue right away and downloads in the background. When a download is finished, the [[Manual:Event_Engine#sysDownloadDone|sysDownloadDone]] event is raised (with the saveto location as the argument), or when a download fails, the [[Manual:Event_Engine#sysDownloadError|sysDownloadError]] event is raised with the reason for failure. You may call downloadFile multiple times and have multiple downloads going on at once - but they aren’t guaranteed to be downloaded in the same order that you started them in. |
− | : See also: [[ | + | :See also: [[#getHTTP|getHTTP()]], [[#postHTTP|postHTTP()]], [[#putHTTP|putHTTP()]], [[#deleteHTTP|deleteHTTP()]] |
− | + | [[File:Downloadfile privacy logging.png|frame|For privacy transparency, URLs accessed are logged in the Central Debug Console]] | |
− | |||
− | + | {{note}} Since Mudlet 3.0, https downloads are supported and the actual url that was used for the download is returned - useful in case of redirects. | |
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- just download a file and save it in our profile folder |
− | + | local saveto = getMudletHomeDir().."/dark-theme-mudlet.zip" | |
+ | local url = "http://www.mudlet.org/wp-content/files/dark-theme-mudlet.zip" | ||
+ | downloadFile(saveto, url) | ||
+ | cecho("<white>Downloading <green>"..url.."<white> to <green>"..saveto.."\n") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | A more advanced example that downloads a webpage, reads it, and prints a result from it: | |
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- create a function to parse the downloaded webpage and display a result |
− | + | function downloaded_file(_, filename) | |
+ | -- is the file that downloaded ours? | ||
+ | if not filename:find("achaea-who-count.html", 1, true) then return end | ||
− | -- | + | -- read the contents of the webpage in |
− | + | local f, s, webpage = io.open(filename) | |
− | + | if f then webpage = f:read("*a"); io.close(f) end | |
+ | -- delete the file on disk, don't clutter | ||
+ | os.remove(filename) | ||
− | + | -- parse our downloaded file for the player count | |
− | + | local pc = webpage:match([[Total: (%d+) players online]]) | |
− | + | display("Achaea has "..tostring(pc).." players on right now.") | |
− | : | + | end |
− | + | -- register our function to run on the event that something was downloaded | |
− | + | registerAnonymousEventHandler("sysDownloadDone", "downloaded_file") | |
− | + | -- download a list of fake users for a demo | |
− | + | downloadFile(getMudletHomeDir().."/achaea-who-count.html", "https://www.achaea.com/game/who") | |
− | -- | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Result should look like this: | |
− | |||
− | : | + | [[File:DownloadFile_demo.png|alt=|1000x1000px]]. |
− | : See also: [[Manual: | + | ==getConnectionInfo== |
+ | ;getConnectionInfo() | ||
+ | :Returns the server address and port that you're currently connected to, and (in Mudlet 4.12+) <code>true</code> or <code>false</code> indicating if you're currently connected to a game. | ||
+ | :See also: [[Manual:Networking_Functions#connectToServer|connectToServer()]] | ||
− | + | {{MudletVersion|4.2}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | local host, port, connected = getConnectionInfo() | |
− | + | cecho(string.format("<light_grey>Playing on <forest_green>%s:%s<light_grey>, currently connected? <forest_green>%s\n", host, port, tostring(connected))) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | -- echo the new connection parameters whenever we connect to a different host with connectToServer() | |
+ | function echoInfo() | ||
+ | local host, port = getConnectionInfo() | ||
+ | cecho(string.format("<light_grey>Now connected to <forest_green>%s:%s\n", host, port)) | ||
+ | end | ||
− | + | registerAnonymousEventHandler("sysConnectionEvent", "echoInfo") | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | : | + | ==getIrcChannels== |
+ | ;getIrcChannels() | ||
+ | :Returns a list of channels the IRC client is joined to as a lua table. If the client is not yet started the value returned is loaded from disk and represents channels the client will auto-join when started. | ||
+ | :See also: [[Manual:Networking_Functions#setIrcChannels|setIrcChannels()]] | ||
− | + | {{MudletVersion|3.3}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | {{MudletVersion| | ||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | display(getIrcChannels()) | |
+ | -- Prints: {"#mudlet", "#lua"} | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==getIrcConnectedHost== |
− | ; | + | ;getIrcConnectedHost() |
− | + | :Returns true+host where host is a string containing the host name of the IRC server, as given to the client by the server while starting the IRC connection. If the client has not yet started or finished connecting this will return false and an empty string. | |
− | : | ||
− | |||
− | |||
− | + | :This function can be particularly useful for testing if the IRC client has connected to a server prior to sending data, and it will not auto-start the IRC client.<br /> | |
+ | :The ''hostname'' value this function returns can be used to test if [[Manual:Event_Engine#sysIrcMessage|sysIrcMessage]] events are sent from the server or a user on the network. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | local | + | local status, hostname = getIrcConnectedHost() |
− | if | + | |
− | + | if status == true then | |
− | else | + | -- do something with connected IRC, send IRC commands, store 'hostname' elsewhere. |
− | + | -- if sysIrcMessage sender = hostname from above, message is likely a status, command response, or an error from the Server. | |
+ | else | ||
+ | -- print a status, change connection settings, or just continue waiting to send IRC data. | ||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | {{MudletVersion|3.3}} | |
− | |||
− | : | + | ==getIrcNick== |
+ | ;getIrcNick() | ||
+ | :Returns a string containing the IRC client nickname. If the client is not yet started, your default nickname is loaded from IRC client configuration. | ||
+ | :See also: [[Manual:Networking_Functions#setIrcNick|setIrcNick()]] | ||
− | + | {{MudletVersion|3.3}} | |
− | |||
− | |||
− | |||
− | |||
+ | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | local nick = getIrcNick() | |
− | + | echo(nick) | |
− | + | -- Prints: "Sheldor" | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | : See also: [[Manual: | + | ==getIrcServer== |
+ | ;getIrcServer() | ||
+ | :Returns the IRC client server name and port as a string and a number respectively. If the client is not yet started your default server is loaded from IRC client configuration. | ||
+ | :See also: [[Manual:Networking_Functions#setIrcServer|setIrcServer()]] | ||
− | {{MudletVersion| | + | {{MudletVersion|3.3}} |
− | + | ;Example | |
− | |||
− | |||
− | ; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | local server, port = getIrcServer() | |
+ | echo("server: "..server..", port: "..port.."\n") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==getNetworkLatency== |
− | ; | + | ; getNetworkLatency() |
− | : | + | : Returns the last measured response time between the sent command and the server reply in seconds - e.g. 0.058 (=58 milliseconds lag) or 0.309 (=309 milliseconds). This is the ''N:'' number you see bottom-right of Mudlet. |
− | + | Also known as server lag. | |
− | ; | + | ;Example |
− | + | ''Need example'' | |
− | |||
− | + | ==openUrl== | |
− | + | ;openUrl (url) | |
− | + | :Opens the default OS browser for the given URL. | |
− | |||
− | |||
− | |||
− | == | ||
− | ; | ||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | ;Example: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | openUrl("http://google.com") | |
− | + | openUrl("www.mudlet.org") | |
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==reconnect== |
− | ; | + | ;reconnect() |
− | : | + | : Force-reconnects (so if you're connected, it'll disconnect) you to the game. |
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- you could trigger this on a log out message to reconnect, if you'd like |
− | + | reconnect() | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==restartIrc== | |
+ | ;restartIrc() | ||
+ | :Restarts the IRC client connection, reloading configurations from disk before reconnecting the IRC client. | ||
− | + | {{MudletVersion|3.3}} | |
− | |||
− | |||
− | + | ==sendAll== | |
+ | ; sendAll(list of things to send, [echo back or not]) | ||
+ | : sends multiple things to the game. If you'd like the commands not to be shown, include ''false'' at the end. | ||
− | + | :See also: [[Manual:Basic_Essentials#send|send()]] | |
− | |||
− | : | ||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- instead of using many send() calls, you can use one sendAll |
− | + | sendAll("outr paint", "outr canvas", "paint canvas") | |
+ | -- can also have the commands not be echoed | ||
+ | sendAll("hi", "bye", false) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==sendATCP== |
− | ; | + | ;sendATCP(message, what) |
− | : | + | : ''Need description'' |
− | : See also: [[# | + | : See also: [[Manual:Supported_Protocols#ATCP|ATCP Protocol]], [[Manual:ATCP_Extensions|ATCP Extensions]], [http://www.ironrealms.com/rapture/manual/files/FeatATCP-txt.html Achaea Telnet Client Protocol specification], [https://forums.mudlet.org/viewtopic.php?p=19502#p19502 Description by forum user KaVir (2013)], [https://forums.mudlet.org/viewtopic.php?p=2015#p2015 Description by forum user Iocun (2009)] |
− | ;Parameters | + | ;Parameters: |
− | * '' | + | * ''message:'' |
− | : | + | : The message that you want to send. |
+ | * ''what:'' | ||
+ | : ''Need description'' | ||
;Example | ;Example | ||
− | + | ''Need example'' | |
− | |||
− | |||
− | |||
− | == | + | ==sendGMCP== |
− | ; | + | ;sendGMCP(command) |
− | : | + | : Sends a GMCP message to the server. The [http://www.ironrealms.com/gmcp-doc IRE document on GMCP] has information about what can be sent, and what tables it will use, etcetera. |
+ | : Note that this function is rarely used in practice. For most GMCP modules, the messages are automatically sent by the server when a relevant event happens in the game. For example, LOOKing in your room prompts the server to send the room description and contents, as well as the GMCP message gmcp.Room. A call to sendGMCP would not be required in this case. | ||
− | : | + | : When playing an IRE game, a call to <code>send(" ")</code> afterwards is necessary due to a bug in the game with compression (MCCP) is enabled. |
− | + | : See also: [[Manual:Scripting#GMCP|GMCP Scripting for Discord status]] | |
− | |||
− | : | ||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | --This would send "Core.KeepAlive" to the server, which resets the timeout |
− | + | sendGMCP("Core.KeepAlive") | |
− | |||
− | + | --This would send a request for the server to send an update to the gmcp.Char.Skills.Groups table. | |
− | + | sendGMCP("Char.Skills.Get {}") | |
− | |||
− | + | --This would send a request for the server to send a list of the skills in the | |
− | + | --vision group to the gmcp.Char.Skills.List table. | |
− | |||
− | + | sendGMCP("Char.Skills.Get " .. yajl.to_string{group = "vision"}) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | --And finally, this would send a request for the server to send the info for | |
− | + | --hide in the woodlore group to the gmcp.Char.Skills.Info table | |
− | |||
− | + | sendGMCP("Char.Skills.Get " .. yajl.to_string{group="MWP", name="block"}) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==sendMSDP== | |
+ | ;sendMSDP(variable[, value][, value...]) | ||
+ | : Sends a MSDP message to the server. | ||
− | + | ;Parameters: | |
− | ; | + | * ''variable:'' |
− | : | + | : The variable, in MSDP terms, that you want to request from the server. |
− | + | * ''value:'' | |
− | + | : The variables value that you want to request. You can request more than one value at a time. | |
− | * '' | ||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | * '' | ||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | + | : See Also: [[Manual:Supported_Protocols#MSDP|MSDP support in Mudlet]], [http://tintin.sourceforge.net/msdp/ Mud Server Data Protocol specification] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | -- ask for a list of commands, lists, and reportable variables that the server supports | |
− | + | sendMSDP("LIST", "COMMANDS", "LISTS", "REPORTABLE_VARIABLES") | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | -- | + | -- ask the server to start keeping you up to date on your health |
− | + | sendMSDP("REPORT", "HEALTH") | |
− | |||
− | |||
− | |||
− | |||
− | + | -- or on your health and location | |
− | + | sendMSDP("REPORT", "HEALTH", "ROOM_VNUM", "ROOM_NAME") | |
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==sendIrc== | |
+ | ;sendIrc(target, message) | ||
+ | : Sends a message to an IRC channel or person. Returns ''true''+''status'' if message could be sent or was successfully processed by the client, or ''nil''+''error'' if the client is not ready for sending, and ''false''+''status'' if the client filtered the message or failed to send it for some reason. If the IRC client hasn't started yet, this function will initiate the IRC client and begin a connection. | ||
− | + | To receive an IRC message, check out the [[Manual:Event_Engine#sysIrcMessage|sysIrcMessage event]]. | |
− | + | {{note}} Since Mudlet 3.3, auto-opens the IRC window and returns the success status. | |
− | |||
− | + | ;Parameters: | |
− | + | * ''target:'' | |
− | ;Parameters | + | : nick or channel name and if omitted will default to the first available channel in the list of joined channels. |
− | * '' | + | * ''message:'' |
− | : | + | : The message to send, may contain IRC client commands which start with <code>/</code> and can use all commands which are available through the client window. |
− | * '' | ||
− | :The | ||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- this would send "hello from Mudlet!" to the channel #mudlet on freenode.net |
− | + | sendIrc("#mudlet", "hello from Mudlet!") | |
− | + | -- this would send "identify password" in a private message to Nickserv on freenode.net | |
− | + | sendIrc("Nickserv", "identify password") | |
− | |||
− | |||
− | |||
− | |||
− | + | -- use an in-built IRC command | |
− | -- | + | sendIrc("#mudlet", "/topic") |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | {{note}} The following IRC commands are available since Mudlet 3.3: | |
− | + | * /ACTION <target> <message...> | |
− | + | * /ADMIN (<server>) | |
− | + | * /AWAY (<reason...>) | |
+ | * /CLEAR (<buffer>) -- Clears the text log for the given buffer name. Uses the current active buffer if none are given. | ||
+ | * /CLOSE (<buffer>) -- Closes the buffer and removes it from the Buffer list. Uses the current active buffer if none are given. | ||
+ | * /HELP (<command>) -- Displays some help information about a given command or lists all available commands. | ||
+ | * /INFO (<server>) | ||
+ | * /INVITE <user> (<#channel>) | ||
+ | * /JOIN <#channel> (<key>) | ||
+ | * /KICK (<#channel>) <user> (<reason...>) | ||
+ | * /KNOCK <#channel> (<message...>) | ||
+ | * /LIST (<channels>) (<server>) | ||
+ | * /ME [target] <message...> | ||
+ | * /MODE (<channel/user>) (<mode>) (<arg>) | ||
+ | * /MOTD (<server>) | ||
+ | * /MSG <target> <message...> -- Sends a message to target, can be used to send Private messages. | ||
+ | * /NAMES (<#channel>) | ||
+ | * /NICK <nick> | ||
+ | * /NOTICE <#channel/user> <message...> | ||
+ | * /PART (<#channel>) (<message...>) | ||
+ | * /PING (<user>) | ||
+ | * /RECONNECT -- Issues a Quit command to the IRC Server and closes the IRC connection then reconnects to the IRC server. The same as calling ircRestart() in Lua. | ||
+ | * /QUIT (<message...>) | ||
+ | * /QUOTE <command> (<parameters...>) | ||
+ | * /STATS <query> (<server>) | ||
+ | * /TIME (<user>) | ||
+ | * /TOPIC (<#channel>) (<topic...>) | ||
+ | * /TRACE (<target>) | ||
+ | * /USERS (<server>) | ||
+ | * /VERSION (<user>) | ||
+ | * /WHO <mask> | ||
+ | * /WHOIS <user> | ||
+ | * /WHOWAS <user> | ||
− | {{ | + | {{note}} The following IRC commands are available since Mudlet 3.15: |
+ | * /MSGLIMIT <limit> (<buffer>) -- Sets the limit for messages to keep in the IRC client message buffers and saves this setting. If a specific buffer/channel name is given the limit is not saved and applies to the given buffer until the application is closed or the limit is changed again. For this reason, global settings should be applied first, before settings for specific channels/PM buffers. | ||
− | ; | + | ==sendTelnetChannel102== |
− | + | ; sendTelnetChannel102(msg) | |
+ | : Sends a message via the 102 subchannel back to the game (that's used in Aardwolf). The msg is in a two byte format; see the link below to the Aardwolf Wiki for how that works. | ||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- turn prompt flags on: |
− | + | sendTelnetChannel102("\52\1") | |
− | -- | + | -- turn prompt flags off: |
− | + | sendTelnetChannel102("\52\2") | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | To see the list of options that Aardwolf supports go to: [http://www.aardwolf.com/blog/2008/07/10/telnet-negotiation-control-mud-client-interaction/ Using Telnet negotiation to control MUD client interaction]. | |
− | |||
− | |||
− | |||
− | ; | + | ==setIrcChannels== |
− | + | ;setIrcChannels(channels) | |
− | + | :Saves the given channels to disk as the new IRC client channel auto-join configuration. This value is not applied to the current active IRC client until it is restarted with [[Manual:Networking_Functions#restartIrc|restartIrc()]] | |
− | + | :See also: [[Manual:Networking_Functions#getIrcChannels|getIrcChannels()]], [[Manual:Networking_Functions#restartIrc|restartIrc()]] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ;Parameters: | |
− | + | * ''channels:'' | |
− | + | : A table containing strings which are valid channel names. Any channels in the list which aren't valid are removed from the list. | |
− | |||
− | |||
− | |||
− | |||
− | + | {{MudletVersion|3.3}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | {{MudletVersion| | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | setIrcChannels( {"#mudlet", "#lua", "irc"} ) | |
− | + | -- Only the first two will be accepted, as "irc" is not a valid channel name. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==setIrcNick== |
− | ; | + | ;setIrcNick(nickname) |
+ | :Saves the given nickname to disk as the new IRC client configuration. This value is not applied to the current active IRC client until it is restarted with restartIrc() | ||
+ | :See also: [[Manual:Networking_Functions#getIrcNick|getIrcNick()]], [[Manual:Networking_Functions#restartIrc|restartIrc()]] | ||
− | : | + | ;Parameters: |
+ | * ''nickname:'' | ||
+ | : A string with your new desired name in IRC. | ||
− | {{MudletVersion| | + | {{MudletVersion|3.3}} |
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | setIrcNick( "Sheldor" ) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==setIrcServer== |
− | ; | + | ;setIrcServer(hostname, port[, secure]) |
+ | :Saves the given server's address to disk as the new IRC client connection configuration. These values are not applied to the current active IRC client until it is restarted with restartIrc() | ||
+ | :See also: [[Manual:Networking_Functions#getIrcServer|getIrcServer()]], [[Manual:Networking_Functions#restartIrc|restartIrc()]] | ||
− | : | + | ;Parameters: |
− | : | + | * ''hostname:'' |
− | + | : A string containing the hostname of the IRC server. | |
− | + | * ''port:'' | |
− | + | : (optional) A number indicating the port of the IRC server. Defaults to 6667, if not provided. | |
− | + | * ''secure:'' | |
− | + | : (optional) Boolean, true if server uses Transport Layer Security. Defaults to false. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | : | ||
− | + | {{MudletVersion|3.3}} | |
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | setIrcServer("irc.libera.chat", 6667) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==getHTTP== |
− | ; | + | ;getHTTP(url, headersTable) |
− | : | + | : Sends an [https://en.wikipedia.org/wiki/GET_(HTTP) HTTP GET] request to the given URL. Raises [[Manual:Event_Engine#sysGetHttpDone|sysGetHttpDone]] on success or [[Manual:Event_Engine#sysGetHttpError|sysGetHttpError]] on failure. |
− | + | :See also: [[#downloadFile|downloadFile()]]. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | [[File:Posthttp privacy logging.png.png|frame|For privacy transparency, URLs accessed are logged in the Central Debug Console]] | |
− | ;Parameters | + | ;Parameters: |
− | * '' | + | * ''url:'' |
− | : | + | : Location to send the request to. |
− | + | * ''headersTable:'' | |
− | + | : (optional) table of headers to send with your request. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {{MudletVersion|4.10}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | {{MudletVersion|4. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Examples | ;Examples | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | function | + | function onHttpGetDone(_, url, body) |
− | + | cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body)) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | < | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | registerAnonymousEventHandler("sysGetHttpDone", onHttpGetDone) | |
− | |||
− | |||
− | -- | + | getHTTP("https://httpbin.org/info") |
− | + | getHTTP("https://httpbin.org/are_you_awesome", {["X-am-I-awesome"] = "yep I am"}) | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- Status requests typically use GET requests |
− | + | local url = "http://postman-echo.com/status" | |
− | + | local header = {["Content-Type"] = "application/json"} | |
− | |||
− | { | ||
− | |||
− | = | ||
− | |||
− | + | -- first we create something to handle the success, and tell us what we got | |
− | + | registerAnonymousEventHandler('sysGetHttpDone', function(event, rurl, response) | |
− | + | if rurl == url then display(r) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler | |
− | + | end, true) -- this sets it to delete itself after it fires | |
− | + | -- then we create something to handle the error message, and tell us what went wrong | |
− | + | registerAnonymousEventHandler('sysGetHttpError', function(event, response, rurl) | |
− | + | if rurl == url then display(r) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler | |
− | + | end, true) -- this sets it to delete itself after it fires | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | -- Lastly, we make the request: | |
− | + | getHTTP(url, header) | |
− | -- | ||
− | |||
− | -- | + | -- Pop this into an alias and try it yourself! |
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==postHTTP== |
− | ; | + | ;postHTTP(dataToSend, url, headersTable, file) |
− | : | + | : Sends an [https://en.wikipedia.org/wiki/POST_(HTTP) HTTP POST] request to the given URL, either as text or with a specific file you'd like to upload. Raises [[Manual:Event_Engine#sysPostHttpDone|sysPostHttpDone]] on success or [[Manual:Event_Engine#sysPostHttpError|sysPostHttpError]] on failure. |
+ | :See also: [[#downloadFile|downloadFile()]], [[#getHTTP|getHTTP()]], [[#putHTTP|putHTTP()]], [[#deleteHTTP|deleteHTTP()]]. | ||
− | + | [[File:Posthttp privacy logging.png.png|frame|For privacy transparency, URLs accessed are logged in the Central Debug Console]] | |
;Parameters: | ;Parameters: | ||
− | * '' | + | * ''dataToSend:'' |
− | * '' | + | : Text to send in the request (unless you provide a file to upload). |
− | * '' | + | * ''url:'' |
− | + | : Location to send the request to. | |
− | + | * ''headersTable:'' | |
− | + | : (optional) table of headers to send with your request. | |
− | + | * ''file:'' | |
− | + | : (optional) file to upload as part of the POST request. If provided, this will replace 'dataToSend'. | |
− | + | : If you use a scripting language (ex. PHP) to handle this post, remember that the file is sent as raw data. Expecially no field name is provided, dispite it works in common html post. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | * '' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {{MudletVersion|4.1}} | |
− | |||
− | |||
− | |||
− | |||
;Examples | ;Examples | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | function onHttpPostDone(_, url, body) | |
− | + | cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body)) | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | registerAnonymousEventHandler("sysPostHttpDone", onHttpPostDone) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | postHTTP("why hello there!", "https://httpbin.org/post") | |
− | + | postHTTP("this us a request with custom headers", "https://httpbin.org/post", {["X-am-I-awesome"] = "yep I am"}) | |
− | + | postHTTP(nil, "https://httpbin.org/post", {}, "<fill in file location to upload here, maybe get from invokeDialog>") | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- create a | + | -- This will create a JSON message body. Many modern REST APIs expect a JSON body. |
− | + | local url = "http://postman-echo.com/post" | |
− | + | local data = {message = "I am the banana", user = "admin"} | |
− | + | local header = {["Content-Type"] = "application/json"} | |
− | -- | + | -- first we create something to handle the success, and tell us what we got |
− | + | registerAnonymousEventHandler('sysPostHttpDone', function(event, rurl, response) | |
− | + | if rurl == url then display(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler | |
+ | end, true) -- this sets it to delete itself after it fires | ||
− | + | -- then we create something to handle the error message, and tell us what went wrong | |
+ | registerAnonymousEventHandler('sysPostHttpError', function(event, response, rurl) | ||
+ | if rurl == url then display(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler | ||
+ | end, true) -- this sets it to delete itself after it fires | ||
− | + | -- Lastly, we make the request: | |
+ | postHTTP(yajl.to_string(data), url, header) -- yajl.to_string converts our Lua table into a JSON-like string so the server can understand it | ||
− | + | -- Pop this into an alias and try it yourself! | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ;HTTP Basic Authentication Example: | |
+ | If your HTTP endpoint requires authentication to post data, HTTP Basic Authentication is a common method for doing so. There are two ways to do so. | ||
− | + | OPTION 1: URL encoding: | |
+ | Many HTTP servers allow you to enter a HTTP Basic Authentication username and password at the beginning of the URL itself, in format: | ||
+ | https://username:password@domain.com/path/to/endpoint | ||
− | + | OPTION 2: Authorization Header: | |
− | + | Some HTTP servers may require you to put your Basic Authentication into the 'Authorization' HTTP header value. | |
− | |||
− | + | This requires encoding the username:password into base64. | |
− | + | For example, if your username is 'user' and your password is '12345', you'd need to run the string "user:12345" through a base64 encoder, which would result in the string: | |
− | + | dXNlcjoxMjM0NQ== | |
− | |||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | : | ||
− | |||
− | |||
− | + | Then, you'd need to set the HTTP header 'Authorization' field value to indicate it is using Basic auth and inserting the base64 string as: | |
− | + | ['Authorization'] = "Basic dXNlcjoxMjM0NQ==" | |
− | + | As you're encoding your username and password, you probably want to do this encoding locally for security reasons. You also probably want to only use https:// and not http:// when sending usernames and passwords over the internet. | |
− | + | In the HTTP Basic Authentication example below, there is an inline base64Encode() function included: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | function base64Encode(data) | |
− | < | + | -- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de> |
+ | -- licensed under the terms of the LGPL2 | ||
+ | local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
+ | return ((data:gsub('.', function(x) | ||
+ | local r,b='',x:byte() | ||
+ | for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end | ||
+ | return r; | ||
+ | end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x) | ||
+ | if (#x < 6) then return '' end | ||
+ | local c=0 | ||
+ | for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end | ||
+ | return b:sub(c+1,c+1) | ||
+ | end)..({ '', '==', '=' })[#data%3+1]) | ||
+ | end | ||
+ | -- Example: base64Encode("user:12345") -> dXNlcjoxMjM0NQ== | ||
− | == | + | function postJSON(url,dataTable,headerTable) |
− | ; | + | -- This will create a JSON message body. Many modern REST APIs expect a JSON body. |
+ | local data = dataTable or {text = "hello world"} | ||
+ | local header = headerTable or {["Content-Type"] = "application/json"} | ||
+ | -- first we create something to handle the success, and tell us what we got | ||
+ | registerAnonymousEventHandler('sysPostHttpDone', function(event, rurl, response) | ||
+ | if rurl == url then sL("HTTP response success"); echo(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler | ||
+ | end, true) -- this sets it to delete itself after it fires | ||
+ | -- then we create something to handle the error message, and tell us what went wrong | ||
+ | registerAnonymousEventHandler('sysPostHttpError', function(event, response, rurl) | ||
+ | if rurl == url then sL("HTTP response error",3); echo(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler | ||
+ | end, true) -- this sets it to delete itself after it fires | ||
+ | -- Lastly, we make the request: | ||
+ | postHTTP(yajl.to_string(data), url, header) -- yajl.to_string converts our Lua table into a JSON-like string so the server can understand it | ||
+ | end | ||
− | + | data = { | |
− | + | message = "I am the banana", | |
− | + | user = "admin" | |
− | + | } | |
− | + | header = { | |
− | + | ["Content-Type"] = "application/json", | |
− | + | ["Authorization"] = "Basic " .. base64Encode("user:12345") | |
− | + | } | |
− | + | postJSON("http://postman-echo.com/post",data,header) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ;URL Encoding vs JSON encoding | |
− | ; | + | Some HTTP endpoints may not support JSON encoding, and instead may require URL encoding. Here's an example function to convert your lua data table into a URL encoded string:: |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- Example: URLEncodeTable({message="hello",person="world"}) -> "message=hello&person=world" |
− | |||
− | -- | + | function URLEncodeTable(Args) |
− | + | -- From: https://help.interfaceware.com/code/details/urlcode-lua | |
− | + | ---------------------------------------------------------------------------- | |
− | + | -- URL-encode the elements of a table creating a string to be used in a | |
− | + | -- URL for passing data/parameters to another script | |
− | -- | + | -- @param args Table where to extract the pairs (name=value). |
− | -- | + | -- @return String with the resulting encoding. |
− | + | ---------------------------------------------------------------------------- | |
− | function | + | -- |
− | + | local ipairs, next, pairs, tonumber, type = ipairs, next, pairs, tonumber, type | |
− | + | local string = string | |
− | end | + | local table = table |
− | + | ||
− | -- | + | --helper functions: |
− | function | + | ---------------------------------------------------------------------------- |
− | + | -- Decode an URL-encoded string (see RFC 2396) | |
− | + | ---------------------------------------------------------------------------- | |
− | + | local unescape = function (str) | |
+ | str = string.gsub (str, "+", " ") | ||
+ | str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) | ||
+ | return str | ||
+ | end | ||
+ | |||
+ | ---------------------------------------------------------------------------- | ||
+ | -- URL-encode a string (see RFC 2396) | ||
+ | ---------------------------------------------------------------------------- | ||
+ | local escape = function (str) | ||
+ | str = string.gsub (str, "([^0-9a-zA-Z !'()*._~-])", -- locale independent | ||
+ | function (c) return string.format ("%%%02X", string.byte(c)) end) | ||
+ | str = string.gsub (str, " ", "+") | ||
+ | return str | ||
+ | end | ||
+ | |||
+ | ---------------------------------------------------------------------------- | ||
+ | -- Insert a (name=value) pair into table [[args]] | ||
+ | -- @param args Table to receive the result. | ||
+ | -- @param name Key for the table. | ||
+ | -- @param value Value for the key. | ||
+ | -- Multi-valued names will be represented as tables with numerical indexes | ||
+ | -- (in the order they came). | ||
+ | ---------------------------------------------------------------------------- | ||
+ | local insertfield = function (args, name, value) | ||
+ | if not args[name] then | ||
+ | args[name] = value | ||
+ | else | ||
+ | local t = type (args[name]) | ||
+ | if t == "string" then | ||
+ | args[name] = {args[name],value,} | ||
+ | elseif t == "table" then | ||
+ | table.insert (args[name], value) | ||
+ | else | ||
+ | error ("CGILua fatal error (invalid args table)!") | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | -- end helper functions | ||
+ | |||
+ | if Args == nil or next(Args) == nil then -- no args or empty args? | ||
+ | return "" | ||
+ | end | ||
+ | local strp = "" | ||
+ | for key, vals in pairs(Args) do | ||
+ | if type(vals) ~= "table" then | ||
+ | vals = {vals} | ||
+ | end | ||
+ | for i,val in ipairs(vals) do | ||
+ | strp = strp.."&"..key.."="..escape(val) | ||
+ | end | ||
end | end | ||
+ | -- remove first & | ||
+ | return string.sub(strp,2) | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==putHTTP== | |
+ | ;putHTTP(dataToSend, url, [headersTable], [file]) | ||
+ | : Sends an [https://en.wikipedia.org/wiki/PUT_(HTTP) HTTP PUT] request to the given URL, either as text or with a specific file you'd like to upload. Raises [[Manual:Event_Engine#sysPutHttpDone|sysPutHttpDone]] on success or [[Manual:Event_Engine#sysPutHttpError|sysPutHttpError]] on failure. | ||
+ | :See also: [[#downloadFile|downloadFile()]], [[#getHTTP|getHTTP()]], [[#postHTTP|postHTTP()]], [[#deleteHTTP|deleteHTTP()]]. | ||
− | + | [[File:PutHTTP privacy logging.png|frame|For privacy transparency, URLs accessed are logged in the Central Debug Console]] | |
− | + | ;Parameters: | |
− | + | * ''dataToSend:'' | |
+ | : Text to send in the request (unless you provide a file to upload). | ||
+ | * ''url:'' | ||
+ | : Location to send the request to. | ||
+ | * ''headersTable:'' | ||
+ | : (optional) table of headers to send with your request. | ||
+ | * ''file:'' | ||
+ | : (optional) file to upload as part of the PUT request. If provided, this will replace 'dataToSend'. | ||
− | + | {{MudletVersion|4.1}} | |
− | |||
− | |||
− | |||
− | {{MudletVersion|4. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | function onHttpPutDone(_, url, body) | |
− | + | cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body)) | |
− | + | end | |
− | |||
− | |||
− | -- | + | registerAnonymousEventHandler("sysPutHttpDone", onHttpPutDone) |
− | + | putHTTP("this us a request with custom headers", "https://httpbin.org/put", {["X-am-I-awesome"] = "yep I am"}) | |
+ | putHTTP("https://httpbin.org/put", "<fill in file location to upload here>") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==deleteHTTP== |
− | ; | + | ;deleteHTTP(url, headersTable) |
+ | : Sends an [https://en.wikipedia.org/wiki/DELETE_(HTTP) HTTP DELETE] request to the given URL. Raises [[Manual:Event_Engine#sysDeleteHttpDone|sysDeleteHttpDone]] on success or [[Manual:Event_Engine#sysDeleteHttpError|sysDeleteHttpError]] on failure. | ||
+ | :See also: [[#downloadFile|downloadFile()]], [[#getHTTP|getHTTP()]], [[#postHTTP|postHTTP()]], [[#putHTTP|putHTTP()]]. | ||
− | : | + | [[File:PutHTTP privacy logging.png|frame|For privacy transparency, URLs accessed are logged in the Central Debug Console]] |
− | |||
− | + | ;Parameters: | |
− | + | * ''url:'' | |
− | ; | + | : Location to send the request to. |
− | + | * ''headersTable:'' | |
− | + | : (optional) table of headers to send with your request. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {{MudletVersion|4.1}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | function onHttpDeleteDone(_, url, body) | |
− | + | cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body)) | |
− | cecho( | ||
− | |||
− | |||
end | end | ||
− | |||
− | + | registerAnonymousEventHandler("sysDeleteHttpDone", onHttpDeleteDone) | |
− | |||
− | |||
− | |||
− | |||
− | + | deleteHTTP("https://httpbin.org/delete") | |
− | + | deleteHTTP("https://httpbin.org/delete", {["X-am-I-awesome"] = "yep I am"}) | |
− | |||
− | |||
− | : | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | -- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | ==customHTTP== |
− | ; | + | ;customHTTP(method, url, headersTable) |
− | : | + | : Sends an custom method request to the given URL. Raises [[Manual:Event_Engine#sysCustomHttpDone|sysCustomHttpDone]] on success or [[Manual:Event_Engine#sysCustomHttpError|sysCustomHttpError]] on failure. |
− | : | + | :See also: [[#downloadFile|downloadFile()]], [[#getHTTP|getHTTP()]], [[#postHTTP|postHTTP()]], [[#putHTTP|putHTTP()]], [[#deleteHTTP|deleteHTTP()]]. |
− | ;Parameters | + | ;Parameters: |
− | * '' | + | * ''method:'' |
− | : ( | + | : Http method to use (eg. PATCH, HEAD etc.) |
− | * '' | + | * ''dataToSend:'' |
− | : | + | : Text to send in the request (unless you provide a file to upload). |
− | + | * ''url:'' | |
− | * '' | + | : Location to send the request to. |
− | : | + | * ''headersTable:'' |
+ | : (optional) table of headers to send with your request. | ||
+ | * ''file:'' | ||
+ | : (optional) file to upload as part of the PUT request. If provided, this will replace 'dataToSend'. | ||
− | + | {{MudletVersion|4.11}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | {{MudletVersion|4. | ||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | function onCustomHttpDone(_, url, body, method) | |
− | + | cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s<white>, method: <dark_green>%s", url, body, method)) | |
− | + | end | |
− | + | registerAnonymousEventHandler("sysCustomHttpDone", sysCustomHttpDone) | |
− | |||
− | |||
− | |||
− | |||
− | + | customHTTP("PATCH", "this us a request with custom headers", "https://httpbin.org/put", {["X-am-I-awesome"] = "yep I am"}) | |
− | + | customHTTP("PATCH", "https://httpbin.org/put", "<fill in file location to upload here>") | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ==sendSocket== | |
− | |||
− | |||
− | + | ;sendSocket(data) | |
− | ; | ||
− | + | :Sends given binary data as-is (or with some predefined special tokens converted to byte values) to the game. You can use this to implement support for a [[Manual:Supported_Protocols#Adding_support_for_a_telnet_protocol|new telnet protocol]], [http://forums.mudlet.org/viewtopic.php?f=5&t=2272 simultronics] [http://forums.mudlet.org/viewtopic.php?f=5&t=2213#p9810 login] etc. | |
− | |||
− | |||
− | ; | + | ; success = sendSocket("data") |
− | |||
− | : | + | ;See also: [[Manual:Lua_Functions#feedTelnet|feedTelnet()]], [[Manual:Lua_Functions#feedTriggers|feedTriggers()]] |
− | {{note}} | + | {{note}} Modified in Mudlet '''tbd''' to accept some tokens like "''<NUL>''" to include byte values that are not possible to insert with the standard Lua string escape "''\###''" form where ### is a three digit number between 000 and 255 inclusive or where the value is more easily provided via a mnemonic. For the table of the tokens that are known about, see the one in [[Manual:Lua_Functions#feedTelnet|feedTelnet()]]. |
− | + | {{note}} The data (as bytes) once the tokens have been converted to their byte values is sent as is to the Game Server; any encoding to, say, a UTF-8 representation or to duplicate ''0xff'' byte values so they are not considered to be Telnet ''<IAC>'' (Interpret As Command) bytes must be done to the data prior to calling this function. | |
− | ; | ||
− | |||
;Parameters | ;Parameters | ||
− | * '' | + | * ''data:'' |
− | + | : String containing the bytes to send to the Game Server possibly containing some tokens that are to be converted to bytes as well. | |
− | ; | + | ;Returns |
− | + | * (Only since Mudlet '''tbd''') Boolean ''true'' if the whole data string (after token replacement) was sent to the Server, ''false'' if that failed for any reason (including if the Server has not been connected or is now disconnected). ''nil'' and an error message for any other defect. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | * | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | -- Tell the Server that we are now willing and able to process to process Ask the Server to a comment explaining what is going on, if there is multiple functionalities (or optional parameters) the examples should start simple and progress in complexity if needed! | |
− | + | -- the examples should be small, but self-contained so new users can copy & paste immediately without going diving in lots other function examples first. | |
+ | -- comments up top should introduce / explain what it does | ||
− | = | + | local something = function(exampleValue) |
− | + | if something then | |
− | + | -- do something with something (assuming there is a meaningful return value) | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
− | + | -- maybe another example for the optional second case | |
− | + | local somethingElse = function(exampleValue, anotherValue) | |
− | |||
− | |||
− | |||
− | + | -- lastly, include an example with error handling to give an idea of good practice | |
− | + | local ok, err = function() | |
− | + | if not ok then | |
− | + | debugc(f"Error: unable to do <particular thing> because {err}\n") | |
− | + | return | |
− | local | ||
− | if | ||
− | |||
− | |||
− | |||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ; Additional development notes | |
− | ; | + | -- This function is still being written up. |
− | |||
− | + | ==feedTelnet== | |
− | |||
− | |||
− | ; | + | ; feedTelnet(data) |
− | |||
− | + | :Sends given binary data with some predefined special tokens converted to byte values, to the internal telnet engine, as if it had been received from the game. This is primarily to enable testing when new Telnet sub-options/protocols are being developed. The data has to be injected into the system nearer to the point where the Game Server's data starts out than ''feedTriggers()'' and unlike the latter the data is not subject to any encoding so as to match the current profile's setting (which normally happens with ''feedTriggers()''). Furthermore - to prevent this function from putting the telnet engine into a state which could damage the processing of real game data it will refuse to work unless the Profile is completely disconnected from the game server. | |
− | |||
− | |||
− | |||
− | |||
− | + | ;See also: [[Manual:Lua_Functions#feedTriggers|feedTriggers()]], [[Manual:Lua_Functions#sendSocket|sendSocket()]] | |
− | + | {{MudletVersion|tbd}} | |
− | |||
− | + | {{note}} This is not really intended for end-user's but might be useful in some circumstances. | |
− | |||
− | |||
;Parameters | ;Parameters | ||
− | * '' | + | * ''data'' |
− | + | : String containing the bytes to send to the internal telnet engine as if it had come from the Game Server, it can containing some tokens listed below that are to be converted to bytes as well. | |
− | |||
− | |||
− | '' | + | ;Returns |
− | + | * Boolean ''true'' if the ''data'' string was sent to the internal telnet engine. ''nil'' and an error message otherwise, specifically the case when there is some traces of a connection or a complete connection to the socket that passes the data to and from the game server. Additionally, if the data is an empty string ''""'' a second return value will be provided as an integer number representing a version for the table of tokens - which will be incremented each time a change is made to that table so that which tokens are valid can be determined. Note that unrecognised tokens should be passed through as is and not get replaced. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | -- | + | {| class="wikitable sortable" |
− | + | |+ Token value table | |
− | + | |- | |
− | + | ! Token !! Byte !! Version!! Notes | |
− | + | |- | |
− | + | || <00> || \0x00 || 1 || 0 dec. | |
− | + | |- | |
− | + | || <O_BINARY> || \0x00 || 1 || Telnet option: Binary | |
− | + | |- | |
− | + | || <NUL> || \0x00 || 1 || ASCII control character: NULL | |
− | -- | + | |- |
− | + | || <01> || \x01 || 1 || 1 dec. | |
− | < | + | |- |
− | + | || <O_ECHO> || \x01 || 1 || Telnet option: Echo | |
− | < | + | |- |
− | -- | + | || <SOH> || \x01 || 1 || ASCII control character: Start of Heading |
− | + | |- | |
− | + | || <02> || \x02 || 1 || 2 dec. Telnet option: Reconnect | |
− | -- | + | |- |
− | + | || <STX> || \x02 || 1 || ASCII control character: Start of Text | |
− | + | |- | |
− | + | || <03> || \x03 || 1 || 3 dec. | |
− | + | |- | |
− | + | || <O_SGA> || \x03 || 1 || Telnet option: Suppress Go Ahead | |
− | + | |- | |
− | + | || <ETX> || \x03 || 1 || ASCII control character: End of Text | |
− | + | |- | |
− | < | + | || <04> || \x04 || 1 || Telnet option: Approx Message Size Negotiation |
− | + | |- | |
− | + | || <EOT> || \x04 || 1 || ASCII control character: End of Transmission | |
− | + | |- | |
− | : | + | || <05> || \x05 || 1 || |
− | + | |- | |
− | + | || <O_STATUS> || \x05 || 1 || | |
− | + | |- | |
− | + | || <ENQ> || \x05 || 1 || ASCII control character: Enquiry | |
− | + | |- | |
+ | || <06> || \x06 || 1 || Telnet option: Timing Mark | ||
+ | |- | ||
+ | || <ACK> || \x06 || 1 || ASCII control character: Acknowledge | ||
+ | |- | ||
+ | || <07> || \x07 || 1 || Telnet option: Remote Controlled Trans and Echo | ||
+ | |- | ||
+ | || <BELL> || \x07 || 1 || ASCII control character: Bell | ||
+ | |- | ||
+ | || <08> || \x08 || 1 || Telnet option: Output Line Width | ||
+ | |- | ||
+ | || <BS> || \x08 || 1 || | ||
+ | |- | ||
+ | || <09> || \x09 || 1 || Telnet option: Output Page Size | ||
+ | |- | ||
+ | || <HTAB> || \x09 || 1 || ASCII control character: Horizontal Tab | ||
+ | |- | ||
+ | || <0A> || \x0a || 1 || Telnet option: Output Carriage-Return Disposition | ||
+ | |- | ||
+ | || <LF> || \x0a || 1 || ASCII control character: Line-Feed | ||
+ | |- | ||
+ | || <0B> || \x0b || 1 || Telnet option: Output Horizontal Tab Stops | ||
+ | |- | ||
+ | || <VTAB> || \x0b || 1 || ASCII control character: Vertical Tab | ||
+ | |- | ||
+ | || <0C> || \x0c || 1 || Telnet option: Output Horizontal Tab Disposition | ||
+ | |- | ||
+ | || <FF> || \x0c || 1 || ASCII control character: Form-Feed | ||
+ | |- | ||
+ | || <0D> || \x0d || 1 || Telnet option: Output Form-feed Disposition | ||
+ | |- | ||
+ | || <CR> || \x0d || 1 || ASCII control character: Carriage-Return | ||
+ | |- | ||
+ | || <0E> || \x0e || 1 || Telnet option: Output Vertical Tab Stops | ||
+ | |- | ||
+ | || <SO> || \x0e || 1 || ASCII control character: Shift-Out | ||
+ | |- | ||
+ | || <0F> || \x0f || 1 || Telnet option: Output Vertical Tab Disposition | ||
+ | |- | ||
+ | || <SI> || \x0f || 1 || ASCII control character: Shift-In | ||
+ | |- | ||
+ | || <10> || \x10 || 1 || Telnet option: Output Linefeed Disposition | ||
+ | |- | ||
+ | || <DLE> || \x10 || 1 || ASCII control character: Data Link Escape | ||
+ | |- | ||
+ | || <11> || \x11 || 1 || Telnet option: Extended ASCII | ||
+ | |- | ||
+ | || <DC1> || \x11 || 1 || ASCII control character: Device Control 1 | ||
+ | |- | ||
+ | || <12> || \x12 || 1 || Telnet option: Logout | ||
+ | |- | ||
+ | || <DC2" || \x12 || 1 || ASCII control character: Device Control 2 | ||
+ | |- | ||
+ | || <13> || \x13 || 1 || Telnet option: Byte Macro | ||
+ | |- | ||
+ | || <DC3> || \x13 || 1 || ASCII control character: Device Control 3 | ||
+ | |- | ||
+ | || <14> || \x14 || 1 || Telnet option: Data Entry Terminal | ||
+ | |- | ||
+ | || <DC4> || \x14 || 1 || ASCII control character: Device Control 4 | ||
+ | |- | ||
+ | || <15> || \x15 || 1 || Telnet option: SUPDUP | ||
+ | |- | ||
+ | || <NAK> || \x15 || 1 || ASCII control character: Negative Acknowledge | ||
+ | |- | ||
+ | || <16> || \x16 || 1 || Telnet option: SUPDUP Output | ||
+ | |- | ||
+ | || <SYN> || \x16 || 1 || ASCII control character: Synchronous Idle | ||
+ | |- | ||
+ | || <17> || \x17 || 1 || Telnet option: Send location | ||
+ | |- | ||
+ | || <ETB> || \x17 || 1 || ASCII control character: End of Transmission Block | ||
+ | |- | ||
+ | || <18> || \x18 || 1 || | ||
+ | |- | ||
+ | || <O_TERM> || \x18 || 1 || Telnet option: Terminal Type | ||
+ | |- | ||
+ | || <CAN> || \x18 || 1 || ASCII control character: Cancel | ||
+ | |- | ||
+ | || <19> || \x19 || 1 || | ||
+ | |- | ||
+ | || <O_EOR> || \x19 || 1 || Telnet option: End-of-Record | ||
+ | |- | ||
+ | || <nowiki><EM></nowiki> || \x19 || 1 || ASCII control character: End of Medium | ||
+ | |- | ||
+ | || <1A> || \x1a || 1 || Telnet option: TACACS User Identification | ||
+ | |- | ||
+ | || <nowiki><SUB></nowiki> || \x1a || 1 || ASCII control character: Substitute | ||
+ | |- | ||
+ | || <1B> || \x1b || 1 || Telnet option: Output Marking | ||
+ | |- | ||
+ | || <ESC> || \x1b || 1 || ASCII control character: Escape | ||
+ | |- | ||
+ | || <1C> || \x1c || 1 || Telnet option: Terminal Location Number | ||
+ | |- | ||
+ | || <FS> || \x1c || 1 || ASCII control character: File Separator | ||
+ | |- | ||
+ | || <1D> || \x1d || 1 || Telnet option: Telnet 3270 Regime | ||
+ | |- | ||
+ | || <GS> || \x1d || 1 || ASCII control character: Group Separator | ||
+ | |- | ||
+ | || <1E> || \x1e || 1 || Telnet option: X.3 PAD | ||
+ | |- | ||
+ | || <RS> || \x1e || 1 || ASCII control character: Record Separator | ||
+ | |- | ||
+ | || <1F> || \x1f || 1 || | ||
+ | |- | ||
+ | || <O_NAWS> || \x1f || 1 || Telnet option: Negotiate About Window Size | ||
+ | |- | ||
+ | || <US> || \x1f || 1 || ASCII control character: Unit Separator | ||
+ | |- | ||
+ | || <SP> || \x20 || 1 || 32 dec. ASCII character: Space | ||
+ | |- | ||
+ | || <O_NENV> || \x27 || 1 || 39 dec. Telnet option: New Environment (also MNES) | ||
+ | |- | ||
+ | || <O_CHARS> || \x2a || 1 || 42 dec. Telnet option: Character Set | ||
+ | |- | ||
+ | || <O_KERMIT> || \x2f || 1 || 47 dec. Telnet option: Kermit | ||
+ | |- | ||
+ | || <O_MSDP> || \x45 || 1 || 69 dec. Telnet option: Mud Server Data Protocol | ||
+ | |- | ||
+ | || <O_MSSP> || \x46 || 1 || 70 dec. Telnet option: Mud Server Status Protocol | ||
+ | |- | ||
+ | || <O_MCCP> || \x55 || 1 || 85 dec | ||
+ | |- | ||
+ | || <O_MCCP2> || \x56 || 1 || 86 dec | ||
+ | |- | ||
+ | || <O_MSP> || \x5a || 1 || 90 dec. Telnet option: Mud Sound Protocol | ||
+ | |- | ||
+ | || <O_MXP> || \x5b || 1 || 91 dec. Telnet option: Mud eXtension Protocol | ||
+ | |- | ||
+ | || <O_ZENITH> || \x5d || 1 || 93 dec. Telnet option: Zenith Mud Protocol | ||
+ | |- | ||
+ | || <O_AARDWULF> || \x66 || 1 || 102 dec. Telnet option: Aardwuld Data Protocol | ||
+ | |- | ||
+ | || <nowiki><DEL></nowiki> || \x7f || 1 || 127 dec. ASCII control character: Delete | ||
+ | |- | ||
+ | || <O_ATCP> || \xc8 || 1 || 200 dec | ||
+ | |- | ||
+ | || <O_GMCP> || \xc9 || 1 || 201 dec | ||
+ | |- | ||
+ | || <T_EOR> || \xef || 1 || 239 dec | ||
+ | |- | ||
+ | || <F0> || \xf0 || 1 || | ||
+ | |- | ||
+ | || <T_SE> || \xf0 || 1 || | ||
+ | |- | ||
+ | || <F1> || \xf1 || 1 || | ||
+ | |- | ||
+ | || <T_NOP> || \xf1 || 1 || | ||
+ | |- | ||
+ | || <F2> || \xf2 || 1 || | ||
+ | |- | ||
+ | || <T_DM> || \xf2 || 1 || | ||
+ | |- | ||
+ | || <F3> || \xf3 || 1 || | ||
+ | |- | ||
+ | || <T_BRK> || \xf3 || 1 || | ||
+ | |- | ||
+ | || <F4> || \xf4 || 1 || | ||
+ | |- | ||
+ | || <T_IP> || \xf4 || 1 || | ||
+ | |- | ||
+ | || <F5> || \xf5 || 1 || | ||
+ | |- | ||
+ | || <T_ABOP> || \xf5 || 1 || | ||
+ | |- | ||
+ | || <F6> || \xf6 || 1 || | ||
+ | |- | ||
+ | || <T_AYT> || \xf6 || 1 || | ||
+ | |- | ||
+ | || <F7> || \xf7 || 1 || | ||
+ | |- | ||
+ | || <T_EC> || \xf7 || 1 || | ||
+ | |- | ||
+ | || <F8> || \xf8 || 1 || | ||
+ | |- | ||
+ | || <T_EL> || \xf8 || 1 || | ||
+ | |- | ||
+ | || <F9> || \xf9 || 1 || | ||
+ | |- | ||
+ | || <T_GA> || \xf9 || 1 || | ||
+ | |- | ||
+ | || <FA> || \xfa || 1 || | ||
+ | |- | ||
+ | || <T_SB> || \xfa || 1 || | ||
+ | |- | ||
+ | || <FB> || \xfb || 1 || | ||
+ | |- | ||
+ | || <T_WILL> || \xfb || 1 || | ||
+ | |- | ||
+ | || <FC> || \xfc || 1 || | ||
+ | |- | ||
+ | || <T_WONT> || \xfc || 1 || | ||
+ | |- | ||
+ | || <FD> || \xfd || 1 || | ||
+ | |- | ||
+ | || <T_DO> || \xfd || 1 || | ||
+ | |- | ||
+ | || <FE> || \xfe || 1 || | ||
+ | |- | ||
+ | || <T_DONT> || \xfe || 1 || | ||
+ | |- | ||
+ | || <FF> || \xff || 1 || | ||
+ | |- | ||
+ | || <T_IAC> || \xff' | ||
+ | |} | ||
;Example | ;Example | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | -- | + | -- a comment explaining what is going on, if there is multiple functionalities (or optional parameters) the examples should start simple and progress in complexity if needed! |
− | + | -- the examples should be small, but self-contained so new users can copy & paste immediately without going diving in lots other function examples first. | |
− | + | -- comments up top should introduce / explain what it does | |
− | |||
− | |||
− | + | local something = feedTelnet(exampleValue) | |
− | + | if something then | |
− | + | -- do something with something (assuming there is a meaningful return value) | |
− | + | end | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | -- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | -- | + | -- maybe another example for the optional second case |
− | + | local somethingElse = function(exampleValue, anotherValue) | |
− | |||
− | + | -- lastly, include an example with error handling to give an idea of good practice | |
− | -- | + | local ok, err = function() |
− | + | if not ok then | |
− | + | debugc(f"Error: unable to do <particular thing> because {err}\n") | |
− | + | return | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | < | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | ;Additional | + | ; Additional development notes |
− | + | -- This function is still being written up. | |
− | |||
− | |||
− | |||
− | |||
+ | [[Category:Mudlet API]] | ||
[[Category:Mudlet Manual]] | [[Category:Mudlet Manual]] | ||
− |
Revision as of 21:29, 11 January 2024
Networking Functions
A collection of functions for managing networking.
connectToServer
- connectToServer(host, port, [save])
- Connects to a given game.
- Parameters
- host:
- Server domain or IP address.
- port:
- Servers port.
- save:
- (optional, boolean) if provided, saves the new connection parameters in the profile so they'll be used next time you open it.
Note: save is available in Mudlet 3.2+.
- Example
connectToServer("midnightsun2.org", 3000)
-- save to disk so these parameters are used next time when opening the profile
connectToServer("midnightsun2.org", 3000, true)
disconnect
- disconnect()
- Disconnects you from the game right away. Note that this will not properly log you out of the game - use an ingame command for that. Such commands vary, but typically QUIT will work.
- See also: reconnect()
- Example
disconnect()
downloadFile
- downloadFile(saveto, url)
- Downloads the resource at the given url into the saveto location on disk. This does not pause the script until the file is downloaded - instead, it lets it continue right away and downloads in the background. When a download is finished, the sysDownloadDone event is raised (with the saveto location as the argument), or when a download fails, the sysDownloadError event is raised with the reason for failure. You may call downloadFile multiple times and have multiple downloads going on at once - but they aren’t guaranteed to be downloaded in the same order that you started them in.
- See also: getHTTP(), postHTTP(), putHTTP(), deleteHTTP()
Note: Since Mudlet 3.0, https downloads are supported and the actual url that was used for the download is returned - useful in case of redirects.
- Example
-- just download a file and save it in our profile folder
local saveto = getMudletHomeDir().."/dark-theme-mudlet.zip"
local url = "http://www.mudlet.org/wp-content/files/dark-theme-mudlet.zip"
downloadFile(saveto, url)
cecho("<white>Downloading <green>"..url.."<white> to <green>"..saveto.."\n")
A more advanced example that downloads a webpage, reads it, and prints a result from it:
-- create a function to parse the downloaded webpage and display a result
function downloaded_file(_, filename)
-- is the file that downloaded ours?
if not filename:find("achaea-who-count.html", 1, true) then return end
-- read the contents of the webpage in
local f, s, webpage = io.open(filename)
if f then webpage = f:read("*a"); io.close(f) end
-- delete the file on disk, don't clutter
os.remove(filename)
-- parse our downloaded file for the player count
local pc = webpage:match([[Total: (%d+) players online]])
display("Achaea has "..tostring(pc).." players on right now.")
end
-- register our function to run on the event that something was downloaded
registerAnonymousEventHandler("sysDownloadDone", "downloaded_file")
-- download a list of fake users for a demo
downloadFile(getMudletHomeDir().."/achaea-who-count.html", "https://www.achaea.com/game/who")
Result should look like this:
getConnectionInfo
- getConnectionInfo()
- Returns the server address and port that you're currently connected to, and (in Mudlet 4.12+)
true
orfalse
indicating if you're currently connected to a game. - See also: connectToServer()
- Example
local host, port, connected = getConnectionInfo()
cecho(string.format("<light_grey>Playing on <forest_green>%s:%s<light_grey>, currently connected? <forest_green>%s\n", host, port, tostring(connected)))
-- echo the new connection parameters whenever we connect to a different host with connectToServer()
function echoInfo()
local host, port = getConnectionInfo()
cecho(string.format("<light_grey>Now connected to <forest_green>%s:%s\n", host, port))
end
registerAnonymousEventHandler("sysConnectionEvent", "echoInfo")
getIrcChannels
- getIrcChannels()
- Returns a list of channels the IRC client is joined to as a lua table. If the client is not yet started the value returned is loaded from disk and represents channels the client will auto-join when started.
- See also: setIrcChannels()
- Example
display(getIrcChannels())
-- Prints: {"#mudlet", "#lua"}
getIrcConnectedHost
- getIrcConnectedHost()
- Returns true+host where host is a string containing the host name of the IRC server, as given to the client by the server while starting the IRC connection. If the client has not yet started or finished connecting this will return false and an empty string.
- This function can be particularly useful for testing if the IRC client has connected to a server prior to sending data, and it will not auto-start the IRC client.
- The hostname value this function returns can be used to test if sysIrcMessage events are sent from the server or a user on the network.
- Example
local status, hostname = getIrcConnectedHost()
if status == true then
-- do something with connected IRC, send IRC commands, store 'hostname' elsewhere.
-- if sysIrcMessage sender = hostname from above, message is likely a status, command response, or an error from the Server.
else
-- print a status, change connection settings, or just continue waiting to send IRC data.
end
getIrcNick
- getIrcNick()
- Returns a string containing the IRC client nickname. If the client is not yet started, your default nickname is loaded from IRC client configuration.
- See also: setIrcNick()
- Example
local nick = getIrcNick()
echo(nick)
-- Prints: "Sheldor"
getIrcServer
- getIrcServer()
- Returns the IRC client server name and port as a string and a number respectively. If the client is not yet started your default server is loaded from IRC client configuration.
- See also: setIrcServer()
- Example
local server, port = getIrcServer()
echo("server: "..server..", port: "..port.."\n")
getNetworkLatency
- getNetworkLatency()
- Returns the last measured response time between the sent command and the server reply in seconds - e.g. 0.058 (=58 milliseconds lag) or 0.309 (=309 milliseconds). This is the N: number you see bottom-right of Mudlet.
Also known as server lag.
- Example
Need example
openUrl
- openUrl (url)
- Opens the default OS browser for the given URL.
- Example
openUrl("http://google.com")
openUrl("www.mudlet.org")
reconnect
- reconnect()
- Force-reconnects (so if you're connected, it'll disconnect) you to the game.
- Example
-- you could trigger this on a log out message to reconnect, if you'd like
reconnect()
restartIrc
- restartIrc()
- Restarts the IRC client connection, reloading configurations from disk before reconnecting the IRC client.
sendAll
- sendAll(list of things to send, [echo back or not])
- sends multiple things to the game. If you'd like the commands not to be shown, include false at the end.
- See also: send()
- Example
-- instead of using many send() calls, you can use one sendAll
sendAll("outr paint", "outr canvas", "paint canvas")
-- can also have the commands not be echoed
sendAll("hi", "bye", false)
sendATCP
- sendATCP(message, what)
- Need description
- See also: ATCP Protocol, ATCP Extensions, Achaea Telnet Client Protocol specification, Description by forum user KaVir (2013), Description by forum user Iocun (2009)
- Parameters
- message:
- The message that you want to send.
- what:
- Need description
- Example
Need example
sendGMCP
- sendGMCP(command)
- Sends a GMCP message to the server. The IRE document on GMCP has information about what can be sent, and what tables it will use, etcetera.
- Note that this function is rarely used in practice. For most GMCP modules, the messages are automatically sent by the server when a relevant event happens in the game. For example, LOOKing in your room prompts the server to send the room description and contents, as well as the GMCP message gmcp.Room. A call to sendGMCP would not be required in this case.
- When playing an IRE game, a call to
send(" ")
afterwards is necessary due to a bug in the game with compression (MCCP) is enabled.
- See also: GMCP Scripting for Discord status
- Example
--This would send "Core.KeepAlive" to the server, which resets the timeout
sendGMCP("Core.KeepAlive")
--This would send a request for the server to send an update to the gmcp.Char.Skills.Groups table.
sendGMCP("Char.Skills.Get {}")
--This would send a request for the server to send a list of the skills in the
--vision group to the gmcp.Char.Skills.List table.
sendGMCP("Char.Skills.Get " .. yajl.to_string{group = "vision"})
--And finally, this would send a request for the server to send the info for
--hide in the woodlore group to the gmcp.Char.Skills.Info table
sendGMCP("Char.Skills.Get " .. yajl.to_string{group="MWP", name="block"})
sendMSDP
- sendMSDP(variable[, value][, value...])
- Sends a MSDP message to the server.
- Parameters
- variable:
- The variable, in MSDP terms, that you want to request from the server.
- value:
- The variables value that you want to request. You can request more than one value at a time.
- Example
-- ask for a list of commands, lists, and reportable variables that the server supports
sendMSDP("LIST", "COMMANDS", "LISTS", "REPORTABLE_VARIABLES")
-- ask the server to start keeping you up to date on your health
sendMSDP("REPORT", "HEALTH")
-- or on your health and location
sendMSDP("REPORT", "HEALTH", "ROOM_VNUM", "ROOM_NAME")
sendIrc
- sendIrc(target, message)
- Sends a message to an IRC channel or person. Returns true+status if message could be sent or was successfully processed by the client, or nil+error if the client is not ready for sending, and false+status if the client filtered the message or failed to send it for some reason. If the IRC client hasn't started yet, this function will initiate the IRC client and begin a connection.
To receive an IRC message, check out the sysIrcMessage event.
Note: Since Mudlet 3.3, auto-opens the IRC window and returns the success status.
- Parameters
- target:
- nick or channel name and if omitted will default to the first available channel in the list of joined channels.
- message:
- The message to send, may contain IRC client commands which start with
/
and can use all commands which are available through the client window.
- Example
-- this would send "hello from Mudlet!" to the channel #mudlet on freenode.net
sendIrc("#mudlet", "hello from Mudlet!")
-- this would send "identify password" in a private message to Nickserv on freenode.net
sendIrc("Nickserv", "identify password")
-- use an in-built IRC command
sendIrc("#mudlet", "/topic")
Note: The following IRC commands are available since Mudlet 3.3:
- /ACTION <target> <message...>
- /ADMIN (<server>)
- /AWAY (<reason...>)
- /CLEAR (<buffer>) -- Clears the text log for the given buffer name. Uses the current active buffer if none are given.
- /CLOSE (<buffer>) -- Closes the buffer and removes it from the Buffer list. Uses the current active buffer if none are given.
- /HELP (<command>) -- Displays some help information about a given command or lists all available commands.
- /INFO (<server>)
- /INVITE <user> (<#channel>)
- /JOIN <#channel> (<key>)
- /KICK (<#channel>) <user> (<reason...>)
- /KNOCK <#channel> (<message...>)
- /LIST (<channels>) (<server>)
- /ME [target] <message...>
- /MODE (<channel/user>) (<mode>) (<arg>)
- /MOTD (<server>)
- /MSG <target> <message...> -- Sends a message to target, can be used to send Private messages.
- /NAMES (<#channel>)
- /NICK <nick>
- /NOTICE <#channel/user> <message...>
- /PART (<#channel>) (<message...>)
- /PING (<user>)
- /RECONNECT -- Issues a Quit command to the IRC Server and closes the IRC connection then reconnects to the IRC server. The same as calling ircRestart() in Lua.
- /QUIT (<message...>)
- /QUOTE <command> (<parameters...>)
- /STATS <query> (<server>)
- /TIME (<user>)
- /TOPIC (<#channel>) (<topic...>)
- /TRACE (<target>)
- /USERS (<server>)
- /VERSION (<user>)
- /WHO <mask>
- /WHOIS <user>
- /WHOWAS <user>
Note: The following IRC commands are available since Mudlet 3.15:
- /MSGLIMIT <limit> (<buffer>) -- Sets the limit for messages to keep in the IRC client message buffers and saves this setting. If a specific buffer/channel name is given the limit is not saved and applies to the given buffer until the application is closed or the limit is changed again. For this reason, global settings should be applied first, before settings for specific channels/PM buffers.
sendTelnetChannel102
- sendTelnetChannel102(msg)
- Sends a message via the 102 subchannel back to the game (that's used in Aardwolf). The msg is in a two byte format; see the link below to the Aardwolf Wiki for how that works.
- Example
-- turn prompt flags on:
sendTelnetChannel102("\52\1")
-- turn prompt flags off:
sendTelnetChannel102("\52\2")
To see the list of options that Aardwolf supports go to: Using Telnet negotiation to control MUD client interaction.
setIrcChannels
- setIrcChannels(channels)
- Saves the given channels to disk as the new IRC client channel auto-join configuration. This value is not applied to the current active IRC client until it is restarted with restartIrc()
- See also: getIrcChannels(), restartIrc()
- Parameters
- channels:
- A table containing strings which are valid channel names. Any channels in the list which aren't valid are removed from the list.
- Example
setIrcChannels( {"#mudlet", "#lua", "irc"} )
-- Only the first two will be accepted, as "irc" is not a valid channel name.
setIrcNick
- setIrcNick(nickname)
- Saves the given nickname to disk as the new IRC client configuration. This value is not applied to the current active IRC client until it is restarted with restartIrc()
- See also: getIrcNick(), restartIrc()
- Parameters
- nickname:
- A string with your new desired name in IRC.
- Example
setIrcNick( "Sheldor" )
setIrcServer
- setIrcServer(hostname, port[, secure])
- Saves the given server's address to disk as the new IRC client connection configuration. These values are not applied to the current active IRC client until it is restarted with restartIrc()
- See also: getIrcServer(), restartIrc()
- Parameters
- hostname:
- A string containing the hostname of the IRC server.
- port:
- (optional) A number indicating the port of the IRC server. Defaults to 6667, if not provided.
- secure:
- (optional) Boolean, true if server uses Transport Layer Security. Defaults to false.
- Example
setIrcServer("irc.libera.chat", 6667)
getHTTP
- getHTTP(url, headersTable)
- Sends an HTTP GET request to the given URL. Raises sysGetHttpDone on success or sysGetHttpError on failure.
- See also: downloadFile().
- Parameters
- url:
- Location to send the request to.
- headersTable:
- (optional) table of headers to send with your request.
- Examples
function onHttpGetDone(_, url, body)
cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body))
end
registerAnonymousEventHandler("sysGetHttpDone", onHttpGetDone)
getHTTP("https://httpbin.org/info")
getHTTP("https://httpbin.org/are_you_awesome", {["X-am-I-awesome"] = "yep I am"})
-- Status requests typically use GET requests
local url = "http://postman-echo.com/status"
local header = {["Content-Type"] = "application/json"}
-- first we create something to handle the success, and tell us what we got
registerAnonymousEventHandler('sysGetHttpDone', function(event, rurl, response)
if rurl == url then display(r) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler
end, true) -- this sets it to delete itself after it fires
-- then we create something to handle the error message, and tell us what went wrong
registerAnonymousEventHandler('sysGetHttpError', function(event, response, rurl)
if rurl == url then display(r) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler
end, true) -- this sets it to delete itself after it fires
-- Lastly, we make the request:
getHTTP(url, header)
-- Pop this into an alias and try it yourself!
postHTTP
- postHTTP(dataToSend, url, headersTable, file)
- Sends an HTTP POST request to the given URL, either as text or with a specific file you'd like to upload. Raises sysPostHttpDone on success or sysPostHttpError on failure.
- See also: downloadFile(), getHTTP(), putHTTP(), deleteHTTP().
- Parameters
- dataToSend:
- Text to send in the request (unless you provide a file to upload).
- url:
- Location to send the request to.
- headersTable:
- (optional) table of headers to send with your request.
- file:
- (optional) file to upload as part of the POST request. If provided, this will replace 'dataToSend'.
- If you use a scripting language (ex. PHP) to handle this post, remember that the file is sent as raw data. Expecially no field name is provided, dispite it works in common html post.
- Examples
function onHttpPostDone(_, url, body)
cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body))
end
registerAnonymousEventHandler("sysPostHttpDone", onHttpPostDone)
postHTTP("why hello there!", "https://httpbin.org/post")
postHTTP("this us a request with custom headers", "https://httpbin.org/post", {["X-am-I-awesome"] = "yep I am"})
postHTTP(nil, "https://httpbin.org/post", {}, "<fill in file location to upload here, maybe get from invokeDialog>")
-- This will create a JSON message body. Many modern REST APIs expect a JSON body.
local url = "http://postman-echo.com/post"
local data = {message = "I am the banana", user = "admin"}
local header = {["Content-Type"] = "application/json"}
-- first we create something to handle the success, and tell us what we got
registerAnonymousEventHandler('sysPostHttpDone', function(event, rurl, response)
if rurl == url then display(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler
end, true) -- this sets it to delete itself after it fires
-- then we create something to handle the error message, and tell us what went wrong
registerAnonymousEventHandler('sysPostHttpError', function(event, response, rurl)
if rurl == url then display(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler
end, true) -- this sets it to delete itself after it fires
-- Lastly, we make the request:
postHTTP(yajl.to_string(data), url, header) -- yajl.to_string converts our Lua table into a JSON-like string so the server can understand it
-- Pop this into an alias and try it yourself!
- HTTP Basic Authentication Example
If your HTTP endpoint requires authentication to post data, HTTP Basic Authentication is a common method for doing so. There are two ways to do so.
OPTION 1: URL encoding: Many HTTP servers allow you to enter a HTTP Basic Authentication username and password at the beginning of the URL itself, in format: https://username:password@domain.com/path/to/endpoint
OPTION 2: Authorization Header: Some HTTP servers may require you to put your Basic Authentication into the 'Authorization' HTTP header value.
This requires encoding the username:password into base64. For example, if your username is 'user' and your password is '12345', you'd need to run the string "user:12345" through a base64 encoder, which would result in the string: dXNlcjoxMjM0NQ==
Then, you'd need to set the HTTP header 'Authorization' field value to indicate it is using Basic auth and inserting the base64 string as: ['Authorization'] = "Basic dXNlcjoxMjM0NQ=="
As you're encoding your username and password, you probably want to do this encoding locally for security reasons. You also probably want to only use https:// and not http:// when sending usernames and passwords over the internet.
In the HTTP Basic Authentication example below, there is an inline base64Encode() function included:
function base64Encode(data)
-- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de>
-- licensed under the terms of the LGPL2
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
-- Example: base64Encode("user:12345") -> dXNlcjoxMjM0NQ==
function postJSON(url,dataTable,headerTable)
-- This will create a JSON message body. Many modern REST APIs expect a JSON body.
local data = dataTable or {text = "hello world"}
local header = headerTable or {["Content-Type"] = "application/json"}
-- first we create something to handle the success, and tell us what we got
registerAnonymousEventHandler('sysPostHttpDone', function(event, rurl, response)
if rurl == url then sL("HTTP response success"); echo(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler
end, true) -- this sets it to delete itself after it fires
-- then we create something to handle the error message, and tell us what went wrong
registerAnonymousEventHandler('sysPostHttpError', function(event, response, rurl)
if rurl == url then sL("HTTP response error",3); echo(response) else return true end -- this will show us the response body, or if it's not the right url, then do not delete the handler
end, true) -- this sets it to delete itself after it fires
-- Lastly, we make the request:
postHTTP(yajl.to_string(data), url, header) -- yajl.to_string converts our Lua table into a JSON-like string so the server can understand it
end
data = {
message = "I am the banana",
user = "admin"
}
header = {
["Content-Type"] = "application/json",
["Authorization"] = "Basic " .. base64Encode("user:12345")
}
postJSON("http://postman-echo.com/post",data,header)
- URL Encoding vs JSON encoding
Some HTTP endpoints may not support JSON encoding, and instead may require URL encoding. Here's an example function to convert your lua data table into a URL encoded string::
-- Example: URLEncodeTable({message="hello",person="world"}) -> "message=hello&person=world"
function URLEncodeTable(Args)
-- From: https://help.interfaceware.com/code/details/urlcode-lua
----------------------------------------------------------------------------
-- URL-encode the elements of a table creating a string to be used in a
-- URL for passing data/parameters to another script
-- @param args Table where to extract the pairs (name=value).
-- @return String with the resulting encoding.
----------------------------------------------------------------------------
--
local ipairs, next, pairs, tonumber, type = ipairs, next, pairs, tonumber, type
local string = string
local table = table
--helper functions:
----------------------------------------------------------------------------
-- Decode an URL-encoded string (see RFC 2396)
----------------------------------------------------------------------------
local unescape = function (str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end)
return str
end
----------------------------------------------------------------------------
-- URL-encode a string (see RFC 2396)
----------------------------------------------------------------------------
local escape = function (str)
str = string.gsub (str, "([^0-9a-zA-Z !'()*._~-])", -- locale independent
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
return str
end
----------------------------------------------------------------------------
-- Insert a (name=value) pair into table [[args]]
-- @param args Table to receive the result.
-- @param name Key for the table.
-- @param value Value for the key.
-- Multi-valued names will be represented as tables with numerical indexes
-- (in the order they came).
----------------------------------------------------------------------------
local insertfield = function (args, name, value)
if not args[name] then
args[name] = value
else
local t = type (args[name])
if t == "string" then
args[name] = {args[name],value,}
elseif t == "table" then
table.insert (args[name], value)
else
error ("CGILua fatal error (invalid args table)!")
end
end
end
-- end helper functions
if Args == nil or next(Args) == nil then -- no args or empty args?
return ""
end
local strp = ""
for key, vals in pairs(Args) do
if type(vals) ~= "table" then
vals = {vals}
end
for i,val in ipairs(vals) do
strp = strp.."&"..key.."="..escape(val)
end
end
-- remove first &
return string.sub(strp,2)
end
putHTTP
- putHTTP(dataToSend, url, [headersTable], [file])
- Sends an HTTP PUT request to the given URL, either as text or with a specific file you'd like to upload. Raises sysPutHttpDone on success or sysPutHttpError on failure.
- See also: downloadFile(), getHTTP(), postHTTP(), deleteHTTP().
- Parameters
- dataToSend:
- Text to send in the request (unless you provide a file to upload).
- url:
- Location to send the request to.
- headersTable:
- (optional) table of headers to send with your request.
- file:
- (optional) file to upload as part of the PUT request. If provided, this will replace 'dataToSend'.
- Example
function onHttpPutDone(_, url, body)
cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body))
end
registerAnonymousEventHandler("sysPutHttpDone", onHttpPutDone)
putHTTP("this us a request with custom headers", "https://httpbin.org/put", {["X-am-I-awesome"] = "yep I am"})
putHTTP("https://httpbin.org/put", "<fill in file location to upload here>")
deleteHTTP
- deleteHTTP(url, headersTable)
- Sends an HTTP DELETE request to the given URL. Raises sysDeleteHttpDone on success or sysDeleteHttpError on failure.
- See also: downloadFile(), getHTTP(), postHTTP(), putHTTP().
- Parameters
- url:
- Location to send the request to.
- headersTable:
- (optional) table of headers to send with your request.
- Example
function onHttpDeleteDone(_, url, body)
cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s", url, body))
end
registerAnonymousEventHandler("sysDeleteHttpDone", onHttpDeleteDone)
deleteHTTP("https://httpbin.org/delete")
deleteHTTP("https://httpbin.org/delete", {["X-am-I-awesome"] = "yep I am"})
customHTTP
- customHTTP(method, url, headersTable)
- Sends an custom method request to the given URL. Raises sysCustomHttpDone on success or sysCustomHttpError on failure.
- See also: downloadFile(), getHTTP(), postHTTP(), putHTTP(), deleteHTTP().
- Parameters
- method:
- Http method to use (eg. PATCH, HEAD etc.)
- dataToSend:
- Text to send in the request (unless you provide a file to upload).
- url:
- Location to send the request to.
- headersTable:
- (optional) table of headers to send with your request.
- file:
- (optional) file to upload as part of the PUT request. If provided, this will replace 'dataToSend'.
- Example
function onCustomHttpDone(_, url, body, method)
cecho(string.format("<white>url: <dark_green>%s<white>, body: <dark_green>%s<white>, method: <dark_green>%s", url, body, method))
end
registerAnonymousEventHandler("sysCustomHttpDone", sysCustomHttpDone)
customHTTP("PATCH", "this us a request with custom headers", "https://httpbin.org/put", {["X-am-I-awesome"] = "yep I am"})
customHTTP("PATCH", "https://httpbin.org/put", "<fill in file location to upload here>")
sendSocket
- sendSocket(data)
- Sends given binary data as-is (or with some predefined special tokens converted to byte values) to the game. You can use this to implement support for a new telnet protocol, simultronics login etc.
- success = sendSocket("data")
- See also
- feedTelnet(), feedTriggers()
Note: Modified in Mudlet tbd to accept some tokens like "<NUL>" to include byte values that are not possible to insert with the standard Lua string escape "\###" form where ### is a three digit number between 000 and 255 inclusive or where the value is more easily provided via a mnemonic. For the table of the tokens that are known about, see the one in feedTelnet().
Note: The data (as bytes) once the tokens have been converted to their byte values is sent as is to the Game Server; any encoding to, say, a UTF-8 representation or to duplicate 0xff byte values so they are not considered to be Telnet <IAC> (Interpret As Command) bytes must be done to the data prior to calling this function.
- Parameters
- data:
- String containing the bytes to send to the Game Server possibly containing some tokens that are to be converted to bytes as well.
- Returns
- (Only since Mudlet tbd) Boolean true if the whole data string (after token replacement) was sent to the Server, false if that failed for any reason (including if the Server has not been connected or is now disconnected). nil and an error message for any other defect.
- Example
-- Tell the Server that we are now willing and able to process to process Ask the Server to a comment explaining what is going on, if there is multiple functionalities (or optional parameters) the examples should start simple and progress in complexity if needed!
-- the examples should be small, but self-contained so new users can copy & paste immediately without going diving in lots other function examples first.
-- comments up top should introduce / explain what it does
local something = function(exampleValue)
if something then
-- do something with something (assuming there is a meaningful return value)
end
-- maybe another example for the optional second case
local somethingElse = function(exampleValue, anotherValue)
-- lastly, include an example with error handling to give an idea of good practice
local ok, err = function()
if not ok then
debugc(f"Error: unable to do <particular thing> because {err}\n")
return
end
- Additional development notes
-- This function is still being written up.
feedTelnet
- feedTelnet(data)
- Sends given binary data with some predefined special tokens converted to byte values, to the internal telnet engine, as if it had been received from the game. This is primarily to enable testing when new Telnet sub-options/protocols are being developed. The data has to be injected into the system nearer to the point where the Game Server's data starts out than feedTriggers() and unlike the latter the data is not subject to any encoding so as to match the current profile's setting (which normally happens with feedTriggers()). Furthermore - to prevent this function from putting the telnet engine into a state which could damage the processing of real game data it will refuse to work unless the Profile is completely disconnected from the game server.
- See also
- feedTriggers(), sendSocket()
Note: This is not really intended for end-user's but might be useful in some circumstances.
- Parameters
- data
- String containing the bytes to send to the internal telnet engine as if it had come from the Game Server, it can containing some tokens listed below that are to be converted to bytes as well.
- Returns
- Boolean true if the data string was sent to the internal telnet engine. nil and an error message otherwise, specifically the case when there is some traces of a connection or a complete connection to the socket that passes the data to and from the game server. Additionally, if the data is an empty string "" a second return value will be provided as an integer number representing a version for the table of tokens - which will be incremented each time a change is made to that table so that which tokens are valid can be determined. Note that unrecognised tokens should be passed through as is and not get replaced.
Token | Byte | Version | Notes |
---|---|---|---|
<00> | \0x00 | 1 | 0 dec. |
<O_BINARY> | \0x00 | 1 | Telnet option: Binary |
<NUL> | \0x00 | 1 | ASCII control character: NULL |
<01> | \x01 | 1 | 1 dec. |
<O_ECHO> | \x01 | 1 | Telnet option: Echo |
<SOH> | \x01 | 1 | ASCII control character: Start of Heading |
<02> | \x02 | 1 | 2 dec. Telnet option: Reconnect |
<STX> | \x02 | 1 | ASCII control character: Start of Text |
<03> | \x03 | 1 | 3 dec. |
<O_SGA> | \x03 | 1 | Telnet option: Suppress Go Ahead |
<ETX> | \x03 | 1 | ASCII control character: End of Text |
<04> | \x04 | 1 | Telnet option: Approx Message Size Negotiation |
<EOT> | \x04 | 1 | ASCII control character: End of Transmission |
<05> | \x05 | 1 | |
<O_STATUS> | \x05 | 1 | |
<ENQ> | \x05 | 1 | ASCII control character: Enquiry |
<06> | \x06 | 1 | Telnet option: Timing Mark |
<ACK> | \x06 | 1 | ASCII control character: Acknowledge |
<07> | \x07 | 1 | Telnet option: Remote Controlled Trans and Echo |
<BELL> | \x07 | 1 | ASCII control character: Bell |
<08> | \x08 | 1 | Telnet option: Output Line Width |
<BS> | \x08 | 1 | |
<09> | \x09 | 1 | Telnet option: Output Page Size |
<HTAB> | \x09 | 1 | ASCII control character: Horizontal Tab |
<0A> | \x0a | 1 | Telnet option: Output Carriage-Return Disposition |
<LF> | \x0a | 1 | ASCII control character: Line-Feed |
<0B> | \x0b | 1 | Telnet option: Output Horizontal Tab Stops |
<VTAB> | \x0b | 1 | ASCII control character: Vertical Tab |
<0C> | \x0c | 1 | Telnet option: Output Horizontal Tab Disposition |
<FF> | \x0c | 1 | ASCII control character: Form-Feed |
<0D> | \x0d | 1 | Telnet option: Output Form-feed Disposition |
<CR> | \x0d | 1 | ASCII control character: Carriage-Return |
<0E> | \x0e | 1 | Telnet option: Output Vertical Tab Stops |
<SO> | \x0e | 1 | ASCII control character: Shift-Out |
<0F> | \x0f | 1 | Telnet option: Output Vertical Tab Disposition |
<SI> | \x0f | 1 | ASCII control character: Shift-In |
<10> | \x10 | 1 | Telnet option: Output Linefeed Disposition |
<DLE> | \x10 | 1 | ASCII control character: Data Link Escape |
<11> | \x11 | 1 | Telnet option: Extended ASCII |
<DC1> | \x11 | 1 | ASCII control character: Device Control 1 |
<12> | \x12 | 1 | Telnet option: Logout |
<DC2" | \x12 | 1 | ASCII control character: Device Control 2 |
<13> | \x13 | 1 | Telnet option: Byte Macro |
<DC3> | \x13 | 1 | ASCII control character: Device Control 3 |
<14> | \x14 | 1 | Telnet option: Data Entry Terminal |
<DC4> | \x14 | 1 | ASCII control character: Device Control 4 |
<15> | \x15 | 1 | Telnet option: SUPDUP |
<NAK> | \x15 | 1 | ASCII control character: Negative Acknowledge |
<16> | \x16 | 1 | Telnet option: SUPDUP Output |
<SYN> | \x16 | 1 | ASCII control character: Synchronous Idle |
<17> | \x17 | 1 | Telnet option: Send location |
<ETB> | \x17 | 1 | ASCII control character: End of Transmission Block |
<18> | \x18 | 1 | |
<O_TERM> | \x18 | 1 | Telnet option: Terminal Type |
<CAN> | \x18 | 1 | ASCII control character: Cancel |
<19> | \x19 | 1 | |
<O_EOR> | \x19 | 1 | Telnet option: End-of-Record |
<EM> | \x19 | 1 | ASCII control character: End of Medium |
<1A> | \x1a | 1 | Telnet option: TACACS User Identification |
<SUB> | \x1a | 1 | ASCII control character: Substitute |
<1B> | \x1b | 1 | Telnet option: Output Marking |
<ESC> | \x1b | 1 | ASCII control character: Escape |
<1C> | \x1c | 1 | Telnet option: Terminal Location Number |
<FS> | \x1c | 1 | ASCII control character: File Separator |
<1D> | \x1d | 1 | Telnet option: Telnet 3270 Regime |
<GS> | \x1d | 1 | ASCII control character: Group Separator |
<1E> | \x1e | 1 | Telnet option: X.3 PAD |
<RS> | \x1e | 1 | ASCII control character: Record Separator |
<1F> | \x1f | 1 | |
<O_NAWS> | \x1f | 1 | Telnet option: Negotiate About Window Size |
<US> | \x1f | 1 | ASCII control character: Unit Separator |
<SP> | \x20 | 1 | 32 dec. ASCII character: Space |
<O_NENV> | \x27 | 1 | 39 dec. Telnet option: New Environment (also MNES) |
<O_CHARS> | \x2a | 1 | 42 dec. Telnet option: Character Set |
<O_KERMIT> | \x2f | 1 | 47 dec. Telnet option: Kermit |
<O_MSDP> | \x45 | 1 | 69 dec. Telnet option: Mud Server Data Protocol |
<O_MSSP> | \x46 | 1 | 70 dec. Telnet option: Mud Server Status Protocol |
<O_MCCP> | \x55 | 1 | 85 dec |
<O_MCCP2> | \x56 | 1 | 86 dec |
<O_MSP> | \x5a | 1 | 90 dec. Telnet option: Mud Sound Protocol |
<O_MXP> | \x5b | 1 | 91 dec. Telnet option: Mud eXtension Protocol |
<O_ZENITH> | \x5d | 1 | 93 dec. Telnet option: Zenith Mud Protocol |
<O_AARDWULF> | \x66 | 1 | 102 dec. Telnet option: Aardwuld Data Protocol |
<DEL> | \x7f | 1 | 127 dec. ASCII control character: Delete |
<O_ATCP> | \xc8 | 1 | 200 dec |
<O_GMCP> | \xc9 | 1 | 201 dec |
<T_EOR> | \xef | 1 | 239 dec |
<F0> | \xf0 | 1 | |
<T_SE> | \xf0 | 1 | |
<F1> | \xf1 | 1 | |
<T_NOP> | \xf1 | 1 | |
<F2> | \xf2 | 1 | |
<T_DM> | \xf2 | 1 | |
<F3> | \xf3 | 1 | |
<T_BRK> | \xf3 | 1 | |
<F4> | \xf4 | 1 | |
<T_IP> | \xf4 | 1 | |
<F5> | \xf5 | 1 | |
<T_ABOP> | \xf5 | 1 | |
<F6> | \xf6 | 1 | |
<T_AYT> | \xf6 | 1 | |
<F7> | \xf7 | 1 | |
<T_EC> | \xf7 | 1 | |
<F8> | \xf8 | 1 | |
<T_EL> | \xf8 | 1 | |
<F9> | \xf9 | 1 | |
<T_GA> | \xf9 | 1 | |
<FA> | \xfa | 1 | |
<T_SB> | \xfa | 1 | |
<FB> | \xfb | 1 | |
<T_WILL> | \xfb | 1 | |
<FC> | \xfc | 1 | |
<T_WONT> | \xfc | 1 | |
<FD> | \xfd | 1 | |
<T_DO> | \xfd | 1 | |
<FE> | \xfe | 1 | |
<T_DONT> | \xfe | 1 | |
<FF> | \xff | 1 | |
<T_IAC> | \xff' |
- Example
-- a comment explaining what is going on, if there is multiple functionalities (or optional parameters) the examples should start simple and progress in complexity if needed!
-- the examples should be small, but self-contained so new users can copy & paste immediately without going diving in lots other function examples first.
-- comments up top should introduce / explain what it does
local something = feedTelnet(exampleValue)
if something then
-- do something with something (assuming there is a meaningful return value)
end
-- maybe another example for the optional second case
local somethingElse = function(exampleValue, anotherValue)
-- lastly, include an example with error handling to give an idea of good practice
local ok, err = function()
if not ok then
debugc(f"Error: unable to do <particular thing> because {err}\n")
return
end
- Additional development notes
-- This function is still being written up.