comparison mod_track_muc_joins/README.markdown @ 2409:b426f1d46938

mod_track_muc_joins/README: Add some example code
author Kim Alvefur <zash@zash.se>
date Sat, 03 Dec 2016 15:01:15 +0100
parents 73096d8d924c
children e327b06b9a1b
comparison
equal deleted inserted replaced
2408:9d132153d786 2409:b426f1d46938
1 --- 1 ---
2 summary: Keep track of joined chat rooms 2 summary: Keep track of joined chat rooms
3 ... 3 ...
4 4
5 # Introduction
6
5 This module attempts to keep track of what MUC chat rooms users have 7 This module attempts to keep track of what MUC chat rooms users have
6 joined. It's not very useful on its own, but can be used by other 8 joined. It's not very useful on its own, but can be used by other
7 modules to influence decisions. 9 modules to influence decisions.
10
11 # Usage
12
13 Rooms joined and the associated nickname is kept in a table field
14 `rooms_joined` on the users session.
15
16 An example:
17
18 ``` lua
19 local jid_bare = require"util.jid".bare;
20
21 module:hook("message/full", function (event)
22 local stanza = event.stanza;
23 local session = prosody.full_sessions[stanza.attr.to];
24 if not session then
25 return -- No such session
26 end
27
28 local joined_rooms = session.joined_rooms;
29 if not joined_rooms then
30 return -- This session hasn't joined any rooms at all
31 end
32
33 -- joined_rooms is a map of room JID -> room nickname
34 local nickname = joined_rooms[jid_bare(stanza.attr.from)];
35 if nickname then
36 session.log("info", "Got a MUC message from %s", stanza.attr.from);
37
38 local body = stanza:get_child_text("body");
39 if body and body:find(nickname, 1, true) then
40 session.log("info", "The message contains my nickname!");
41 end
42 end
43 end);
44 ```