Advertisement
Guest User

Untitled

a guest
Jun 21st, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.05 KB | None | 0 0
  1. #!/usr/bin/ruby -w
  2. # encoding: utf-8
  3.  
  4. require 'socket'
  5. require 'cinch' # Why reinvent the wheel, y'know?
  6. require 'json' # We need clean, easy to parse information. Apparently XML is shitty.
  7. require 'sqlite3' # We're gonna use a database handler for database handling. Ahurr.
  8.  
  9. $db = SQLite3::Database.new("etc.db") # Global so we can get at it from anywhere, obviously.
  10. $irc = "etc" # Global variable to push the irc data through to the udpserv so it can format and pass it along.
  11.  
  12. class IRCRelay
  13.     def udpserv # Create the server to recieve input from modules.
  14.         udps = UDPSocket.new
  15.         udps.bind(nil, 1134)
  16.    
  17.         $modarray = Array.new
  18.    
  19.         loop do
  20.             data, addr = udps.recvfrom(4096)
  21.             $sender = addr[2] # Who sent the information? Global so we can pass this around.
  22.             print "[#{$sender}] #{data}" # Data recieved should be JSON formatted.
  23.             begin
  24.                 pdata =  JSON.parse(data) # Check for a module 'sign in.'
  25.             rescue
  26.                 print "[!] Invalid JSON string recieved from #{$sender}"
  27.                 udpsend("[!] Invalid JSON string detected! Dropped.", $sender, '1134')
  28.             end
  29.             #if pdata['connect'] =~ /MOD-TUNEIN/  # Do they want parsed data? Are they in the allowed list?
  30.             #begin
  31.                 if pdata.key?("connect")
  32.                     $modarray = $db.execute("SELECT name FROM modules")
  33.                     print "\nDB.EXEC IS: #{$db.execute("SELECT name FROM modules")}\n\n"
  34.                     print "MODARRAY IS: #{$modarray}\n\n"
  35.                     print "PDATA IS: #{pdata['connect']['name']}\n\n"
  36.                     $modarray.each do |el|
  37.                         puts el
  38.                         if el.include? pdata['connect']['name']
  39.                             print "[!] Module already connected!"
  40.                         end
  41.                         if !el.include? pdata['connect']['name']
  42.                             print "[!] Activating module!"
  43.                             $db.execute("INSERT INTO modules (name,dev,desc) values ('#{pdata['connect']['name']}','#{pdata['connect']['dev']}','#{pdata['connect']['desc']}');")
  44.                         end
  45.                     end
  46.                 end
  47.             #rescue
  48.                 #print "[!] Error in db.execute!"
  49.             #end
  50.        
  51.             #if pdata.key?("tuneout") # Mark active mods that don't use the broadcast.
  52.             #   File.open('tunedout.log', 'a') do |mods|
  53.             #       mods.write("#{sender}")
  54.             #   end
  55.             #end
  56.  
  57.             #File.open('recv.log', 'a') do |log|
  58.             #   log.write(data)
  59.             #end
  60.             #if data =~ /DBG-TUNERS/ # Debug command. Show the tuners array.
  61.             #   tunedmods.each { |i| print i, "\n" }
  62.             #end
  63.        
  64.         end
  65.     end
  66.  
  67.     def udpsend(string,target,port)
  68.         udps = UDPSocket.new
  69.         udps.send(string, 0, target, port)
  70.     end
  71.  
  72.     def ircinch
  73.         # Connect to IRC. Read and parse. Cinch is fucking ace for this shit.
  74.         bot = Cinch::Bot.new do
  75.             configure do |c|
  76.                 c.server = "irc.rizon.net"
  77.                 c.channels = ["#orange"]
  78.                 c.nick = "dxe"
  79.             end
  80.        
  81.             #on :message do |m|
  82.        
  83.  
  84.             on :message, ".info" do |m|
  85.                 m.reply "\\\\ IRC Relay バージョン 0.1a\n"
  86.                 m.reply "\\\\ greyEAXでプログラムされた\n"
  87.                 m.reply "\\\\ 現在実行中のモジュール: #{$modarray}\n"
  88.             end
  89.         end
  90.  
  91.         bot.start
  92.     end
  93.  
  94.     puts "Starting at #{Time.now}"
  95.     t1 = Thread.new{IRCRelay::udpserv.new()}
  96.     t2 = Thread.new{IRCRelay::ircinch.new()}
  97.     t1.join
  98.     t2.join
  99.     puts "End at #{Time.now}"
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement