Commit | Line | Data |
21a18863 |
1 | package App::Clifton::Tower::Jabber; |
2 | |
3 | use aliased 'App::Clifton::UserConfigSet'; |
4 | use aliased 'App::Clifton::ChainSetByJabberUser' => 'ChainSet'; |
5 | use aliased 'Net::Async::XMPP::Client' => 'XMPP_Client'; |
6 | use Log::Contextual qw(:log); |
7 | use Moo; |
8 | |
9 | extends 'App::Clifton::Component'; |
10 | |
11 | has xmpp_client => (is => 'lazy'); |
12 | |
13 | sub xmpp_active { shift->xmpp_client->xmpp->is_loggedin } |
14 | |
15 | has $_ => (is => 'ro', required => 1) |
16 | for qw(bridge_name server jabber_config); |
17 | |
18 | has userconfig_set => ( |
19 | is => 'ro', # Moobug init_arg => undef, |
20 | default => sub { UserConfigSet->new } |
21 | ); |
22 | |
23 | has chain_set => ( |
24 | is => 'ro', # Moobug init_arg => undef, |
25 | default => sub { ChainSet->new } |
26 | ); |
27 | |
28 | sub _build_xmpp_client { |
29 | my ($self) = @_; |
30 | $self->_login_xmpp_client($self->_new_child(XMPP_Client, { })); |
31 | } |
32 | |
33 | sub _login_xmpp_client { |
34 | my ($self, $xmpp) = @_; |
35 | my $conf = $self->jabber_config; |
36 | $xmpp->login( |
37 | jid => $conf->user, host => $conf->server, password => $conf->pass, |
38 | ); |
39 | $xmpp; |
40 | } |
41 | |
42 | sub handle_message { |
43 | my ($self, $msg) = @_; |
44 | |
45 | my $me = $self->jabber_config->user; |
46 | |
47 | return log_debug { |
48 | "Received message for ${\$msg->to} instead of $me - ignoring" |
49 | } unless $msg->to eq $me; |
50 | |
51 | (my $from = $msg->from) =~ s/\/.*//; |
52 | |
53 | return log_debug { |
54 | "Received message from me ($me) - ignoring" |
55 | } if $from eq $me; |
56 | |
57 | return log_debug { |
58 | "Received message from user ${from} not in my config" |
59 | } unless my $user = $self->userconfig_set->get({ name => $from }); |
60 | |
61 | if (my $chain = $self->chain_set->get({ jabber_user => $from })) { |
62 | $chain->handle_xmpp_message($msg); |
63 | } else { |
64 | if ($msg->body =~ m{^\s*/start\s*$}) { |
65 | $self->send_xmpp_message({ |
66 | to => $from, body => 'Connecting ...' |
67 | }); |
68 | $self->start_chain_for($user)->on_finished( |
69 | $self->_capture_weakself(sub { |
70 | my ($self, $result) = @_; |
71 | $self->send_xmpp_message({ |
72 | to => $from, body => 'Connection result: '.$result |
73 | }); |
74 | }) |
75 | ); |
76 | } else { |
77 | $self->send_xmpp_message({ |
78 | to => $from, |
79 | body => 'Not currently connected - send /start to connect' |
80 | }); |
81 | } |
82 | } |
83 | } |
84 | |
85 | sub send_xmpp_message { |
86 | my ($self, $args) = @_; |
87 | $self->xmpp_client->compose(%$args)->send; |
88 | } |
89 | |
90 | 1; |
91 | |
92 | # and later |
93 | |
94 | # package App::Clifton::Tower::IRC; |
95 | |
96 | # has irc_client |
97 | |
98 | # has irc_server_name |
99 | |
100 | # has user_config |
101 | |
102 | # has chain_set |
103 | |
104 | # package App::Clifton::Chain |
105 | |
106 | # |