Guest User

Untitled

a guest
May 24th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use Net::IRC;
  4. use strict;
  5. use Data::Dumper;
  6.  
  7. # create a WeatherGet object (downloads and parses weather from
  8. # weatherunderground.com)
  9.  
  10.  
  11. my $irc = new Net::IRC;
  12.  
  13. my $conn = $irc->newconn(
  14. Server => shift || 'irc.freenode.com',
  15. Port => shift || '6667',
  16. Nick => 'Upper',
  17. Ircname => '!uptime',
  18. Username => 'upper'
  19. );
  20.  
  21. $conn->{channel} = shift || '#uptime-demo';
  22.  
  23. sub on_connect {
  24.  
  25. my $conn = shift;
  26.  
  27. # when we connect, join our channel and greet it
  28. $conn->join($conn->{channel});
  29. $conn->privmsg($conn->{channel}, 'booya! it\'s Upper time!');
  30. }
  31.  
  32. sub on_public {
  33.  
  34. # on an event, we get connection object and event hash
  35. my ($conn, $event) = @_;
  36.  
  37. # this is what was said in the event
  38. my $text = $event->{args}[0];
  39.  
  40. # regex the text to see if it begins with !roker
  41. if ($text =~ /^\!uptime/) {
  42.  
  43.  
  44. # if so, pass the text and the nick off to the weather method
  45. #my $weather_text = weather($1, $event->{nick});
  46. my $uptime_text = qx{ uptime };
  47. $conn->privmsg($event->{to}[0], $uptime_text);
  48. }
  49. }
  50.  
  51.  
  52. sub on_msg {
  53. my ($conn, $event) = @_;
  54.  
  55. # This is a *really* hacky part. I usually use this bot on an IRC
  56. # server that doesn't send the end of MOTD that signifies connect, so
  57. # I use this to simulate the connect.
  58. #
  59. # It basically says "if someone with my nick /msg you with 'join',
  60. # then you've connected"
  61. if (
  62. ($event->{nick} eq 'wholok') and
  63. ($event->{args}[0] eq 'join')){
  64. on_connect($conn);
  65. }
  66.  
  67. # Under normal circumstances, simply reply to the nick that there's
  68. # no reason to /msg RokerBot.
  69. else {
  70. $conn->privmsg($event->{nick}, "Don't nobody /msg Upper.");
  71. }
  72.  
  73. }
  74.  
  75. sub on_notice {
  76.  
  77. my ($conn, $event) = @_;
  78.  
  79. # This handles nick registration. On some IRC networks, you can
  80. # password-protect you nick. The IRC server will send you a "notice"
  81. # as NickServ that you have to identify with your passowrd.
  82. if (
  83. ($event->{nick} eq 'NickServ') and
  84. ($event->{args}[0] eq 'If you do not change within one minute, I will change your nick.')
  85. ) {
  86. # send an /msg to NickServ with the password
  87. $conn->privmsg('NickServ', 'identify rokerman');
  88.  
  89. # This is another hack, based on the fact that I've registered
  90. # the nick "RokerBot" on the IRC server which doesn't
  91. # send the end of MOTD message. So when I'm asked to
  92. # send my password, I know I've connected.
  93. #
  94. # This is redundant with the behavior covered in on_msg
  95. on_connect($conn);
  96. }
  97. }
  98.  
  99. sub default {
  100.  
  101. # This is helpful to see what an event returns. Data::Dumper will
  102. # recursively reveal the structure of any value
  103. my ($conn, $event) = @_;
  104. print Dumper($event);
  105. }
  106.  
  107.  
  108.  
  109. # add handlers for our standard events
  110. $conn->add_handler('public', \&on_public);
  111. $conn->add_handler('msg', \&on_msg);
  112. $conn->add_handler('notice', \&on_notice);
  113. $conn->add_handler('376', \&on_connect);
  114.  
  115. # experiment with the cping event, printing out to standard output
  116. $conn->add_handler('cping', \&default);
  117.  
  118. # start IRC
  119. $irc->start;
Add Comment
Please, Sign In to add comment