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