init.lua

Tue, 22 Dec 2009 05:06:29 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Tue, 22 Dec 2009 05:06:29 +0000
changeset 2
6b31cc678fd7
parent 1
f9e4ee00f557
child 3
d1a9fb6495c6
permissions
-rw-r--r--

Add configured plugins to the bot instead of the stream

0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local verse = require "verse";
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local st = require "util.stanza";
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 module("riddim", package.seeall);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 plugins = {};
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local riddim_mt = {};
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 riddim_mt.__index = riddim_mt;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function new(stream, config)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 if not stream then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 error("riddim.new(): Verse stream required as first parameter", 2);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 return setmetatable({ stream = stream, config = config or {} }, riddim_mt);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 -- self.conn is ready for stanzas
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function riddim_mt:start()
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 self:add_plugin("groupchat");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 self:add_plugin("commands");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 self:add_plugin("ping");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 self:event("started");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 self.stream:hook("stanza", function (stanza)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local body = stanza:get_child("body");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local event = {
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 sender = { jid = stanza.attr.from };
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 body = (body and body:get_text()) or nil;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 stanza = stanza;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 };
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if stanza.name == "message" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local replied;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local bot = self;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 function event:reply(reply)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if replied then return false; end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 replied = true;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return bot:send_message(stanza.attr.from, reply);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local ret;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if xmlns then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 event.xmlns = xmlns;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 print(event.stanza)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 ret = self:event("iq/"..xmlns, event);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if not ret then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 ret = self:event(stanza.name, event);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 else
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 ret = self:event(stanza.name, event);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 if ret and type(ret) == "table" and ret.name then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 self:send(ret);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 return ret;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end, 1);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 function riddim_mt:send(s)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 return self.stream:send(tostring(s));
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 function riddim_mt:event(name, ...)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return self.stream:event("bot/"..name, ...);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 function riddim_mt:hook(name, ...)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 return self.stream:hook("bot/"..name, ...);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 function riddim_mt:send_message(to, text, formatted_text)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 self:send(st.message({ to = to, type = "chat" }):tag("body"):text(text));
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 function riddim_mt:add_plugin(name)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 require("riddim.plugins."..name);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 return riddim.plugins[name](self);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 -- Built-in bot starter
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 if not (... and package.loaded[...] ~= nil) then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 require "verse.client";
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 -- Config loading
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local chunk, err = loadfile("config.lua");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if not chunk then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 print("File or syntax error:", err);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return 1;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 local config = {};
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 setfenv(chunk, setmetatable(config, {__index = _G}));
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 local ok, err = pcall(chunk);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 if not ok then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 print("Error while processing config:", err);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return 1;
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 setmetatable(config, nil);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 if not config.jid then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 io.write("Enter the bot's JID: ");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 config.jid = io.read("*l");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 if not config.password then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 io.write("Enter the password for "..config.jid..": ");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 config.password = io.read("*l");
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 -- Create the stream object and bot object
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 local c = verse.new();
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 local b = riddim.new(c, config);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 if config.debug then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 c:hook("incoming-raw", print);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 for _, plugin in ipairs(config.plugins or {"ping"}) do
2
6b31cc678fd7 Add configured plugins to the bot instead of the stream
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
121 b:add_plugin(plugin);
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 b:hook("started", function ()
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 b:send(verse.presence());
1
f9e4ee00f557 Fix to fetch autojoin list from the config (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
126 for k, v in pairs(config.autojoin or {}) do
0
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 if type(k) == "number" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 b:join_room(v);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 elseif type(k) == "string" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 if type(v) == "string" then
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 b:join_room(k, v);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 c:hook("binding-success", function () b:start(); end)
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 c:connect_client(config.jid, config.password);
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 verse.loop();
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143
7d84f4403d67 Initial commit, hello world!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 return _M;

mercurial