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