init.lua

changeset 16
ae69cea97598
parent 15
22e6c003a83a
child 17
9fe723988f3c
equal deleted inserted replaced
15:22e6c003a83a 16:ae69cea97598
16 16
17 -- self.conn is ready for stanzas 17 -- self.conn is ready for stanzas
18 function riddim_mt:start() 18 function riddim_mt:start()
19 self:event("started"); 19 self:event("started");
20 self.stream:hook("stanza", function (stanza) 20 self.stream:hook("stanza", function (stanza)
21 local body = stanza:get_child("body"); 21 local body = stanza:get_child("body");
22 local event = { 22 local event = {
23 sender = { jid = stanza.attr.from }; 23 sender = { jid = stanza.attr.from };
24 body = (body and body:get_text()) or nil; 24 body = (body and body:get_text()) or nil;
25 stanza = stanza; 25 stanza = stanza;
26 }; 26 };
27 if stanza.name == "message" then 27 if stanza.name == "message" then
28 local replied; 28 local replied;
29 local bot = self; 29 local bot = self;
30 function event:reply(reply) 30 function event:reply(reply)
31 if replied then return false; end 31 if replied then return false; end
32 replied = true; 32 replied = true;
33 return bot:send_message(stanza.attr.from, stanza.attr.type, reply); 33 return bot:send_message(stanza.attr.from, stanza.attr.type, reply);
34 end
35 end 34 end
36 local ret; 35 end
37 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then 36 local ret;
38 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns; 37 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
39 if xmlns then 38 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
40 event.xmlns = xmlns; 39 if xmlns then
41 print(event.stanza) 40 event.xmlns = xmlns;
42 ret = self:event("iq/"..xmlns, event); 41 ret = self:event("iq/"..xmlns, event);
43 if not ret then
44 ret = self:event(stanza.name, event);
45 end
46 end
47 else
48 ret = self:event(stanza.name, event);
49 end 42 end
50 43 end
51 if ret and type(ret) == "table" and ret.name then 44 if not ret then
52 self:send(ret); 45 ret = self:event(stanza.name, event);
53 end 46 end
54 return ret; 47 if ret and type(ret) == "table" and ret.name then
55 end, 1); 48 self:send(ret);
49 end
50 return ret;
51 end, 1);
56 end 52 end
57 53
58 function riddim_mt:send(s) 54 function riddim_mt:send(s)
59 return self.stream:send(tostring(s)); 55 return self.stream:send(tostring(s));
60 end 56 end
126 presence:add_child(b:caps()) 122 presence:add_child(b:caps())
127 end 123 end
128 b:send(presence); 124 b:send(presence);
129 end); 125 end);
130 126
131 c:hook("binding-success", function () b:start(); end) 127 c:hook("binding-success", function () b:start(); end);
132 128
133 if config.connect_host then 129 if config.connect_host then
134 c.connect_host = config.connect_host 130 c.connect_host = config.connect_host;
135 end 131 end
136 if config.connect_port then 132 if config.connect_port then
137 c.connect_port = config.connect_port 133 c.connect_port = config.connect_port;
138 end 134 end
139 135
140 c:connect_client(config.jid, config.password); 136 c:connect_client(config.jid, config.password);
141 137
142 verse.loop(); 138 verse.loop();
143 end 139 end

mercurial