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