diff --git a/API-Internet.md b/API-Internet.md index b5cc892..1be7fea 100644 --- a/API-Internet.md +++ b/API-Internet.md @@ -14,4 +14,43 @@ This library wraps functionality of Internet cards. - `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.close(handle:number)` - Closes the socket with the specified handle. \ No newline at end of file + Closes the socket with the specified handle. + + +This is an example of a basic IRC bot that echos back what you say to it, using the sockets in the internet api. +```lua +--config +local nickname = "myircbot" +local channel = "#mybotchannel" + +local net = require("internet") +local con = net.open("irc.esper.net",6667) --define server / port here +function split(data, pat) + local ret = {} + for i in string.gmatch(data,pat) do + table.insert(ret,i) + end + return ret +end + +local line,png,linesplt,msgfrom = "" +while(true) do + line = con:read() + 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("NICK " + nickname + "\r\n") + 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") + 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 +``` \ No newline at end of file