usr bin env perl use strict use warnings use LWP Simple use URI Escape

  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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env perl
use strict;
use warnings;
use LWP::Simple;
use URI::Escape;
use utf8;
use Math::Trig;
$| = 1;
binmode(STDOUT, ':utf8');
use Data::Dumper;
use AnyEvent::XMPP::IM::Connection;
use AnyEvent::XMPP::Ext::Disco;
use AnyEvent::XMPP::Ext::MUC;
my $nick = "O_O";
my $room = 'sg@conference.jabber.ru';
my $own_jid = "azq2\@jabber.ru";
my $start_time = time;
my $cond = AnyEvent->condvar;
my $conn = AnyEvent::XMPP::IM::Connection->new(
jid => $own_jid,
password => '',
resource => "xuj"
);
my $disco = AnyEvent::XMPP::Ext::Disco->new;
my $muc = AnyEvent::XMPP::Ext::MUC->new(disco => $disco);
$conn->add_extension($disco);
$conn->add_extension($muc);
$conn->reg_cb(contact_request_subscribe => sub {
my (undef, undef, $contact) = @_;
$contact->send_subscribed;
});
$conn->reg_cb(message => sub {
my (undef, $msg) = @_;
print "message = ".$msg->body."\n";
my $out_msg = parse_cmd($msg->body, $msg->from);
if (defined $out_msg) {
my $reply = $msg->make_reply;
$reply->add_body($out_msg);
$reply->send;
}
});
$conn->reg_cb(session_ready => sub {
$muc->join_room($conn, $room, $nick);
});
$muc->reg_cb(message => sub {
my (undef, undef, $msg) = @_;
print "body = ".$msg->body."\n";
return if $msg->is_delayed;
my $body = $msg->body;
if ($msg->type && $msg->type eq 'groupchat') {
return if ($msg->from eq $own_jid);
if ($body =~ /^$nick([:,])\s*(.*)/) {
my $from = $msg->from_nick;
$body = $2;
} else {
#return;
}
}
my $out_msg = parse_cmd($body, $msg->from);
if (defined $out_msg) {
my $reply = $msg->make_reply;
$reply->add_body($out_msg);
$reply->send;
return;
}
if ($body =~ /Your messages to/) {
AnyEvent::XMPP::IM::Message->new (
body => $msg->from.": ".$msg->body,
to => "azq2\@ya.ru"
)->send($conn);
}
});
my @money_types = ('ATS','AUD','BEF','BYR','CAD','CHF','CNY','DEM','DKK','EEK',
'EGP','ESP','EUR','FIM','FRF','GBP','GRD','IEP','ISK','ITL',
'JPY','KGS','KWD','KZT','LTL','NLG','NOK','PTE','SDR','SEK',
'SGD','TRL','TRY','UAH','USD','XDR','YUN','RUB');
my @months = ("Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь");
sub parse_cmd {
my ($cmd, $jid) = @_;
my @args = split(/\s+/, $cmd);
if ($args[0] eq "cy") {
my $from = undef;
my $to = undef;
my $sum = 0;
for (my $i = 1; $i <= 3; ++$i) {
last if (!defined($args[$i]));
if ($args[$i] =~ /^\d+(\.\d+)?$/) {
$sum = $args[$i];
} elsif (!defined($from)) {
$from = uc($args[$i]);
} elsif (!defined($to)) {
$to = uc($args[$i]);
}
}
if (!defined($to) || !defined($from) || !($from ~~ @money_types) || !($to ~~ @money_types)) {
return $args[0]." <from> <to> <sum>\nВалюты: ".join(", ", @money_types);
}
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
my $data = get "http://quote.rbc.ru/cgi-bin/conv/external/converter.shtml?source=cb.0&tid_from=$from&commission=1.0&tid_to=$to&summa=$sum&day=$mday&mon=".uri_escape_utf8($months[$mon - 1])."&year=".(1900 + $year);
if ($data =~ /<td class="b">([\d\.]+)<\/td>\s+<\/tr>\s+<tr>\s+<td>[^<>]+<\/td>\s+<td class="b">([\.\d]+)<\/td>\s+<\/tr>\s+<tr>\s+<td>[a-z]+<\/td>\s+<td class="b">([\.\d]+)<\/td>/sui) {
return "$1 $from = $3 $to (Курс: 1 $from = $2 $to)";
}
} elsif ($args[0] eq "dec2bin") {
return dec2bin($args[1]);
} elsif ($args[0] eq "bin2dec") {
return bin2dec($args[1]);
} elsif ($args[0] eq "hex2dec") {
return hex($args[1]);
} elsif ($args[0] eq "dex2hex") {
return sprintf("%d", $args[1]);
} elsif ($args[0] eq "ping") {
return "pong от тебя ".((-100 + int(rand(6000))) / 1000)." секунд. ";
} elsif ($args[0] eq "shell") {
if ($jid =~ /^azq2\@ya.ru\//) {
my $code = " ".substr($cmd, length $args[0]);
return `$code 2>&1`;
}
} elsif ($args[0] eq "dex2hex") {
return sprintf("%d", $args[1]);
} elsif ($args[0] eq "dex2hex") {
return sprintf("%d", $args[1]);
} elsif ($args[0] eq "icalc") {
my $code = " ".substr($cmd, length $args[0]);
$code =~ s/([^\d\+\-\*\/\^\%\|\&\sa-z\,><\(\)])//sig;
$code =~ s/([^a-z0-9])([a-z][a-z0-9]*)/check_calc_function($1, $2)/sieg;
my $out;
$out = eval("\$out = ".$code.";");
$out = "" if (!defined $out);
$out .= $@ if ($@);
print "code = '$code'\n'$out'\n";
return $out;
}
return undef;
}
our %calc_functions = (
"pow" => sub { return $_[0] ** $_[1] },
"sin" => sub { return sin($_[0]); },
"cos" => sub { return cos($_[0]); },
"atan2" => sub { return atan2($_[0], $_[1]); },
"exp" => sub { return exp($_[0]); },
"hex" => sub { return hex($_[0]); },
"int" => sub { return int($_[0]); },
"log" => sub { return log($_[0]); },
"oct" => sub { return oct($_[0]); },
"rand" => sub { return rand($_[0]); },
"time" => sub { return time(); },
"sqrt" => sub { return sqrt($_[0]); },
"abs" => sub { return $_[0] < 0 ? -$_[0] : $_[0]; },
"max" => sub { return $_[0] > $_[1] ? $_[0] : $_[1]; },
"min" => sub { return $_[0] < $_[1] ? $_[0] : $_[1]; },
"tan" => sub { return tan($_[0]); },
"csc" => sub { return csc($_[0]); },
"cosec" => sub { return cosec($_[0]); },
"sec" => sub { return sec($_[0]); },
"cot" => sub { return cot($_[0]); },
"cotan" => sub { return cotan($_[0]); },
"asin" => sub { return asin($_[0]); },
"acos" => sub { return acos($_[0]); },
"atan" => sub { return atan($_[0]); },
"atan2" => sub { return atan2($_[0], $_[0]); },
"acsc" => sub { return acsc($_[0]); },
"acosec" => sub { return acosec($_[0]); },
"asec" => sub { return asec($_[0]); },
"acot" => sub { return acot($_[0]); },
"acotan" => sub { return acotan($_[0]); },
"sinh" => sub { return sinh($_[0]); },
"cosh" => sub { return cosh($_[0]); },
"tanh" => sub { return tanh($_[0]); },
"csch" => sub { return csch($_[0]); },
"cosech" => sub { return cosech($_[0]); },
"sech" => sub { return sech($_[0]); },
"coth" => sub { return coth($_[0]); },
"cotanh" => sub { return cotanh($_[0]); },
"asinh" => sub { return asinh($_[0]); },
"acosh" => sub { return acosh($_[0]); },
"atanh" => sub { return atanh($_[0]); },
"acsch" => sub { return acsch($_[0]); },
"acosech" => sub { return acosech($_[0]); },
"asech" => sub { return asech($_[0]); },
"acoth" => sub { return acoth($_[0]); },
"acotanh" => sub { return acotanh($_[0]); },
"pi" => sub { return pi(); },
"pi2" => sub { return pi2(); },
"pi4" => sub { return pi4(); },
"pip2" => sub { return pip2(); },
"pip4" => sub { return pip4(); },
"deg2rad" => sub { return deg2rad($_[0]); },
"grad2rad" => sub { return grad2rad($_[0]); },
"rad2deg" => sub { return rad2deg($_[0]); },
"grad2deg" => sub { return grad2deg($_[0]); },
"deg2grad" => sub { return deg2grad($_[0]); },
"rad2grad" => sub { return rad2grad($_[0]); },
"dec2bin" => sub { return dec2bin($_[0]); },
"system" => sub { return "bash: ".$_[0].": команда не найдена"; },
);
sub calc {
die "function ".$_[0]." not found!"
if (!exists $calc_functions{$_[0]});
return $calc_functions{$_[0]};
}
sub check_calc_function {
return $_[0]."&{calc('".$_[1]."')}";
}
sub dec2bin {
my $str = unpack("B32", pack("N", shift));
$str =~ s/^0+(?=\d)//;
return $str;
}
sub bin2dec {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
$conn->connect;
$cond->recv;