This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
schema.pgsql
414 lines (350 loc) · 10.4 KB
/
schema.pgsql
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
--
-- Table structure for table "account_messages"
--
CREATE TYPE enum_n_y AS ENUM('N','Y');
DROP TABLE IF EXISTS "account_messages";
CREATE TABLE "account_messages" (
"id" SERIAL NOT NULL,
"account" INTEGER NOT NULL,
"type" varchar(16) DEFAULT NULL,
"message_html" TEXT NOT NULL,
"read" enum_n_y NOT NULL DEFAULT 'N',
"sent" INTEGER NOT NULL DEFAULT date_part('epoch',NOW()),
PRIMARY KEY ("id"),
UNIQUE ("account","type")
);
--
-- Table structure for table "accounts"
--
DROP TABLE IF EXISTS "accounts";
CREATE TABLE "accounts" (
"id" SERIAL NOT NULL,
"apikey" char(18) NOT NULL,
"created" TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
"email" TEXT,
"apikeyhash" bytea /*16*/ NOT NULL DEFAULT '',
"script" enum_n_y NOT NULL DEFAULT 'N',
"lasthost" TEXT,
PRIMARY KEY ("id"),
UNIQUE ("apikey")
);
CREATE INDEX "account_apikeyhash" ON accounts(apikeyhash);
CREATE LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION account_hash() RETURNS trigger AS
$BODY$
BEGIN
NEW.apikeyhash := decode(md5( E'^&$@$2\n' || COALESCE(NEW.apikey,'') || '@@'),'hex');
return NEW;
END;
$BODY$
LANGUAGE plpgsql STABLE;
CREATE TRIGGER "account_hash" BEFORE INSERT ON accounts FOR EACH ROW EXECUTE PROCEDURE account_hash();
--
-- Table structure for table "bayestotal"
--
DROP TABLE IF EXISTS "bayestotal";
CREATE TABLE "bayestotal" (
"totalspam" INTEGER NOT NULL DEFAULT 0,
"totalham" INTEGER NOT NULL DEFAULT 0
);
--
-- Table structure for table "bayestranslate"
--
DROP TABLE IF EXISTS "bayestranslate";
CREATE TABLE "bayestranslate" (
"wordh" bytea /*16*/ NOT NULL,
"word" TEXT NOT NULL,
PRIMARY KEY ("wordh")
);
--
-- Table structure for table "bayeswordsh"
--
DROP TABLE IF EXISTS "bayeswordsh";
CREATE TABLE "bayeswordsh" (
"wordh" bytea /*16*/ NOT NULL,
"ham" INTEGER NOT NULL DEFAULT 0,
"spam" INTEGER NOT NULL DEFAULT 0,
"added" timestamp without time zone NOT NULL DEFAULT NOW(),
"flags" "char" NOT NULL,
PRIMARY KEY ("wordh")
);
--
-- Table structure for table "bayeswordsh_s"
--
DROP TABLE IF EXISTS "bayeswordsh_s";
CREATE TABLE "bayeswordsh_s" (
"wordh" bytea /*16*/ NOT NULL,
"ham" INTEGER NOT NULL DEFAULT 0,
"spam" INTEGER NOT NULL DEFAULT 0,
"added" timestamp without time zone NOT NULL DEFAULT NOW(),
PRIMARY KEY ("wordh")
);
CREATE OR REPLACE FUNCTION bayeswordsh_i() RETURNS trigger AS
$BODY$
BEGIN
UPDATE bayeswordsh_s SET spam=NEW.spam, ham=NEW.ham, added=NEW.added WHERE wordh = NEW.wordh;
IF NOT FOUND THEN
BEGIN
INSERT INTO bayeswordsh_s(wordh,spam,ham,added) VALUES (NEW.wordh,NEW.spam,NEW.ham,NEW.added);
EXCEPTION WHEN unique_violation THEN
-- do nothing
END;
END IF;
END;
$BODY$
LANGUAGE plpgsql STABLE;
CREATE TRIGGER "bayeswordsh_i" BEFORE INSERT ON bayeswordsh FOR EACH ROW EXECUTE PROCEDURE bayeswordsh_i();
CREATE TRIGGER "bayeswordsh_u" BEFORE UPDATE ON bayeswordsh FOR EACH ROW EXECUTE PROCEDURE bayeswordsh_i();
--
-- Table structure for table "dnscache"
--
DROP TABLE IF EXISTS "dnscache";
CREATE TABLE "dnscache" (
"host" TEXT NOT NULL,
"ip" INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY ("host","ip")
);
--
-- Table structure for table "dnsrevcache"
--
DROP TABLE IF EXISTS "dnsrevcache";
CREATE TABLE "dnsrevcache" (
"ip" INTEGER NOT NULL DEFAULT 0,
"host" TEXT NOT NULL,
PRIMARY KEY ("ip")
);
CREATE INDEX "dnsrevcache_host" on dnsrevcache(host);
--
-- Table structure for table "dupes"
--
DROP TABLE IF EXISTS "dupes";
CREATE TABLE "dupes" (
"checksum" bytea /*16*/ NOT NULL,
"count" INTEGER NOT NULL DEFAULT 1,
"ip" INTEGER NOT NULL,
"expires" INTEGER NOT NULL,
PRIMARY KEY ("checksum")
);
--
-- Table structure for table "feeds"
--
DROP TABLE IF EXISTS "feeds";
CREATE TABLE "feeds" (
"id" SERIAL NOT NULL,
"url" TEXT NOT NULL,
"ignoreurls" TEXT,
"killbylink" TEXT,
"killbytext" TEXT,
"prefilter" TEXT NOT NULL,
"postfilter" TEXT NOT NULL,
"ignoretitles" enum_n_y NOT NULL,
"lastdate" INTEGER,
"lastcheck" INTEGER,
PRIMARY KEY ("id")
);
--
-- Table structure for table "linkstotal"
--
DROP TABLE IF EXISTS "linkstotal";
CREATE TABLE "linkstotal" (
"totalspam" INTEGER NOT NULL DEFAULT 0,
"totalham" INTEGER NOT NULL DEFAULT 0
);
--
-- Table structure for table "linkstranslate"
--
DROP TABLE IF EXISTS "linkstranslate";
CREATE TABLE "linkstranslate" (
"wordh" bytea /*16*/ NOT NULL,
"word" TEXT NOT NULL,
PRIMARY KEY ("wordh")
);
--
-- Table structure for table "linkswordsh"
--
DROP TABLE IF EXISTS "linkswordsh";
CREATE TABLE "linkswordsh" (
"wordh" bytea /*16*/ NOT NULL,
"ham" INTEGER NOT NULL DEFAULT 0,
"spam" INTEGER NOT NULL DEFAULT 0,
"added" timestamp without time zone NOT NULL DEFAULT NOW(),
"flags" "char" NOT NULL,
PRIMARY KEY ("wordh")
);
--
-- Table structure for table "linkswordsh_s"
--
DROP TABLE IF EXISTS "linkswordsh_s";
CREATE TABLE "linkswordsh_s" (
"wordh" bytea /*16*/ NOT NULL,
"ham" INTEGER NOT NULL DEFAULT 0,
"spam" INTEGER NOT NULL DEFAULT 0,
"added" timestamp without time zone NOT NULL DEFAULT NOW(),
PRIMARY KEY ("wordh")
);
CREATE OR REPLACE FUNCTION linkswordsh_i() RETURNS trigger AS
$BODY$
BEGIN
UPDATE linkswordsh_s SET spam=NEW.spam, ham=NEW.ham, added=NEW.added WHERE wordh = NEW.wordh;
IF NOT FOUND THEN
BEGIN
INSERT INTO linkswordsh_s(wordh,spam,ham,added) VALUES (NEW.wordh,NEW.spam,NEW.ham,NEW.added);
EXCEPTION WHEN unique_violation THEN
-- do nothing
END;
END IF;
END;
$BODY$
LANGUAGE plpgsql STABLE;
CREATE TRIGGER "linkswordsh_i" BEFORE INSERT ON linkswordsh FOR EACH ROW EXECUTE PROCEDURE linkswordsh_i();
CREATE TRIGGER "linkswordsh_u" BEFORE UPDATE ON linkswordsh FOR EACH ROW EXECUTE PROCEDURE linkswordsh_i();
--
-- Table structure for table "plonker"
--
DROP TABLE IF EXISTS "plonker";
CREATE TABLE "plonker" (
"ip" INTEGER NOT NULL,
"spampoints" INTEGER NOT NULL,
"added" timestamp without time zone NOT NULL DEFAULT NOW(),
"flags" INTEGER NOT NULL, /* set('dul','nodul','wild','nowild') */
PRIMARY KEY ("ip")
);
--
-- Table structure for table "posts_meta"
--
DROP TABLE IF EXISTS "posts_meta";
CREATE TABLE "posts_meta" (
"id" SERIAL NOT NULL,
"account" INTEGER,
"ip" INTEGER NOT NULL DEFAULT 0,
"timestamp" INTEGER,
"spambayes" SMALLINT,
"spamscore" INTEGER,
"spamcert" INTEGER,
"worktime" INTEGER,
"added" SMALLINT,
"manualspam" SMALLINT,
"serverid" varchar(64) NOT NULL,
PRIMARY KEY ("id")
);
CREATE INDEX "posts_meta_manualspam" ON "posts_meta" ("manualspam","spamscore");
CREATE INDEX "posts_meta_account" ON "posts_meta" ("account");
CREATE INDEX "posts_meta_spamscore" ON "posts_meta" ("spamscore","spamcert");
--
-- Table structure for table "posts_data"
--
DROP TABLE IF EXISTS "posts_data";
CREATE TABLE "posts_data" (
"id" INTEGER NOT NULL REFERENCES posts_data(id),
"content" text NOT NULL,
"name" TEXT,
"email" TEXT,
"url" TEXT,
"headers" TEXT,
"cookies" BOOLEAN NOT NULL DEFAULT false,
"session" BOOLEAN NOT NULL DEFAULT false,
"host" TEXT,
"hostip" INTEGER NOT NULL DEFAULT 0,
"path" TEXT,
"post" TEXT,
"chcookie" TEXT,
"spamreason" TEXT,
"profiling" TEXT,
UNIQUE ("id")
);
--
-- Table structure for table "postsarchive"
--
DROP TABLE IF EXISTS "postsarchive";
CREATE TABLE "postsarchive" (
"id" INTEGER NOT NULL,
"spambayes" SMALLINT,
"spamscore" INTEGER,
"spamcert" INTEGER,
"spamreason" TEXT,
"manualspam" SMALLINT,
"content" TEXT NOT NULL,
"name" TEXT,
"email" TEXT,
"url" TEXT,
"ip" INTEGER NOT NULL,
"timestamp" INTEGER,
"headers" TEXT,
"cookies" BOOLEAN NOT NULL DEFAULT false,
"session" BOOLEAN NOT NULL DEFAULT false,
"host" TEXT,
"hostip" INTEGER NOT NULL,
"path" TEXT,
"submitname" TEXT,
"added" SMALLINT,
"checksum" varchar(56) DEFAULT NULL,
"post" TEXT,
"chcookie" TEXT,
"worktime" INTEGER,
"account" INTEGER,
"profiling" TEXT
);
--
-- Table structure for table "trustedproxies"
--
DROP TABLE IF EXISTS "trustedproxies";
CREATE TABLE "trustedproxies" (
"host" TEXT NOT NULL,
PRIMARY KEY ("host")
);
--- shims
CREATE OR REPLACE FUNCTION from_unixtime(bigint)
RETURNS timestamp without time zone AS $$
SELECT pg_catalog.to_timestamp($1)::timestamp without time zone
$$ IMMUTABLE STRICT LANGUAGE SQL;
CREATE OR REPLACE FUNCTION timestampdiff(text, timestamp without time zone, timestamp without time zone)
RETURNS integer AS $$
SELECT CEIL(EXTRACT(epoch FROM ($3 - $2)) / EXTRACT(epoch FROM ('1 ' operator(pg_catalog.||) $1)::interval))::integer
$$ IMMUTABLE STRICT LANGUAGE SQL;
CREATE OR REPLACE FUNCTION timestampdiff(text, timestamp without time zone, timestamp with time zone)
RETURNS integer AS $$
SELECT timestampdiff($1, $2, $3::timestamp without time zone)
$$ IMMUTABLE STRICT LANGUAGE SQL;
-- GROUP_CONCAT()
-- Note: only supports the comma separator
-- Note: For DISTINCT and ORDER BY a subquery is required
CREATE OR REPLACE FUNCTION _group_concat(text, text)
RETURNS text AS $$
SELECT CASE
WHEN $2 IS NULL THEN $1
WHEN $1 IS NULL THEN $2
ELSE $1 operator(pg_catalog.||) ',' operator(pg_catalog.||) $2
END
$$ IMMUTABLE LANGUAGE SQL;
CREATE AGGREGATE group_concat (
BASETYPE = text,
SFUNC = _group_concat,
STYPE = text
);
-- DAY()
CREATE OR REPLACE FUNCTION day(timestamp without time zone)
RETURNS integer AS $$
SELECT EXTRACT(DAY FROM DATE($1))::integer
$$ IMMUTABLE STRICT LANGUAGE SQL;
CREATE OR REPLACE FUNCTION hour(timestamp without time zone)
RETURNS integer AS $$
SELECT EXTRACT (HOUR FROM $1)::integer
$$ IMMUTABLE STRICT LANGUAGE SQL;
CREATE OR REPLACE FUNCTION unix_timestamp(timestamp with time zone)
RETURNS bigint AS $$
SELECT EXTRACT(EPOCH FROM $1)::bigint
$$ VOLATILE LANGUAGE SQL;
CREATE OR REPLACE FUNCTION timediff(timestamp with time zone, timestamp without time zone)
RETURNS interval AS $$
SELECT $1::timestamp without time zone - $2
$$ IMMUTABLE STRICT LANGUAGE SQL;
-- INET_NTOA()
-- done in SQL to take advantage of inlining
CREATE OR REPLACE FUNCTION inet_ntoa(bigint)
RETURNS text AS $$
SELECT CASE WHEN $1 > 4294967295 THEN NULL ELSE
((($1::bigint >> 24) % 256) + 256) % 256 operator(pg_catalog.||) '.' operator(pg_catalog.||)
((($1::bigint >> 16) % 256) + 256) % 256 operator(pg_catalog.||) '.' operator(pg_catalog.||)
((($1::bigint >> 8) % 256) + 256) % 256 operator(pg_catalog.||) '.' operator(pg_catalog.||)
((($1::bigint ) % 256) + 256) % 256 END;
$$ IMMUTABLE STRICT LANGUAGE SQL;