deleted possibility for adding an asterisk as toplevel
[srvx.git] / src / mod-hostserv.c
1 /* mod-hostserv.c - HostServ module for srvx
2  * Copyright 2012-2013 pk910, Stricted, NurPech
3  *
4  * This file is part of srvx.
5  *
6  * srvx is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with srvx; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
19  */
20
21 /* Adds new section to srvx.conf:
22  * "modules" {
23  *     "hostserv" {
24  *         "nick" "HostServ";
25  *         "modes" "+iok";
26  *         "toplevel_access" "600";
27  *         "fallback_other_assignment" "1"; //fall back to another assignment when active assignment gets removed
28  *         "manager_can_rename_toplevel" "0"; //managers of a toplevel group may rename the whole group
29  *         "manager_can_del_toplevel" "0"; //managers of a toplevel group may delete the whole group
30  *         "manager_can_rename_secondlevel" "0"; //managers of a secondlevel group may rename the whole group
31  *         "manager_can_del_secondlevel" "0"; //managers of a secondlevel group may delete the whole group
32  *     };
33  *  };
34  *
35  * After you started srvx make the bot active:
36  /msg opserv bind hostserv * hostserv.*
37  /msg opserv bind hostserv help *modcmd.help
38  */
39
40 #include "chanserv.h"
41 #include "opserv.h"
42 #include "nickserv.h"
43 #include "conf.h"
44 #include "modcmd.h"
45 #include "saxdb.h"
46 #include "timeq.h"
47
48 #define KEY_TOPLEVEL "TopLevel"
49 #define KEY_SECONDLEVEL "SecondLevel"
50 #define KEY_MANAGERS "Manager"
51 #define KEY_ASSIGNMENTS "Assignments"
52 #define KEY_ACTIVE "active"
53
54 #define HS_ASSIGNMENTSTATE_AUTO -1
55 #define HS_ASSIGNMENTSTATE_OFF  0
56 #define HS_ASSIGNMENTSTATE_ON   1
57
58 static const struct message_entry msgtab[] = {
59     { "HSMSG_ACCESS_DENIED", "Access denied." },
60     { "HSMSG_ASSIGNED_FAKEHOSTS", "Assigned Fakehosts for User $b%s$b:" },
61     { "HSMSG_ASSIGNED_FAKEHOST", "  $b%s.%s$b" },
62     { "HSMSG_ASSIGNED_FAKEHOST_ACTTIVE", "  $b%s.%s$b (active)" },
63     { "HSMSG_ASSIGNED_FAKEHOST_NOT_ACTIVE", "Fakehost $b%s.%s.$b is not active." },
64     { "HSMSG_ASSIGN_HOWTO", "Use $bset xxx.yyy$b to activate one of the listed fakehosts or $bset *$b to use the default fakehost." },
65     { "HSMSG_ASSIGNED_NONE", "  None." },
66     { "HSMSG_MANAGED_FAKEHOSTS", "Fakehosts managed by User $b%s$b:" },
67     { "HSMSG_MANAGED_TOPLEVEL", "  $b*.%s$b   fakehosts: %d   assignments: %d" },
68     { "HSMSG_MANAGED_TOPLEVEL_OWN", "  $b*.%s$b   fakehosts: %d   assignments: %d   (active)" },
69     { "HSMSG_MANAGED_FAKEHOST", "  $b%s.%s$b   assignments: %d" },
70     { "HSMSG_MANAGE_HOWTO", "Use $bview xxx.yyy$b to view more information about a fakehost group." },
71     { "HSMSG_UNKNOWN_FAKEHOST", "Fakehost $b%s.%s$b is unknown or you have no access to manage it." },
72     { "HSMSG_TOPLEVEL_FAKEHOSTS", "Fakehosts in group $b*.%s$b:" },
73     { "HSMSG_TOPLEVEL_FAKEHOST", "  $b%s.%s$b   assignments: %d   managers: %d" },
74     { "HSMSG_TOPLEVEL_INVALID", "The name of the group you entered is invalid ($b%s$b)" },
75     { "HSMSG_MANAGERS_TOPLEVEL", "Managers of group $b*.%s$b:" },
76     { "HSMSG_MANAGERS_FAKEHOST", "Managers of group $b%s.%s$b:" },
77     { "HSMSG_MANAGERS_MANAGERS", "  %s" },
78     { "HSMSG_FAKEHOST_ASSIGNMENTS", "Assignments in group $b%s.%s$b:" },
79     { "HSMSG_FAKEHOST_ASSIGNMENT", "  $b%s$b (%s.%s.%s)" },
80     { "HSMSG_FAKEHOST_ASSIGNMENT_ACTIVE", "  $b%s$b (%s.%s.%s)   active" },
81     { "HSMSG_FAKEHOST_SET_SUCCESS", "$b%s.%s$b where set successfully." },
82     { "HSMSG_FAKEHOST_TOPLEVEL_ADDED", "Group $b%s$b successfully added." },
83     { "HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", "Group $b%s$b already exists." },
84     { "HSMSG_FAKEHOST_TOPLEVEL_DELETED", "Group $b%s$b successfully deleted." },
85     { "HSMSG_FAKEHOST_SECONDLEVEL_ADDED", "Group $b%s.%s$b successfully added." },
86     { "HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", "Group $b%s.%s$b already exists." },
87     { "HSMSG_FAKEHOST_SECONDLEVEL_DELETED", "Group $b%s.%s$b successfully deleted." },
88     { "HSMSG_FAKEHOST_RENAMED", "Group $b%s.%s$b renamed to $b%s.%s$b." },
89     { "HSMSG_MANAGER_ALREADY", "$b%s$b is already a manager of %s.%s" },
90     { "HSMSG_MANAGER_ADDED", "$b%s$b is now a manager of %s.%s" },
91     { "HSMSG_MANAGER_NOT", "$b%s$b is not a manager of %s.%s" },
92     { "HSMSG_MANAGER_DELETED", "$b%s$b is no longer a manager of %s.%s" },
93     { "HSMSG_FAKEHOST_ASSIGN_SUCCESS", "Group $b%s.%s$b was assigned successfully." },
94     { "HSMSG_FAKEHOST_ASSIGNED", "Group $b%s.%s$b is already assigned to the user." },
95     { "HSMSG_FAKEHOST_UNASSIGN_SUCCESS", "Group $b%s.%s$b was unassigned successfully." },
96     
97     { NULL, NULL }
98 };
99
100 static struct {
101     const char *nick;
102     const char *modes;
103     int toplevel_access;
104     int fallback_other_assignment : 1;
105     int manager_can_del_toplevel : 1;
106     int manager_can_del_secondlevel : 1;
107     int manager_can_rename_toplevel : 1;
108     int manager_can_rename_secondlevel : 1;
109 } hostserv_conf;
110
111 const char *hostserv_module_deps[] = { NULL };
112 struct userNode *hostserv;
113 struct helpfile *hostserv_helpfile;
114 static struct module *hostserv_module;
115 static struct service *hostserv_service;
116 static struct log_type *HS_LOG;
117 static struct hs_toplevel *toplevels = NULL;
118 static struct hs_user *hostserv_users = NULL;
119
120 /* FAKEHOST STRUCTS */
121 struct hs_toplevel {
122     char *fakehost;
123     struct hs_manager *managers;
124     struct hs_secondlevel *secondlevel;
125     struct hs_toplevel *next;
126 };
127
128 struct hs_secondlevel {
129     struct hs_toplevel *toplevel;
130     char *fakehost;
131     struct hs_manager *managers;
132     struct hs_assignment *assignments;
133     struct hs_secondlevel *next;
134 };
135
136 struct hs_assignment {
137     struct hs_secondlevel *secondlevel;
138     struct hs_user *user;
139     int active;
140     struct hs_assignment *next;
141     struct hs_assignment *unext; /* for hs_user */
142 };
143
144 struct hs_manager {
145     char type;
146     void *object;
147     int active;
148     struct hs_user *user;
149     struct hs_manager *next;
150     struct hs_manager *unext; /* for hs_user */
151 };
152
153 struct hs_user {
154     struct handle_info *hi;
155     struct hs_assignment *assignments;
156     struct hs_manager *managements;
157     struct hs_user *next;
158 };
159
160 /* MANAGEMENT FUNCTIONS for FAKEHOST STRUCTS */
161 static void hs_del_secondlevel(struct hs_secondlevel *slfh, int remove_from_tlfh);
162 static void hs_del_manager(struct hs_manager *manager, int remove_from_object);
163 static void hs_del_assignment(struct hs_assignment *assignment, int remove_from_slfh);
164 static void hs_del_user(struct hs_user *user);
165 static void hs_activate_assignment(struct hs_user *user, struct hs_assignment *assignment);
166
167 static void hs_free_all() {
168     struct hs_toplevel *tlfh, *next_tlfh;
169     struct hs_secondlevel *slfh, *next_slfh;
170     struct hs_assignment *assng, *next_assng;
171     struct hs_manager *manager, *next_manager;
172     struct hs_user *user, *next_user;
173     for(tlfh = toplevels; tlfh; tlfh = next_tlfh) {
174         next_tlfh = tlfh->next;
175         for(manager = tlfh->managers; manager; manager = next_manager) {
176             next_manager = manager->next;
177             free(manager);
178         }
179         for(slfh = tlfh->secondlevel; slfh; slfh = next_slfh) {
180             next_slfh = slfh->next;
181             for(manager = slfh->managers; manager; manager = next_manager) {
182                 next_manager = manager->next;
183                 free(manager);
184             }
185             for(assng = slfh->assignments; assng; assng = next_assng) {
186                 next_assng = assng->next;
187                 free(assng);
188             }
189             free(slfh->fakehost);
190             free(slfh);
191         }
192         free(tlfh->fakehost);
193         free(tlfh);
194     }
195     for(user = hostserv_users; user; user = next_user) {
196         next_user = user->next;
197         free(user);
198     }
199     toplevels = NULL;
200     hostserv_users = NULL;
201 }
202
203 static struct hs_toplevel *hs_add_toplevel(const char *name) {
204     struct hs_toplevel *tlfh = calloc(1, sizeof(*tlfh));
205     tlfh->fakehost = strdup(name);
206     tlfh->next = toplevels;
207     toplevels = tlfh;
208     return tlfh;
209 }
210
211 static void hs_del_toplevel(struct hs_toplevel *tlfh) {
212     //unassign all assignments
213     struct hs_secondlevel *slfh, *next_slfh;
214     struct hs_manager *manager, *next_manager;
215     for(manager = tlfh->managers; manager; manager = next_manager) {
216         next_manager = manager->next;
217         hs_del_manager(manager, 0);
218     }
219     for(slfh = tlfh->secondlevel; slfh; slfh = next_slfh) {
220         next_slfh = slfh->next;
221         hs_del_secondlevel(slfh, 0);
222     }
223     
224     struct hs_toplevel *ctlfh, *last_tlfh = NULL;
225     for(ctlfh = toplevels; ctlfh; ctlfh = ctlfh->next) {
226         if(ctlfh == tlfh) {
227             if(last_tlfh)
228                 last_tlfh->next = ctlfh->next;
229             else
230                 toplevels = ctlfh->next;
231         } else
232             last_tlfh = ctlfh;
233     }
234     free(tlfh->fakehost);
235     free(tlfh);
236 }
237
238 static void hs_rename_toplevel(struct hs_toplevel *tlfh, const char *name) {
239     struct hs_secondlevel *slfh;
240     struct hs_assignment *assng;
241     
242     free(tlfh->fakehost);
243     tlfh->fakehost = strdup(name);
244     
245     //trigger rename for all assignments
246     for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
247         for(assng = slfh->assignments; assng; assng = assng->next) {
248             if(assng->active) {
249                 hs_activate_assignment(assng->user, assng);
250             }
251         }
252     }
253 }
254
255 static struct hs_secondlevel *hs_add_secondlevel(struct hs_toplevel *tlfh, const char *name) {
256     struct hs_secondlevel *slfh = calloc(1, sizeof(*slfh));
257     slfh->toplevel = tlfh;
258     slfh->fakehost = strdup(name);
259     slfh->next = tlfh->secondlevel;
260     tlfh->secondlevel = slfh;
261     return slfh;
262 }
263
264 static void hs_del_secondlevel(struct hs_secondlevel *slfh, int remove_from_tlfh) {
265     if(remove_from_tlfh) {
266         struct hs_secondlevel *cslfh, *prev_slfh = NULL;
267         for(cslfh = slfh->toplevel->secondlevel; cslfh; cslfh = cslfh->next) {
268             if(cslfh == slfh) {
269                 if(prev_slfh)
270                     prev_slfh->next = slfh->next;
271                 else
272                     slfh->toplevel->secondlevel = slfh->next;
273                 break;
274             } else
275                 prev_slfh = cslfh;
276         }
277     }
278     struct hs_assignment *assng, *next_assng;
279     struct hs_manager *manager, *next_manager;
280     for(manager = slfh->managers; manager; manager = next_manager) {
281         next_manager = manager->next;
282         hs_del_manager(manager, 0);
283     }
284     for(assng = slfh->assignments; assng; assng = next_assng) {
285         next_assng = assng->next;
286         hs_del_assignment(assng, 0);
287     }
288     free(slfh->fakehost);
289     free(slfh);
290 }
291
292 static void hs_rename_secondlevel(struct hs_secondlevel *slfh, const char *name) {
293     struct hs_assignment *assng;
294     
295     free(slfh->fakehost);
296     slfh->fakehost = strdup(name);
297     
298     //trigger rename for all assignments
299     for(assng = slfh->assignments; assng; assng = assng->next) {
300         if(assng->active) {
301             hs_activate_assignment(assng->user, assng);
302         }
303     }
304 }
305
306 static struct hs_manager *hs_add_manager_toplevel(struct hs_toplevel *tlfh, struct hs_user *user) {
307     struct hs_manager *manager = calloc(1, sizeof(*manager));
308     manager->user = user;
309     manager->type = 1;
310     manager->object = tlfh;
311     manager->unext = user->managements;
312     user->managements = manager;
313     manager->next = tlfh->managers;
314     tlfh->managers = manager;
315     return manager;
316 }
317
318 static struct hs_manager *hs_add_manager_secondlevel(struct hs_secondlevel *slfh, struct hs_user *user) {
319     struct hs_manager *manager = calloc(1, sizeof(*manager));
320     manager->user = user;
321     manager->type = 2;
322     manager->object = slfh;
323     manager->unext = user->managements;
324     user->managements = manager;
325     manager->next = slfh->managers;
326     slfh->managers = manager;
327     return manager;
328 }
329
330 static void hs_del_manager(struct hs_manager *manager, int remove_from_object) {
331     struct hs_manager *cmanager, *prev_manager = NULL;
332     if(remove_from_object) {
333         if(manager->type == 1) {
334             struct hs_toplevel *tlfh = manager->object;
335             for(cmanager = tlfh->managers; cmanager; cmanager = cmanager->next) {
336                 if(cmanager == manager) {
337                     if(prev_manager)
338                         prev_manager->next = manager->next;
339                     else
340                         tlfh->managers = manager->next;
341                     break;
342                 } else
343                     prev_manager = cmanager;
344             }
345         } else if(manager->type == 2) {
346             struct hs_secondlevel *slfh = manager->object;
347             for(cmanager = slfh->managers; cmanager; cmanager = cmanager->next) {
348                 if(cmanager == manager) {
349                     if(prev_manager)
350                         prev_manager->next = manager->next;
351                     else
352                         slfh->managers = manager->next;
353                     break;
354                 } else
355                     prev_manager = cmanager;
356             }
357         }
358         prev_manager = NULL;
359     }
360     if(remove_from_object != 2) {
361         for(cmanager = manager->user->managements; cmanager; cmanager = cmanager->unext) {
362             if(cmanager == manager) {
363                 if(prev_manager)
364                     prev_manager->unext = manager->unext;
365                 else
366                     manager->user->managements = manager->unext;
367                 break;
368             } else
369                 prev_manager = cmanager;
370         }
371         if(manager->user->managements == NULL && manager->user->assignments == NULL)
372             hs_del_user(manager->user);
373     }
374     free(manager);
375 }
376
377 static void hs_activate_assignment(struct hs_user *user, struct hs_assignment *assignment) {
378     struct hs_toplevel *tlfh;
379     struct hs_secondlevel *slfh;
380     struct hs_assignment *assgn;
381     char fakehost[HOSTLEN];
382     
383     assert((!assignment || (assignment->user == user)));
384     
385     if(user->assignments) {
386         for(assgn = user->assignments; assgn; assgn = assgn->unext)
387             assgn->active = 0;
388     }
389     
390     if(user->hi->fakehost) {
391         free(user->hi->fakehost);
392         user->hi->fakehost = NULL;
393     }
394     
395     if(assignment) {
396         slfh = assignment->secondlevel;
397         tlfh = slfh->toplevel;
398         snprintf(fakehost, sizeof(fakehost), "$.%s.%s", slfh->fakehost, tlfh->fakehost);
399         user->hi->fakehost = strdup(fakehost);
400         assignment->active = 1;
401     }
402     
403     apply_fakehost(user->hi, NULL);
404 }
405
406 static struct hs_assignment *hs_add_assignment(struct hs_secondlevel *slfh, struct hs_user *user, int active) {
407     struct hs_assignment *assignment = calloc(1, sizeof(*assignment));
408     assignment->secondlevel = slfh;
409     assignment->user = user;
410     if(active == HS_ASSIGNMENTSTATE_AUTO)
411         assignment->active = (user->assignments == NULL ? 1 : 0);
412     else
413         assignment->active = (active == HS_ASSIGNMENTSTATE_ON ? 1 : 0);
414     assignment->next = slfh->assignments;
415     slfh->assignments = assignment;
416     assignment->unext = user->assignments;
417     user->assignments = assignment;
418     if(assignment->active) {
419         hs_activate_assignment(user, assignment);
420     }
421     return assignment;
422 }
423
424 static void hs_del_assignment(struct hs_assignment *assignment, int remove_from_slfh) {
425     struct hs_assignment *cassignment, *prev_assignment = NULL;
426     if(remove_from_slfh) {
427         for(cassignment = assignment->secondlevel->assignments; cassignment; cassignment = cassignment->next) {
428             if(cassignment == assignment) {
429                 if(prev_assignment)
430                     prev_assignment->next = assignment->next;
431                 else
432                     assignment->secondlevel->assignments = assignment->next;
433                 break;
434             } else
435                 prev_assignment = cassignment;
436         }
437         prev_assignment = NULL;
438     }
439     if(remove_from_slfh != 2) {
440         prev_assignment = NULL;
441         for(cassignment = assignment->user->assignments; cassignment; cassignment = cassignment->unext) {
442             if(cassignment == assignment) {
443                 if(prev_assignment)
444                     prev_assignment->unext = assignment->unext;
445                 else
446                     assignment->user->assignments = assignment->unext;
447                 break;
448             } else
449                 prev_assignment = cassignment;
450         }
451         
452         if(assignment->active) {
453             /* use another assignment - or fall back to default user host? */
454             cassignment = NULL;
455             if(hostserv_conf.fallback_other_assignment && assignment->user->assignments) {
456                 /* try to find another assignment from same slfh first */
457                 for(cassignment = assignment->secondlevel->assignments; cassignment; cassignment = cassignment->next) {
458                     if(cassignment != assignment && cassignment->user == assignment->user)
459                         break;
460                 }
461                 /* use another tlfh assignment */
462                 if(!cassignment)
463                     cassignment = assignment->user->assignments;
464             }
465             hs_activate_assignment(assignment->user, cassignment);
466         }
467         
468         if(assignment->user->managements == NULL && assignment->user->assignments == NULL)
469             hs_del_user(assignment->user);
470     }
471     free(assignment);
472 }
473
474 static struct hs_assignment *hs_get_assignment(struct hs_secondlevel *slfh, struct hs_user *user) {
475     struct hs_assignment *cassignment;
476     for(cassignment = slfh->assignments; cassignment; cassignment = cassignment->next) {
477         if(cassignment->user == user)
478             return cassignment;
479     }
480     return NULL;
481 }
482
483 static struct hs_user *hs_get_user(struct handle_info *hi, int create) {
484     struct hs_user *cuser;
485     for(cuser = hostserv_users; cuser; cuser = cuser->next) {
486         if(cuser->hi == hi)
487             return cuser;
488     }
489     if(create) {
490         cuser = calloc(1, sizeof(*cuser));
491         cuser->hi = hi;
492         cuser->next = hostserv_users;
493         hostserv_users = cuser;
494         return cuser;
495     } else
496         return NULL;
497 }
498
499 static void hs_del_user(struct hs_user *user) {
500     if(user->managements) {
501         struct hs_manager *manager, *next_manager;
502         for(manager = user->managements; manager; manager = next_manager) {
503             next_manager = manager->unext;
504             hs_del_manager(manager, 2);
505         }
506     }
507     if(user->assignments) {
508         struct hs_assignment *assng, *next_assng;
509         for(assng = user->assignments; assng; assng = next_assng) {
510             next_assng = assng->unext;
511             hs_del_assignment(assng, 2);
512         }
513     }
514     struct hs_user *cuser, *prev_user = NULL;
515     for(cuser = hostserv_users; cuser; cuser = cuser->next) {
516         if(cuser == user) {
517             if(prev_user)
518                 prev_user->next = user->next;
519             else
520                 hostserv_users = user->next;
521             break;
522         } else
523             prev_user = cuser;
524     }
525     free(user);
526 }
527
528 /* END OF MANAGEMENT FUNCTIONS */
529
530 static int check_management_access(struct handle_info *hi, struct hs_toplevel *tlfh, struct hs_secondlevel *slfh) {
531     if(!hi) 
532         return 0;
533     if(hi->opserv_level >= hostserv_conf.toplevel_access) 
534         return 1;
535     struct hs_user *user = hs_get_user(hi, 0);
536     if(!user)
537         return 0;
538     struct hs_manager *manager;
539     if(slfh) {
540         for(manager = user->managements; manager; manager = manager->next) {
541             if(manager->type == 2 && manager->object == slfh) 
542                 return 1;
543         }
544     }
545     if(tlfh) {
546         for(manager = user->managements; manager; manager = manager->next) {
547             if(manager->type == 1 && manager->object == tlfh) 
548                 return 1;
549         }
550     }
551     return 0;
552 }
553
554
555 static void cmd_view_toplevel_information(UNUSED_ARG(struct userNode *user), UNUSED_ARG(struct svccmd *cmd), struct hs_toplevel *tlfh) {
556     reply("HSMSG_TOPLEVEL_FAKEHOSTS", tlfh->fakehost);
557     struct hs_secondlevel *slfh;
558     if(!tlfh->secondlevel)
559         reply("HSMSG_ASSIGNED_NONE");
560     else
561     for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
562         struct hs_manager *cmanager;
563         int managers = 0;
564         for(cmanager = slfh->managers; cmanager; cmanager = cmanager->next)
565             managers++;
566         struct hs_assignment *assignment;
567         int assignments = 0;
568         for(assignment = slfh->assignments; assignment; assignment = assignment->next)
569             assignments++;
570         reply("HSMSG_TOPLEVEL_FAKEHOST", slfh->fakehost, tlfh->fakehost, assignments, managers);
571     }
572     reply("HSMSG_MANAGERS_TOPLEVEL", tlfh->fakehost);
573     struct hs_manager *cmanager;
574     if(!tlfh->managers)
575         reply("HSMSG_ASSIGNED_NONE");
576     else {
577         char managerBuf[351];
578         int managerPos = 0;
579         for(cmanager = tlfh->managers; cmanager; cmanager = cmanager->next) {
580             if(managerPos + strlen(cmanager->user->hi->handle) + 2 >= 350) {
581                 reply("HSMSG_MANAGERS_MANAGERS", managerBuf);
582                 managerPos = 0;
583             }
584             managerPos += sprintf(managerBuf + managerPos, (managerPos ? ", %s" : "%s"), cmanager->user->hi->handle);
585         }
586         reply("HSMSG_MANAGERS_MANAGERS", managerBuf);
587     }
588 }
589
590 static void cmd_view_secondlevel_information(UNUSED_ARG(struct userNode *user), UNUSED_ARG(struct svccmd *cmd), struct hs_secondlevel *slfh) {
591     reply("HSMSG_FAKEHOST_ASSIGNMENTS", slfh->fakehost, slfh->toplevel->fakehost);
592     struct hs_assignment *assignment;
593     for(assignment = slfh->assignments; assignment; assignment = assignment->next) {
594         reply((assignment->active ? "HSMSG_FAKEHOST_ASSIGNMENT_ACTIVE" : "HSMSG_FAKEHOST_ASSIGNMENT"), assignment->user->hi->handle, assignment->user->hi->handle, slfh->fakehost, slfh->toplevel->fakehost);
595     }
596     reply("HSMSG_MANAGERS_FAKEHOST", slfh->fakehost, slfh->toplevel->fakehost);
597     struct hs_manager *cmanager;
598     if(!slfh->managers)
599         reply("HSMSG_ASSIGNED_NONE");
600     else {
601         char managerBuf[351];
602         int managerPos = 0;
603         for(cmanager = slfh->managers; cmanager; cmanager = cmanager->next) {
604             if(managerPos + strlen(cmanager->user->hi->handle) + 2 >= 350) {
605                 reply("HSMSG_MANAGERS_MANAGERS", managerBuf);
606                 managerPos = 0;
607             }
608             managerPos += sprintf(managerBuf + managerPos, (managerPos ? ", %s" : "%s"), cmanager->user->hi->handle);
609         }
610         reply("HSMSG_MANAGERS_MANAGERS", managerBuf);
611     }
612 }
613
614 static MODCMD_FUNC(cmd_view) {
615     struct handle_info *hi;
616     if(argc >= 2 && !strchr(argv[1], '.')) {
617         if (!(hi = modcmd_get_handle_info(user, argv[1])))
618             return 0;
619     } else if(argc >= 2) {
620         if (!(hi = user->handle_info)) {
621             reply("NSMSG_MUST_AUTH");
622             return 0;
623         }
624         char *slfh_name = argv[1];
625         char *tlfh_name = strchr(argv[1], '.');
626         if(!tlfh_name) {
627             reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
628             return 0;
629         }
630         *tlfh_name = '\0';
631         tlfh_name++;
632         if(strchr(tlfh_name, '.')) {
633             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
634             return 0;
635         }
636         struct hs_toplevel *tlfh;
637         for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
638             if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
639         }
640         if(!tlfh) {
641             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
642             return 0;
643         }
644         if(!irccasecmp(slfh_name, "*")) {
645             if(!check_management_access(hi, tlfh, NULL)) {
646                 reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
647                 return 0;
648             }
649             cmd_view_toplevel_information(user, cmd, tlfh);
650             return 1;
651         } else {
652             struct hs_secondlevel *slfh;
653             for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
654                 if(!irccasecmp(slfh->fakehost, slfh_name)) break;
655             }
656             if(!slfh || !check_management_access(hi, tlfh, slfh)) {
657                 reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
658                 return 0;
659             }
660             cmd_view_secondlevel_information(user, cmd, slfh);
661             return 1;
662         }
663     } else {
664         if (!(hi = user->handle_info)) {
665             reply("NSMSG_MUST_AUTH");
666             return 0;
667         }
668     }
669     struct hs_user *huser = hs_get_user(hi, 0);
670     reply("HSMSG_ASSIGNED_FAKEHOSTS", hi->handle);
671     int assigncount = 0;
672     if(huser) {
673         struct hs_assignment *assignment;
674         for(assignment = huser->assignments; assignment; assignment = assignment->unext) {
675             reply((assignment->active ? "HSMSG_ASSIGNED_FAKEHOST_ACTTIVE" : "HSMSG_ASSIGNED_FAKEHOST"), assignment->secondlevel->fakehost, assignment->secondlevel->toplevel->fakehost);
676             assigncount++;
677         }
678         if(assigncount && huser->hi == user->handle_info)
679             reply("HSMSG_ASSIGN_HOWTO");
680     }
681     if(!assigncount)
682         reply("HSMSG_ASSIGNED_NONE");
683     if(user->handle_info == hi && hi->opserv_level >= hostserv_conf.toplevel_access) {
684         struct hs_toplevel *tlfh;
685         for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
686             struct hs_secondlevel *slfh;
687             struct hs_assignment *assignment;
688             int slfhs = 0, assignments = 0;
689             for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
690                 slfhs++;
691                 for(assignment = slfh->assignments; assignment; assignment = assignment->next)
692                     assignments++;
693             }
694             reply("HSMSG_MANAGED_TOPLEVEL", tlfh->fakehost, slfhs, assignments);
695         }
696         reply("HSMSG_MANAGE_HOWTO");
697     } else if(huser && huser->managements) {
698         reply("HSMSG_MANAGED_FAKEHOSTS", hi->handle);
699         struct hs_manager *manager;
700         for(manager = huser->managements; manager; manager = manager->unext) {
701             if(manager->type == 1) {
702                 struct hs_toplevel *tlfh = manager->object;
703                 struct hs_secondlevel *slfh;
704                 struct hs_assignment *assignment;
705                 int slfhs = 0, assignments = 0;
706                 for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
707                     slfhs++;
708                     for(assignment = slfh->assignments; assignment; assignment = assignment->next)
709                         assignments++;
710                 }
711                 reply("HSMSG_MANAGED_TOPLEVEL", tlfh->fakehost, slfhs, assignments);
712             }
713         }
714         for(manager = huser->managements; manager; manager = manager->next) {
715             if(manager->type == 2) {
716                 struct hs_secondlevel *slfh = manager->object;
717                 struct hs_toplevel *tlfh = slfh->toplevel;
718                 //check if the user is already a manager of the tlfh
719                 struct hs_manager *cmanager;
720                 for(cmanager = tlfh->managers; cmanager; cmanager = cmanager->next) {
721                     if(cmanager->user == huser) break;
722                 }
723                 if(cmanager) continue;
724                 struct hs_assignment *assignment;
725                 int assignments = 0;
726                 for(assignment = slfh->assignments; assignment; assignment = assignment->next)
727                     assignments++;
728                 reply("HSMSG_MANAGED_FAKEHOST", slfh->fakehost, tlfh->fakehost, assignments);
729             }
730         }
731         if(huser->hi == user->handle_info)
732             reply("HSMSG_MANAGE_HOWTO");
733     }
734     return 1;
735 }
736
737 static MODCMD_FUNC(cmd_addhost) {
738     struct handle_info *hi;
739     struct hs_toplevel *tlfh;
740     struct hs_secondlevel *slfh;
741     if (!(hi = user->handle_info)) {
742         reply("NSMSG_MUST_AUTH");
743         return 0;
744     }
745     char *slfh_name = argv[1];
746     char *tlfh_name = strchr(argv[1], '.');
747     if(!tlfh_name) {
748         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
749         return 0;
750     }
751     *tlfh_name = '\0';
752     tlfh_name++;
753     if(strchr(tlfh_name, '.')) {
754         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
755         return 0;
756     }
757     if(!irccasecmp(slfh_name, "*")) {
758         if(!check_management_access(hi, NULL, NULL)) {
759             reply("HSMSG_ACCESS_DENIED");
760             return 0;
761         }
762         for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
763             if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
764         }
765         if(tlfh) {
766             reply("HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", slfh_name, tlfh_name);
767             return 0;
768         }
769         if(irccasecmp(tlfh_name, "*")) {
770                     reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
771                     return 0;   
772         }
773         hs_add_toplevel(tlfh_name);
774         reply("HSMSG_FAKEHOST_TOPLEVEL_ADDED", tlfh_name);
775     } else {
776         for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
777             if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
778         }
779         if(!tlfh) {
780             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
781             return 0;
782         }
783         if(!check_management_access(hi, tlfh, NULL)) {
784             reply("HSMSG_ACCESS_DENIED");
785             return 0;
786         }
787         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
788             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
789         }
790         if(slfh) {
791             reply("HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", slfh_name, tlfh_name);
792             return 0;
793         }
794         hs_add_secondlevel(tlfh, slfh_name);
795         reply("HSMSG_FAKEHOST_SECONDLEVEL_ADDED", slfh_name, tlfh_name);
796     }
797     return 1;
798 }
799
800 static MODCMD_FUNC(cmd_delhost) {
801     struct handle_info *hi;
802     if (!(hi = user->handle_info)) {
803         reply("NSMSG_MUST_AUTH");
804         return 0;
805     }
806     char *slfh_name = argv[1];
807     char *tlfh_name = strchr(argv[1], '.');
808     *tlfh_name = '\0';
809     tlfh_name++;
810     if(strchr(tlfh_name, '.')) {
811         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
812         return 0;
813     }
814     struct hs_toplevel *tlfh;
815     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
816         if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
817     }
818     if(!tlfh) {
819         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
820         return 0;
821     }
822     if(!irccasecmp(slfh_name, "*")) {
823         if(!check_management_access(hi, (hostserv_conf.manager_can_del_toplevel ? tlfh : NULL), NULL)) { /* manager access is enough to delete whole toplevel? */
824             reply("HSMSG_ACCESS_DENIED");
825             return 0;
826         }
827         hs_del_toplevel(tlfh);
828         reply("HSMSG_FAKEHOST_TOPLEVEL_DELETED", tlfh_name);
829     } else {
830         struct hs_secondlevel *slfh;
831         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
832             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
833         }
834         if(!slfh) {
835             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
836             return 0;
837         }
838         if(!check_management_access(hi, tlfh, (hostserv_conf.manager_can_del_secondlevel ? slfh : NULL))) {
839             reply("HSMSG_ACCESS_DENIED");
840             return 0;
841         }
842         hs_del_secondlevel(slfh, 1);
843         reply("HSMSG_FAKEHOST_SECONDLEVEL_DELETED", slfh_name, tlfh_name);
844     }
845     return 1;
846 }
847
848 static MODCMD_FUNC(cmd_renamehost) {
849     struct handle_info *hi;
850     if (!(hi = user->handle_info)) {
851         reply("NSMSG_MUST_AUTH");
852         return 0;
853     }
854     //old fakehost name
855     char *slfh_name = argv[1];
856     char *tlfh_name = strchr(argv[1], '.');
857     if(!tlfh_name) {
858         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
859         return 0;
860     }
861     *tlfh_name = '\0';
862     tlfh_name++;
863     if(strchr(tlfh_name, '.')) {
864         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
865         return 0;
866     }
867     struct hs_toplevel *tlfh;
868     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
869         if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
870     }
871     if(!tlfh) {
872         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
873         return 0;
874     }
875     //new fakehost name
876     char *new_slfh_name = argv[2];
877     char *new_tlfh_name = strchr(argv[2], '.');
878     if(!new_tlfh_name) {
879         reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
880         return 0;
881     }
882     *new_tlfh_name = '\0';
883     new_tlfh_name++;
884     if(strchr(new_tlfh_name, '.')) {
885         new_tlfh_name--;
886         *new_tlfh_name = '.';
887         reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
888         return 0;
889     }
890     if(!irccasecmp(slfh_name, "*")) {
891         if(!check_management_access(hi, (hostserv_conf.manager_can_rename_toplevel ? tlfh : NULL), NULL)) { /* manager access is enough to delete whole toplevel? */
892             reply("HSMSG_ACCESS_DENIED");
893             return 0;
894         }
895         if(irccasecmp(new_slfh_name, "*")) {
896             //can't rename toplevel into secondlevel fakehost!
897             new_tlfh_name--;
898             *new_tlfh_name = '.';
899             reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
900             return 0;
901         }
902         struct hs_toplevel *ctlfh;
903         for(ctlfh = toplevels; ctlfh; ctlfh = ctlfh->next) {
904             if(!irccasecmp(ctlfh->fakehost, new_tlfh_name)) break;
905         }
906         if(ctlfh) {
907             reply("HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name);
908             return 0;
909         }
910                 if(irccasecmp(new_tlfh_name, "*")) {
911                     reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
912                     return 0;   
913         }
914         if(strcmp(new_tlfh_name, tlfh->fakehost))
915             hs_rename_toplevel(tlfh, new_tlfh_name);
916         reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name);
917     } else {
918         struct hs_secondlevel *slfh;
919         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
920             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
921         }
922         if(!slfh) {
923             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
924             return 0;
925         }
926         if(!check_management_access(hi, tlfh, (hostserv_conf.manager_can_rename_secondlevel ? slfh : NULL))) {
927             reply("HSMSG_ACCESS_DENIED");
928             return 0;
929         }
930         if(irccasecmp(new_tlfh_name, tlfh_name)) {
931             //can't rename toplevel and secondlevel fakehost with one command!
932             new_tlfh_name--;
933             *new_tlfh_name = '.';
934             reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
935         }
936         struct hs_secondlevel *cslfh;
937         for(cslfh = tlfh->secondlevel; cslfh; cslfh = cslfh->next) {
938             if(!irccasecmp(cslfh->fakehost, new_slfh_name)) break;
939         }
940         if(cslfh) {
941             reply("HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name);
942             return 0;
943         }
944         if(strcmp(new_slfh_name, slfh->fakehost))
945             hs_rename_secondlevel(slfh, new_slfh_name);
946         reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name);
947     }
948     return 1;
949 }
950
951 static MODCMD_FUNC(cmd_addmanager) {
952     struct handle_info *hi;
953     char *fakehost;
954     if(argc >= 3) {
955         if(!strchr(argv[1], '.')) {
956             if (!(hi = modcmd_get_handle_info(user, argv[1])))
957                 return 0;
958             fakehost = argv[2];
959         } else {
960             if (!(hi = modcmd_get_handle_info(user, argv[2])))
961                 return 0;
962             fakehost = argv[1];
963         }
964         char *slfh_name = fakehost;
965         char *tlfh_name = strchr(fakehost, '.');
966         if(!tlfh_name) {
967             reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
968             return 0;
969         }
970         *tlfh_name = '\0';
971         tlfh_name++;
972         if(strchr(tlfh_name, '.')) {
973             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
974             return 0;
975         }
976         struct hs_toplevel *tlfh;
977         struct hs_secondlevel *slfh = NULL;
978         for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
979             if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
980         }
981         if(!tlfh) {
982             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
983             return 0;
984         }
985         if(!irccasecmp(slfh_name, "*")) {
986             if(!check_management_access(user->handle_info, tlfh, NULL)) {
987                 reply("HSMSG_ACCESS_DENIED");
988                 return 0;
989             }
990         } else {
991             for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
992                 if(!irccasecmp(slfh->fakehost, slfh_name)) break;
993             }
994             if(!slfh) {
995                 reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
996                 return 0;
997             }
998             if(!check_management_access(user->handle_info, tlfh, slfh)) {
999                 reply("HSMSG_ACCESS_DENIED");
1000                 return 0;
1001             }
1002         }
1003         struct hs_user *huser = hs_get_user(hi, 1);
1004         struct hs_manager *manager;
1005         if(slfh) {
1006             for(manager = huser->managements; manager; manager = manager->next) {
1007                 if(manager->type == 2 && manager->object == slfh) {
1008                     reply("HSMSG_MANAGER_ALREADY", hi->handle, slfh_name, tlfh_name);
1009                     return 0;
1010                 }
1011             }
1012         }
1013         for(manager = huser->managements; manager; manager = manager->next) {
1014             if(manager->type == 1 && manager->object == tlfh) {
1015                 reply("HSMSG_MANAGER_ALREADY", hi->handle, "*", tlfh_name);
1016                 return 0;
1017             }
1018         }
1019         if(slfh)
1020             hs_add_manager_secondlevel(slfh, huser);
1021         else
1022             hs_add_manager_toplevel(tlfh, huser);
1023         reply("HSMSG_MANAGER_ADDED", hi->handle, slfh_name, tlfh_name);
1024         return 1;
1025     }
1026     return 0;
1027 }
1028
1029 static MODCMD_FUNC(cmd_delmanager) {
1030     struct handle_info *hi;
1031     char *fakehost;
1032     if(!strchr(argv[1], '.')) {
1033         if (!(hi = modcmd_get_handle_info(user, argv[1])))
1034             return 0;
1035         fakehost = argv[2];
1036     } else {
1037         if (!(hi = modcmd_get_handle_info(user, argv[2])))
1038             return 0;
1039         fakehost = argv[1];
1040     }
1041     char *slfh_name = fakehost;
1042     char *tlfh_name = strchr(fakehost, '.');
1043     if(!tlfh_name) {
1044         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
1045         return 0;
1046     }
1047     tlfh_name++;
1048     if(strchr(tlfh_name, '.')) {
1049         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1050         return 0;
1051     }
1052     struct hs_toplevel *tlfh;
1053     struct hs_secondlevel *slfh = NULL;
1054     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
1055         if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
1056     }
1057     if(!tlfh) {
1058         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1059         return 0;
1060     }
1061     if(!irccasecmp(slfh_name, "*")) {
1062         if(!check_management_access(user->handle_info, tlfh, NULL)) {
1063             reply("HSMSG_ACCESS_DENIED");
1064             return 0;
1065         }
1066     } else {
1067         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
1068             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
1069         }
1070         if(!slfh) {
1071             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1072             return 0;
1073         }
1074         if(!check_management_access(user->handle_info, tlfh, slfh)) {
1075             reply("HSMSG_ACCESS_DENIED");
1076             return 0;
1077         }
1078     }
1079     struct hs_user *huser = hs_get_user(hi, 0);
1080     struct hs_manager *manager;
1081     if(!huser) {
1082         reply("HSMSG_MANAGER_NOT", hi->handle, slfh_name, tlfh_name);
1083         return 0;
1084     }
1085     if(slfh) {
1086         for(manager = huser->managements; manager; manager = manager->next) {
1087             if(manager->type == 2 && manager->object == slfh) 
1088                 break;
1089         }
1090         if(!manager) {
1091             reply("HSMSG_MANAGER_NOT", hi->handle, slfh_name, tlfh_name);
1092             return 0;
1093         }
1094     } else {
1095         for(manager = huser->managements; manager; manager = manager->next) {
1096             if(manager->type == 1 && manager->object == tlfh) 
1097                 break;
1098         }
1099         if(!manager) {
1100             reply("HSMSG_MANAGER_NOT", hi->handle, "*", tlfh_name);
1101             return 0;
1102         }
1103     }
1104     hs_del_manager(manager, 1);
1105     reply("HSMSG_MANAGER_DELETED", hi->handle, slfh_name, tlfh_name);
1106     return 1;
1107 }
1108
1109 static MODCMD_FUNC(cmd_set) {
1110     struct handle_info *hi;
1111     struct hs_user *hs_user;
1112     struct hs_assignment *assignment;
1113     struct hs_toplevel *tlfh;
1114     struct hs_secondlevel *slfh;
1115     char *fakehost;
1116     
1117     if (!(hi = user->handle_info)) {
1118         reply("NSMSG_MUST_AUTH");
1119         return 0;
1120     }
1121     hs_user = hs_get_user(hi, 0);
1122     if(!hs_user)
1123         return 0; //nothing to do here
1124     if(!strcmp(argv[1], "*")) {
1125         hs_activate_assignment(hs_user, NULL);
1126         return 1;
1127     } else {
1128         fakehost = argv[1];
1129         char *slfh_name = fakehost;
1130         char *tlfh_name = strchr(fakehost, '.');
1131         if(tlfh_name) {
1132             *tlfh_name = '\0';
1133             tlfh_name++;
1134         }
1135         if(!tlfh_name || strchr(tlfh_name, '.')) {
1136             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, (tlfh_name ? tlfh_name : ""));
1137             return 0;
1138         }
1139         for(assignment = hs_user->assignments; assignment; assignment = assignment->unext) {
1140             slfh = assignment->secondlevel;
1141             tlfh = slfh->toplevel;
1142             if(!irccasecmp(tlfh_name, tlfh->fakehost) && !irccasecmp(slfh_name, slfh->fakehost)) {
1143                 hs_activate_assignment(hs_user, assignment);
1144                 reply("HSMSG_FAKEHOST_SET_SUCCESS", slfh->fakehost, tlfh->fakehost);
1145                 return 1;
1146             }
1147         }
1148         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1149         return 0;
1150     }
1151 }
1152
1153 static MODCMD_FUNC(cmd_assign) {
1154     struct handle_info *hi;
1155     char *fakehost;
1156     if(!strchr(argv[1], '.')) {
1157         if (!(hi = modcmd_get_handle_info(user, argv[1])))
1158             return 0;
1159         fakehost = argv[2];
1160     } else {
1161         if (!(hi = modcmd_get_handle_info(user, argv[2])))
1162             return 0;
1163         fakehost = argv[1];
1164     }
1165     if (!user->handle_info) {
1166         reply("NSMSG_MUST_AUTH");
1167         return 0;
1168     }
1169     char *slfh_name = fakehost;
1170     char *tlfh_name = strchr(fakehost, '.');
1171     if(!tlfh_name) {
1172         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
1173         return 0;
1174     }
1175     tlfh_name++;
1176     if(strchr(tlfh_name, '.')) {
1177         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1178         return 0;
1179     }
1180     struct hs_toplevel *tlfh;
1181     struct hs_secondlevel *slfh;
1182     struct hs_user *hs_user = hs_get_user(hi, 1);
1183     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
1184         if(!irccasecmp(tlfh_name, tlfh->fakehost)) {
1185             for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
1186                 if(!irccasecmp(slfh_name, slfh->fakehost)) {
1187                     if(!check_management_access(user->handle_info, tlfh, slfh)) {
1188                         reply("HSMSG_ACCESS_DENIED");
1189                         return 0;
1190                     }
1191                     if(hs_get_assignment(slfh, hs_user)) {
1192                         reply("HSMSG_FAKEHOST_ASSIGNED", slfh_name, tlfh_name);
1193                         return 0;
1194                     }
1195                     hs_add_assignment(slfh, hs_user, HS_ASSIGNMENTSTATE_AUTO);
1196                     reply("HSMSG_FAKEHOST_ASSIGN_SUCCESS", slfh_name, tlfh_name);
1197                     return 1;
1198                 }
1199             }
1200         }
1201     }
1202     reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1203     return 0;
1204 }
1205
1206 static MODCMD_FUNC(cmd_unassign) {
1207     struct handle_info *hi;
1208     char *fakehost;
1209     if(!strchr(argv[1], '.')) {
1210         if (!(hi = modcmd_get_handle_info(user, argv[1])))
1211             return 0;
1212         fakehost = argv[2];
1213     } else {
1214         if (!(hi = modcmd_get_handle_info(user, argv[2])))
1215             return 0;
1216         fakehost = argv[1];
1217     }
1218     if (!user->handle_info) {
1219         reply("NSMSG_MUST_AUTH");
1220         return 0;
1221     }
1222     char *slfh_name = fakehost;
1223     char *tlfh_name = strchr(fakehost, '.');
1224     if(!tlfh_name) {
1225         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
1226         return 0;
1227     }
1228     tlfh_name++;
1229     if(strchr(tlfh_name, '.')) {
1230         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1231         return 0;
1232     }
1233     struct hs_assignment *assignment;
1234     struct hs_user *hs_user = hs_get_user(hi, 0);
1235     if(!hs_user) {
1236         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1237         return 0;
1238     }
1239     for(assignment = hs_user->assignments; assignment; assignment = assignment->unext) {
1240         if(!irccasecmp(slfh_name, assignment->secondlevel->fakehost)) {
1241             if(!irccasecmp(tlfh_name, assignment->secondlevel->toplevel->fakehost)) {
1242                 if(!check_management_access(user->handle_info, assignment->secondlevel->toplevel, assignment->secondlevel)) {
1243                     reply("HSMSG_ACCESS_DENIED");
1244                     return 0;
1245                 }
1246                 hs_del_assignment(assignment, 1);
1247                 reply("HSMSG_FAKEHOST_UNASSIGN_SUCCESS", slfh_name, tlfh_name);
1248                 return 1;
1249             }
1250         }
1251     }
1252     reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1253     return 0;
1254 }
1255
1256 static void hostserv_conf_read(void) {
1257     dict_t conf_node;
1258     const char *str;
1259
1260     str = "modules/hostserv";
1261     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
1262         log_module(HS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
1263         return;
1264     }
1265
1266     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
1267     if(hostserv_conf.nick && strcmp(hostserv_conf.nick, str)) {
1268         //nick changed
1269     }
1270     hostserv_conf.nick = str;
1271     
1272     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
1273     hostserv_conf.modes = (str ? str : NULL);
1274     
1275     str = database_get_data(conf_node, "toplevel_access", RECDB_QSTRING);
1276     unsigned int toplevel_access = atoi(str);
1277     hostserv_conf.toplevel_access = (toplevel_access ? toplevel_access : 600);
1278     
1279     str = database_get_data(conf_node, "fallback_other_assignment", RECDB_QSTRING);
1280     hostserv_conf.fallback_other_assignment = (atoi(str) ? 1 : 0);
1281     
1282     str = database_get_data(conf_node, "manager_can_del_toplevel", RECDB_QSTRING);
1283     hostserv_conf.manager_can_del_toplevel = (atoi(str) ? 1 : 0);
1284     
1285     str = database_get_data(conf_node, "manager_can_del_secondlevel", RECDB_QSTRING);
1286     hostserv_conf.manager_can_del_secondlevel = (atoi(str) ? 1 : 0);
1287     
1288     str = database_get_data(conf_node, "manager_can_rename_toplevel", RECDB_QSTRING);
1289     hostserv_conf.manager_can_rename_toplevel = (atoi(str) ? 1 : 0);
1290     
1291     str = database_get_data(conf_node, "manager_can_rename_secondlevel", RECDB_QSTRING);
1292     hostserv_conf.manager_can_rename_secondlevel = (atoi(str) ? 1 : 0);
1293     
1294     /*str = database_get_data(conf_node, "description", RECDB_QSTRING);
1295     hostserv_conf.description = (str ? str : NULL);*/
1296 }
1297
1298 static int hostserv_saxdb_read_secondlevel(const char *name, void *data, UNUSED_ARG(void *extra));
1299 static int hostserv_saxdb_read_assignments(const char *name, void *data, UNUSED_ARG(void *extra));
1300
1301 static int hostserv_saxdb_read_toplevel(const char *name, void *data, UNUSED_ARG(void *extra)) {
1302     struct record_data *rd = data;
1303     struct hs_toplevel *tlfh;
1304     struct hs_manager *managerTL;
1305     struct hs_user *user;
1306     struct dict *object;
1307
1308      if (rd->type == RECDB_OBJECT) {
1309         dict_t db = GET_RECORD_OBJECT(rd);
1310         dict_iterator_t it;
1311         
1312         tlfh = hs_add_toplevel(name);
1313         
1314         if ((object = database_get_data(db, KEY_MANAGERS, RECDB_OBJECT))) {
1315             for (it = dict_first(object); it; it = iter_next(it)) {
1316                 user = hs_get_user(get_handle_info(iter_key(it)), 1);
1317                 //rd = iter_data(it);
1318                 /* nothing in here, yet */
1319                 managerTL = hs_add_manager_toplevel(tlfh, user);
1320                 if (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING))
1321                     managerTL->active = 1;
1322                 else
1323                     managerTL->active = 0;
1324             }
1325         }
1326         
1327         if ((object = database_get_data(db, KEY_SECONDLEVEL, RECDB_OBJECT)))
1328             dict_foreach(object, hostserv_saxdb_read_secondlevel, tlfh);
1329     }
1330     return 0;
1331 }
1332
1333 static int hostserv_saxdb_read_secondlevel(const char *name, void *data, UNUSED_ARG(void *extra)) {
1334     struct record_data *rd = data;
1335     struct hs_toplevel *tlfh = extra;
1336     struct hs_secondlevel *slfh;
1337     struct hs_manager *managerSL;
1338     struct hs_user *user;
1339     struct dict *object;
1340
1341     if (rd->type == RECDB_OBJECT) {
1342         dict_t db = GET_RECORD_OBJECT(rd);
1343         dict_iterator_t it;
1344         
1345         slfh = hs_add_secondlevel(tlfh, name);
1346         
1347         if ((object = database_get_data(db, KEY_MANAGERS, RECDB_OBJECT))) {
1348             for (it = dict_first(object); it; it = iter_next(it)) {
1349                 user = hs_get_user(get_handle_info(iter_key(it)), 1);
1350                 //rd = iter_data(it);
1351                 /* nothing in here, yet */
1352                 managerSL = hs_add_manager_secondlevel(slfh, user);
1353                 if (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING))
1354                     managerSL->active = 1;
1355                 else
1356                     managerSL->active = 0;
1357             }
1358         }
1359         
1360         if ((object = database_get_data(db, KEY_ASSIGNMENTS, RECDB_OBJECT)))
1361             dict_foreach(object, hostserv_saxdb_read_assignments, slfh);
1362     }
1363     return 0;
1364 }
1365
1366 static int hostserv_saxdb_read_assignments(const char *name, void *data, UNUSED_ARG(void *extra)) {
1367     struct record_data *rd = data;
1368     struct hs_secondlevel *slfh = extra;
1369     struct hs_user *user;
1370     int active;
1371     
1372     if (rd->type == RECDB_OBJECT) {
1373         dict_t db = GET_RECORD_OBJECT(rd);
1374         
1375         user = hs_get_user(get_handle_info(name), 1);
1376         active = (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING) ? HS_ASSIGNMENTSTATE_ON : HS_ASSIGNMENTSTATE_OFF);
1377         
1378         hs_add_assignment(slfh, user, active);
1379     }
1380     
1381     return 0;
1382 }
1383
1384 static int
1385 hostserv_saxdb_read(struct dict *db)
1386 {
1387     struct dict *object;
1388
1389     if ((object = database_get_data(db, KEY_TOPLEVEL, RECDB_OBJECT)))
1390         dict_foreach(object, hostserv_saxdb_read_toplevel, NULL);
1391
1392     return 1;
1393 }
1394
1395 static int
1396 hostserv_saxdb_write(struct saxdb_context *ctx)
1397 {
1398     struct hs_toplevel *tlfh;
1399     struct hs_secondlevel *slfh;
1400     struct hs_assignment *assng;
1401     struct hs_manager *manager;
1402
1403     saxdb_start_record(ctx, KEY_TOPLEVEL, 1);
1404     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
1405         saxdb_start_record(ctx, tlfh->fakehost, 1);
1406         
1407         saxdb_start_record(ctx, KEY_MANAGERS, 1);
1408         for(manager = tlfh->managers; manager; manager = manager->next) {
1409             saxdb_start_record(ctx, manager->user->hi->handle, 0);
1410             //additional manager information?
1411             if(manager->active)
1412                 saxdb_write_int(ctx, KEY_ACTIVE, manager->active);
1413             saxdb_end_record(ctx);
1414         }
1415         saxdb_end_record(ctx);
1416         
1417         saxdb_start_record(ctx, KEY_SECONDLEVEL, 1);
1418         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
1419             saxdb_start_record(ctx, slfh->fakehost, 1);
1420             
1421             saxdb_start_record(ctx, KEY_MANAGERS, 1);
1422             for(manager = slfh->managers; manager; manager = manager->next) {
1423                 saxdb_start_record(ctx, manager->user->hi->handle, 0);
1424                 //additional manager information?
1425                 if(manager->active)
1426                     saxdb_write_int(ctx, KEY_ACTIVE, manager->active);
1427                 saxdb_end_record(ctx);
1428             }
1429             saxdb_end_record(ctx);
1430             
1431             saxdb_start_record(ctx, KEY_ASSIGNMENTS, 1);
1432             for(assng = slfh->assignments; assng; assng = assng->next) {
1433                 saxdb_start_record(ctx, assng->user->hi->handle, 0);
1434                 //additional assignment information?
1435                 if(assng->active)
1436                     saxdb_write_int(ctx, KEY_ACTIVE, assng->active);
1437                 saxdb_end_record(ctx);
1438             }
1439             saxdb_end_record(ctx);
1440             
1441             saxdb_end_record(ctx);
1442         }
1443         saxdb_end_record(ctx);
1444         
1445         saxdb_end_record(ctx);
1446     }
1447     saxdb_end_record(ctx);
1448     
1449     return 0;
1450 }
1451
1452
1453 static void hostserv_db_cleanup(void) {
1454     hs_free_all();
1455 }
1456
1457 int hostserv_init() {
1458     HS_LOG = log_register_type("HostServ", "file:hostserv.log");
1459     
1460     const char *nick, *modes;
1461     if((nick = conf_get_data("modules/hostserv/nick", RECDB_QSTRING))) {
1462         modes = conf_get_data("modules/hostserv/modes", RECDB_QSTRING);
1463         hostserv = AddLocalUser(nick, nick, NULL, "Host Service", modes);
1464         hostserv_service = service_register(hostserv);
1465         hostserv_service->trigger = '*';
1466     }
1467         
1468     conf_register_reload(hostserv_conf_read);
1469     reg_exit_func(hostserv_db_cleanup);
1470     saxdb_register("HostServ", hostserv_saxdb_read, hostserv_saxdb_write);
1471     hostserv_module = module_register("HostServ", HS_LOG, "mod-hostserv.help", NULL);
1472     modcmd_register(hostserv_module, "view", cmd_view, 0, MODCMD_REQUIRE_AUTHED, NULL);
1473     modcmd_register(hostserv_module, "addmanager", cmd_addmanager, 3, MODCMD_REQUIRE_AUTHED, NULL);
1474     modcmd_register(hostserv_module, "delmanager", cmd_delmanager, 3, MODCMD_REQUIRE_AUTHED, NULL);
1475     modcmd_register(hostserv_module, "set", cmd_set, 2, MODCMD_REQUIRE_AUTHED, NULL);
1476     modcmd_register(hostserv_module, "assign", cmd_assign, 3, MODCMD_REQUIRE_AUTHED, NULL);
1477     modcmd_register(hostserv_module, "unassign", cmd_unassign, 3, MODCMD_REQUIRE_AUTHED, NULL);
1478     modcmd_register(hostserv_module, "addhost", cmd_addhost, 2, MODCMD_REQUIRE_AUTHED, NULL);
1479     modcmd_register(hostserv_module, "delhost", cmd_delhost, 2, MODCMD_REQUIRE_AUTHED, NULL);
1480     modcmd_register(hostserv_module, "renamehost", cmd_renamehost, 3, MODCMD_REQUIRE_AUTHED, NULL);
1481     message_register_table(msgtab);
1482     return 1;
1483 }
1484
1485 int hostserv_finalize(void) {
1486     dict_t conf_node;
1487     const char *str;
1488
1489     str = "modules/hostserv";
1490     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
1491         log_module(HS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
1492         return 0;
1493     }
1494
1495     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
1496     if (str) hostserv_conf.nick = str;
1497     
1498     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
1499     if (str) hostserv_conf.modes = str; 
1500     return 1;
1501 }