| 1 | package XIRCD::Component::Wassr; |
|---|
| 2 | use MooseX::POE; |
|---|
| 3 | |
|---|
| 4 | with 'MooseX::POE::Aliased'; |
|---|
| 5 | |
|---|
| 6 | use POE qw( |
|---|
| 7 | Component::Jabber |
|---|
| 8 | Component::Jabber::Error |
|---|
| 9 | Component::Jabber::Status |
|---|
| 10 | Component::Jabber::ProtocolFactory |
|---|
| 11 | Filter::XML::Node |
|---|
| 12 | Filter::XML::Utils |
|---|
| 13 | ); |
|---|
| 14 | use POE::Filter::XML::NS qw/:JABBER :IQ/; |
|---|
| 15 | |
|---|
| 16 | has 'config' => ( |
|---|
| 17 | isa => 'HashRef', |
|---|
| 18 | is => 'rw', |
|---|
| 19 | ); |
|---|
| 20 | |
|---|
| 21 | has 'jabber' => ( |
|---|
| 22 | isa => 'POE::Component::Jabber', |
|---|
| 23 | is => 'rw', |
|---|
| 24 | ); |
|---|
| 25 | |
|---|
| 26 | has 'jid' => ( |
|---|
| 27 | isa => 'Str', |
|---|
| 28 | is => 'rw', |
|---|
| 29 | ); |
|---|
| 30 | |
|---|
| 31 | sub debug(@) { ## no critic. |
|---|
| 32 | print @_ if $ENV{XIRCD_DEBUG}; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | sub get_args(@) { ## no critic. |
|---|
| 36 | return @_[9..19]; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | sub START { |
|---|
| 40 | my $self = shift; |
|---|
| 41 | |
|---|
| 42 | $self->alias('wassr'); |
|---|
| 43 | |
|---|
| 44 | debug "start wassr\n"; |
|---|
| 45 | |
|---|
| 46 | my ($username, $hostname) = split '@', $self->config->{jabber}->{username}; |
|---|
| 47 | |
|---|
| 48 | $self->jabber( |
|---|
| 49 | POE::Component::Jabber->new( |
|---|
| 50 | IP => $self->config->{jabber}->{server}, |
|---|
| 51 | Port => $self->config->{jabber}->{port} || 5222, |
|---|
| 52 | Hostname => $hostname, |
|---|
| 53 | Username => $username, |
|---|
| 54 | Password => $self->config->{jabber}->{password}, |
|---|
| 55 | Alias => 'jabber', |
|---|
| 56 | States => { |
|---|
| 57 | StatusEvent => 'status_handler', |
|---|
| 58 | InputEvent => 'input_handler', |
|---|
| 59 | ErrorEvent => 'error_handler', |
|---|
| 60 | }, |
|---|
| 61 | ConnectionType => +XMPP, |
|---|
| 62 | ) |
|---|
| 63 | ); |
|---|
| 64 | |
|---|
| 65 | POE::Kernel->post( ircd => 'join_channel', $self->config->{channel}, $self->alias ); |
|---|
| 66 | POE::Kernel->post( jabber => 'connect' ); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | event status_handler => sub { |
|---|
| 70 | my $self = shift; |
|---|
| 71 | my ($state,) = get_args(@_); |
|---|
| 72 | |
|---|
| 73 | if ($state == +PCJ_INIT_FINISHED) { |
|---|
| 74 | debug "init finished\n"; |
|---|
| 75 | $self->jid($self->jabber->jid); |
|---|
| 76 | |
|---|
| 77 | POE::Kernel->post(jabber => 'output_handler', POE::Filter::XML::Node->new('presence')); |
|---|
| 78 | POE::Kernel->post(jabber => 'purge_queue'); |
|---|
| 79 | } |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | event input_handler => sub { |
|---|
| 83 | my $self = shift; |
|---|
| 84 | my ($node,) = get_args(@_); |
|---|
| 85 | |
|---|
| 86 | debug "recv:", $node->to_str, "\n\n"; |
|---|
| 87 | |
|---|
| 88 | my ($body,) = $node->get_tag('body'); |
|---|
| 89 | |
|---|
| 90 | if ($body && $node->attr('from') =~ /^wassr-bot\@wassr\.jp/) { |
|---|
| 91 | my ($nick, $text) = $body->data =~ /^([A-Za-z0-9_.-]+): (.*)/s; |
|---|
| 92 | if ($nick && $text) { |
|---|
| 93 | POE::Kernel->post( ircd => 'publish_message', $nick, $self->config->{channel}, $text ); |
|---|
| 94 | } else { |
|---|
| 95 | POE::Kernel->post( ircd => 'publish_notice', $self->config->{channel}, $body->data ); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | event send_message => sub { |
|---|
| 101 | my $self = shift; |
|---|
| 102 | my ($message,) = get_args(@_); |
|---|
| 103 | |
|---|
| 104 | my $node = POE::Filter::XML::Node->new('message'); |
|---|
| 105 | |
|---|
| 106 | $node->attr('to', 'wassr-bot@wassr.jp'); |
|---|
| 107 | $node->attr('from', $self->{jid} ); |
|---|
| 108 | $node->attr('type', 'chat'); |
|---|
| 109 | $node->insert_tag('body')->data( $message ); |
|---|
| 110 | |
|---|
| 111 | debug "send:", $node->to_str, "\n\n"; |
|---|
| 112 | |
|---|
| 113 | POE::Kernel->post( jabber => output_handler => $node ); |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | event error_handler => sub { |
|---|
| 117 | my $self = shift; |
|---|
| 118 | my ($error,) = get_args(@_); |
|---|
| 119 | |
|---|
| 120 | if ( $error == +PCJ_SOCKETFAIL or $error == +PCJ_SOCKETDISCONNECT or $error == +PCJ_CONNECTFAIL ) { |
|---|
| 121 | print "Reconnecting!\n"; |
|---|
| 122 | POE::Kernel->post( jabber => 'reconnect' ); |
|---|
| 123 | } |
|---|
| 124 | elsif ( $error == +PCJ_SSLFAIL ) { |
|---|
| 125 | print "TLS/SSL negotiation failed\n"; |
|---|
| 126 | } |
|---|
| 127 | elsif ( $error == +PCJ_AUTHFAIL ) { |
|---|
| 128 | print "Failed to authenticate\n"; |
|---|
| 129 | } |
|---|
| 130 | elsif ( $error == +PCJ_BINDFAIL ) { |
|---|
| 131 | print "Failed to bind a resource\n"; |
|---|
| 132 | } |
|---|
| 133 | elsif ( $error == +PCJ_SESSIONFAIL ) { |
|---|
| 134 | print "Failed to establish a session\n"; |
|---|
| 135 | } |
|---|
| 136 | }; |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | 1; |
|---|