blob: 15f1a24f2a426c49a545af0bcb58553466c3b4a6 [file] [log] [blame]
MG Mud User88f12472016-06-24 23:31:02 +02001// MorgenGrauen MUDlib
2/** \file /file.c
3* Kurzbeschreibung.
4* Langbeschreibung...
5* \author <Autor>
6* \date <date>
7* \version $Id$
8*/
9/* Changelog:
10*/
11#pragma strong_types,save_types,rtt_checks
12#pragma no_clone,no_inherit,no_shadow
13#pragma pedantic, range_check
14
15#include <defines.h>
16#include <events.h>
17#include <wizlevels.h>
18#include <player/base.h>
19#include <userinfo.h>
Zesstra3c093512019-01-07 22:55:26 +010020#include <config.h>
MG Mud User88f12472016-06-24 23:31:02 +020021
Zesstra3c093512019-01-07 22:55:26 +010022#define DBPATH "/"LIBDATADIR"/"SECUREDIR"/ARCH/second.sqlite"
23
MG Mud User88f12472016-06-24 23:31:02 +020024#define ZDEBUG(x) tell_room("/players/zesstra/workroom",\
25 sprintf("second: %O\n",x));
26
27protected void create()
28{
29 seteuid(getuid());
Zesstra3c093512019-01-07 22:55:26 +010030 if (sl_open(DBPATH) != 1)
MG Mud User88f12472016-06-24 23:31:02 +020031 {
32 raise_error("Datenbank konnte nicht geoeffnet werden.\n");
33 }
34 // Tabellen und Indices anlegen, falls die nicht existieren.
35 sl_exec("CREATE TABLE IF NOT EXISTS zweities(uuid TEXT PRIMARY KEY ASC, "
36 "name TEXT NOT NULL, erstieuuid TEXT NOT NULL, "
37 "erstie TEXT NOT NULL);");
38 sl_exec("CREATE TABLE IF NOT EXISTS testies(name TEXT PRIMARY KEY ASC, "
39 "magier TEXT NOT NULL, "
40 "lastlogin DATETIME DEFAULT current_timestamp);");
41 sl_exec("CREATE TABLE IF NOT EXISTS familien("
42 "erstieuuid TEXT PRIMARY KEY ASC, familie TEXT NOT NULL);");
43 sl_exec("CREATE INDEX IF NOT EXISTS idx_erstie ON zweities(erstie);");
44 sl_exec("CREATE INDEX IF NOT EXISTS idx_name ON zweities(name);");
45 sl_exec("CREATE INDEX IF NOT EXISTS idx_magiername ON testies(magier);");
46 sl_exec("CREATE INDEX IF NOT EXISTS idx_familie ON familien(familie);");
47
48 // Login-Event abonnieren
49 if (EVENTD->RegisterEvent(EVT_LIB_LOGIN,
50 "listen", this_object()) <= 0)
51 {
52 raise_error("Loginevent konnte nicht abonniert werden.\n");
53 }
54}
55
56public void listen(string eid, object trigob, mixed data)
57{
58 if (previous_object() != find_object(EVENTD)
59 || !trigob
60 || !query_once_interactive(trigob)
61 || IS_LEARNER(trigob))
62 return;
63 // wenn testie, wird der Char als Testie behandelt und P_SECOND ignoriert.
64 mixed testie=trigob->QueryProp(P_TESTPLAYER);
65 if (stringp(testie)
66 && strstr(testie,"Gilde")==-1)
67 {
Zesstrac5cbd082019-01-07 23:06:13 +010068 testie=lower_case(testie);
69 mixed plinfo = master()->get_userinfo(testie);
MG Mud User88f12472016-06-24 23:31:02 +020070 if (pointerp(plinfo))
71 {
72 sl_exec("INSERT OR REPLACE INTO testies(name, magier, lastlogin) "
73 "VALUES(?1,?2,?3);",
74 trigob->query_real_name(),
75 testie, time());
76 return; // zweitie jetzt auf jeden Fall ignorieren.
77 }
78 }
79
80 mixed erstie=trigob->QueryProp(P_SECOND);
81 if (stringp(erstie))
82 {
Zesstrac5cbd082019-01-07 23:06:13 +010083 erstie=lower_case(erstie);
84 mixed plinfo = master()->get_userinfo(erstie);
MG Mud User88f12472016-06-24 23:31:02 +020085 if (pointerp(plinfo))
86 {
87 sl_exec("INSERT OR REPLACE INTO zweities(uuid, name, erstieuuid, erstie) "
88 "VALUES(?1,?2,?3,?4);",
89 getuuid(trigob),
90 trigob->query_real_name(),
91 erstie + "_" + plinfo[USER_CREATION_DATE+1],
92 erstie);
93 }
94 //ZDEBUG(sprintf("%O, %O, %O\n",eid,trigob,data));
95 }
96}
97
98varargs int remove(int silent)
99{
100 EVENTD->UnregisterEvent(EVT_LIB_LOGIN, this_object());
101 sl_close();
102 destruct(ME);
103 return 1;
104}
105
106public mixed sql_query(string query)
107{
108 if (ARCH_SECURITY)
109 return sl_exec(query);
110 return 0;
111}
112
113private string get_erstie_data(string datum, object zweitie)
114{
115 if (!zweitie) return 0;
116 mixed res = sl_exec("SELECT " + datum + " FROM zweities WHERE uuid=?1",
117 getuuid(zweitie));
118 if (sizeof(res))
119 return res[0][0];
120
121 return 0;
122}
123
124public varargs string QueryErstieName(object zweitie)
125{
126 return get_erstie_data("erstie", zweitie || previous_object());
127}
128
129public varargs string QueryErstieUUID(object zweitie)
130{
131 return get_erstie_data("erstieuuid", zweitie || previous_object());
132}
133
134private string* get_zweitie_data(string datum, object erstie)
135{
136 if (!erstie) return 0;
137 mixed tmp = sl_exec("SELECT " + datum + " FROM zweities WHERE erstieuuid=?1",
138 getuuid(erstie));
139 if (sizeof(tmp))
140 {
141 string *res=({});
142 foreach(string *row: tmp)
143 res+=({row[0]});
144 return res;
145 }
146 return 0;
147}
148
149public varargs string* QueryZweities(object erstie)
150{
151 return get_zweitie_data("name", erstie || previous_object());
152}
153
154public varargs string QueryFamilie(object pl)
155{
Zesstrac0b56a32019-01-09 00:23:33 +0100156 if (!pl || !query_once_interactive(pl))
157 return 0;
MG Mud User88f12472016-06-24 23:31:02 +0200158 string erstie = get_erstie_data("erstieuuid",
159 pl || previous_object());
160 // Wenn !erstie, dann ist pl kein Zweitie, also ist er selber erstie
161 erstie ||= getuuid(pl);
162 // jetzt noch gucken, ob ne explizite Familie fuer den erstie erfasst ist.
163 mixed tmp = sl_exec("SELECT familie FROM familien WHERE "
164 "erstieuuid=?1",erstie);
165 if (sizeof(tmp))
166 return tmp[0][0];
167 // wenn nicht, dann ist die Familie die Zweitieuuid
168 return erstie;
169}
170
Zesstra60ec4972019-01-08 21:07:16 +0100171public string SetFamilie(object|string pl, string familie)
172{
173 if (!ARCH_SECURITY)
174 return 0;
175
176 // Wenn Spielerobjekt, UUID ermitteln
177 if (objectp(pl) && query_once_interactive(pl))
178 pl = getuuid(pl);
179
180 sl_exec("INSERT OR REPLACE INTO familien(erstieuuid, familie) "
181 "VALUES(?1, ?2);", pl, familie);
182
183 mixed tmp = sl_exec("SELECT familie FROM familien WHERE "
184 "erstieuuid=?1", pl);
185 if (sizeof(tmp))
186 return tmp[0][0];
187
188 return 0;
189}
190
191public int DeleteFamilie(object|string pl)
192{
193 if (!ARCH_SECURITY)
194 return 0;
195
196 // Wenn Spielerobjekt, UUID ermitteln
197 if (objectp(pl) && query_once_interactive(pl))
198 pl = getuuid(pl);
199
200 sl_exec("DELETE FROM familien WHERE erstieuuid=?1;",
201 pl);
202
203 mixed tmp = sl_exec("SELECT familie FROM familien WHERE "
204 "erstieuuid=?1", pl);
205 if (sizeof(tmp))
206 return -1;
207
208 return 1;
209}
210
Zesstrab9dc01d2019-01-08 21:22:21 +0100211public int DeleteZweitie(object|string pl)
212{
213 if (!ARCH_SECURITY)
214 return 0;
215
216 // Wenn Spielerobjekt, UUID ermitteln
217 if (objectp(pl) && query_once_interactive(pl))
218 pl = getuuid(pl);
219
220 sl_exec("DELETE FROM zweities WHERE uuid=?1;", pl);
221
222 mixed tmp = sl_exec("SELECT name FROM zweities WHERE "
223 "uuid=?1", pl);
224 if (sizeof(tmp))
225 return -1;
226
227 return 1;
228}
229
230public int DeleteTestie(object|string pl)
231{
232 if (!ARCH_SECURITY)
233 return 0;
234
235 // Wenn Spielerobjekt, UID ermitteln
236 if (objectp(pl) && query_once_interactive(pl))
237 pl = getuid(pl);
238
239 sl_exec("DELETE FROM testies WHERE name=?1;", pl);
240
241 mixed tmp = sl_exec("SELECT magier FROM testies WHERE "
242 "name=?1", pl);
243 if (sizeof(tmp))
244 return -1;
245
246 return 1;
247}
248