Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl - w
- use strict;
- use AnyEvent;
- use AnyEvent::XMPP::IM::Connection;
- use AnyEvent::XMPP::IM::Presence;
- use AnyEvent::XMPP::Ext::Disco;
- use AnyEvent::XMPP::Ext::MUC;
- use AnyEvent::XMPP::IM::Roster;
- use AnyEvent::XMPP::Util qw/split_jid/;
- use Net::LastFM;
- use Term::ReadKey;
- # stores whether a condition was flagged
- my $j = AnyEvent->condvar;
- my ($newMsg, $pres, $recentTracks, $key, %tracks, @track, %song, $joinmsg, $repl, $u, $domain, $pw);
- print "Please enter your node: ";
- $u = <STDIN>;
- chomp($u);
- print "Please enter your domain: ";
- $domain = <STDIN>;
- chomp($domain);
- print "Please enter your password: ";
- ReadMode 'noecho';
- $pw = ReadLine;
- ReadMode 'normal';
- chomp($pw);
- # Connection constructor
- my $cl = AnyEvent::XMPP::IM::Connection->new (
- jid => $u .'@'. $domain,
- password => $pw
- );
- my $lastfm = Net::LastFM->new (
- api_key => '967fd9a44681c5ecf5f20eac0f01e2f6',
- api_secret => '04473a951f7a59cf84b55735755a4a67'
- );
- my $disco = AnyEvent::XMPP::Ext::Disco->new;
- my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
- # Callback functions
- $cl->reg_cb (
- session_ready => sub {
- my ($con, $acc) = @_;
- print "session ready\n";
- $cl->add_extension ($disco);
- $cl->add_extension ($muc);
- $muc->join_room($con, 'perlbot@conference.jabber.ccc.de', 'PerlBot');
- $muc->reg_cb (
- #when we join the room
- enter => sub {
- my($con, $room, $user) = @_;
- $joinmsg = $room->make_message;
- $joinmsg->add_body("Hooray, i joined");
- $joinmsg->send;
- },
- #when somebody joins the room
- join => sub {
- my($con, $room, $user) = @_;
- $joinmsg = $room->make_message;
- $joinmsg->add_body("Hi, ". $user->nick);
- $joinmsg->send;
- },
- message => sub {
- my ($con, $room, $msg, $is_echo) = @_;
- my $user = $msg->from_nick;
- my $username = $room->get_user($user);
- return if $is_echo;
- if($msg->any_body =~ m/^perlbot\s+what(\s+song\s+is\s+|'s\s+)playing\??/){
- $recentTracks = $lastfm->request_signed (
- method => 'user.getRecentTracks',
- user => $msg->from_nick,
- limit => 5
- );
- %tracks = %{$recentTracks};
- if(defined($tracks{"recenttracks"}{"track"})){
- foreach my $kkey($tracks{"recenttracks"}{"track"}){
- @track = @{$kkey};
- foreach my $index(@track){
- %song = %{$index};
- $repl = $msg->make_reply;
- $repl->add_body ($song{"url"} . " -> ". $song{"name"});
- $repl->send;
- }
- }
- }else{
- $repl = $msg->make_reply;
- $repl->add_body ("Sorry can't find your name on last.fm");
- $repl->send;
- }
- }elsif($msg->any_body =~ m/^perlbot\s+how\s+are\s+you\??/){
- $repl = $msg->make_reply;
- my @mood = ("I'm fine, thanks.", "I'm sick.");
- $repl->add_body($mood[int(rand(2))]);
- $repl->send;
- }elsif($msg->any_body =~ m/^perlbot\s+add\s+me\s*$/){
- #check if contact is already listed
- print "JID : ". $username->real_jid ."\n";
- print "nick: ". $user. "\n";
- my $roster = $cl->get_roster;
- if($roster->get_contact($username->real_jid)){
- $repl = $msg->make_reply;
- $repl->add_body ("You are already on my roster");
- $repl->send;
- }else{
- $roster->new_contact($username->real_jid, $user, ("Jabber"), \®ister);
- }
- }elsif($msg->any_body =~ m/^perlbot/) {
- $repl = $msg->make_reply;
- $repl->add_body ("i don't understand you");
- $repl->send;
- }
- }
- );
- },
- connect => sub {
- print "Connected \n";
- },
- message => sub {
- my ($con, $msg) = @_;
- my ($user, $host, $res) = split_jid ($msg->from);
- my $username = join("", $user,'@',$host);
- if($msg->any_body =~ m/^!quit$/){
- if($username eq 'crunch09@jabber.org'){
- $j->broadcast;
- }else{
- $newMsg = AnyEvent::XMPP::IM::Message->new(
- to => $username,
- body => "You cannot stop me.\n");
- $newMsg->send($con);
- }
- }
- },
- stream_pre_authentication => sub {
- print "Pre-authentication \n";
- },
- error => sub {
- my ($cl, $err) = @_;
- print "ERROR: " . $err->string . "\n";
- },
- roster_update => sub {
- my ($con, $roster, $contacts) = @_;
- print "Roster Update\n";
- for my $contact ($roster->get_contacts) {
- print "In my Roster: " . $contact->jid . "\n";
- }
- },
- presence_update => sub {
- my ($con, $roster, $contacts, $old_presence, $new_presence) = @_;
- for my $cont ($contacts) {
- if($pres = $cont->get_priority_presence ne undef){
- # if contact is online
- if(!defined($new_presence->show)){
- print $cont->jid . " is now online\n";
- }else{
- print $cont->jid . " is now " . $new_presence->show . "\n";
- }
- } else {
- # When contact is offline
- print $cont->jid . " is now offline \n";
- }
- }
- },
- contact_request_subscribe => sub {
- my($con, $roster, $contact, $message) = @_;
- print "Received subscribtion from ". $contact->jid;
- $contact->send_subscribed();
- },
- message_error => sub {
- print "error";
- }
- );
- sub register {
- my($contact, $error) = @_;
- if(!defined($error)){
- print "Contact added\n";
- $contact->send_subscribe();
- }
- }
- $cl->connect();
- $j->wait;
- print "Programm wird beendet.\n";
Add Comment
Please, Sign In to add comment