Advertisement
Guest User

dfds

a guest
Nov 26th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.54 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3. require "socket"
  4. require "rss"
  5. require "open-uri"
  6. # Don't allow use of "tainted" data by potentially dangerous operations
  7.  
  8. $SAFE=1
  9. # The irc class, which talks to the server and holds the main event loop
  10. class IRC
  11.     def initialize(server, port, nick, channel)
  12.         @server = server
  13.         @port = port
  14.         @nick = nick
  15.         @channel = channel
  16.     end
  17.  
  18.     def send(s)
  19.         # Send a message to the irc server and print it to the screen
  20.         puts "--> #{s}"
  21.         @irc.send "#{s}\n", 0
  22.     end
  23.  
  24.     def connect()
  25.         # Connect to the IRC server
  26.         @irc = TCPSocket.open(@server, @port)
  27.         send "USER blah blah blah :blah blah"
  28.         send "NICK #{@nick}"
  29.         send "JOIN #{@channel}"
  30.     end
  31.  
  32.     def evaluate(s)
  33.         # Make sure we have a valid expression (for security reasons), and
  34.         # evaluate it if we do, otherwise return an error message
  35.         if s =~ /^[-+*\/\d\s\eE.()]*$/ then
  36.             begin
  37.                 s.untaint
  38.                 return eval(s).to_s
  39.             rescue Exception => detail
  40.                 puts detail.message()
  41.             end
  42.         end
  43.         return "Error"
  44.     end
  45.  
  46.     def rssfeed
  47.         url = 'http://www.missingkids.com/missingkids/servlet/XmlServlet?act=rss&LanguageCountry=en_US&orgPrefix=NCMC'
  48.         open(url) do |rss|
  49.         feed = RSS::Parser.parse(rss)
  50. #       puts "Title: #{feed.channel.title}"
  51.         feed.items.each do |item|
  52.         puts "#{item.description}"
  53.          end
  54.      end
  55.     end
  56.  
  57.     def handle_server_input(s)
  58.         # This isn't at all efficient, but it shows what we can do with Ruby
  59.         # (Dave Thomas calls this construct "a multiway if on steroids")
  60.         case s.strip
  61.             when /^PING :(.+)$/i
  62.                 puts "[ Server ping ]"
  63.                 send "PONG :#{$1}"
  64.             when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]PING (.+)[\001]$/i
  65.                 puts "[ CTCP PING from #{$1}!#{$2}@#{$3} ]"
  66.                 send "NOTICE #{$1} :\001PING #{$4}\001"
  67.             when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]VERSION[\001]$/i
  68.                 puts "[ CTCP VERSION from #{$1}!#{$2}@#{$3} ]"
  69.                 send "NOTICE #{$1} :\001VERSION \00304rubaeus\003 \00303IRCbot\003\001"
  70.             when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:EVAL (.+)$/i
  71.                 puts "[ EVAL #{$5} from #{$1}!#{$2}@#{$3} ]"
  72.                 send "PRIVMSG #{(($4==@nick)?$1:$4)} :#{evaluate($5)}"
  73.         when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:test$/i
  74.         puts "rsstest"
  75.         send "PRIVMSG #dev :#{rssfeed.item.description}"
  76.             else
  77.                 puts s
  78.         end
  79.     end
  80.  
  81.     def main_loop()
  82.         # Just keep on truckin' until we disconnect
  83.         while true
  84.             ready = select([@irc, $stdin], nil, nil, nil)
  85.             next if !ready
  86.             for s in ready[0]
  87.                 if s == $stdin then
  88.                     return if $stdin.eof
  89.                     s = $stdin.gets
  90.                     send s
  91.                 elsif s == @irc then
  92.                     return if @irc.eof
  93.                     s = @irc.gets
  94.                     handle_server_input(s)
  95.                 end
  96.             end
  97.         end
  98.     end
  99.  
  100. end
  101.  
  102. # The main program
  103. # If we get an exception, then print it out and keep going (we do NOT want
  104. # to disconnect unexpectedly!)
  105. irc = IRC.new('irc.supernets.org', 6667, 'rubaeus', '#dev')
  106. irc.connect()
  107. begin
  108.     irc.main_loop()
  109. rescue Interrupt
  110. rescue Exception => detail
  111.     puts detail.message()
  112.     print detail.backtrace.join("\n")
  113.     retry
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement