Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. --this is just a basic split function we'll use to split the messages
  2. function split(data, pat)
  3. local ret = {}
  4. for i in string.gmatch(data,pat) do
  5. table.insert(ret,i)
  6. end
  7. return ret
  8. end
  9. --config
  10. local nickname = "myircbot"
  11. local channel = "#mybotchannel"
  12.  
  13. local net = require("internet")
  14. local con = net.open("irc.esper.net",6667) --define server / port here, this will connect to the server
  15. if(con) then
  16. local line,png,linesplt,msgfrom = ""
  17. while(true) do
  18. line = con:read() --read a line from the socket
  19. print(line)
  20. linesplt = split(line,"[^:]+")
  21. if #linesplt >= 2 and string.find(linesplt[2], "No Ident response") ~= nil then
  22. print("JOIN")
  23. con:write("USER " .. nickname .. " 0 * :" .. nickname .. "\r\n") --con:write(msg) is used to send messages, con:read() will read a line
  24. con:write("NICK " .. nickname .. "\r\n") --for IRC, remember to append the \r\n on the end of all messages
  25. con:write("JOIN :" .. channel .. "\r\n")
  26. elseif linesplt[1] == "PING" or linesplt[1] == "PING " then
  27. print("PING")
  28. png = split(line,"[^:]+")
  29. con:write("PONG :"..png[#png].."\r\n") --respond to pings so we don't get disconnected
  30. elseif string.find(linesplt[1], "PRIVMSG #") ~= nil then
  31. msgfrom = split(linesplt[1],"[^ ]+")
  32. msgfrom = msgfrom[3]
  33. con:write("PRIVMSG "..msgfrom.." :"..linesplt[2].."\r\n")
  34. end
  35. end
  36. else
  37. print("Connection failed.")
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement