public
Description: An IRC bot that keeps track of who is who and makes links between users.
Homepage:
Clone URL: git://github.com/seadog/ybttre.git
Click here to lend your support to: ybttre and make a donation at www.pledgie.com !
ybttre / Bot.rb
100644 117 lines (107 sloc) 2.873 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require 'IRC.rb'
require 'Database.rb'
require 'Admin.rb'
#ruby-irc does not support SSL yet! we'll move over when it does though
 
class Bot
  attr_accessor :nick, :ident, :real, :server, :port, :listener, :channels, :admins, :ssl, :version
  def initialize database
    @sock = nil
    @database = database
    @time = Time.now.to_i
    #defaults
    @nick = "Ybttre"
    @ident = "Obj"
    @real = "Ybbtre Obj"
    @server = nil
    @port = nil
@listener = nil
    @channels = nil
    @admins = nil
    @ssl = 0
    @version = "mIRC v6.21 Khaled Mardam-Bey"
    #end defaults
  end
 
  def connect
#do we need to enter the server into the database?
if !@database.server?(@server)
@database.insert_server @server
end
    #register with our admins class!
    @admins = Admins.new(@admins)
@irc ||= IRC.new @nick, @ident, @real, @server, @port, @ssl
@irc.connect
register_bot
  end
 
  private
 
  def on_connect
    join_channels
  end
 
  def join_channels
    channels.each do |chan|
      @irc.join_channel(chan)
    end
  end
 
  def process(line)
    if line =~ /PING :(.*)/
      @irc.pong $~[1]
    elsif line =~ /:([^!]+).* :\001VERSION\001/
      @irc.notice $~[1], "\001VERSION #{@version}\001"
    elsif line =~ /:(\S+) (\S+) (.*)/
      process_command($~[1], $~[2], $~[3])
    end
  end
 
  def process_command(host, command_number, arguments)
    if command_number == "001"
on_connect
return
end
#other command numbers here
nick, ident, host = parse_host(host)
if ( nick && command_number == "PRIVMSG" )
      arguments =~ /([^ ]*) :(.*)/
      channel = $~[1]
      message = $~[2]
unless @database.user_exists? nick, ident, host, @server
@database.insert_user nick, ident, host, @server
end
      if (@admins.admin?(ident, host) && message =~ /^[ ]*!(.*)/)
        #we want to turn COMMAND arg1 arg2 arg3... into (com, *arg), let's do that now
        args = Array.new
        comargs = $~[1]
        unless comargs =~ /([^ ]*)[ ]+(.*)/
          @admins.command(comargs, Array.new)
        end
        command = $~[1]
        argstring = $~[2]
        args = argstring.split(/[ ]+/)
        @admins.command(command, args)
      else #don't log commands sent by the admins to the bot!
@database.log_message message, nick, ident, host, @server
      end
end
  end
 
def parse_host host
if host =~ /^(.*?)!(.*?)@(.*)$/
return $~[1], $~[2], $~[3];
end
return nil, nil, nil
end
 
  def register_bot
@irc.send_pass_nick_user
    while @irc.connected?
      if (Time.now.to_i - @time) > 600
        join_channels
        @time = Time.now.to_i
        @database.ping
      end
      line = @irc.readline
      puts(" <-- #{line}")
      process(line)
    end
  rescue Exception => e
    puts "**EXCEPTION** #{e} **EXCEPTION**"
    @irc.close
    sleep(300)
    self.connect
  end
end