Chatroom invitations
Martin Atkins
mart at degeneration.co.uk
Wed Nov 7 20:27:11 UTC 2007
Clive Holloway wrote:
>
> This snippet allows chatroom members to send invites to other users.
>
[snip]
>
> if ($stanza->isa("DJabberd::Message")
> && $stanza->{children}[0]{element} eq 'x'
> && $stanza->{children}[0]{children}[0]{element} eq 'invite') {
>
This seems risky, since I believe <message> stanzas can come with a
<body> in them for fallback in clients without MUC support, like this:
<message ...>
<subject>Conference Invitation</subject>
<body>Please join me in the conference room "blah"
on conference.example.com.</body>
<x xmlns="jabber:x:conference">
<invite ... />
</x>
</message>
Probably should also verify the namespace of that "x" element, too. The
MUC spec seems to suggest that it should be:
<http://jabber.org/protocol/muc#user>
You can support this succinctly as follows:
use List::Util qw(first);
# ...
my $invite_elem = first {
$_->element eq "{http://jabber.org/protocol/muc#user}x"
} $stanza->children_elements;
DJabberd::Bot uses this technique to extract "body" and
"{http://jabber.org/protocol/xhtml-im}html" elements from incoming messages.
>
> Is there a cleaner way of calling attributes like this?
>
> $stanza->{children}[0]{element}
>
I believe that you could also write this as
$stanza->first_element->element, but this will return the name qualified
with its namespace as in the example above, rather than the bare local
name as in your example.
See DJabberd::XMLElement for further accessor methods.
More information about the Djabberd
mailing list