PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/asyncoperations.lua

http://github.com/JakobOvrum/LuaIRC
Lua | 89 lines | 70 code | 17 blank | 2 comment | 11 complexity | 85d502aa33ba569a9ff21822d1e66ec1 MD5 | raw file
  1. local table = table
  2. local assert = assert
  3. local error = error
  4. local select = select
  5. local pairs = pairs
  6. module "irc"
  7. local meta = _META
  8. function meta:send(msg, ...)
  9. if select("#", ...) > 0 then
  10. msg = msg:format(...)
  11. end
  12. self:invoke("OnSend", msg)
  13. local bytes, err = self.socket:send(msg .. "\r\n")
  14. if not bytes and err ~= "timeout" and err ~= "wantwrite" then
  15. self:invoke("OnDisconnect", err, true)
  16. self:shutdown()
  17. error(err, errlevel)
  18. end
  19. end
  20. local function verify(str, errLevel)
  21. if str:find("^:") or str:find("%s%z") then
  22. error(("malformed parameter '%s' to irc command"):format(str), errLevel)
  23. end
  24. return str
  25. end
  26. function meta:sendChat(target, msg)
  27. -- Split the message into segments if it includes newlines.
  28. for line in msg:gmatch("([^\r\n]+)") do
  29. self:send("PRIVMSG %s :%s", verify(target, 3), line)
  30. end
  31. end
  32. function meta:sendNotice(target, msg)
  33. -- Split the message into segments if it includes newlines.
  34. for line in msg:gmatch("([^\r\n]+)") do
  35. self:send("NOTICE %s :%s", verify(target, 3), line)
  36. end
  37. end
  38. function meta:join(channel, key)
  39. if key then
  40. self:send("JOIN %s :%s", verify(channel, 3), verify(key, 3))
  41. else
  42. self:send("JOIN %s", verify(channel, 3))
  43. end
  44. end
  45. function meta:part(channel)
  46. channel = verify(channel, 3)
  47. self:send("PART %s", channel)
  48. if self.track_users then
  49. self.channels[channel] = nil
  50. end
  51. end
  52. function meta:trackUsers(b)
  53. self.track_users = b
  54. if not b then
  55. for k,v in pairs(self.channels) do
  56. self.channels[k] = nil
  57. end
  58. end
  59. end
  60. function meta:setMode(t)
  61. local target = t.target or self.nick
  62. local mode = ""
  63. local add, rem = t.add, t.remove
  64. assert(add or rem, "table contains neither 'add' nor 'remove'")
  65. if add then
  66. mode = table.concat{"+", verify(add, 3)}
  67. end
  68. if rem then
  69. mode = table.concat{mode, "-", verify(rem, 3)}
  70. end
  71. self:send("MODE %s %s", verify(target, 3), mode)
  72. end