-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.lisp
71 lines (58 loc) · 2.83 KB
/
context.lisp
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
;;;; context.lisp
(in-package #:harlie)
(defclass bot-context ()
((bot-nick :initarg :bot-nick :initform nil :accessor bot-nick)
(bot-irc-server :initarg :bot-irc-server :initform nil :accessor bot-irc-server)
(bot-irc-channel :initarg :bot-irc-channel :initform nil :accessor bot-irc-channel)
(bot-web-server :initarg :bot-web-server :initform nil :accessor bot-web-server)
(bot-web-port :initarg :bot-web-port :initform nil :accessor bot-web-port)
(bot-uri-prefix :initarg :bot-uri-prefix :initform nil :accessor bot-uri-prefix)))
(defmethod initialize-instance :after ((context bot-context) &key)
(with-connection (psql-botdb-credentials *bot-config*)
(let* ((context-query-head '(:select 'context-name 'irc-server 'irc-channel
'web-server 'web-port 'web-uri-prefix
:from 'contexts))
(context-query (cond ((bot-nick context)
(append context-query-head
`(:where (:= (:raw "lower(context_name)")
,(string-downcase (bot-nick context))))))
((bot-web-port context)
(append context-query-head
`(:where (:= 'web-port ,(bot-web-port context)))))
(t nil))))
(let ((conlist (first (query (sql-compile context-query)))))
(setf (bot-nick context) (string-downcase (first conlist)))
(setf (bot-irc-server context) (second conlist))
(setf (bot-irc-channel context) (third conlist))
(setf (bot-web-server context) (fourth conlist))
(setf (bot-web-port context) (fifth conlist))
(setf (bot-uri-prefix context) (sixth conlist))))))
(defun chain-context (nick)
(with-connection (psql-botdb-credentials *bot-config*)
(query (:select 'context-id
:from 'contexts
:where (:= (:raw "lower(context_name)")
(string-downcase nick)))
:single)))
(defgeneric chain-read-context-id (context)
(:documentation "Returns the context ID for reading the chaining DB in a given context."))
(defmethod chain-read-context-id ((context bot-context))
(chain-context (bot-nick context)))
(defgeneric chain-write-context-id (context)
(:documentation "Returns the context ID for writing to the chaining DB in a given context."))
(defmethod chain-write-context-id ((context bot-context))
(chain-context (bot-nick context)))
(defgeneric url-read-context-id (context)
(:documentation "Returns the context ID for reading the URL DB in a given context."))
(defun url-context (port)
(with-connection (psql-botdb-credentials *bot-config*)
(query (:select 'context-id
:from 'contexts
:where (:= 'web-port port))
:single)))
(defmethod url-read-context-id ((context bot-context))
(url-context (bot-web-port context)))
(defgeneric url-write-context-id (context)
(:documentation "Returns the context ID for writing to the URL DB in a given context."))
(defmethod url-write-context-id ((context bot-context))
(url-context (bot-web-port context)))