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) |
327a4b1a |
16 | for qw(bridge_name jabber_config irc_config); |
17 | |
18 | has server => (is => 'ro', required => 1, weak_ref => 1); |
21a18863 |
19 | |
20 | has userconfig_set => ( |
21 | is => 'ro', # Moobug init_arg => undef, |
22 | default => sub { UserConfigSet->new } |
23 | ); |
24 | |
25 | has chain_set => ( |
26 | is => 'ro', # Moobug init_arg => undef, |
27 | default => sub { ChainSet->new } |
28 | ); |
29 | |
30 | sub _build_xmpp_client { |
31 | my ($self) = @_; |
32 | $self->_login_xmpp_client($self->_new_child(XMPP_Client, { })); |
33 | } |
34 | |
35 | sub _login_xmpp_client { |
36 | my ($self, $xmpp) = @_; |
37 | my $conf = $self->jabber_config; |
38 | $xmpp->login( |
39 | jid => $conf->user, host => $conf->server, password => $conf->pass, |
327a4b1a |
40 | on_message => $self->_replace_weakself('handle_message'), |
21a18863 |
41 | ); |
42 | $xmpp; |
43 | } |
44 | |
327a4b1a |
45 | sub BUILD { $_[0]->xmpp_client } |
46 | |
21a18863 |
47 | sub handle_message { |
48 | my ($self, $msg) = @_; |
49 | |
50 | my $me = $self->jabber_config->user; |
51 | |
52 | return log_debug { |
53 | "Received message for ${\$msg->to} instead of $me - ignoring" |
54 | } unless $msg->to eq $me; |
55 | |
56 | (my $from = $msg->from) =~ s/\/.*//; |
57 | |
58 | return log_debug { |
59 | "Received message from me ($me) - ignoring" |
60 | } if $from eq $me; |
61 | |
62 | return log_debug { |
63 | "Received message from user ${from} not in my config" |
64 | } unless my $user = $self->userconfig_set->get({ name => $from }); |
65 | |
66 | if (my $chain = $self->chain_set->get({ jabber_user => $from })) { |
67 | $chain->handle_xmpp_message($msg); |
68 | } else { |
69 | if ($msg->body =~ m{^\s*/start\s*$}) { |
70 | $self->send_xmpp_message({ |
71 | to => $from, body => 'Connecting ...' |
72 | }); |
73 | $self->start_chain_for($user)->on_finished( |
74 | $self->_capture_weakself(sub { |
75 | my ($self, $result) = @_; |
327a4b1a |
76 | if ($result->{chain}) { |
77 | $self->chain_set->add($result->{chain}); |
78 | } |
21a18863 |
79 | $self->send_xmpp_message({ |
327a4b1a |
80 | to => $from, body => 'Connection result: '.$result->{message} |
21a18863 |
81 | }); |
82 | }) |
83 | ); |
84 | } else { |
85 | $self->send_xmpp_message({ |
86 | to => $from, |
87 | body => 'Not currently connected - send /start to connect' |
88 | }); |
89 | } |
90 | } |
91 | } |
92 | |
93 | sub send_xmpp_message { |
94 | my ($self, $args) = @_; |
95 | $self->xmpp_client->compose(%$args)->send; |
96 | } |
97 | |
327a4b1a |
98 | sub start_chain_for { |
99 | my ($self, $user) = @_; |
100 | $self->server->start_chain({ |
101 | jabber_tower => $self, |
102 | jabber_user => $user->name, |
103 | irc_tower => { |
104 | server => $self->irc_config->server, |
105 | irc_nick => $user->irc_nick |
106 | }, |
107 | irc_channel => $self->irc_config->channel, |
108 | }); |
109 | } |
110 | |
21a18863 |
111 | 1; |
112 | |
113 | # and later |
114 | |
115 | # package App::Clifton::Tower::IRC; |
116 | |
117 | # has irc_client |
118 | |
119 | # has irc_server_name |
120 | |
121 | # has user_config |
122 | |
123 | # has chain_set |
124 | |
125 | # package App::Clifton::Chain |
126 | |
127 | # |