PageRenderTime 41ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/handlers.lua

http://github.com/JakobOvrum/LuaIRC
Lua | 174 lines | 137 code | 26 blank | 11 comment | 24 complexity | 17c0b6ee0cf7650bab10a2b398fad90e MD5 | raw file
  1. local pairs = pairs
  2. local error = error
  3. local tonumber = tonumber
  4. local table = table
  5. module "irc"
  6. handlers = {}
  7. handlers["PING"] = function(o, prefix, query)
  8. o:send("PONG :%s", query)
  9. end
  10. handlers["001"] = function(o, prefix, me)
  11. o.authed = true
  12. o.nick = me
  13. end
  14. handlers["PRIVMSG"] = function(o, prefix, channel, message)
  15. o:invoke("OnChat", parsePrefix(prefix), channel, message)
  16. end
  17. handlers["NOTICE"] = function(o, prefix, channel, message)
  18. o:invoke("OnNotice", parsePrefix(prefix), channel, message)
  19. end
  20. handlers["JOIN"] = function(o, prefix, channel)
  21. local user = parsePrefix(prefix)
  22. if o.track_users then
  23. if user.nick == o.nick then
  24. o.channels[channel] = {users = {}}
  25. else
  26. o.channels[channel].users[user.nick] = user
  27. end
  28. end
  29. o:invoke("OnJoin", user, channel)
  30. end
  31. handlers["PART"] = function(o, prefix, channel, reason)
  32. local user = parsePrefix(prefix)
  33. if o.track_users then
  34. if user.nick == o.nick then
  35. o.channels[channel] = nil
  36. else
  37. o.channels[channel].users[user.nick] = nil
  38. end
  39. end
  40. o:invoke("OnPart", user, channel, reason)
  41. end
  42. handlers["QUIT"] = function(o, prefix, msg)
  43. local user = parsePrefix(prefix)
  44. if o.track_users then
  45. for channel, v in pairs(o.channels) do
  46. v.users[user.nick] = nil
  47. end
  48. end
  49. o:invoke("OnQuit", user, msg)
  50. end
  51. handlers["NICK"] = function(o, prefix, newnick)
  52. local user = parsePrefix(prefix)
  53. if o.track_users then
  54. for channel, v in pairs(o.channels) do
  55. local users = v.users
  56. local oldinfo = users[user.nick]
  57. if oldinfo then
  58. users[newnick] = oldinfo
  59. users[user.nick] = nil
  60. o:invoke("NickChange", user, newnick, channel)
  61. end
  62. end
  63. else
  64. o:invoke("NickChange", user, newnick)
  65. end
  66. if user.nick == o.nick then
  67. o.nick = newnick
  68. end
  69. end
  70. local function needNewNick(o, prefix, target, badnick)
  71. local newnick = o.nickGenerator(badnick)
  72. o:send("NICK %s", newnick)
  73. end
  74. -- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons)
  75. handlers["432"] = needNewNick
  76. -- ERR_NICKNAMEINUSE
  77. handlers["433"] = needNewNick
  78. --NAMES list
  79. handlers["353"] = function(o, prefix, me, chanType, channel, names)
  80. if o.track_users then
  81. o.channels[channel] = o.channels[channel] or {users = {}, type = chanType}
  82. local users = o.channels[channel].users
  83. for nick in names:gmatch("(%S+)") do
  84. local access, name = parseNick(nick)
  85. users[name] = {access = access}
  86. end
  87. end
  88. end
  89. --end of NAMES
  90. handlers["366"] = function(o, prefix, me, channel, msg)
  91. if o.track_users then
  92. o:invoke("NameList", channel, msg)
  93. end
  94. end
  95. --no topic
  96. handlers["331"] = function(o, prefix, me, channel)
  97. o:invoke("OnTopic", channel, nil)
  98. end
  99. --new topic
  100. handlers["TOPIC"] = function(o, prefix, channel, topic)
  101. o:invoke("OnTopic", channel, topic)
  102. end
  103. handlers["332"] = function(o, prefix, me, channel, topic)
  104. o:invoke("OnTopic", channel, topic)
  105. end
  106. --topic creation info
  107. handlers["333"] = function(o, prefix, me, channel, nick, time)
  108. o:invoke("OnTopicInfo", channel, nick, tonumber(time))
  109. end
  110. handlers["KICK"] = function(o, prefix, channel, kicked, reason)
  111. o:invoke("OnKick", channel, kicked, parsePrefix(prefix), reason)
  112. end
  113. --RPL_UMODEIS
  114. --To answer a query about a client's own mode, RPL_UMODEIS is sent back
  115. handlers["221"] = function(o, prefix, user, modes)
  116. o:invoke("OnUserMode", modes)
  117. end
  118. --RPL_CHANNELMODEIS
  119. --The result from common irc servers differs from that defined by the rfc
  120. handlers["324"] = function(o, prefix, user, channel, modes)
  121. o:invoke("OnChannelMode", channel, modes)
  122. end
  123. handlers["MODE"] = function(o, prefix, target, modes, ...)
  124. if o.track_users and target ~= o.nick then
  125. local add = true
  126. local optList = {...}
  127. for c in modes:gmatch(".") do
  128. if c == "+" then add = true
  129. elseif c == "-" then add = false
  130. elseif c == "o" then
  131. local user = table.remove(optList, 1)
  132. o.channels[target].users[user].access.op = add
  133. elseif c == "h" then
  134. local user = table.remove(optList, 1)
  135. o.channels[target].users[user].access.halfop = add
  136. elseif c == "v" then
  137. local user = table.remove(optList, 1)
  138. o.channels[target].users[user].access.voice = add
  139. end
  140. end
  141. end
  142. o:invoke("OnModeChange", parsePrefix(prefix), target, modes, ...)
  143. end
  144. handlers["ERROR"] = function(o, prefix, message)
  145. o:invoke("OnDisconnect", message, true)
  146. o:shutdown()
  147. error(message, 3)
  148. end