File: db.lua

package info (click to toggle)
mongrel2 1.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,976 kB
  • sloc: ansic: 39,083; python: 2,833; sql: 1,555; sh: 467; makefile: 360; asm: 189; yacc: 145; php: 73; awk: 28; sed: 5
file content (55 lines) | stat: -rw-r--r-- 1,086 bytes parent folder | download | duplicates (5)
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
local strict = require 'strict'
local sidereal = require 'sidereal'
local table_concat = table.concat
local print = print
local assert = assert

module 'db'

local DB = assert(sidereal.connect('localhost', 6379))

function reconnect()
    return sidereal.connect('localhost', 6379)
end

local function format_message(user, msg)
    return ('From: %s\n%s'):format(user, table_concat(msg, "\n"))
end

function post_message(user, msg)
    DB:rpush("MESSAGES", format_message(user, msg))
end

function message_count()
    return DB:llen("MESSAGES")
end

function message_read(i)
    return DB:lindex("MESSAGES", i)
end

function send_to(to, from, msg)
    DB:lpush("INBOX:" .. to, format_message(from, msg))
end


function register_user(user, password)
    DB:hset("LOGINS", user, password)
end

function user_exists(user)
    return DB:hexists("LOGINS", user)
end

function inbox_count(user)
    return DB:llen("INBOX:" .. user)
end

function read_inbox(user)
    return DB:lpop("INBOX:" .. user)
end

function auth_user(user, password)
    return DB:hget("LOGINS", user) == password
end