forked from alcy/OpsBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJabber.pm
More file actions
80 lines (70 loc) · 1.83 KB
/
Jabber.pm
File metadata and controls
80 lines (70 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package OpsBot::Jabber;
use strict;
use warnings;
require Exporter;
use AnyEvent::XMPP::Client;
use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Ext::MUC;
use OpsBot::Config;
use OpsBot::RT qw(&process_rt);
use OpsBot::Nagios qw(&nagios);
our $VERSION = 1.00;
our @ISA = qw(Exporter);
our @EXPORT = ();
our @EXPORT_OK = qw(jabber);
$| = 1;
my $client = AnyEvent::XMPP::Client->new();
my $disco = AnyEvent::XMPP::Ext::Disco->new;
my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
$client->add_extension($disco);
$client->add_extension($muc);
sub initialize {
my $jabber = $plugins{jabber};
my @params = ( 'user', 'pass', 'nick', 'rooms');
return my ( $u, $p, $nick, $rooms ) = map { $jabber->{$_} } @params;
}
my $j = AnyEvent->condvar;
my $sendreply = sub {
my ( $msg, $output_from_plugin ) = @_;
my $reply = $msg->make_reply;
$reply->add_body($output_from_plugin);
$reply->send;
};
my $parsemsg = sub {
my $msg = shift;
my $body = $msg->body;
for my $plugin ( keys %plugins ) {
if ( $body =~ /^\!$plugin/ ) {
return $plugins{$plugin}->{entrysub}->($body);
}
}
};
sub jabber {
my ( $u, $p, $nick, $rooms ) = initialize();
$client->add_account($u, $p);
$client->reg_cb(
session_ready => sub {
my ( $cl, $acc ) = @_;
for my $room ( @$rooms ) {
$muc->join_room($acc->connection, $room, $nick, { history => { chars => 0 } });
}
$muc->reg_cb(
message => sub {
my ( $cl, $acc, $msg, $is_echo ) = @_;
return if $is_echo;
return if $msg->is_delayed;
my $output = $parsemsg->($msg);
if ( !$output ) {
return;
} else {
$sendreply->($msg, $output);
}
}
);
}
);
$client->start;
$j->wait;
}
1;