]>
Commit | Line | Data |
---|---|---|
3e3692bf CP |
1 | /* |
2 | * NOperserv v0.01 | |
3 | * | |
4 | * A replacement for Germania's ageing Operservice2 | |
5 | * DB functions | |
6 | * | |
7 | * Copyright (C) 2005 Chris Porter. | |
8 | */ | |
9 | ||
10 | #include "../nick/nick.h" | |
11 | #include "../core/error.h" | |
12 | #include "../lib/irc_string.h" | |
13 | #include "../core/schedule.h" | |
114b4eea | 14 | #include "../dbapi2/dbapi2.h" |
3e3692bf | 15 | |
aef920fc | 16 | #include "control.h" |
3e3692bf | 17 | |
3e3692bf CP |
18 | #include <stdlib.h> |
19 | ||
e43481af | 20 | int db_loaded = 0; |
3e3692bf CP |
21 | unsigned long loadedusers = 0; |
22 | ||
e43481af CP |
23 | void noperserv_create_tables(void); |
24 | ||
3e3692bf | 25 | void noperserv_free_user(no_autheduser *au); |
e54cd6b5 | 26 | void noperserv_load_users(const DBAPIResult *res, void *arg); |
3e3692bf | 27 | |
114b4eea CP |
28 | static DBAPIConn *nodb; |
29 | ||
7a32ca6e | 30 | int noperserv_load_db(void) { |
114b4eea CP |
31 | if(!nodb) { |
32 | nodb = dbapi2open(DBAPI2_DEFAULT, "noperserv"); | |
33 | if(!nodb) { | |
34 | Error("noperserv", ERR_STOP, "Could not connect to database."); | |
35 | return 0; | |
36 | } | |
ad50c27c | 37 | } |
7a32ca6e | 38 | |
e43481af | 39 | db_loaded = 1; |
3e3692bf | 40 | |
e43481af CP |
41 | noperserv_create_tables(); |
42 | ||
e4b54524 | 43 | nodb->query(nodb, noperserv_load_users, NULL, |
7078aa50 | 44 | "SELECT userid, authname, flags, noticelevel FROM ?", "T", "users"); |
7a32ca6e CP |
45 | |
46 | return 1; | |
3e3692bf CP |
47 | } |
48 | ||
e54cd6b5 | 49 | void noperserv_load_users(const DBAPIResult *res, void *arg) { |
3e3692bf | 50 | no_autheduser *nu; |
3e3692bf | 51 | |
114b4eea | 52 | if(!res) |
ef792d86 | 53 | Error("control", ERR_STOP, "Failed to load noperserv database. Your database might be corrupted or the schema is incompatible."); |
114b4eea CP |
54 | |
55 | if(!res->success) { | |
3e3692bf | 56 | Error("noperserv", ERR_ERROR, "Error loading user list."); |
114b4eea CP |
57 | res->clear(res); |
58 | return; | |
3e3692bf CP |
59 | } |
60 | ||
114b4eea | 61 | while(res->next(res)) { |
7078aa50 | 62 | nu = noperserv_new_autheduser(strtoul(res->get(res, 0), NULL, 10), res->get(res, 1)); |
3e3692bf CP |
63 | if(!nu) |
64 | continue; | |
65 | ||
114b4eea CP |
66 | nu->authlevel = strtoul(res->get(res, 2), NULL, 10); |
67 | nu->noticelevel = strtoul(res->get(res, 3), NULL, 10); | |
3e3692bf | 68 | nu->newuser = 0; |
3e3692bf CP |
69 | } |
70 | ||
16739dbe | 71 | Error("noperserv", ERR_INFO, "Loaded %lu users", loadedusers); |
3e3692bf | 72 | |
114b4eea | 73 | res->clear(res); |
3e3692bf CP |
74 | } |
75 | ||
76 | void noperserv_create_tables(void) { | |
e4b54524 | 77 | nodb->createtable(nodb, NULL, NULL, |
e54cd6b5 | 78 | "CREATE TABLE ? (" |
7078aa50 GB |
79 | "userid INT NOT NULL," |
80 | "authname VARCHAR(?) NOT NULL," | |
3e3692bf CP |
81 | "flags INT NOT NULL," |
82 | "noticelevel INT NOT NULL," | |
7078aa50 | 83 | "PRIMARY KEY (userid))", "Td", "users", ACCOUNTLEN); |
3e3692bf CP |
84 | } |
85 | ||
86 | void noperserv_cleanup_db(void) { | |
7078aa50 GB |
87 | int i; |
88 | authname *anp, *next; | |
89 | no_autheduser *au; | |
90 | ||
91 | for (i=0;i<AUTHNAMEHASHSIZE;i++) { | |
92 | for (anp=authnametable[i];anp;) { | |
93 | next = anp->next; | |
3e3692bf | 94 | |
7078aa50 GB |
95 | au = anp->exts[noperserv_ext]; |
96 | if(au) | |
97 | noperserv_free_user(au); | |
3e3692bf | 98 | |
7078aa50 GB |
99 | anp = next; |
100 | } | |
3e3692bf | 101 | } |
aa944539 | 102 | |
114b4eea CP |
103 | nodb->close(nodb); |
104 | nodb = NULL; | |
3e3692bf CP |
105 | } |
106 | ||
7078aa50 GB |
107 | no_autheduser *noperserv_new_autheduser(unsigned long userid, char *name) { |
108 | authname *anp; | |
109 | no_autheduser *au; | |
110 | ||
111 | anp = findorcreateauthname(userid, name); | |
112 | if(!anp) | |
3e3692bf CP |
113 | return NULL; |
114 | ||
7078aa50 GB |
115 | au = malloc(sizeof(no_autheduser)); |
116 | if(!au) | |
3e3692bf | 117 | return NULL; |
7078aa50 GB |
118 | |
119 | au->authname = anp; | |
3e3692bf CP |
120 | |
121 | loadedusers++; | |
122 | au->newuser = 1; | |
3e3692bf | 123 | |
7078aa50 | 124 | anp->exts[noperserv_ext] = au; |
3e3692bf CP |
125 | |
126 | return au; | |
127 | } | |
128 | ||
129 | void noperserv_delete_autheduser(no_autheduser *au) { | |
3e3692bf | 130 | if(!au->newuser) |
7078aa50 GB |
131 | nodb->squery(nodb, "DELETE FROM ? WHERE userid = ?", "Tu", "users", au->authname->userid); |
132 | ||
133 | noperserv_free_user(au); | |
3e3692bf CP |
134 | } |
135 | ||
136 | void noperserv_update_autheduser(no_autheduser *au) { | |
137 | if(au->newuser) { | |
7078aa50 | 138 | nodb->squery(nodb, "INSERT INTO ? (userid, authname, flags, noticelevel) VALUES (?,?,?,?)", "Tusuu", "users", au->authname->userid, au->authname->name, NOGetAuthLevel(au), NOGetNoticeLevel(au)); |
3e3692bf CP |
139 | au->newuser = 0; |
140 | } else { | |
7078aa50 | 141 | nodb->squery(nodb, "UPDATE ? SET flags = ?, noticelevel = ? WHERE userid = ?", "Tuuu", "users", NOGetAuthLevel(au), NOGetNoticeLevel(au), au->authname->userid); |
3e3692bf CP |
142 | } |
143 | } | |
144 | ||
145 | void noperserv_free_user(no_autheduser *au) { | |
d1f7d27a GB |
146 | authname *anp = au->authname; |
147 | anp->exts[noperserv_ext] = NULL; | |
148 | releaseauthname(anp); | |
3e3692bf CP |
149 | free(au); |
150 | ||
151 | loadedusers--; | |
152 | } | |
153 | ||
4355956f GB |
154 | no_autheduser *noperserv_get_autheduser(authname *anp) { |
155 | if (!anp) | |
7078aa50 | 156 | return NULL; |
3e3692bf | 157 | |
4355956f | 158 | return anp->exts[noperserv_ext]; |
3e3692bf CP |
159 | } |
160 | ||
161 | unsigned long noperserv_get_autheduser_count(void) { | |
162 | return loadedusers; | |
163 | } | |
164 |