]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <string.h> | |
3 | ||
4 | #include "../irc/irc_config.h" | |
5 | #include "../lib/irc_string.h" | |
6 | #include "../lib/strlfunc.h" | |
7 | #include "../nick/nick.h" | |
8 | #include "request.h" | |
9 | #include "user.h" | |
10 | ||
11 | r_user_t *r_userlist = NULL; | |
12 | int ru_loading = 0; | |
13 | ||
14 | r_user_t *ru_find(char *name); | |
15 | ||
16 | int ru_create(char *name, unsigned int level) { | |
17 | r_user_t *user, *trav; | |
18 | ||
19 | if (ru_setlevel(name, level) != 0) | |
20 | return 1; | |
21 | ||
22 | user = (r_user_t*)malloc(sizeof(r_user_t)); | |
23 | ||
24 | if (user == NULL) | |
25 | return 0; | |
26 | ||
27 | user->next = NULL; | |
28 | strlcpy(user->name, name, sizeof(user->name)); | |
29 | user->level = level; | |
30 | ||
31 | if (r_userlist == NULL) | |
32 | r_userlist = user; | |
33 | else { | |
34 | trav = r_userlist; | |
35 | ||
36 | while (trav->next) | |
37 | trav = trav->next; | |
38 | ||
39 | trav->next = user; | |
40 | } | |
41 | ||
42 | ru_persist(); | |
43 | ||
44 | return 1; | |
45 | } | |
46 | ||
47 | void ru_destroy(char *name) { | |
48 | r_user_t *puser; | |
49 | r_user_t *user = r_userlist; | |
50 | ||
51 | if (user && ircd_strcmp(user->name, name) == 0) { | |
52 | free(r_userlist); | |
53 | ||
54 | r_userlist = NULL; | |
55 | ||
56 | ru_persist(); | |
57 | ||
58 | return; | |
59 | } | |
60 | ||
61 | if (user == NULL) | |
62 | return; | |
63 | ||
64 | while (user && user->next) { | |
65 | if (ircd_strcmp(user->next->name, name) == 0) { | |
66 | puser = user->next; | |
67 | user->next = user->next->next; | |
68 | ||
69 | free(puser); | |
70 | } | |
71 | ||
72 | user = user->next; | |
73 | } | |
74 | ||
75 | ru_persist(); | |
76 | } | |
77 | ||
78 | int ru_parseline(char *line) { | |
79 | char name[ACCOUNTLEN]; | |
80 | unsigned int level; | |
81 | int result; | |
82 | ||
83 | if (sscanf(line, "%s %u", name, &level) < 2) | |
84 | return 0; | |
85 | ||
86 | ru_loading = 1; | |
87 | result = ru_create(name, level); | |
88 | ru_loading = 0; | |
89 | ||
90 | return result; | |
91 | } | |
92 | ||
93 | int ru_load(void) { | |
94 | char line[4096]; | |
95 | FILE *rudata; | |
96 | int count; | |
97 | ||
98 | rudata = fopen(RQ_USERFILE, "r"); | |
99 | ||
100 | if (rudata == NULL) | |
101 | return 0; | |
102 | ||
103 | count = 0; | |
104 | ||
105 | while (!feof(rudata)) { | |
106 | if (fgets(line, sizeof(line), rudata) == NULL) | |
107 | break; | |
108 | ||
109 | if (line[strlen(line) - 1] == '\n') | |
110 | line[strlen(line) - 1] = '\0'; | |
111 | ||
112 | if (line[strlen(line) - 1] == '\r') | |
113 | line[strlen(line) - 1] = '\0'; | |
114 | ||
115 | if (line[0] != '\0') { | |
116 | if (ru_parseline(line)) | |
117 | count++; | |
118 | } | |
119 | } | |
120 | ||
121 | fclose(rudata); | |
122 | ||
123 | return count; | |
124 | } | |
125 | ||
126 | int ru_persist(void) { | |
127 | FILE *rudata; | |
128 | int count = 0; | |
129 | r_user_t *user = r_userlist; | |
130 | ||
131 | if (ru_loading) | |
132 | return 0; | |
133 | ||
134 | rudata = fopen(RQ_USERFILE, "w"); | |
135 | ||
136 | if (rudata == NULL) | |
137 | return 0; | |
138 | ||
139 | while (user) { | |
140 | fprintf(rudata, "%s %u\n", user->name, user->level); | |
141 | ||
142 | user = user->next; | |
143 | } | |
144 | ||
145 | fclose(rudata); | |
146 | ||
147 | return count; | |
148 | } | |
149 | ||
150 | r_user_t *ru_find(char *name) { | |
151 | r_user_t *user = r_userlist; | |
152 | ||
153 | while (user) { | |
154 | if (ircd_strcmp(user->name, name) == 0) | |
155 | return user; | |
156 | ||
157 | user = user->next; | |
158 | } | |
159 | ||
160 | return NULL; | |
161 | } | |
162 | ||
163 | unsigned int ru_getlevel(nick *np) { | |
164 | if (IsOper(np)) | |
165 | return 999; | |
166 | else if (!IsAccount(np)) | |
167 | return 0; | |
168 | ||
169 | return ru_getlevel_str(np->authname); | |
170 | } | |
171 | ||
172 | unsigned int ru_getlevel_str(char *name) { | |
173 | r_user_t *user = ru_find(name); | |
174 | ||
175 | if (user) | |
176 | return user->level; | |
177 | else | |
178 | return 0; | |
179 | } | |
180 | ||
181 | int ru_setlevel(char *name, unsigned int level) { | |
182 | r_user_t *user = ru_find(name); | |
183 | ||
184 | if (user) { | |
185 | user->level = level; | |
186 | ||
187 | ru_persist(); | |
188 | ||
189 | return 1; | |
190 | } else | |
191 | return 0; | |
192 | } |