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 / Database.rb
100644 129 lines (111 sloc) 3.408 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
118
119
120
121
122
123
124
125
126
127
128
129
require 'mysql'
 
class Database
attr_reader :database, :servers, :users, :channels, :message_log, :link
  def initialize(username, password, database, db_type)
    @username = username
    @password = password
@db_name = database
@db_type = db_type
if db_type == :mysql
@database = Mysql::new("localhost", @username, @password, @db_name)
      @database.reconnect = true
else
raise "Database not supported"
end
@servers = Servers.new @database
@users = Users.new @database
@message_log = Message_log.new @database
@link = Link.new @database
  end
 
  def ping
    @database.ping
  end
 
def server? hostname
@servers.exists? hostname
end
 
def insert_server hostname
@servers.insert hostname
end
 
  def user_exists? nick, ident, host, server
    @users.exists? nick, ident, host, server
  end
 
  def insert_user nick, ident, host, server
    @users.insert nick, ident, host, server
  end
 
  def log_message msg, nick, ident, host, server
    @message_log.insert msg, nick, ident, host, server
  end
end
 
class Table
def initialize handle
@handle = handle
end
  protected
  def clean_input(*args)
    retval = Array.new
    args.each do |arg|
      retval << @handle.escape_string(arg)
    end
    retval
  end
end
 
#Each of these classes will handle a specific table that the bot needs to
#have access to, they will implement a documented API on a per-table basis,
#so that they made be substituted for other mechanisms without disrupting
#other code, i.e flat files for the message log and such.
#
#The API will be located in the README.tableapi file
#
#the current set all use MySQL!
 
class Servers < Table
def initialize handle
super
end
 
def exists? hostname
result = @handle.query("SELECT * FROM servers WHERE hostname='#{hostname}'")
if ((result != nil)&&(result.num_rows > 0))
return true
end
return false
end
 
def insert hostname
    hostname = clean_input(hostname)
@handle.query("INSERT INTO servers (hostname) VALUES ('#{hostname}')")
end
end
 
class Users < Table
def initialize handle
super
end
 
  def exists? nick, ident, host, server
    result = @handle.query("SELECT * FROM users LEFT JOIN (servers) ON (servers.id = users.sid) WHERE (nick = '#{nick}' AND ident = '#{ident}' AND host = '#{host}' AND servers.hostname='#{server}')")
    if ((result != nil)&&(result.num_rows > 0))
      return true
    end
    return false
  end
 
  def insert nick, ident, host, server
    nick, ident, host, server = clean_input(nick, ident, host, server)
    sid_res = @handle.query("SELECT id FROM servers WHERE hostname = '#{server}'")
    sid = sid_res.fetch_row
    @handle.query("INSERT INTO users (nick, host, ident, sid) VALUES ('#{nick}', '#{host}', '#{ident}', #{sid[0].to_i})")
  end
end
 
class Message_log < Table
def initialize handle
super
end
  def insert msg, nick, ident, host, server
    msg, nick, ident, host, server = clean_input(msg, nick, ident, host, server)
    result = @handle.query("SELECT users.id, sid FROM users INNER JOIN (servers) ON (servers.id = users.sid) WHERE ( hostname='#{server}' AND nick='#{nick}' AND ident='#{ident}' AND host='#{host}' )")
    res = result.fetch_row
    uid = res[0].to_i
    sid = res[1].to_i
    @handle.query("INSERT INTO message_log (message, uid, sid) VALUES ('#{msg}', #{uid}, #{sid})")
  end
end
 
class Link < Table
def initialize handle
super
end
end