12 SLEEP_TABLE = [1, 2, 5, 15, 30, 60, 120, 300]
14 RUBYBOT_VERSION = "0.1"
19 options[:config_file] = "dcbot.conf"
20 OptionParser.new do |opts|
21 opts.banner = "Usage: dcbot.rb [options]"
23 opts.on("--config filename", "Use filename as the config file", "[default: dcbot.conf]") do |filename|
24 options[:config_file] = filename
26 opts.on("--[no-]debug", "Sets the debug flag") { |flag| options[:debug] = flag }
29 config = IniReader.read(options[:config_file])
31 global_config = config.find { |section| section[0] == "global" }
32 if options.has_key? :debug then
33 $debug = options[:debug]
34 elsif global_config and global_config[1].has_key? "debug" then
35 debug = global_config[1]["debug"].downcase
36 if debug == "true" then
43 connections = config.select { |section| section[0] == "connection" }
44 # FIXME: use all config sections
45 connection = connections[0][1]
46 host = connection["host"]
47 port = connection["port"].to_i
48 nickname = connection["nickname"]
49 description = connection["description"]
51 if connection.has_key? "prefix" then
52 PluginBase.cmd_prefix = connection["prefix"]
56 EventMachine::open_keyboard KeyboardInput
57 setupConnection(host, port, nickname, description, 0)
62 def setupConnection(host, port, nickname, description, sleep)
63 $socket = DCClientProtocol.connect(host, port, nickname,
64 :description => description, :debug => $debug,
65 :version => RUBYBOT_VERSION) do |c|
66 c.registerCallback :message do |socket, sender, message, isprivate|
67 if isprivate or sender == "*Dtella" then
68 puts "<#{sender}> #{message}"
70 if message[0,1] == PluginBase::CMD_PREFIX then
71 cmd, args = message[1..-1].split(" ", 2)
72 args = "" if args.nil?
73 if cmd == "reload" and isprivate then
74 # special sekrit reload command
75 PluginBase.loadPlugins
76 socket.sendPrivateMessage(sender, "Plugins have been reloaded")
77 elsif cmd == "quit" and isprivate then
78 # super-sekrit quit command
80 elsif PluginBase.has_command?(cmd) then
82 PluginBase.dispatch(socket, cmd, sender, isprivate, args)
83 rescue StandardError => e
84 socket.sendPrivateMessage(sender, "An error occurred executing your command: #{e.to_s}")
85 STDERR.puts "ERROR: #{e.to_s}"
86 PP.pp(e.backtrace, STDERR)
89 socket.sendPrivateMessage(sender, "Unknown command: #{PluginBase::CMD_PREFIX}#{cmd}")
93 c.registerCallback :error do |socket, message|
94 STDERR.puts "! #{message}"
96 c.registerCallback :peer_error do |socket, peer, message|
97 STDERR.puts "! Peer #{peer.host}:#{peer.port}"
99 c.registerCallback :unbind do |socket|
101 # this is our only socket for the moment
102 EventMachine.stop_event_loop
104 EventMachine::add_timer(SLEEP_TABLE[sleep]) do
105 sleep += 1 unless sleep == SLEEP_TABLE.size - 1
106 setupConnection(host, port, nickname, description, sleep)
110 c.registerCallback :connected do |socket|
112 socket.registerCallback :unbind do |socket|
114 # this is our only socket for the moment
115 EventMachine.stop_event_loop
117 setupConnection(host, port, nickname, description, 0)
121 c.registerCallback :reverse_connection do |socket, nick|
122 puts "* Bouncing RevConnectToMe back to user: #{nick}"
124 c.registerCallback :peer_initialized do |socket, peer|
125 puts "* Connecting to peer: #{peer.host}:#{peer.port}"
127 c.registerCallback :peer_unbind do |socket, peer|
128 puts "* Connection to peer #{peer.remote_nick} closed"
130 c.registerCallback :peer_get do |socket,peer,filename|
131 puts "* Peer #{peer.remote_nick} requested: #{filename}"