DJabberd/Bot.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 168 CC 11 SCAR 23.2
CC
11
Cov
0%
send_stanza
42.0
filter_incoming_client_builtin
12.0
finalize
6.0
CRAP 10.2
1 package DJabberd::Bot;
2 use strict;
3 use warnings;
4 use base 'DJabberd::Plugin';
5 use DJabberd::JID;
6 use Carp;
7 use Scalar::Util;
8 use List::Util qw(first);
9 use DJabberd::BotContext;
10  
11 our $logger = DJabberd::Log->get_logger();
12  
13 ### store the object in a closure, so we can retrieve it later
14 { my $singleton;
15 sub singleton { $singleton };
16
17 sub new {
18 my $self = shift;
19 $singleton = $self->SUPER::new( @_ );
20 }
21 }
22  
23 sub set_config_nodename {
24 my ($self, $nodename) = @_;
25 $self->{nodename} = $nodename;
26 }
27  
28 sub set_config_resource {
29 my ($self, $resource) = @_;
30 $self->{resource} = $resource;
31 }
32  
33 sub finalize {
34 my ($self) = @_;
35  
36 Carp::confess("Bot needs a nodename") unless $self->{nodename};
37 $self->{resource} ||= "bot";
38 }
39  
40 sub jid {
41 return $_[0]->{jid};
42 }
43  
44 sub bound_jid {
45 return $_[0]->jid;
46 }
47  
48 sub vhost {
49 return $_[0]->{vhost};
50 }
51  
52 sub register {
53 my ($self, $vhost) = @_;
54 my $barejid = DJabberd::JID->new("$self->{nodename}\@" . $vhost->server_name );
55 my $resource = $self->{resource};
56  
57 $self->{vhost} = $vhost;
58 Scalar::Util::weaken($self->{vhost});
59  
60 my $reg_jid;
61 my $regcb = DJabberd::Callback->new({
62 registered => sub {
63 (undef, $reg_jid) = @_;
64 $logger->debug("Bot $reg_jid is now registered");
65 },
66 error => sub {
67 $logger->error("Bot $barejid/$resource failed to register");
68 },
69 });
70  
71 $vhost->register_jid($barejid, $resource, $self, $regcb);
72 if ($reg_jid) {
73 $self->{jid} = $reg_jid;
74 DJabberd::Presence->set_local_presence(
75 $self->{jid},
76 DJabberd::Presence->available( from => $self->{jid} )
77 );
78 }
79 }
80  
81 # no-op bot logic:
82 sub process_text {
83 my ($self, $text, $from, $cb) = @_;
84 $cb->done;
85 }
86  
87 # TODO: not yet used.
88 sub context_timeout { 300 }
89  
90 sub get_context {
91 my ($self, $method, $fromjid) = @_;
92 # TODO: cache these, and time them out according to $self->context_timeout
93 if ($method eq "stanza") {
94 return DJabberd::BotContext->new($self->{vhost}, $method, $fromjid, $self->jid);
95 }
96 }
97  
98  
99 ############################################################################
100 # Connection-like methods, for when we're pretending to be a roster item, and registered
101 # ourselves with jidmap. (as opposed to being a component)
102 ############################################################################
103  
104 sub write {
105 # currently don't do anything here
106 $logger->warn("Ignoring writes");
107 }
108  
109 sub is_available { 1 }
110  
111 sub requested_roster { 0 }
112  
113 sub send_stanza {
114 my ($self, $stanza) = @_;
115  
116 if ($stanza->isa('DJabberd::Message')) {
117 my ($text, $html);
118 # find the plaintext
119 {
120 my $body = first { $_->element_name eq "body" } $stanza->children_elements
121 or return;
122 $text = $body->first_child
123 or return;
124 }
125 # find the HTML text
126 if (my $htmlnode = first { $_->element eq "{http://jabber.org/protocol/xhtml-im}html" }
127 $stanza->children_elements)
128 {
129 my $bodynode = $htmlnode->first_element;
130 if ($bodynode->element eq "{http://www.w3.org/1999/xhtml}body") {
131 $html = $bodynode->innards_as_xml;
132 }
133 }
134  
135 my $ctx = $self->get_context("stanza", $stanza->from_jid);
136 $ctx->add_text($text, $html);
137 $self->process_text($text, $stanza->from_jid, $ctx);
138 return;
139 }
140 elsif ($stanza->isa('DJabberd::Presence')) {
141 # Most presence-related stuff is handled by the server itself, However, we still
142 # need to handle presence subscription requests.
143 my $type = $stanza->attr('{}type');
144
145 if ($type eq 'subscribe') {
146 $logger->debug("Accepting presence subscripton request from ", $stanza->from_jid->as_string);
147 my $response = DJabberd::Presence->new('jabber:client', 'presence', {
148 '{}type' => 'chat',
149 '{}to' => $stanza->from_jid->as_string,
150 '{}type' => 'subscribed',
151 }, []);
152 $self->send_stanza_as_client($response);
153 }
154  
155 }
156  
157 }
158  
159 # Since DJabberd::Bot is acting like a client, it must deliver messages using
160 # this method which does the same thing as a DJabberd::Connection::ClientIn would
161 # do if a stanza was recieved from a real client.
162 sub send_stanza_as_client {
163 my ($self, $stanza) = @_;
164 $self->vhost->hook_chain_fast(
165 "filter_incoming_client",
166 [ $stanza, $self ],
167 {
168 reject => sub { }, # just stops the chain
169 },
170 \&filter_incoming_client_builtin,
171 );
172 }
173 sub filter_incoming_client_builtin {
174 my ($vhost, $cb, $stanza, $self) = @_;
175  
176 # if no from, we set our own
177 if (! $stanza->from_jid) {
178 my $bj = $self->jid;
179 $stanza->set_from($bj->as_string) if $bj;
180 }
181  
182 $vhost->hook_chain_fast(
183 "switch_incoming_client",
184 [ $stanza ],
185 {
186 process => sub { }, # Do nothing, because we don't actually have a $conn object to pass into $stanza->process
187 deliver => sub { $stanza->deliver($vhost) },
188 },
189 sub {
190 $stanza->on_recv_from_client($self);
191 },
192 );
193 }
194  
195 1;