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         hs_add_toplevel(tlfh_name);
772         reply("HSMSG_FAKEHOST_TOPLEVEL_ADDED", tlfh_name);
773     } else {
774         for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
775             if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
776         }
777         if(!tlfh) {
778             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
779             return 0;
780         }
781         if(!check_management_access(hi, tlfh, NULL)) {
782             reply("HSMSG_ACCESS_DENIED");
783             return 0;
784         }
785         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
786             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
787         }
788         if(slfh) {
789             reply("HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", slfh_name, tlfh_name);
790             return 0;
791         }
792         hs_add_secondlevel(tlfh, slfh_name);
793         reply("HSMSG_FAKEHOST_SECONDLEVEL_ADDED", slfh_name, tlfh_name);
794     }
795     return 1;
796 }
797
798 static MODCMD_FUNC(cmd_delhost) {
799     struct handle_info *hi;
800     if (!(hi = user->handle_info)) {
801         reply("NSMSG_MUST_AUTH");
802         return 0;
803     }
804     char *slfh_name = argv[1];
805     char *tlfh_name = strchr(argv[1], '.');
806     *tlfh_name = '\0';
807     tlfh_name++;
808     if(strchr(tlfh_name, '.')) {
809         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
810         return 0;
811     }
812     struct hs_toplevel *tlfh;
813     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
814         if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
815     }
816     if(!tlfh) {
817         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
818         return 0;
819     }
820     if(!irccasecmp(slfh_name, "*")) {
821         if(!check_management_access(hi, (hostserv_conf.manager_can_del_toplevel ? tlfh : NULL), NULL)) { /* manager access is enough to delete whole toplevel? */
822             reply("HSMSG_ACCESS_DENIED");
823             return 0;
824         }
825         hs_del_toplevel(tlfh);
826         reply("HSMSG_FAKEHOST_TOPLEVEL_DELETED", tlfh_name);
827     } else {
828         struct hs_secondlevel *slfh;
829         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
830             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
831         }
832         if(!slfh) {
833             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
834             return 0;
835         }
836         if(!check_management_access(hi, tlfh, (hostserv_conf.manager_can_del_secondlevel ? slfh : NULL))) {
837             reply("HSMSG_ACCESS_DENIED");
838             return 0;
839         }
840         hs_del_secondlevel(slfh, 1);
841         reply("HSMSG_FAKEHOST_SECONDLEVEL_DELETED", slfh_name, tlfh_name);
842     }
843     return 1;
844 }
845
846 static MODCMD_FUNC(cmd_renamehost) {
847     struct handle_info *hi;
848     if (!(hi = user->handle_info)) {
849         reply("NSMSG_MUST_AUTH");
850         return 0;
851     }
852     //old fakehost name
853     char *slfh_name = argv[1];
854     char *tlfh_name = strchr(argv[1], '.');
855     if(!tlfh_name) {
856         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
857         return 0;
858     }
859     *tlfh_name = '\0';
860     tlfh_name++;
861     if(strchr(tlfh_name, '.')) {
862         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
863         return 0;
864     }
865     struct hs_toplevel *tlfh;
866     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
867         if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
868     }
869     if(!tlfh) {
870         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
871         return 0;
872     }
873     //new fakehost name
874     char *new_slfh_name = argv[2];
875     char *new_tlfh_name = strchr(argv[2], '.');
876     if(!new_tlfh_name) {
877         reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
878         return 0;
879     }
880     *new_tlfh_name = '\0';
881     new_tlfh_name++;
882     if(strchr(new_tlfh_name, '.')) {
883         new_tlfh_name--;
884         *new_tlfh_name = '.';
885         reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
886         return 0;
887     }
888     if(!irccasecmp(slfh_name, "*")) {
889         if(!check_management_access(hi, (hostserv_conf.manager_can_rename_toplevel ? tlfh : NULL), NULL)) { /* manager access is enough to delete whole toplevel? */
890             reply("HSMSG_ACCESS_DENIED");
891             return 0;
892         }
893         if(irccasecmp(new_slfh_name, "*")) {
894             //can't rename toplevel into secondlevel fakehost!
895             new_tlfh_name--;
896             *new_tlfh_name = '.';
897             reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
898             return 0;
899         }
900         struct hs_toplevel *ctlfh;
901         for(ctlfh = toplevels; ctlfh; ctlfh = ctlfh->next) {
902             if(!irccasecmp(ctlfh->fakehost, new_tlfh_name)) break;
903         }
904         if(ctlfh) {
905             reply("HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name);
906             return 0;
907         }
908         if(strcmp(new_tlfh_name, tlfh->fakehost))
909             hs_rename_toplevel(tlfh, new_tlfh_name);
910         reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name);
911     } else {
912         struct hs_secondlevel *slfh;
913         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
914             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
915         }
916         if(!slfh) {
917             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
918             return 0;
919         }
920         if(!check_management_access(hi, tlfh, (hostserv_conf.manager_can_rename_secondlevel ? slfh : NULL))) {
921             reply("HSMSG_ACCESS_DENIED");
922             return 0;
923         }
924         if(irccasecmp(new_tlfh_name, tlfh_name)) {
925             //can't rename toplevel and secondlevel fakehost with one command!
926             new_tlfh_name--;
927             *new_tlfh_name = '.';
928             reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name);
929         }
930         struct hs_secondlevel *cslfh;
931         for(cslfh = tlfh->secondlevel; cslfh; cslfh = cslfh->next) {
932             if(!irccasecmp(cslfh->fakehost, new_slfh_name)) break;
933         }
934         if(cslfh) {
935             reply("HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name);
936             return 0;
937         }
938         if(strcmp(new_slfh_name, slfh->fakehost))
939             hs_rename_secondlevel(slfh, new_slfh_name);
940         reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name);
941     }
942     return 1;
943 }
944
945 static MODCMD_FUNC(cmd_addmanager) {
946     struct handle_info *hi;
947     char *fakehost;
948     if(argc >= 3) {
949         if(!strchr(argv[1], '.')) {
950             if (!(hi = modcmd_get_handle_info(user, argv[1])))
951                 return 0;
952             fakehost = argv[2];
953         } else {
954             if (!(hi = modcmd_get_handle_info(user, argv[2])))
955                 return 0;
956             fakehost = argv[1];
957         }
958         char *slfh_name = fakehost;
959         char *tlfh_name = strchr(fakehost, '.');
960         if(!tlfh_name) {
961             reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
962             return 0;
963         }
964         *tlfh_name = '\0';
965         tlfh_name++;
966         if(strchr(tlfh_name, '.')) {
967             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
968             return 0;
969         }
970         struct hs_toplevel *tlfh;
971         struct hs_secondlevel *slfh = NULL;
972         for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
973             if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
974         }
975         if(!tlfh) {
976             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
977             return 0;
978         }
979         if(!irccasecmp(slfh_name, "*")) {
980             if(!check_management_access(user->handle_info, tlfh, NULL)) {
981                 reply("HSMSG_ACCESS_DENIED");
982                 return 0;
983             }
984         } else {
985             for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
986                 if(!irccasecmp(slfh->fakehost, slfh_name)) break;
987             }
988             if(!slfh) {
989                 reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
990                 return 0;
991             }
992             if(!check_management_access(user->handle_info, tlfh, slfh)) {
993                 reply("HSMSG_ACCESS_DENIED");
994                 return 0;
995             }
996         }
997         struct hs_user *huser = hs_get_user(hi, 1);
998         struct hs_manager *manager;
999         if(slfh) {
1000             for(manager = huser->managements; manager; manager = manager->next) {
1001                 if(manager->type == 2 && manager->object == slfh) {
1002                     reply("HSMSG_MANAGER_ALREADY", hi->handle, slfh_name, tlfh_name);
1003                     return 0;
1004                 }
1005             }
1006         }
1007         for(manager = huser->managements; manager; manager = manager->next) {
1008             if(manager->type == 1 && manager->object == tlfh) {
1009                 reply("HSMSG_MANAGER_ALREADY", hi->handle, "*", tlfh_name);
1010                 return 0;
1011             }
1012         }
1013         if(slfh)
1014             hs_add_manager_secondlevel(slfh, huser);
1015         else
1016             hs_add_manager_toplevel(tlfh, huser);
1017         reply("HSMSG_MANAGER_ADDED", hi->handle, slfh_name, tlfh_name);
1018         return 1;
1019     }
1020     return 0;
1021 }
1022
1023 static MODCMD_FUNC(cmd_delmanager) {
1024     struct handle_info *hi;
1025     char *fakehost;
1026     if(!strchr(argv[1], '.')) {
1027         if (!(hi = modcmd_get_handle_info(user, argv[1])))
1028             return 0;
1029         fakehost = argv[2];
1030     } else {
1031         if (!(hi = modcmd_get_handle_info(user, argv[2])))
1032             return 0;
1033         fakehost = argv[1];
1034     }
1035     char *slfh_name = fakehost;
1036     char *tlfh_name = strchr(fakehost, '.');
1037     if(!tlfh_name) {
1038         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
1039         return 0;
1040     }
1041     tlfh_name++;
1042     if(strchr(tlfh_name, '.')) {
1043         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1044         return 0;
1045     }
1046     struct hs_toplevel *tlfh;
1047     struct hs_secondlevel *slfh = NULL;
1048     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
1049         if(!irccasecmp(tlfh->fakehost, tlfh_name)) break;
1050     }
1051     if(!tlfh) {
1052         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1053         return 0;
1054     }
1055     if(!irccasecmp(slfh_name, "*")) {
1056         if(!check_management_access(user->handle_info, tlfh, NULL)) {
1057             reply("HSMSG_ACCESS_DENIED");
1058             return 0;
1059         }
1060     } else {
1061         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
1062             if(!irccasecmp(slfh->fakehost, slfh_name)) break;
1063         }
1064         if(!slfh) {
1065             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1066             return 0;
1067         }
1068         if(!check_management_access(user->handle_info, tlfh, slfh)) {
1069             reply("HSMSG_ACCESS_DENIED");
1070             return 0;
1071         }
1072     }
1073     struct hs_user *huser = hs_get_user(hi, 0);
1074     struct hs_manager *manager;
1075     if(!huser) {
1076         reply("HSMSG_MANAGER_NOT", hi->handle, slfh_name, tlfh_name);
1077         return 0;
1078     }
1079     if(slfh) {
1080         for(manager = huser->managements; manager; manager = manager->next) {
1081             if(manager->type == 2 && manager->object == slfh) 
1082                 break;
1083         }
1084         if(!manager) {
1085             reply("HSMSG_MANAGER_NOT", hi->handle, slfh_name, tlfh_name);
1086             return 0;
1087         }
1088     } else {
1089         for(manager = huser->managements; manager; manager = manager->next) {
1090             if(manager->type == 1 && manager->object == tlfh) 
1091                 break;
1092         }
1093         if(!manager) {
1094             reply("HSMSG_MANAGER_NOT", hi->handle, "*", tlfh_name);
1095             return 0;
1096         }
1097     }
1098     hs_del_manager(manager, 1);
1099     reply("HSMSG_MANAGER_DELETED", hi->handle, slfh_name, tlfh_name);
1100     return 1;
1101 }
1102
1103 static MODCMD_FUNC(cmd_set) {
1104     struct handle_info *hi;
1105     struct hs_user *hs_user;
1106     struct hs_assignment *assignment;
1107     struct hs_toplevel *tlfh;
1108     struct hs_secondlevel *slfh;
1109     char *fakehost;
1110     
1111     if (!(hi = user->handle_info)) {
1112         reply("NSMSG_MUST_AUTH");
1113         return 0;
1114     }
1115     hs_user = hs_get_user(hi, 0);
1116     if(!hs_user)
1117         return 0; //nothing to do here
1118     if(!strcmp(argv[1], "*")) {
1119         hs_activate_assignment(hs_user, NULL);
1120         return 1;
1121     } else {
1122         fakehost = argv[1];
1123         char *slfh_name = fakehost;
1124         char *tlfh_name = strchr(fakehost, '.');
1125         if(tlfh_name) {
1126             *tlfh_name = '\0';
1127             tlfh_name++;
1128         }
1129         if(!tlfh_name || strchr(tlfh_name, '.')) {
1130             reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, (tlfh_name ? tlfh_name : ""));
1131             return 0;
1132         }
1133         for(assignment = hs_user->assignments; assignment; assignment = assignment->unext) {
1134             slfh = assignment->secondlevel;
1135             tlfh = slfh->toplevel;
1136             if(!irccasecmp(tlfh_name, tlfh->fakehost) && !irccasecmp(slfh_name, slfh->fakehost)) {
1137                 hs_activate_assignment(hs_user, assignment);
1138                 reply("HSMSG_FAKEHOST_SET_SUCCESS", slfh->fakehost, tlfh->fakehost);
1139                 return 1;
1140             }
1141         }
1142         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1143         return 0;
1144     }
1145 }
1146
1147 static MODCMD_FUNC(cmd_assign) {
1148     struct handle_info *hi;
1149     char *fakehost;
1150     if(!strchr(argv[1], '.')) {
1151         if (!(hi = modcmd_get_handle_info(user, argv[1])))
1152             return 0;
1153         fakehost = argv[2];
1154     } else {
1155         if (!(hi = modcmd_get_handle_info(user, argv[2])))
1156             return 0;
1157         fakehost = argv[1];
1158     }
1159     if (!user->handle_info) {
1160         reply("NSMSG_MUST_AUTH");
1161         return 0;
1162     }
1163     char *slfh_name = fakehost;
1164     char *tlfh_name = strchr(fakehost, '.');
1165     if(!tlfh_name) {
1166         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
1167         return 0;
1168     }
1169     tlfh_name++;
1170     if(strchr(tlfh_name, '.')) {
1171         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1172         return 0;
1173     }
1174     struct hs_toplevel *tlfh;
1175     struct hs_secondlevel *slfh;
1176     struct hs_user *hs_user = hs_get_user(hi, 1);
1177     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
1178         if(!irccasecmp(tlfh_name, tlfh->fakehost)) {
1179             for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
1180                 if(!irccasecmp(slfh_name, slfh->fakehost)) {
1181                     if(!check_management_access(user->handle_info, tlfh, slfh)) {
1182                         reply("HSMSG_ACCESS_DENIED");
1183                         return 0;
1184                     }
1185                     if(hs_get_assignment(slfh, hs_user)) {
1186                         reply("HSMSG_FAKEHOST_ASSIGNED", slfh_name, tlfh_name);
1187                         return 0;
1188                     }
1189                     hs_add_assignment(slfh, hs_user, HS_ASSIGNMENTSTATE_AUTO);
1190                     reply("HSMSG_FAKEHOST_ASSIGN_SUCCESS", slfh_name, tlfh_name);
1191                     return 1;
1192                 }
1193             }
1194         }
1195     }
1196     reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1197     return 0;
1198 }
1199
1200 static MODCMD_FUNC(cmd_unassign) {
1201     struct handle_info *hi;
1202     char *fakehost;
1203     if(!strchr(argv[1], '.')) {
1204         if (!(hi = modcmd_get_handle_info(user, argv[1])))
1205             return 0;
1206         fakehost = argv[2];
1207     } else {
1208         if (!(hi = modcmd_get_handle_info(user, argv[2])))
1209             return 0;
1210         fakehost = argv[1];
1211     }
1212     if (!user->handle_info) {
1213         reply("NSMSG_MUST_AUTH");
1214         return 0;
1215     }
1216     char *slfh_name = fakehost;
1217     char *tlfh_name = strchr(fakehost, '.');
1218     if(!tlfh_name) {
1219         reply("HSMSG_TOPLEVEL_INVALID", slfh_name);
1220         return 0;
1221     }
1222     tlfh_name++;
1223     if(strchr(tlfh_name, '.')) {
1224         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1225         return 0;
1226     }
1227     struct hs_assignment *assignment;
1228     struct hs_user *hs_user = hs_get_user(hi, 0);
1229     if(!hs_user) {
1230         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1231         return 0;
1232     }
1233     for(assignment = hs_user->assignments; assignment; assignment = assignment->unext) {
1234         if(!irccasecmp(slfh_name, assignment->secondlevel->fakehost)) {
1235             if(!irccasecmp(tlfh_name, assignment->secondlevel->toplevel->fakehost)) {
1236                 if(!check_management_access(user->handle_info, assignment->secondlevel->toplevel, assignment->secondlevel)) {
1237                     reply("HSMSG_ACCESS_DENIED");
1238                     return 0;
1239                 }
1240                 hs_del_assignment(assignment, 1);
1241                 reply("HSMSG_FAKEHOST_UNASSIGN_SUCCESS", slfh_name, tlfh_name);
1242                 return 1;
1243             }
1244         }
1245     }
1246     reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
1247     return 0;
1248 }
1249
1250 static void hostserv_conf_read(void) {
1251     dict_t conf_node;
1252     const char *str;
1253
1254     str = "modules/hostserv";
1255     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
1256         log_module(HS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
1257         return;
1258     }
1259
1260     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
1261     if(hostserv_conf.nick && strcmp(hostserv_conf.nick, str)) {
1262         //nick changed
1263     }
1264     hostserv_conf.nick = str;
1265     
1266     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
1267     hostserv_conf.modes = (str ? str : NULL);
1268     
1269     str = database_get_data(conf_node, "toplevel_access", RECDB_QSTRING);
1270     unsigned int toplevel_access = atoi(str);
1271     hostserv_conf.toplevel_access = (toplevel_access ? toplevel_access : 600);
1272     
1273     str = database_get_data(conf_node, "fallback_other_assignment", RECDB_QSTRING);
1274     hostserv_conf.fallback_other_assignment = (atoi(str) ? 1 : 0);
1275     
1276     str = database_get_data(conf_node, "manager_can_del_toplevel", RECDB_QSTRING);
1277     hostserv_conf.manager_can_del_toplevel = (atoi(str) ? 1 : 0);
1278     
1279     str = database_get_data(conf_node, "manager_can_del_secondlevel", RECDB_QSTRING);
1280     hostserv_conf.manager_can_del_secondlevel = (atoi(str) ? 1 : 0);
1281     
1282     str = database_get_data(conf_node, "manager_can_rename_toplevel", RECDB_QSTRING);
1283     hostserv_conf.manager_can_rename_toplevel = (atoi(str) ? 1 : 0);
1284     
1285     str = database_get_data(conf_node, "manager_can_rename_secondlevel", RECDB_QSTRING);
1286     hostserv_conf.manager_can_rename_secondlevel = (atoi(str) ? 1 : 0);
1287     
1288     /*str = database_get_data(conf_node, "description", RECDB_QSTRING);
1289     hostserv_conf.description = (str ? str : NULL);*/
1290 }
1291
1292 static int hostserv_saxdb_read_secondlevel(const char *name, void *data, UNUSED_ARG(void *extra));
1293 static int hostserv_saxdb_read_assignments(const char *name, void *data, UNUSED_ARG(void *extra));
1294
1295 static int hostserv_saxdb_read_toplevel(const char *name, void *data, UNUSED_ARG(void *extra)) {
1296     struct record_data *rd = data;
1297     struct hs_toplevel *tlfh;
1298     struct hs_manager *managerTL;
1299     struct hs_user *user;
1300     struct dict *object;
1301
1302      if (rd->type == RECDB_OBJECT) {
1303         dict_t db = GET_RECORD_OBJECT(rd);
1304         dict_iterator_t it;
1305         
1306         tlfh = hs_add_toplevel(name);
1307         
1308         if ((object = database_get_data(db, KEY_MANAGERS, RECDB_OBJECT))) {
1309             for (it = dict_first(object); it; it = iter_next(it)) {
1310                 user = hs_get_user(get_handle_info(iter_key(it)), 1);
1311                 //rd = iter_data(it);
1312                 /* nothing in here, yet */
1313                 managerTL = hs_add_manager_toplevel(tlfh, user);
1314                 if (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING))
1315                     managerTL->active = 1;
1316                 else
1317                     managerTL->active = 0;
1318             }
1319         }
1320         
1321         if ((object = database_get_data(db, KEY_SECONDLEVEL, RECDB_OBJECT)))
1322             dict_foreach(object, hostserv_saxdb_read_secondlevel, tlfh);
1323     }
1324     return 0;
1325 }
1326
1327 static int hostserv_saxdb_read_secondlevel(const char *name, void *data, UNUSED_ARG(void *extra)) {
1328     struct record_data *rd = data;
1329     struct hs_toplevel *tlfh = extra;
1330     struct hs_secondlevel *slfh;
1331     struct hs_manager *managerSL;
1332     struct hs_user *user;
1333     struct dict *object;
1334
1335     if (rd->type == RECDB_OBJECT) {
1336         dict_t db = GET_RECORD_OBJECT(rd);
1337         dict_iterator_t it;
1338         
1339         slfh = hs_add_secondlevel(tlfh, name);
1340         
1341         if ((object = database_get_data(db, KEY_MANAGERS, RECDB_OBJECT))) {
1342             for (it = dict_first(object); it; it = iter_next(it)) {
1343                 user = hs_get_user(get_handle_info(iter_key(it)), 1);
1344                 //rd = iter_data(it);
1345                 /* nothing in here, yet */
1346                 managerSL = hs_add_manager_secondlevel(slfh, user);
1347                 if (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING))
1348                     managerSL->active = 1;
1349                 else
1350                     managerSL->active = 0;
1351             }
1352         }
1353         
1354         if ((object = database_get_data(db, KEY_ASSIGNMENTS, RECDB_OBJECT)))
1355             dict_foreach(object, hostserv_saxdb_read_assignments, slfh);
1356     }
1357     return 0;
1358 }
1359
1360 static int hostserv_saxdb_read_assignments(const char *name, void *data, UNUSED_ARG(void *extra)) {
1361     struct record_data *rd = data;
1362     struct hs_secondlevel *slfh = extra;
1363     struct hs_user *user;
1364     int active;
1365     
1366     if (rd->type == RECDB_OBJECT) {
1367         dict_t db = GET_RECORD_OBJECT(rd);
1368         
1369         user = hs_get_user(get_handle_info(name), 1);
1370         active = (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING) ? HS_ASSIGNMENTSTATE_ON : HS_ASSIGNMENTSTATE_OFF);
1371         
1372         hs_add_assignment(slfh, user, active);
1373     }
1374     
1375     return 0;
1376 }
1377
1378 static int
1379 hostserv_saxdb_read(struct dict *db)
1380 {
1381     struct dict *object;
1382
1383     if ((object = database_get_data(db, KEY_TOPLEVEL, RECDB_OBJECT)))
1384         dict_foreach(object, hostserv_saxdb_read_toplevel, NULL);
1385
1386     return 1;
1387 }
1388
1389 static int
1390 hostserv_saxdb_write(struct saxdb_context *ctx)
1391 {
1392     struct hs_toplevel *tlfh;
1393     struct hs_secondlevel *slfh;
1394     struct hs_assignment *assng;
1395     struct hs_manager *manager;
1396
1397     saxdb_start_record(ctx, KEY_TOPLEVEL, 1);
1398     for(tlfh = toplevels; tlfh; tlfh = tlfh->next) {
1399         saxdb_start_record(ctx, tlfh->fakehost, 1);
1400         
1401         saxdb_start_record(ctx, KEY_MANAGERS, 1);
1402         for(manager = tlfh->managers; manager; manager = manager->next) {
1403             saxdb_start_record(ctx, manager->user->hi->handle, 0);
1404             //additional manager information?
1405             if(manager->active)
1406                 saxdb_write_int(ctx, KEY_ACTIVE, manager->active);
1407             saxdb_end_record(ctx);
1408         }
1409         saxdb_end_record(ctx);
1410         
1411         saxdb_start_record(ctx, KEY_SECONDLEVEL, 1);
1412         for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) {
1413             saxdb_start_record(ctx, slfh->fakehost, 1);
1414             
1415             saxdb_start_record(ctx, KEY_MANAGERS, 1);
1416             for(manager = slfh->managers; manager; manager = manager->next) {
1417                 saxdb_start_record(ctx, manager->user->hi->handle, 0);
1418                 //additional manager information?
1419                 if(manager->active)
1420                     saxdb_write_int(ctx, KEY_ACTIVE, manager->active);
1421                 saxdb_end_record(ctx);
1422             }
1423             saxdb_end_record(ctx);
1424             
1425             saxdb_start_record(ctx, KEY_ASSIGNMENTS, 1);
1426             for(assng = slfh->assignments; assng; assng = assng->next) {
1427                 saxdb_start_record(ctx, assng->user->hi->handle, 0);
1428                 //additional assignment information?
1429                 if(assng->active)
1430                     saxdb_write_int(ctx, KEY_ACTIVE, assng->active);
1431                 saxdb_end_record(ctx);
1432             }
1433             saxdb_end_record(ctx);
1434             
1435             saxdb_end_record(ctx);
1436         }
1437         saxdb_end_record(ctx);
1438         
1439         saxdb_end_record(ctx);
1440     }
1441     saxdb_end_record(ctx);
1442     
1443     return 0;
1444 }
1445
1446
1447 static void hostserv_db_cleanup(void) {
1448     hs_free_all();
1449 }
1450
1451 int hostserv_init() {
1452     HS_LOG = log_register_type("HostServ", "file:hostserv.log");
1453     
1454     const char *nick, *modes;
1455     if((nick = conf_get_data("modules/hostserv/nick", RECDB_QSTRING))) {
1456         modes = conf_get_data("modules/hostserv/modes", RECDB_QSTRING);
1457         hostserv = AddLocalUser(nick, nick, NULL, "Host Service", modes);
1458         hostserv_service = service_register(hostserv);
1459         hostserv_service->trigger = '*';
1460     }
1461         
1462     conf_register_reload(hostserv_conf_read);
1463     reg_exit_func(hostserv_db_cleanup);
1464     saxdb_register("HostServ", hostserv_saxdb_read, hostserv_saxdb_write);
1465     hostserv_module = module_register("HostServ", HS_LOG, "mod-hostserv.help", NULL);
1466     modcmd_register(hostserv_module, "view", cmd_view, 0, MODCMD_REQUIRE_AUTHED, NULL);
1467     modcmd_register(hostserv_module, "addmanager", cmd_addmanager, 3, MODCMD_REQUIRE_AUTHED, NULL);
1468     modcmd_register(hostserv_module, "delmanager", cmd_delmanager, 3, MODCMD_REQUIRE_AUTHED, NULL);
1469     modcmd_register(hostserv_module, "set", cmd_set, 2, MODCMD_REQUIRE_AUTHED, NULL);
1470     modcmd_register(hostserv_module, "assign", cmd_assign, 3, MODCMD_REQUIRE_AUTHED, NULL);
1471     modcmd_register(hostserv_module, "unassign", cmd_unassign, 3, MODCMD_REQUIRE_AUTHED, NULL);
1472     modcmd_register(hostserv_module, "addhost", cmd_addhost, 2, MODCMD_REQUIRE_AUTHED, NULL);
1473     modcmd_register(hostserv_module, "delhost", cmd_delhost, 2, MODCMD_REQUIRE_AUTHED, NULL);
1474     modcmd_register(hostserv_module, "renamehost", cmd_renamehost, 3, MODCMD_REQUIRE_AUTHED, NULL);
1475     message_register_table(msgtab);
1476     return 1;
1477 }
1478
1479 int hostserv_finalize(void) {
1480     dict_t conf_node;
1481     const char *str;
1482
1483     str = "modules/hostserv";
1484     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
1485         log_module(HS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
1486         return 0;
1487     }
1488
1489     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
1490     if (str) hostserv_conf.nick = str;
1491     
1492     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
1493     if (str) hostserv_conf.modes = str; 
1494     return 1;
1495 }