Guest User

Untitled

a guest
Aug 26th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. #!/usr/bin/perl - w
  2.  
  3. use strict;
  4. use AnyEvent;
  5. use AnyEvent::XMPP::IM::Connection;
  6. use AnyEvent::XMPP::IM::Presence;
  7.  
  8. use AnyEvent::XMPP::Ext::Disco;
  9. use AnyEvent::XMPP::Ext::MUC;
  10. use AnyEvent::XMPP::IM::Roster;
  11.  
  12. use AnyEvent::XMPP::Util qw/split_jid/;
  13. use Net::LastFM;
  14. use Term::ReadKey;
  15.  
  16.  
  17.  
  18. # stores whether a condition was flagged
  19. my $j = AnyEvent->condvar;
  20.  
  21. my ($newMsg, $pres, $recentTracks, $key, %tracks, @track, %song, $joinmsg, $repl, $u, $domain, $pw);
  22.  
  23. print "Please enter your node: ";
  24. $u = <STDIN>;
  25. chomp($u);
  26.  
  27. print "Please enter your domain: ";
  28. $domain = <STDIN>;
  29. chomp($domain);
  30.  
  31. print "Please enter your password: ";
  32. ReadMode 'noecho';
  33. $pw = ReadLine;
  34. ReadMode 'normal';
  35. chomp($pw);
  36.  
  37. # Connection constructor
  38. my $cl = AnyEvent::XMPP::IM::Connection->new (
  39. jid => $u .'@'. $domain,
  40. password => $pw
  41. );
  42.  
  43. my $lastfm = Net::LastFM->new (
  44. api_key => '967fd9a44681c5ecf5f20eac0f01e2f6',
  45. api_secret => '04473a951f7a59cf84b55735755a4a67'
  46. );
  47.  
  48.  
  49. my $disco = AnyEvent::XMPP::Ext::Disco->new;
  50. my $muc = AnyEvent::XMPP::Ext::MUC->new (disco => $disco);
  51.  
  52.  
  53. # Callback functions
  54. $cl->reg_cb (
  55. session_ready => sub {
  56. my ($con, $acc) = @_;
  57. print "session ready\n";
  58.  
  59. $cl->add_extension ($disco);
  60. $cl->add_extension ($muc);
  61.  
  62. $muc->join_room($con, 'perlbot@conference.jabber.ccc.de', 'PerlBot');
  63.  
  64. $muc->reg_cb (
  65.  
  66. #when we join the room
  67. enter => sub {
  68. my($con, $room, $user) = @_;
  69.  
  70. $joinmsg = $room->make_message;
  71. $joinmsg->add_body("Hooray, i joined");
  72. $joinmsg->send;
  73.  
  74. },
  75.  
  76. #when somebody joins the room
  77. join => sub {
  78. my($con, $room, $user) = @_;
  79.  
  80. $joinmsg = $room->make_message;
  81. $joinmsg->add_body("Hi, ". $user->nick);
  82. $joinmsg->send;
  83. },
  84.  
  85. message => sub {
  86. my ($con, $room, $msg, $is_echo) = @_;
  87.  
  88. my $user = $msg->from_nick;
  89. my $username = $room->get_user($user);
  90.  
  91. return if $is_echo;
  92.  
  93. if($msg->any_body =~ m/^perlbot\s+what(\s+song\s+is\s+|'s\s+)playing\??/){
  94.  
  95. $recentTracks = $lastfm->request_signed (
  96. method => 'user.getRecentTracks',
  97. user => $msg->from_nick,
  98. limit => 5
  99. );
  100. %tracks = %{$recentTracks};
  101.  
  102. if(defined($tracks{"recenttracks"}{"track"})){
  103.  
  104. foreach my $kkey($tracks{"recenttracks"}{"track"}){
  105. @track = @{$kkey};
  106. foreach my $index(@track){
  107. %song = %{$index};
  108. $repl = $msg->make_reply;
  109. $repl->add_body ($song{"url"} . " -> ". $song{"name"});
  110. $repl->send;
  111. }
  112. }
  113. }else{
  114. $repl = $msg->make_reply;
  115. $repl->add_body ("Sorry can't find your name on last.fm");
  116. $repl->send;
  117. }
  118.  
  119. }elsif($msg->any_body =~ m/^perlbot\s+how\s+are\s+you\??/){
  120. $repl = $msg->make_reply;
  121. my @mood = ("I'm fine, thanks.", "I'm sick.");
  122. $repl->add_body($mood[int(rand(2))]);
  123. $repl->send;
  124.  
  125. }elsif($msg->any_body =~ m/^perlbot\s+add\s+me\s*$/){
  126. #check if contact is already listed
  127. print "JID : ". $username->real_jid ."\n";
  128. print "nick: ". $user. "\n";
  129. my $roster = $cl->get_roster;
  130. if($roster->get_contact($username->real_jid)){
  131. $repl = $msg->make_reply;
  132. $repl->add_body ("You are already on my roster");
  133. $repl->send;
  134. }else{
  135. $roster->new_contact($username->real_jid, $user, ("Jabber"), \&register);
  136. }
  137. }elsif($msg->any_body =~ m/^perlbot/) {
  138. $repl = $msg->make_reply;
  139. $repl->add_body ("i don't understand you");
  140. $repl->send;
  141. }
  142. }
  143.  
  144. );
  145.  
  146.  
  147. },
  148. connect => sub {
  149. print "Connected \n";
  150. },
  151. message => sub {
  152. my ($con, $msg) = @_;
  153. my ($user, $host, $res) = split_jid ($msg->from);
  154. my $username = join("", $user,'@',$host);
  155.  
  156. if($msg->any_body =~ m/^!quit$/){
  157. if($username eq 'crunch09@jabber.org'){
  158. $j->broadcast;
  159. }else{
  160. $newMsg = AnyEvent::XMPP::IM::Message->new(
  161. to => $username,
  162. body => "You cannot stop me.\n");
  163. $newMsg->send($con);
  164. }
  165. }
  166. },
  167. stream_pre_authentication => sub {
  168. print "Pre-authentication \n";
  169. },
  170. error => sub {
  171. my ($cl, $err) = @_;
  172. print "ERROR: " . $err->string . "\n";
  173. },
  174. roster_update => sub {
  175. my ($con, $roster, $contacts) = @_;
  176. print "Roster Update\n";
  177. for my $contact ($roster->get_contacts) {
  178. print "In my Roster: " . $contact->jid . "\n";
  179. }
  180. },
  181. presence_update => sub {
  182. my ($con, $roster, $contacts, $old_presence, $new_presence) = @_;
  183. for my $cont ($contacts) {
  184. if($pres = $cont->get_priority_presence ne undef){
  185. # if contact is online
  186. if(!defined($new_presence->show)){
  187. print $cont->jid . " is now online\n";
  188. }else{
  189. print $cont->jid . " is now " . $new_presence->show . "\n";
  190. }
  191. } else {
  192. # When contact is offline
  193. print $cont->jid . " is now offline \n";
  194. }
  195. }
  196. },
  197.  
  198. contact_request_subscribe => sub {
  199. my($con, $roster, $contact, $message) = @_;
  200. print "Received subscribtion from ". $contact->jid;
  201. $contact->send_subscribed();
  202. },
  203. message_error => sub {
  204. print "error";
  205. }
  206. );
  207.  
  208.  
  209. sub register {
  210. my($contact, $error) = @_;
  211.  
  212. if(!defined($error)){
  213. print "Contact added\n";
  214. $contact->send_subscribe();
  215. }
  216.  
  217. }
  218.  
  219.  
  220.  
  221. $cl->connect();
  222. $j->wait;
  223.  
  224. print "Programm wird beendet.\n";
Add Comment
Please, Sign In to add comment