-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.pl
executable file
·94 lines (83 loc) · 2.52 KB
/
bot.pl
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/perl
use common::sense;
use AnyEvent;
use AnyEvent::XMPP::Client;
use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::Version;
use AnyEvent::XMPP::Namespaces qw(xmpp_ns);
use YAML;
use File::Slurp;
use Carp; qw( carp croak );
my $config_file = 'bot.yaml';
if ( ! -e $config_file) {
croak("Config file <$config_file> does not exist, copy default from doc/examples/bot.yaml");
}
my $tmp = read_file($config_file) or croak("Can't load config: $!");
my $cfg = Load($tmp) or croak("Can't parse config: $!");
binmode STDOUT, ":utf8";
if (!defined($cfg->{'xmpp_user'}) || !defined($cfg->{'xmpp_pass'}) ) {
croak("Need xmmp_user and xmpp_pass in config!");
}
my $j = AnyEvent->condvar;
my $cl = AnyEvent::XMPP::Client->new (debug => 1);
my $disco = AnyEvent::XMPP::Ext::Disco->new;
my $version = AnyEvent::XMPP::Ext::Version->new;
$cl->add_extension ($disco);
$cl->add_extension ($version);
$cl->set_presence (undef, 'I\'m a talking bot.', 1);
$cl->add_account ($cfg->{'xmpp_user'}, $cfg->{'xmpp_pass'});
warn "connecting to $cfg->{xmpp_user}...\n";
my $module = {};
$module->{'help'} = sub {
my ($cl, $acc, $msg) = @_;
my $repl = $msg->make_reply;
$repl->add_body (
"\nSupported commands:\n"
. join("\n", keys(%$module))
);
warn "Got message: '".$msg->any_body."' from ".$msg->from."\n";
$repl->send;
};
$module->{'echo'} = sub {
my ($cl, $acc, $msg) = @_;
my $repl = $msg->make_reply;
my (undef, $reply) = split(/\s/,$msg->any_body);
$repl->add_body ( "Echo: " . $reply);
$repl->send;
};
$module->{'time'} = sub {
my ($cl, $acc, $msg) = @_;
my $repl = $msg->make_reply;
$repl->add_body (scalar localtime(time));
$repl->send;
};
$cl->reg_cb (
session_ready => sub {
my ($cl, $acc) = @_;
warn "connected!\n";
},
message => sub {
my ($cl, $acc, $msg) = @_;
my ($target_module, undef) = split(/\s+/,$msg->any_body);
if ( ! defined( $module->{$target_module} ) ) {
$target_module = 'help'; #show help if nonexisting module is called
}
&{$module->{$target_module}}($cl, $acc, $msg);
},
contact_request_subscribe => sub {
my ($cl, $acc, $roster, $contact) = @_;
$contact->send_subscribed;
warn "Subscribed to ".$contact->jid."\n";
},
error => sub {
my ($cl, $acc, $error) = @_;
warn "Error encountered: ".$error->string."\n";
$j->broadcast;
},
disconnect => sub {
warn "Got disconnected: [@_]\n";
$j->broadcast;
},
);
$cl->start;
$j->wait;