· 7 years ago · Jan 12, 2019, 07:38 PM
1
2defaultvalue "user_db" "default" // database for use if empty dont use databse
3users_table = "users" // hardcoded table name with authkeys
4
5sanitize__string = [
6 //this doesn't really work for some reason
7 feedback = (format "sanitizing %1" $arg2)
8 echo $feedback
9
10 s = $arg2
11 s = (strreplace $s "^"" "")
12 s = (strreplace $s "'" "")
13 result $s
14]
15
16user__exist = [
17
18 db_init $user_db
19 nick = $arg1 //for reasons I can't fathom, sanitize_string screws up the nick
20
21 //feedback = (format "user__exist: postsanitization testing if user exists arg1=%1 nick=%2" $arg1 $nick)
22 //echo $feedback
23
24 query = (format "SELECT nick FROM %1 WHERE nick=':0'" $users_table)
25 qh = (db_pquery $query $nick $user_db)
26 res = ""
27 // check for errors
28 if (= $qh -1) [
29 db_error $user_db
30 pm $arg1 "user_exist: Database error"
31 res = 0
32 ] [
33 res = (db_getrow $qh $user_db)
34 (db_finalize $qh $user_db)
35
36 if (=s $res "") [
37 res = 0
38
39 //err = (format "user__exist: user %1 not found in db." $nick)
40 //echo $err
41 ] [
42 res = 1
43 //err = (format "user__exist: user %1 found in db." $nick)
44 //echo $err
45 //(format "^f1Player ^f0%1 ^f1was connected from ip ^f0%2 ^f1at ^f0%3" $arg1 (int2ip (at $res 0)) (timef (at $res 1) "%d.%m.%Y, %X"))
46 ]
47
48 ]
49 result $res
50]
51
52cmd_userexist = [
53 //untested, but should work
54 nick = (sanitize__string $arg2)
55
56 if (= (user__exist $nick) 0) [
57 pm $arg1 "$nick is not in the user database."
58 ] [
59 pm $arg1 "$nick is in the user database."
60 ]
61]
62
63registercommand "userexist" cmd_userexist 1 "s" "userexist ^f1Check to see if a user has registered. #userexist <nick>"
64
65password__match = [
66 db_init $user_db
67
68 nick = $arg1
69 password = $arg2
70
71 query = (format "SELECT nick FROM %1 WHERE nick=':0' AND password=':1'" $users_table)
72 qh = (db_pquery $query (concat $nick $password) $user_db)
73 res = ""
74 // check for errors
75 if (= $qh -1) [
76 db_error $user_db
77 pm $arg1 "user_exist: Database error"
78 res = 0
79 ] [
80 res = (db_getrow $qh $user_db)
81 (db_finalize $qh $user_db)
82
83 if (=s $res "") [
84 res = 0
85
86 //err = (format "user__exist: passwords do not match." $nick)
87 //echo $err
88 //(format "^f1Player ^f0%1 ^f1have never connected" $arg1)
89 ] [
90 res = 1
91
92 //err = (format "user__exist: passwords do match!" $nick)
93 //echo $err
94
95 //(format "^f1Player ^f0%1 ^f1was connected from ip ^f0%2 ^f1at ^f0%3" $arg1 (int2ip (at $res 0)) (timef (at $res 1) "%d.%m.%Y, %X"))
96 ]
97
98 ]
99 result $res
100
101]
102
103cmd_identify = [
104 nick = (getname $arg1)
105 password = (sanitize__string $arg2)
106 ipaddress = (getip $arg1)
107
108 if (= (user__exist $nick) 1) [
109 if (= (password__match $nick $password) 1) [
110 welcomemsg = (format "Welcome back %1! Unspecing..." (getname $arg1))
111 pm $arg1 $welcomemsg
112 spectator $arg1 0 //unspec player
113
114 ] [
115 pm $arg1 "Invalid password."
116 ]
117 ] [
118 pm $arg1 "Your nick has not been registered. Register your nick first using #register <password>"
119 ]
120]
121
122registercommand "identify" cmd_identify 1 "s" "identify ^f1Identify yourself, #identify <password>"
123
124cmd_register = [
125 nick = (getname $arg1)
126
127 //feedback = (format "pre-sanitized register nick=%1 arg2=%2" $nick $arg2)
128 //echo $feedback
129
130 password = (sanitize__string $arg2)
131 ipaddress = (getip $arg1)
132
133 //feedback = (format "postsanitized register nick=%1 password=%2" $nick $password)
134 //echo $feedback
135
136
137 // load keys from database if needed
138 if (= (user__exist $nick) 0) [
139 db_init $user_db
140
141 //create table users(nick text, password text, email text, ip text, status integer);
142
143 //qh = (db_insert_replace $users_table "VALUES(':0',':1','',':2',1)" (concat $nick $password $ipaddress) $adduser_query $user_db)
144 qh = (db_insert_replace $users_table "VALUES(':0',':1','',':2',1)" (concat $nick $password $ipaddress) $user_db)
145
146 // check for errors
147 if (= $qh -1) [
148 pm $arg1 "Database error, unable to add user."
149 db_error $user_db
150 ] [
151 pm $arg1 "User successfully added. Welcome!"
152 spectator $arg1 0 //unspec player
153 // no errors
154 //authcount = 0
155 //while [row = (db_getrow $qh $auth_db); result (!=s $row "")] [
156 // // row format "degrave 23abc54bca4b3c5bc 0"
157 // adduser (at $row 0) (at $row 1)
158 // authcount = (+ 1 $authcount)
159 // //echo (format "adduser %1 %2" (at $row 0) (at $row 1))
160 //]
161 //syncmsg = (format "Authkeys reloaded, %1 authkeys were loaded from database" $authcount)
162 db_finalize $qh $user_db
163 ]
164
165 ] [
166 pm $arg1 "User already exists."
167 ]
168
169
170]
171
172// register our command, for admins only
173registercommand "register" cmd_register 1 "s" "register ^f1Register yourself, #register <password>"