]>
jfr.im git - irc/quakenet/newserv.git/blob - control/control_db.c
4 * A replacement for Germania's ageing Operservice2
7 * Copyright (C) 2005 Chris Porter.
10 #include "../nick/nick.h"
11 #include "../core/error.h"
12 #include "../lib/irc_string.h"
13 #include "../core/schedule.h"
14 #include "../dbapi2/dbapi2.h"
21 unsigned long loadedusers
= 0;
23 void noperserv_create_tables(void);
25 void noperserv_free_user(no_autheduser
*au
);
26 void noperserv_load_users(const DBAPIResult
*res
, void *arg
);
28 static DBAPIConn
*nodb
;
30 int noperserv_load_db(void) {
32 nodb
= dbapi2open(DBAPI2_DEFAULT
, "noperserv");
34 Error("noperserv", ERR_STOP
, "Could not connect to database.");
41 noperserv_create_tables();
43 nodb
->query(nodb
, noperserv_load_users
, NULL
,
44 "SELECT userid, authname, flags, noticelevel FROM ?", "T", "users");
49 void noperserv_load_users(const DBAPIResult
*res
, void *arg
) {
56 Error("noperserv", ERR_ERROR
, "Error loading user list.");
61 while(res
->next(res
)) {
62 nu
= noperserv_new_autheduser(strtoul(res
->get(res
, 0), NULL
, 10), res
->get(res
, 1));
66 nu
->authlevel
= strtoul(res
->get(res
, 2), NULL
, 10);
67 nu
->noticelevel
= strtoul(res
->get(res
, 3), NULL
, 10);
71 Error("noperserv", ERR_INFO
, "Loaded %lu users", loadedusers
);
76 void noperserv_create_tables(void) {
77 nodb
->createtable(nodb
, NULL
, NULL
,
79 "userid INT NOT NULL,"
80 "authname VARCHAR(?) NOT NULL,"
82 "noticelevel INT NOT NULL,"
83 "PRIMARY KEY (userid))", "Td", "users", ACCOUNTLEN
);
86 void noperserv_cleanup_db(void) {
91 for (i
=0;i
<AUTHNAMEHASHSIZE
;i
++) {
92 for (anp
=authnametable
[i
];anp
;) {
95 au
= anp
->exts
[noperserv_ext
];
97 noperserv_free_user(au
);
107 no_autheduser
*noperserv_new_autheduser(unsigned long userid
, char *name
) {
111 anp
= findorcreateauthname(userid
, name
);
115 au
= malloc(sizeof(no_autheduser
));
124 anp
->exts
[noperserv_ext
] = au
;
129 void noperserv_delete_autheduser(no_autheduser
*au
) {
131 nodb
->squery(nodb
, "DELETE FROM ? WHERE userid = ?", "Tu", "users", au
->authname
->userid
);
133 noperserv_free_user(au
);
136 void noperserv_update_autheduser(no_autheduser
*au
) {
138 nodb
->squery(nodb
, "INSERT INTO ? (userid, authname, flags, noticelevel) VALUES (?,?,?,?)", "Tusuu", "users", au
->authname
->userid
, au
->authname
->name
, NOGetAuthLevel(au
), NOGetNoticeLevel(au
));
141 nodb
->squery(nodb
, "UPDATE ? SET flags = ?, noticelevel = ? WHERE userid = ?", "Tuuu", "users", NOGetAuthLevel(au
), NOGetNoticeLevel(au
), au
->authname
->userid
);
145 void noperserv_free_user(no_autheduser
*au
) {
146 authname
*anp
= au
->authname
;
147 anp
->exts
[noperserv_ext
] = NULL
;
148 releaseauthname(anp
);
154 no_autheduser
*noperserv_get_autheduser(authname
*anp
) {
158 return anp
->exts
[noperserv_ext
];
161 unsigned long noperserv_get_autheduser_count(void) {