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 | |
7c5b5a3a |
30 | has full_jid => (is => 'rw'); |
31 | |
21a18863 |
32 | sub _build_xmpp_client { |
33 | my ($self) = @_; |
38180dc8 |
34 | $self->_login_xmpp_client($self->_new_child(XMPP_Client, { |
35 | on_message => $self->_replace_weakself('handle_message'), |
36 | })); |
21a18863 |
37 | } |
38 | |
39 | sub _login_xmpp_client { |
40 | my ($self, $xmpp) = @_; |
41 | my $conf = $self->jabber_config; |
42 | $xmpp->login( |
43 | jid => $conf->user, host => $conf->server, password => $conf->pass, |
44 | ); |
45 | $xmpp; |
46 | } |
47 | |
327a4b1a |
48 | sub BUILD { $_[0]->xmpp_client } |
49 | |
21a18863 |
50 | sub handle_message { |
51 | my ($self, $msg) = @_; |
52 | |
53 | my $me = $self->jabber_config->user; |
54 | |
7c5b5a3a |
55 | return log_info { |
56 | "Received error message" |
57 | } if $msg->type eq 'error'; |
58 | |
21a18863 |
59 | return log_debug { |
60 | "Received message for ${\$msg->to} instead of $me - ignoring" |
7c5b5a3a |
61 | } unless $msg->to =~ /^\Q$me/; # may be foo@gmail.com or .../gsklgsh |
62 | |
440ab057 |
63 | if (!$self->full_jid |
64 | or ($self->full_jid !~ m{/} and $msg->to =~ m{/}) |
65 | ) { |
66 | $self->full_jid($msg->to); # need foo@gmail.com/skldshgsdg here |
67 | } |
21a18863 |
68 | |
69 | (my $from = $msg->from) =~ s/\/.*//; |
70 | |
71 | return log_debug { |
72 | "Received message from me ($me) - ignoring" |
73 | } if $from eq $me; |
74 | |
75 | return log_debug { |
76 | "Received message from user ${from} not in my config" |
77 | } unless my $user = $self->userconfig_set->get({ name => $from }); |
78 | |
79 | if (my $chain = $self->chain_set->get({ jabber_user => $from })) { |
80 | $chain->handle_xmpp_message($msg); |
81 | } else { |
82 | if ($msg->body =~ m{^\s*/start\s*$}) { |
83 | $self->send_xmpp_message({ |
84 | to => $from, body => 'Connecting ...' |
85 | }); |
86 | $self->start_chain_for($user)->on_finished( |
87 | $self->_capture_weakself(sub { |
88 | my ($self, $result) = @_; |
327a4b1a |
89 | if ($result->{chain}) { |
90 | $self->chain_set->add($result->{chain}); |
91 | } |
21a18863 |
92 | $self->send_xmpp_message({ |
327a4b1a |
93 | to => $from, body => 'Connection result: '.$result->{message} |
21a18863 |
94 | }); |
95 | }) |
96 | ); |
97 | } else { |
98 | $self->send_xmpp_message({ |
99 | to => $from, |
100 | body => 'Not currently connected - send /start to connect' |
101 | }); |
102 | } |
103 | } |
104 | } |
105 | |
106 | sub send_xmpp_message { |
107 | my ($self, $args) = @_; |
3b539344 |
108 | s/&/&/g, s/"/"/g, s/</</g, s/>/>/g for $args->{body}; |
7c5b5a3a |
109 | $args->{from} = $self->full_jid; |
21a18863 |
110 | $self->xmpp_client->compose(%$args)->send; |
111 | } |
112 | |
327a4b1a |
113 | sub start_chain_for { |
114 | my ($self, $user) = @_; |
115 | $self->server->start_chain({ |
116 | jabber_tower => $self, |
117 | jabber_user => $user->name, |
118 | irc_tower => { |
119 | server => $self->irc_config->server, |
120 | irc_nick => $user->irc_nick |
121 | }, |
122 | irc_channel => $self->irc_config->channel, |
123 | }); |
124 | } |
125 | |
21a18863 |
126 | 1; |