From ead8e9a3041e688562e8bce1cdf501cb56313d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20=27q3k=27=20Baza=C5=84ski?= Date: Sun, 29 Sep 2013 18:20:12 +0200 Subject: [PATCH] Added basic DB interface. --- core/irc.lua | 8 ++------ core/plugin.lua | 5 ++++- core/reactor.lua | 2 +- plugins/auth-postgres.lua | 30 ++++++------------------------ start.lua | 1 + 5 files changed, 14 insertions(+), 32 deletions(-) diff --git a/core/irc.lua b/core/irc.lua index 704df3c..ef7bf09 100644 --- a/core/irc.lua +++ b/core/irc.lua @@ -195,12 +195,8 @@ function irc:Connect(server, port, nickname, username, realname) -- Connection procedure (callback hell!) local FinishedInitialNotices = function() hook.Remove('irc.Notice', 'irc.Connect') - hook.Add('irc.Mode', 'irc.Connect', function(Target, Mode) - if Target == self._nickname then - print("Got mode") - hook.Remove('irc.Mode', 'irc.Connect') - hook.Call('irc.Connected') - end + self:OnResponse(376, function() + hook.Call('irc.Connected') end) self:SetNick(self._nickname) self:LoginUser(username, realname) diff --git a/core/plugin.lua b/core/plugin.lua index be6c95a..4b76288 100644 --- a/core/plugin.lua +++ b/core/plugin.lua @@ -72,7 +72,6 @@ function API.Register(plugin_id, plugin_name, version, url, author) Plugin.Version = Version Plugin.URL = url Plugin.Author = author - end function API.CurrentTime(plugin_id) @@ -83,6 +82,10 @@ function API.ConfigGet(plugin_id, Key) return config:Get("plugin-" .. plugin_id, Key) end +function API.DBOpen(plugin_id, Handle) + return db.Open(Handle) +end + local Quotas = {} -- Start counting a coroutine quota function plugin.StartQuota(Seconds) diff --git a/core/reactor.lua b/core/reactor.lua index 2a9f422..6397b93 100644 --- a/core/reactor.lua +++ b/core/reactor.lua @@ -75,7 +75,7 @@ function reactor:Run() -- we actually got something on our sockets for Socket, Data in pairs(self._read_sockets) do if r[Socket] ~= nil then - Socket:settimeout(2) + --Socket:settimeout(3) local Line, Error = Socket:receive('*l') if Error then error('Could not receive line: ' .. Error) diff --git a/plugins/auth-postgres.lua b/plugins/auth-postgres.lua index 7393211..7032c2a 100644 --- a/plugins/auth-postgres.lua +++ b/plugins/auth-postgres.lua @@ -1,29 +1,11 @@ postgres = {} -local function check_connection() - if not postgres.db or not postgres.db:ping() then - local Server = plugin.ConfigGet('server') - local Username = plugin.ConfigGet('username') - local Password = plugin.ConfigGet('password') - local Database = plugin.ConfigGet('database') - local Port = tonumber(plugin.ConfigGet('port')) or 5432 - postgres.db = DBI.Connect('PostgreSQL', Database, Username, Password, Server, Port) - end - if not postgres.db then - error("Could not connect to the PostgreSQL database!") - return false - end - return true -end - plugin.AddHook('auth.GetLevel', 'GetLevel', function(Channel, Account) - if check_connection() then - local Statement = postgres.db:prepare("select _level from _level where _account = ? and _channel = ?") - print(Account, Channel.Name) - Statement:execute(Account, Channel.Name) - for Row in Statement:rows(true) do - return tonumber(Row._level) - end - return 0 + local DB = plugin.DBOpen('auth') + local Query = DB:Query("select _level from _level where _account = ? and _channel = ?", + Account, Channel.Name) + for Row in Query do + return tonumber(Row._level) end + return 0 end) diff --git a/start.lua b/start.lua index b07cbb8..9db333b 100644 --- a/start.lua +++ b/start.lua @@ -1,5 +1,6 @@ require('core.hook') require('core.config') +require('core.db') require('core.reactor') require('core.irc') require('core.bot')