/
awkbot_db_mysql.awk
192 lines (148 loc) · 5.93 KB
/
awkbot_db_mysql.awk
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 43) borrowed from FreeBSD's jail.c:
# <tag@cpan.org> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Scott S. McCoy
# -----------------------------------------------------------------------------
# Keep support for CPP (for awkpaste, for now)
#import <config.awk>
#import <mysql.awk>
# Also support module loading...
#use module.awk
#use config.awk
#use mysql.awk
#use log.awk
function awkbot_db_init () {
config_load("etc/awkbot.conf")
mysql_login(config("mysql.username"), config("mysql.password"))
mysql_db(config("mysql.database"))
}
function awkbot_db_karma_inc (nick,amount ,rv,row) {
# the "0" is because undefined == 0 is true, we want to see if its actually
# exactly zero.
if ((amount != "0") && ! amount) amount = 1;
rv = mysql_query("select * from karma where nick = " mysql_quote(nick))
mysql_fetch_assoc(rv, row)
mysql_finish(rv)
row["karma"] += amount
mysql_finish(mysql_query("replace into karma (nick,karma) values (" \
mysql_quote(nick) ", " row["karma"] ")"))
}
function awkbot_db_karma_dec (nick,amount,rv,row) {
if ((amount != "0") && ! amount) amount = 1;
rv = mysql_query("select * from karma where nick = " mysql_quote(nick))
mysql_fetch_assoc(rv, row)
mysql_finish(rv)
row["karma"] -= amount
mysql_finish(mysql_query("replace into karma (nick,karma) values (" \
mysql_quote(nick) ", " row["karma"] ")"))
}
function awkbot_db_karma (nick ,rv,row) {
rv = mysql_query("SELECT karma FROM karma WHERE nick = " mysql_quote(nick))
mysql_fetch_assoc(rv,row)
mysql_finish(rv)
return 0 + row["karma"]
}
function awkbot_db_question (question ,rv,row) {
rv = mysql_query("SELECT answer FROM qna WHERE question = " \
mysql_quote(question))
mysql_fetch_assoc(rv, row)
mysql_finish(rv)
return row["answer"]
}
function awkbot_db_answer (question,answer) {
mysql_finish(mysql_query("INSERT INTO qna (question, answer) " \
" VALUES (" mysql_quote(question) "," mysql_quote(answer) ")"))
return 1
}
function awkbot_db_forget (question) {
debug("database->forget(\"%s\")", question)
mysql_finish(mysql_query("DELETE FROM qna WHERE question = " \
mysql_quote(question)))
return 1
}
function awkbot_db_status_running (running) {
mysql_finish(mysql_query("UPDATE status SET running = " \
(running ? 1 : 0)))
if (! running) {
mysql_finish(mysql_query("UPDATE status SET livefeed = NULL"))
mysql_finish(mysql_query("UPDATE status SET started = NULL"))
}
else {
mysql_finish( \
mysql_query("UPDATE status SET started = CURRENT_TIMESTAMP"))
}
}
function awkbot_db_status_connected (connected) {
mysql_finish(mysql_query("UPDATE status SET connected = " \
(connected ? 1 : 0)))
}
function awkbot_db_status_livefeed (filename ,rv,row) {
if (filename) {
mysql_finish(mysql_query("UPDATE status SET livefeed = " \
mysql_quote(filename)))
}
else {
rv = mysql_query("SELECT livefeed FROM status")
mysql_fetch_row(rv, row)
filename = row[1]
mysql_finish(rv)
}
return filename
}
function awkbot_db_info (keyword ,result,rv,row) {
rv = mysql_query("select keyword, text from info where keyword like " \
mysql_quote(keyword "%"))
if (mysql_fetch_assoc(rv, row)) {
result = row["keyword"] row["text"]
}
else {
result = 0
}
mysql_finish(rv)
return result
}
function awkbot_db_paste_add (nick, description, language, content) {
mysql_finish(mysql_query("INSERT INTO paste (nick, subject, language, "\
"content) VALUES (" mysql_quote(nick) "," \
mysql_quote(description) "," \
mysql_quote(language) "," \
mysql_quote(content) ")"))
}
function awkbot_db_paste_get (id,row ,rv,result) {
rv = mysql_query("SELECT paste_id, nick, subject, language, content " \
"FROM paste WHERE paste_id = " mysql_quote(id))
result = mysql_fetch_assoc(rv, row)
mysql_finish(rv)
return result
}
function awkbot_db_paste_last ( result,row,rv) {
rv = mysql_query("SELECT max(paste_id) as paste_id FROM paste");
if (mysql_fetch_assoc(rv, row)) {
result = row["paste_id"]
}
mysql_finish(rv)
return result
}
# -----------------------------------------------------------------------------
# Modularization - This is a little more than a dispatch table, the following
# ports an unmodifiable old API to make this code a pub/sub module. This
# leaves the old API unchanged, with one exception - the awkbot_db_init()
# method now loads the config file (this is necessary, since it will be in a
# separate process in the new awkbot), causing awkpaste to parse the config
# file twice.
"init" == $1 { awkbot_db_init() }
# Read messages publish their results... second and third parameters are
# "state" parameters for the listeners.
"info" == $1 { kernel_publish("info", $2, $3, awkbot_db_info($4)) }
"karma" == $1 { kernel_publish("karma", $2, $3, awkbot_db_karma($4)) }
"question" == $1 { kernel_publish("answer", $2, $3, awkbot_db_question($4)) }
# Write messages are just mapped
"forget" == $1 { awkbot_db_forget($2) }
"answer" == $1 { awkbot_db_answer($2,$3) }
"livefeed" == $1 { awkbot_db_status_livefeed($2) }
"karma_inc" == $1 { awkbot_db_karma_inc($2) }
"karma_dec" == $1 { awkbot_db_karma_dec($2) }
"connected" == $1 { awkbot_db_status_connected($2) }
"running" == $1 { awkbot_db_status_running($2) }
"livefeed" == $1 { awkbot_db_status_livefeed($2) }