]>
Commit | Line | Data |
---|---|---|
be2823bc | 1 | #include "../control/control.h" |
2d4ba67d | 2 | #include "../lib/irc_string.h" |
be2823bc CP |
3 | #include "trusts.h" |
4 | ||
5 | int trusts_migration_start(TrustDBMigrationCallback, void *); | |
6 | void trusts_migration_stop(void); | |
7 | static void registercommands(void); | |
8 | static void deregistercommands(void); | |
9 | ||
be2823bc CP |
10 | static void migrate_status(int errcode, void *tag) { |
11 | long sender = (long)tag; | |
12 | nick *np = getnickbynumeric(sender); | |
13 | ||
14 | if(!np) | |
15 | return; | |
16 | ||
17 | if(!errcode) { | |
45e8ce62 CP |
18 | controlreply(np, "Migration complete."); |
19 | ||
20 | /* stops flush destroying the database */ | |
21 | trusts_freeall(); | |
22 | ||
23 | trusts_unload(); | |
24 | controlreply(np, "All functionality disabled, database unloaded -- please reload the module."); | |
be2823bc | 25 | } else { |
bf5b66e5 | 26 | controlreply(np, "Error %d occured during migration, commands reregistered.", errcode); |
be2823bc CP |
27 | registercommands(); |
28 | } | |
29 | } | |
30 | ||
31 | static int trusts_cmdmigrate(void *source, int cargc, char **cargv) { | |
32 | nick *sender = source; | |
33 | int ret; | |
34 | ||
35 | /* iffy but temporary */ | |
36 | ret = trusts_migration_start(migrate_status, (void *)(sender->numeric)); | |
37 | if(!ret) { | |
bf5b66e5 | 38 | controlreply(sender, "Migration started, commands deregistered."); |
be2823bc CP |
39 | deregistercommands(); |
40 | } else { | |
41 | controlreply(sender, "Error %d starting migration.", ret); | |
42 | } | |
43 | ||
44 | return CMD_OK; | |
45 | } | |
46 | ||
2d4ba67d CP |
47 | static trustgroup *tg_strtotg(char *name) { |
48 | unsigned long id; | |
49 | trustgroup *tg; | |
50 | ||
51 | /* legacy format */ | |
52 | if(name[0] == '#') { | |
53 | id = strtoul(&name[1], NULL, 10); | |
54 | if(id == ULONG_MAX) | |
55 | return NULL; | |
56 | ||
57 | for(tg=tglist;tg;tg=tg->next) | |
58 | if(tg->id == id) | |
59 | return tg; | |
60 | } | |
61 | ||
62 | for(tg=tglist;tg;tg=tg->next) | |
63 | if(tg->name && !match(name, tg->name->content)) | |
64 | return tg; | |
65 | ||
66 | id = strtoul(name, NULL, 10); | |
67 | if(id == ULONG_MAX) | |
68 | return NULL; | |
69 | ||
70 | /* legacy format */ | |
71 | for(tg=tglist;tg;tg=tg->next) | |
72 | if(tg->id == id) | |
73 | return tg; | |
74 | ||
75 | return NULL; | |
76 | } | |
77 | ||
78 | static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) { | |
79 | nick *sender = source; | |
80 | trustgroup *tg; | |
81 | trusthost *th; | |
82 | time_t t; | |
83 | ||
84 | if(cargc < 1) | |
85 | return CMD_USAGE; | |
86 | ||
87 | tg = tg_strtotg(cargv[0]); | |
88 | if(!tg) { | |
89 | controlreply(sender, "Couldn't find a trustgroup with that id."); | |
90 | return CMD_ERROR; | |
91 | } | |
92 | ||
93 | t = time(NULL); | |
94 | ||
95 | controlreply(sender, "Name: : %s", tg->name?tg->name->content:"(unknown)"); | |
96 | controlreply(sender, "Trusted for : %d", tg->trustedfor); | |
1bbe1ac3 | 97 | controlreply(sender, "Currently using : %d", tg->count); |
2d4ba67d CP |
98 | controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not "); |
99 | controlreply(sender, "Contact: : %s", tg->contact?tg->contact->content:"(unknown)"); | |
100 | controlreply(sender, "Expires in : %s", (tg->expires>t)?longtoduration(tg->expires - t, 2):"(in the past)"); | |
101 | controlreply(sender, "Last changed by : %s", tg->createdby?tg->createdby->content:"(unknown)"); | |
102 | controlreply(sender, "Comment: : %s", tg->comment?tg->comment->content:"(unknown)"); | |
103 | controlreply(sender, "ID: : %u", tg->id); | |
4be1aaf2 CP |
104 | controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":trusts_timetostr(tg->lastseen)); |
105 | controlreply(sender, "Max usage : %d", tg->maxusage); | |
2d4ba67d CP |
106 | controlreply(sender, "Last max reset : %s", tg->lastmaxuserreset?trusts_timetostr(tg->lastmaxuserreset):"(never)"); |
107 | ||
4be1aaf2 | 108 | controlreply(sender, "Host Current Max Last seen"); |
2d4ba67d CP |
109 | |
110 | for(th=tg->hosts;th;th=th->next) | |
4be1aaf2 | 111 | controlreply(sender, " %-20s %-10d %-10d %s", trusts_cidr2str(th->ip, th->mask), th->count, th->maxusage, (th->count>0)?"(now)":trusts_timetostr(th->lastseen)); |
2d4ba67d CP |
112 | |
113 | controlreply(sender, "End of list."); | |
114 | ||
115 | return CMD_OK; | |
116 | } | |
117 | ||
be2823bc CP |
118 | static void dbloaded(int hooknum, void *arg) { |
119 | registercommands(); | |
120 | } | |
121 | ||
122 | void _init(void) { | |
123 | registerhook(HOOK_TRUSTS_DB_LOADED, dbloaded); | |
124 | ||
125 | if(trustsdbloaded) | |
126 | registercommands(); | |
127 | } | |
128 | ||
129 | void _fini(void) { | |
130 | deregisterhook(HOOK_TRUSTS_DB_LOADED, dbloaded); | |
131 | deregistercommands(); | |
132 | ||
133 | trusts_migration_stop(); | |
134 | } | |
135 | ||
136 | static void registercommands(void) { | |
62ef5288 | 137 | registercontrolhelpcmd("trustmigrate", NO_DEVELOPER, 0, trusts_cmdmigrate, "Usage: trustmigrate\nCopies trust data from O and reloads the database."); |
2d4ba67d | 138 | registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|id>\nShows trust data for the specified trust group."); |
be2823bc CP |
139 | } |
140 | ||
141 | static void deregistercommands(void) { | |
bf5b66e5 | 142 | if(!trustsdbloaded) |
be2823bc CP |
143 | return; |
144 | ||
145 | deregistercontrolcmd("trustmigrate", trusts_cmdmigrate); | |
2d4ba67d | 146 | deregistercontrolcmd("trustlist", trusts_cmdtrustlist); |
be2823bc | 147 | } |