-
Notifications
You must be signed in to change notification settings - Fork 442
API Internet
CokaCola edited this page Mar 10, 2014
·
11 revisions
This library wraps functionality of Internet cards.
-
internet.isHttpEnabled():boolean
Returns whether HTTP requests are allowed on the server (config setting). -
internet.request(url: string[, data: string or table]): function
Sends an HTTP request to the specified URL, with the specified POST data, if any. If no data is specified, a GET request will be made. The POST data can be in one of two formats: if it's a string, it will be sent as-is. If it's a table, it will be converted to a string by assuming that each key is the name of a POST variable, and it's associated value is the value for that variable. So, for example:
internet.request(url, {some = "variable", another = 1})
Will sendsome=variable&another=1
.
The returned function is an iterator over the lines of the result, use it like so:
for line in internet.request(...) do stuff() end
Important: you should not callos.sleep
,event.pull
or any other functions that directly or indirectly consume signals while iterating the result lines, since the lines of the response are one signal each (to avoid running out of memory for large results). -
internet.isTcpEnabled():boolean
Returns whether TCP sockets are allowed on the server (config setting). -
internet.connect(address:string[, port:number]):number
Opens a new TCP connection. Returns the handle of the connection. The returned handle can be used to interact with the opened socket using the other callbacks. This can error if TCP sockets are not enabled, there are too many open connections or some other I/O error occurs. -
internet.read(handle:number,n:number):string
Tries to read data from the socket stream. Returns the read byte array. -
internet.write(handle:number, data:string): number
Tries to write data to the socket stream. Returns the number of bytes written. -
internet.close(handle:number)
Closes the socket with the specified handle.
Note for internet.open, you may also do this:
local handle = internet.open(...)
data = handle:read(10)
handle:write("1234")
handle:close()
This is an example of a basic IRC bot that echos back what you say to it, using the sockets in the internet api.
--this is just a basic split function we'll use to split the messages
function split(data, pat)
local ret = {}
for i in string.gmatch(data,pat) do
table.insert(ret,i)
end
return ret
end
--config
local nickname = "myircbot"
local channel = "#mybotchannel"
local net = require("internet")
local con = net.open("irc.esper.net",6667) --define server / port here, this will connect to the server
if(con) then
local line,png,linesplt,msgfrom = ""
while(true) do
line = con:read() --read a line from the socket
print(line)
linesplt = split(line,"[^:]+")
if #linesplt >= 2 and string.find(linesplt[2], "No Ident response") ~= nil then
print("JOIN")
con:write("USER " .. nickname .. " 0 * :" .. nickname .. "\r\n") --con:write(msg) is used to send messages, con:read() will read a line
con:write("NICK " .. nickname .. "\r\n") --for IRC, remember to append the \r\n on the end of all messages
con:write("JOIN :" .. channel .. "\r\n")
elseif linesplt[1] == "PING" or linesplt[1] == "PING " then
print("PING")
png = split(line,"[^:]+")
con:write("PONG :"..png[#png].."\r\n") --respond to pings so we don't get disconnected
elseif string.find(linesplt[1], "PRIVMSG #") ~= nil then
msgfrom = split(linesplt[1],"[^ ]+")
msgfrom = msgfrom[3]
con:write("PRIVMSG "..msgfrom.." :"..linesplt[2].."\r\n")
end
end
else
print("Connection failed.")
end
For a more advanced example, check out the IRC Client program available in the latest release of OpenComputers: irc.lua