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