Comparison

mod_ircd/mod_ircd.in.lua @ 466:0fcd34ee7301

mod_ircd: Proper line parsing and generating. Fix PMs
author Kim Alvefur <zash@zash.se>
date Tue, 01 Nov 2011 18:08:15 +0100
parent 465:030404dd7609
child 468:640e6c0b563d
comparison
equal deleted inserted replaced
465:030404dd7609 466:0fcd34ee7301
50 -- Now, actually start the connection: 50 -- Now, actually start the connection:
51 --c.connect_host = "127.0.0.1" 51 --c.connect_host = "127.0.0.1"
52 --c:connect_component(component_jid, component_secret); 52 --c:connect_component(component_jid, component_secret);
53 53
54 local jid = require "util.jid"; 54 local jid = require "util.jid";
55 local nodeprep = require "util.encodings".stringprep.nodeprep;
56
57 local function parse_line(line)
58 local ret = {};
59 if line:sub(1,1) == ":" then
60 ret.from, line = line:match("^:(%w+)%s+(.*)$");
61 end
62 for part in line:gmatch("%S+") do
63 if part:sub(1,1) == ":" then
64 ret[#ret+1] = line:match(":(.*)$");
65 break
66 end
67 ret[#ret+1]=part;
68 end
69 return ret;
70 end
71
72 local function build_line(parts)
73 if #parts > 1 then
74 parts[#parts] = ":" .. parts[#parts];
75 end
76 return (parts.from and ":"..parts.from.." " or "")..table.concat(parts, " ");
77 end
55 78
56 local function irc2muc(channel, nick) 79 local function irc2muc(channel, nick)
57 return jid.join(channel:gsub("^#", ""), muc_server, nick) 80 local room = channel and nodeprep(channel:match("^#(%w+)")) or nil;
81 return jid.join(room, muc_server, nick)
58 end 82 end
59 local function muc2irc(room) 83 local function muc2irc(room)
60 local channel, _, nick = jid.split(room); 84 local channel, _, nick = jid.split(room);
61 return "#"..channel, nick; 85 return "#"..channel, nick;
62 end 86 end
92 close = irc_close_session, log = logger.init("irc"..(conn.id or "1")), 116 close = irc_close_session, log = logger.init("irc"..(conn.id or "1")),
93 rooms = {}, 117 rooms = {},
94 roster = {} }; 118 roster = {} };
95 sessions[conn] = session; 119 sessions[conn] = session;
96 function session.data(data) 120 function session.data(data)
97 local command, args = data:match("^%s*([^ ]+) *(.*)%s*$"); 121 local parts = parse_line(data);
122 module:log("debug", require"util.serialization".serialize(parts));
123 local command = table.remove(parts, 1);
98 if not command then 124 if not command then
99 return; 125 return;
100 end 126 end
101 command = command:upper(); 127 command = command:upper();
102 if not session.nick then 128 if not session.nick then
103 if not (command == "USER" or command == "NICK") then 129 if not (command == "USER" or command == "NICK") then
104 session.send(":" .. muc_server .. " 451 " .. command .. " :You have not registered") 130 module:log("debug", "Client tried to send command %s before registering", command);
105 return true; 131 return session.send{from=muc_server, 451, command, "You have not registered"}
106 end 132 end
107 end 133 end
108 if commands[command] then 134 if commands[command] then
109 local ret = commands[command](session, args); 135 local ret = commands[command](session, parts);
110 if ret then 136 if ret then
111 session.send(ret.."\r\n"); 137 return session.send(ret);
112 end 138 end
113 else 139 else
114 session.send(":" .. muc_server .. " 421 " .. session.nick .. " " .. command .. " :Unknown command") 140 session.send{from=muc_server, 421, session.nick, command, "Unknown command"};
115 module:log("debug", "Unknown command: %s", command); 141 return module:log("debug", "Unknown command: %s", command);
116 end 142 end
117 end 143 end
118 function session.send(data) 144 function session.send(data)
119 return conn:write(data.."\r\n"); 145 if type(data) == "string" then
146 return conn:write(data.."\r\n");
147 elseif type(data) == "table" then
148 local line = build_line(data);
149 module:log("debug", line);
150 conn:write(line.."\r\n");
151 end
120 end 152 end
121 end 153 end
122 if data then 154 if data then
123 session.data(data); 155 session.data(data);
124 end 156 end
125 end 157 end
126 158
127 function irc_listener.ondisconnect(conn, error) 159 function irc_listener.ondisconnect(conn, error)
128 local session = sessions[conn]; 160 local session = sessions[conn];
129 for _, room in pairs(session.rooms) do 161 if session then
130 room:leave("Disconnected"); 162 for _, room in pairs(session.rooms) do
131 end 163 room:leave("Disconnected");
132 jids[session.full_jid] = nil; 164 end
133 nicks[session.nick] = nil; 165 if session.nick then
166 nicks[session.nick] = nil;
167 end
168 if session.full_jid then
169 jids[session.full_jid] = nil;
170 end
171 end
134 sessions[conn] = nil; 172 sessions[conn] = nil;
135 end 173 end
136 174
137 function commands.NICK(session, nick) 175 function commands.NICK(session, args)
138 if session.nick then 176 if session.nick then
139 session.send(":"..muc_server.." 484 * "..nick.." :I'm afraid I can't let you do that, "..nick); 177 session.send(":"..muc_server.." 484 * "..nick.." :I'm afraid I can't let you do that, "..session.nick);
140 --TODO Loop throug all rooms and change nick, with help from Verse. 178 --TODO Loop throug all rooms and change nick, with help from Verse.
141 return; 179 return;
142 end 180 end
143 nick = nick:match("^[%w_]+"); 181 local nick = args[1];
182 nick = nick:gsub("[^%w_]","");
144 if nicks[nick] then 183 if nicks[nick] then
145 session.send(":"..session.host.." 433 * "..nick.." :The nickname "..nick.." is already in use"); 184 session.send{from=muc_server, 433, nick, "The nickname "..nick.." is already in use"};
146 return; 185 return;
147 end 186 end
148 local full_jid = jid.join(nick, component_jid, "ircd"); 187 local full_jid = jid.join(nick, component_jid, "ircd");
149 jids[full_jid] = session; 188 jids[full_jid] = session;
150 nicks[nick] = session; 189 nicks[nick] = session;
151 session.nick = nick; 190 session.nick = nick;
152 session.full_jid = full_jid; 191 session.full_jid = full_jid;
153 session.type = "c2s"; 192 session.type = "c2s";
154 session.send(":"..session.host.." 001 "..session.nick.." :Welcome to XMPP via the "..session.host.." gateway "..session.nick); 193 session.send{from = muc_server, 001, nick, "Welcome to XMPP via the "..session.host.." gateway "..session.nick};
194 session.send{from=nick, "MODE", nick, "+i"}; -- why
155 end 195 end
156 196
157 function commands.USER(session, params) 197 function commands.USER(session, params)
158 -- FIXME 198 -- FIXME
159 -- Empty command for now 199 -- Empty command for now
160 end 200 end
161 201
162 function commands.JOIN(session, channel) 202 function commands.JOIN(session, args)
203 local channel = args[1];
163 local room_jid = irc2muc(channel); 204 local room_jid = irc2muc(channel);
164 print(session.full_jid); 205 print(session.full_jid);
165 local room, err = c:join_room(room_jid, session.nick, { source = session.full_jid } ); 206 local room, err = c:join_room(room_jid, session.nick, { source = session.full_jid } );
166 if not room then 207 if not room then
167 return ":"..session.host.." ERR :Could not join room: "..err 208 return ":"..session.host.." ERR :Could not join room: "..err
168 end 209 end
169 session.rooms[channel] = room; 210 session.rooms[channel] = room;
170 room.channel = channel; 211 room.channel = channel;
171 room.session = session; 212 room.session = session;
172 session.send(":"..session.nick.." JOIN :"..channel); 213 session.send{from=session.nick, "JOIN", channel};
173 session.send(":"..session.host.." 332 "..session.nick.." "..channel.." :Connection in progress..."); 214 session.send{from=muc_server, 332, session.nick, channel ,"Connection in progress..."};
215
174 room:hook("message", function(event) 216 room:hook("message", function(event)
175 if not event.body then return end 217 if not event.body then return end
176 local nick, body = event.nick, event.body; 218 local nick, body = event.nick, event.body;
177 if nick ~= session.nick then 219 if nick ~= session.nick then
178 if body:sub(1,4) == "/me " then 220 if body:sub(1,4) == "/me " then
179 body = "\1ACTION ".. body:sub(5) .. "\1" 221 body = "\1ACTION ".. body:sub(5) .. "\1"
180 end 222 end
181 session.send(":"..nick.." PRIVMSG "..channel.." :"..body); 223 local type = event.stanza.attr.type;
224 session.send{from=nick, "PRIVMSG", type == "groupchat" and channel or nick, body};
182 --FIXME PM's probably won't work 225 --FIXME PM's probably won't work
183 end 226 end
184 end); 227 end);
185 end 228 end
186 229
187 c:hook("groupchat/joined", function(room) 230 c:hook("groupchat/joined", function(room)
188 local session = room.session or jids[room.opts.source]; 231 local session = room.session or jids[room.opts.source];
189 local channel = room.channel; 232 local channel = room.channel;
233 session.send{from=session.nick.."!"..session.nick, "JOIN", channel};
190 session.send((":%s!%s JOIN %s :"):format(session.nick, session.nick, channel)); 234 session.send((":%s!%s JOIN %s :"):format(session.nick, session.nick, channel));
191 if room.topic then 235 if room.topic then
192 session.send((":%s 332 %s :%s"):format(session.host, channel, room.topic)); 236 session.send{from=muc_server, 332, room.topic};
193 end 237 end
194 commands.NAMES(session, channel) 238 commands.NAMES(session, channel)
195 --FIXME Ones own mode get's lost 239 if session.nick.role then
196 --session.send((":%s MODE %s +%s %s"):format(session.host, room.channel, modemap[nick.role], nick.nick)); 240 session.send{from=muc_server, "MODE", channel, session.nick, modemap[session.nick.role], session.nick}
241 end
197 room:hook("occupant-joined", function(nick) 242 room:hook("occupant-joined", function(nick)
198 session.send((":%s!%s JOIN :%s"):format(nick.nick, nick.nick, channel)); 243 session.send{from=nick.nick.."!"..nick.nick, "JOIN", channel};
199 if nick.role and modemap[nick.role] then 244 if nick.role and modemap[nick.role] then
200 session.send((":%s MODE %s +%s %s"):format(session.host, room.channel, modemap[nick.role], nick.nick)); 245 session.send{from=nick.nick.."!"..nick.nick, "MODE", channel, modemap[nick.role], nick.nick};
201 end 246 end
202 end); 247 end);
203 room:hook("occupant-left", function(nick) 248 room:hook("occupant-left", function(nick)
204 session.send((":%s!%s PART %s :"):format(nick.nick, nick.nick, channel)); 249 session.send{from=nick.nick.."!"..nick.nick, "PART", room.channel};
205 end); 250 end);
206 end); 251 end);
207 252
208 function commands.NAMES(session, channel) 253 function commands.NAMES(session, channel)
209 local nicks = { }; 254 local nicks = { };
215 nick = rolemap[n.role] .. nick; 260 nick = rolemap[n.role] .. nick;
216 end 261 end
217 table.insert(nicks, nick); 262 table.insert(nicks, nick);
218 end 263 end
219 nicks = table.concat(nicks, " "); 264 nicks = table.concat(nicks, " ");
220 --:molyb.irc.bnfh.org 353 derp = #grill-bit :derp hyamobi walt snuggles_ E-Rock kng grillbit gunnarbot Frink shedma zagabar zash Mrw00t Appiah J10 lectus peck EricJ soso mackt offer hyarion @pettter MMN-o
221 session.send((":%s 353 %s = %s :%s"):format(session.host, session.nick, channel, nicks)); 265 session.send((":%s 353 %s = %s :%s"):format(session.host, session.nick, channel, nicks));
222 session.send((":%s 366 %s %s :End of /NAMES list."):format(session.host, session.nick, channel)); 266 session.send((":%s 366 %s %s :End of /NAMES list."):format(session.host, session.nick, channel));
223 session.send(":"..session.host.." 353 "..session.nick.." = "..channel.." :"..nicks); 267 session.send(":"..session.host.." 353 "..session.nick.." = "..channel.." :"..nicks);
224 end 268 end
225 269
226 function commands.PART(session, channel) 270 function commands.PART(session, args)
227 local channel, part_message = channel:match("^([^:]+):?(.*)$"); 271 local channel, part_message = unpack(args);
228 channel = channel:match("^([%S]*)"); 272 channel = channel:match("^([%S]*)");
229 session.rooms[channel]:leave(part_message); 273 session.rooms[channel]:leave(part_message);
230 session.send(":"..session.nick.." PART :"..channel); 274 session.send(":"..session.nick.." PART :"..channel);
231 end 275 end
232 276
233 function commands.PRIVMSG(session, message) 277 function commands.PRIVMSG(session, args)
234 local channel, message = message:match("^(%S+) :(.+)$"); 278 local channel, message = unpack(args);
235 if message and #message > 0 and session.rooms[channel] then 279 if message and #message > 0 then
236 if message:sub(1,8) == "\1ACTION " then 280 if message:sub(1,8) == "\1ACTION " then
237 message = "/me ".. message:sub(9,-2) 281 message = "/me ".. message:sub(9,-2)
238 end 282 end
239 module:log("debug", "%s sending PRIVMSG \"%s\" to %s", session.nick, message, channel); 283 -- TODO clean out invalid chars
240 session.rooms[channel]:send_message(message); 284 if channel:sub(1,1) == "#" then
241 end 285 if session.rooms[channel] then
242 end 286 module:log("debug", "%s sending PRIVMSG \"%s\" to %s", session.nick, message, channel);
243 287 session.rooms[channel]:send_message(message);
244 function commands.PING(session, server) 288 end
245 session.send(":"..session.host..": PONG "..server); 289 else -- private message
246 end 290 local nick = channel;
247 291 module:log("debug", "PM to %s", nick);
248 function commands.WHO(session, channel) 292 for channel, room in pairs(session.rooms) do
293 module:log("debug", "looking for %s in %s", nick, channel);
294 if room.occupants[nick] then
295 module:log("debug", "found %s in %s", nick, channel);
296 local who = room.occupants[nick];
297 -- FIXME PMs in verse
298 --room:send_private_message(nick, message);
299 local pm = st.message({type="chat",to=who.jid}, message);
300 module:log("debug", "sending PM to %s: %s", nick, tostring(pm));
301 room:send(pm)
302 break
303 end
304 end
305 end
306 end
307 end
308
309 function commands.PING(session, args)
310 session.send{from=muc_server, "PONG", args[1]};
311 end
312
313 function commands.WHO(session, args)
314 local channel = args[1];
249 if session.rooms[channel] then 315 if session.rooms[channel] then
250 local room = session.rooms[channel] 316 local room = session.rooms[channel]
251 for nick in pairs(room.occupants) do 317 for nick in pairs(room.occupants) do
252 --n=MattJ 91.85.191.50 irc.freenode.net MattJ H :0 Matthew Wild 318 --n=MattJ 91.85.191.50 irc.freenode.net MattJ H :0 Matthew Wild
253 session.send(":"..session.host.." 352 "..session.nick.." "..channel.." "..nick.." "..nick.." "..session.host.." "..nick.." H :0 "..nick); 319 session.send{from=muc_server, 352, session.nick, channel, nick, nick, muc_server, nick, "H", "0 "..nick}
254 end 320 end
255 session.send(":"..session.host.." 315 "..session.nick.." "..channel.. " :End of /WHO list"); 321 session.send{from=muc_server, 315, session.nick, channel, "End of /WHO list"};
256 end 322 end
257 end 323 end
258 324
259 function commands.MODE(session, channel) 325 function commands._MODE(session, args) -- FIXME
260 session.send(":"..session.host.." 324 "..session.nick.." "..channel.." +J"); 326 local channel, target = unpack(args);
261 end 327 if target then
262 328 -- do stuff?
263 function commands.QUIT(session, message) 329 --room:set_affiliation(...)
264 session.send("ERROR :Closing Link: "..session.nick); 330 else
331 -- What's 324? And +J ?
332 session.send{from=muc_server, 324, session.nick, channel, "+J"}
333 end
334 end
335
336 function commands.QUIT(session, args)
337 session.send{"ERROR", "Closing Link: "..session.nick};
265 for _, room in pairs(session.rooms) do 338 for _, room in pairs(session.rooms) do
266 room:leave(message); 339 room:leave(args[1]);
267 end 340 end
268 jids[session.full_jid] = nil; 341 jids[session.full_jid] = nil;
269 nicks[session.nick] = nil; 342 nicks[session.nick] = nil;
270 sessions[session.conn] = nil; 343 sessions[session.conn] = nil;
271 session:close(); 344 session:close();