This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.pl
145 lines (122 loc) · 3.47 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl
use strict;
use utf8;
use AnyEvent;
use AnyEvent::XMPP::Client;
use AnyEvent::XMPP::Util qw/split_jid/;
use Net::Ping;
use Module::Find;
use Module::Load;
use Time::HiRes;
use FindBin qw($Bin);
binmode STDOUT, ":utf8";
my (%events, @help);
my %config;
my @configs = (
"$Bin/wbot.conf",
$ENV{HOME} . '/.config/wbot/wbot.conf',
'/etc/wbot.conf',
);
foreach my $config (sort @configs) {
if (-e $config && -f $config && -r $config) {
open CONFIG, "<", $config || die $!;
flock CONFIG, 2;
while (<CONFIG>){
chomp;
s/#.*//; s/^\s+//;
s/\s+$//; s/\[.*\]//;
next unless length;
my ($var, $value) = split /\s*=\s*/, $_, 2;
$config{$var} = $value;
}
close CONFIG;
last;
}
}
die "%config is empty" if $config{ownerJID} eq '' || $config{hostname} eq '' || $config{username} eq '' || $config{password} eq '';
map { load $_; $_->import(); $_->myINIT();} findallmod plugins;
sub registerEvent {
my ($type, $handler) = @_;
$events{$type} = $handler;
}
sub registerHelp {
push @help, shift;
}
registerHelp('.h, .help, help, ? или хелп - покажет справку по командам.');
my $j = AnyEvent->condvar;
my $clientObj = AnyEvent::XMPP::Client->new (debug => 0);
$clientObj->add_account($config{username} . '@' . $config{hostname} . '/' . $config{resource}, $config{password});
$clientObj->reg_cb(
session_ready => sub {
my ($client, $acc) = @_;
$client->set_presence('chat', undef, 10);
},
contact_request_subscribe => sub {
my ($client, $acc, $roster, $contact) = @_;
$contact->send_subscribed;
$contact->send_subscribe;
},
contact_subscribed => sub {
my ($client, $acc, $roster, $contact ) = @_;
$client->send_message("\n@{[ join qq{\n}, sort @help ]}" => $contact->jid, undef, 'chat');
},
disconnect => sub {
my ($client, $acc, $h, $p, $reas) = @_;
#print "disconnect ($h:$p): $reas\n";
$j->broadcast;
},
error => sub {
my ($client, $acc, $err) = @_;
#print "ERROR: " . $err->string . "\n";
},
message => sub {
my ($client, $acc, $msg) = @_;
my ($userName, $hostName, $senderResource) = split_jid($msg->from);
my $reply='';
for ($msg->any_body) {
accessLog($msg->any_body, $userName . '@' . $hostName, $senderResource) if $msg->any_body ne '';
if (length > 1 && substr($_, 0, 1) eq ".") {
foreach my $key (keys %events) {
if (/$key/is) {
$reply = $events{$key}->(
saved1 => $1,
saved2 => $2,
saved3 => $3,
senderJID => $userName . '@' . $hostName,
senderResource => $senderResource,
ownerJID => $config{ownerJID},
);
last;
}
}
$reply = "\n" . join "\n", sort @help if /^(?:\.h|\.help)/i;
} else {
$reply = "\n" . join "\n", sort @help if /^(?:help|хелп)/i;
if (/^(?:exit|quit)$/i) {
if ($config{ownerJID} eq $userName . '@' . $hostName) {
$clientObj->disconnect;
exit;
} else {
$reply = "Я не знаю Вас.";
}
}
if (/^ping\s+([0-9a-z.]+)$/i) {
my $host = $1;
my ($ret, $duration, $ip);
my $pingObj = Net::Ping->new("syn");
$pingObj->hires();
($ret, $duration, $ip) = $pingObj->ping($host);
if ($ret) {
$reply = sprintf("$host [ip: $ip] is alive (packet return time: %.2f ms)", 1000 * $duration);
} else {
$reply = "Не получается.";
}
$pingObj->close();
}
}
}
$client->send_message($reply => $msg->from, undef, 'chat');
}
);
$clientObj->start;
$j->wait;