Fri, 14 May 2010 14:18:23 +0100
tell.lua: prevent executing the command in a non-groupchat environment.
0 | 1 | local verse = require "verse"; |
2 | local st = require "util.stanza"; | |
3 | ||
4 | module("riddim", package.seeall); | |
5 | plugins = {}; | |
6 | ||
7 | local riddim_mt = {}; | |
8 | riddim_mt.__index = riddim_mt; | |
9 | ||
10 | function new(stream, config) | |
11 | if not stream then | |
12 | error("riddim.new(): Verse stream required as first parameter", 2); | |
13 | end | |
3
d1a9fb6495c6
Don't allow loading the same plugin more than once
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
14 | return setmetatable({ stream = stream, config = config or {}, plugins = {} }, riddim_mt); |
0 | 15 | end |
16 | ||
17 | -- self.conn is ready for stanzas | |
18 | function riddim_mt:start() | |
19 | self:add_plugin("groupchat"); | |
20 | self:add_plugin("commands"); | |
21 | self:add_plugin("ping"); | |
8
9663866ca475
add basic command tell support
Thilo Cestonaro <thilo@cestona.ro>
parents:
6
diff
changeset
|
22 | self:add_plugin("tell"); |
0 | 23 | self:event("started"); |
24 | self.stream:hook("stanza", function (stanza) | |
25 | local body = stanza:get_child("body"); | |
26 | local event = { | |
27 | sender = { jid = stanza.attr.from }; | |
28 | body = (body and body:get_text()) or nil; | |
29 | stanza = stanza; | |
30 | }; | |
31 | if stanza.name == "message" then | |
32 | local replied; | |
33 | local bot = self; | |
34 | function event:reply(reply) | |
35 | if replied then return false; end | |
36 | replied = true; | |
37 | return bot:send_message(stanza.attr.from, reply); | |
38 | end | |
39 | end | |
40 | local ret; | |
41 | if stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then | |
42 | local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns; | |
43 | if xmlns then | |
44 | event.xmlns = xmlns; | |
45 | print(event.stanza) | |
46 | ret = self:event("iq/"..xmlns, event); | |
47 | if not ret then | |
48 | ret = self:event(stanza.name, event); | |
49 | end | |
50 | end | |
51 | else | |
52 | ret = self:event(stanza.name, event); | |
53 | end | |
54 | ||
55 | if ret and type(ret) == "table" and ret.name then | |
56 | self:send(ret); | |
57 | end | |
58 | return ret; | |
59 | end, 1); | |
60 | end | |
61 | ||
62 | function riddim_mt:send(s) | |
63 | return self.stream:send(tostring(s)); | |
64 | end | |
65 | ||
66 | function riddim_mt:event(name, ...) | |
67 | return self.stream:event("bot/"..name, ...); | |
68 | end | |
69 | ||
70 | function riddim_mt:hook(name, ...) | |
71 | return self.stream:hook("bot/"..name, ...); | |
72 | end | |
73 | ||
74 | function riddim_mt:send_message(to, text, formatted_text) | |
75 | self:send(st.message({ to = to, type = "chat" }):tag("body"):text(text)); | |
76 | end | |
77 | ||
78 | function riddim_mt:add_plugin(name) | |
3
d1a9fb6495c6
Don't allow loading the same plugin more than once
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
79 | if not self.plugins[name] then |
d1a9fb6495c6
Don't allow loading the same plugin more than once
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
80 | self.plugins[name] = require("riddim.plugins."..name); |
d1a9fb6495c6
Don't allow loading the same plugin more than once
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
81 | return riddim.plugins[name](self); |
d1a9fb6495c6
Don't allow loading the same plugin more than once
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
82 | end |
0 | 83 | end |
84 | ||
85 | -- Built-in bot starter | |
86 | if not (... and package.loaded[...] ~= nil) then | |
87 | require "verse.client"; | |
88 | ||
89 | -- Config loading | |
90 | local chunk, err = loadfile("config.lua"); | |
91 | if not chunk then | |
92 | print("File or syntax error:", err); | |
93 | return 1; | |
94 | end | |
95 | ||
96 | local config = {}; | |
97 | setfenv(chunk, setmetatable(config, {__index = _G})); | |
98 | local ok, err = pcall(chunk); | |
99 | if not ok then | |
100 | print("Error while processing config:", err); | |
101 | return 1; | |
102 | end | |
103 | setmetatable(config, nil); | |
104 | ||
105 | if not config.jid then | |
106 | io.write("Enter the bot's JID: "); | |
107 | config.jid = io.read("*l"); | |
108 | end | |
109 | ||
110 | if not config.password then | |
111 | io.write("Enter the password for "..config.jid..": "); | |
112 | config.password = io.read("*l"); | |
113 | end | |
114 | ||
115 | -- Create the stream object and bot object | |
116 | local c = verse.new(); | |
117 | local b = riddim.new(c, config); | |
118 | ||
119 | if config.debug then | |
120 | c:hook("incoming-raw", print); | |
121 | end | |
122 | ||
123 | 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
|
124 | b:add_plugin(plugin); |
0 | 125 | end |
126 | ||
127 | b:hook("started", function () | |
6
b0fec41e695b
initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
5
diff
changeset
|
128 | local presence = verse.presence() |
b0fec41e695b
initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
5
diff
changeset
|
129 | if b.caps then |
b0fec41e695b
initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
5
diff
changeset
|
130 | presence:add_child(b:caps()) |
b0fec41e695b
initial implementation of disco responses (XEP-0030) and entity caps sending (XEP-0115)
Hubert Chathi <hubert@uhoreg.ca>
parents:
5
diff
changeset
|
131 | end |
1
f9e4ee00f557
Fix to fetch autojoin list from the config (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
132 | for k, v in pairs(config.autojoin or {}) do |
0 | 133 | if type(k) == "number" then |
134 | b:join_room(v); | |
135 | elseif type(k) == "string" then | |
136 | if type(v) == "string" then | |
137 | b:join_room(k, v); | |
138 | end | |
139 | end | |
140 | end | |
141 | end); | |
142 | ||
143 | c:hook("binding-success", function () b:start(); end) | |
5
d9ed6e7d9936
allow specifying connect host and port in config file
Hubert Chathi <hubert@uhoreg.ca>
parents:
3
diff
changeset
|
144 | |
d9ed6e7d9936
allow specifying connect host and port in config file
Hubert Chathi <hubert@uhoreg.ca>
parents:
3
diff
changeset
|
145 | if config.connect_host then |
d9ed6e7d9936
allow specifying connect host and port in config file
Hubert Chathi <hubert@uhoreg.ca>
parents:
3
diff
changeset
|
146 | c.connect_host = config.connect_host |
d9ed6e7d9936
allow specifying connect host and port in config file
Hubert Chathi <hubert@uhoreg.ca>
parents:
3
diff
changeset
|
147 | end |
d9ed6e7d9936
allow specifying connect host and port in config file
Hubert Chathi <hubert@uhoreg.ca>
parents:
3
diff
changeset
|
148 | if config.connect_port then |
d9ed6e7d9936
allow specifying connect host and port in config file
Hubert Chathi <hubert@uhoreg.ca>
parents:
3
diff
changeset
|
149 | c.connect_port = config.connect_port |
d9ed6e7d9936
allow specifying connect host and port in config file
Hubert Chathi <hubert@uhoreg.ca>
parents:
3
diff
changeset
|
150 | end |
0 | 151 | |
152 | c:connect_client(config.jid, config.password); | |
153 | ||
154 | verse.loop(); | |
155 | end | |
156 | ||
157 | return _M; |