Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##jbot.rb
- #!/usr/bin/ruby
- require 'xmpp4r/client'
- include Jabber
- require 'bebysku.rb'
- Bebysku.new
- ##bebysku.rb
- require 'xmpp4r/client'
- include Jabber
- require 'brain'
- class Bebysku
- def die message
- puts message
- exit 1
- end
- def read_conf
- File.open("bebysku.conf", "r") do |file|
- file.each_line do |line|
- if not line =~ /^\s*". "\s*=\s*".*"\s*$/
- #throw something
- die('Configuration file is invalid: ' line)
- elsif line.count('"') != 4
- die('Configuration file is invalid: ' line)
- else
- @conf[line[/^\s*"(. )"\s*=\s*".*"\s*$/,1]] = line[/^\s*". "\s*=\s*"(.*)"\s*$/,1]
- end
- end
- end
- end
- def connect
- jid = JID::new(@conf["jid"])
- #converting string to JID, back and forth... Just so that our fancy
- #processors had something to do. Human factor is the weakest link in
- #computing.
- #p @conf["jid"]
- #p username = @conf["jid"][/^(. )@. \....?\/.*$/,1]
- #p server = @conf["jid"][/^. @(. \....?)\/.*$/,1]
- #p resource = @conf["jid"][/^. @. \....?\/(.*)$/,1]
- password = @conf["password"]
- @client = Client::new(jid)
- @client.connect
- @client.auth(password)
- @client.send(Presence::new)
- puts "Connected to #{@conf["jid"]}"
- end
- def startup
- mainthread = Thread.current
- @client.add_message_callback do |m|
- @client.send(@brain.think(m))
- mainthread.wakeup
- end
- Thread.stop
- end
- def initialize
- @conf = {}
- @brain = Brain.new
- read_conf
- connect
- #the main loop is here
- startup
- #and the cleanup follows:
- @client.close
- end
- end
- ##brain.rb
- require 'xmpp4r/client'
- class Brain
- def initialize
- archive = []
- knowledge = {}
- end
- def think(m)
- p m.body
- if m.type != :error
- return Message::new(m.from, "You sent: #{m.body}")
- end
- end
- end
- ##the original echo_threaded.rb
- #!/usr/bin/ruby
- # This bot will reply to every message it receives. To end the game, send 'exit'
- # THREADED VERSION
- require 'xmpp4r/client'
- include Jabber
- # settings
- if ARGV.length != 2
- puts "Run with ./echo_thread.rb user@server/resource password"
- exit 1
- end
- myJID = JID::new(ARGV[0])
- myPassword = ARGV[1]
- cl = Client::new(myJID)
- cl.connect
- cl.auth(myPassword)
- cl.send(Presence::new)
- puts "Connected ! send messages to #{myJID.strip.to_s}."
- mainthread = Thread.current
- cl.add_message_callback do |m|
- if m.type != :error
- cl.send(Message::new(m.from, "You sent: #{m.body}"))
- if m.body == 'exit'
- cl.send(Message::new(m.from, "Exiting ..."))
- mainthread.wakeup
- end
- end
- end
- Thread.stop
- cl.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement