| 1 |
/* |
| 2 |
* Copyright (c) 2005 Robin Burchell, et al. |
| 3 |
* Copyright (c) 2010 William Pitcock <[email protected]>. |
| 4 |
* Rights to this code are as documented in doc/LICENSE. |
| 5 |
* |
| 6 |
* List chanserv-controlled channels. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
|
| 11 |
DECLARE_MODULE_V1 |
| 12 |
( |
| 13 |
"chanserv/list", false, _modinit, _moddeinit, |
| 14 |
PACKAGE_STRING, |
| 15 |
VENDOR_STRING |
| 16 |
); |
| 17 |
|
| 18 |
static void cs_cmd_list(sourceinfo_t *si, int parc, char *parv[]); |
| 19 |
|
| 20 |
command_t cs_list = { "LIST", N_("Lists channels registered matching a given pattern."), PRIV_CHAN_AUSPEX, 10, cs_cmd_list, { .path = "cservice/list" } }; |
| 21 |
|
| 22 |
void _modinit(module_t *m) |
| 23 |
{ |
| 24 |
service_named_bind_command("chanserv", &cs_list); |
| 25 |
} |
| 26 |
|
| 27 |
void _moddeinit(module_unload_intent_t intent) |
| 28 |
{ |
| 29 |
service_named_unbind_command("chanserv", &cs_list); |
| 30 |
} |
| 31 |
|
| 32 |
typedef enum { |
| 33 |
OPT_BOOL, |
| 34 |
OPT_INT, |
| 35 |
OPT_STRING, |
| 36 |
OPT_FLAG, |
| 37 |
OPT_AGE, |
| 38 |
} list_opttype_t; |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
char *option; |
| 42 |
list_opttype_t opttype; |
| 43 |
union { |
| 44 |
bool *boolval; |
| 45 |
int *intval; |
| 46 |
char **strval; |
| 47 |
unsigned int *flagval; |
| 48 |
time_t *ageval; |
| 49 |
} optval; |
| 50 |
unsigned int flag; |
| 51 |
} list_option_t; |
| 52 |
|
| 53 |
static time_t parse_age(char *s) |
| 54 |
{ |
| 55 |
time_t duration; |
| 56 |
|
| 57 |
duration = (atol(s) * 60); |
| 58 |
while (isdigit((unsigned char)*s)) |
| 59 |
s++; |
| 60 |
|
| 61 |
if (*s == 'h' || *s == 'H') |
| 62 |
duration *= 60; |
| 63 |
else if (*s == 'd' || *s == 'D') |
| 64 |
duration *= 1440; |
| 65 |
else if (*s == 'w' || *s == 'W') |
| 66 |
duration *= 10080; |
| 67 |
else if (*s == '\0') |
| 68 |
; |
| 69 |
else |
| 70 |
duration = 0; |
| 71 |
|
| 72 |
return duration; |
| 73 |
} |
| 74 |
|
| 75 |
static void process_parvarray(list_option_t *opts, size_t optsize, int parc, char *parv[]) |
| 76 |
{ |
| 77 |
int i; |
| 78 |
size_t j; |
| 79 |
|
| 80 |
for (i = 0; i < parc; i++) |
| 81 |
{ |
| 82 |
for (j = 0; j < optsize; j++) |
| 83 |
{ |
| 84 |
if (!strcasecmp(opts[j].option, parv[i])) |
| 85 |
{ |
| 86 |
switch(opts[j].opttype) |
| 87 |
{ |
| 88 |
case OPT_BOOL: |
| 89 |
*opts[j].optval.boolval = true; |
| 90 |
break; |
| 91 |
case OPT_INT: |
| 92 |
if (i + 1 < parc) |
| 93 |
{ |
| 94 |
*opts[j].optval.intval = atoi(parv[i + 1]); |
| 95 |
i++; |
| 96 |
} |
| 97 |
break; |
| 98 |
case OPT_STRING: |
| 99 |
if (i + 1 < parc) |
| 100 |
{ |
| 101 |
*opts[j].optval.strval = parv[i + 1]; |
| 102 |
i++; |
| 103 |
} |
| 104 |
break; |
| 105 |
case OPT_FLAG: |
| 106 |
*opts[j].optval.flagval |= opts[j].flag; |
| 107 |
break; |
| 108 |
case OPT_AGE: |
| 109 |
if (i + 1 < parc) |
| 110 |
{ |
| 111 |
*opts[j].optval.ageval = parse_age(parv[i + 1]); |
| 112 |
i++; |
| 113 |
} |
| 114 |
break; |
| 115 |
default: |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
static void build_criteriastr(char *buf, int parc, char *parv[]) |
| 124 |
{ |
| 125 |
int i; |
| 126 |
|
| 127 |
return_if_fail(buf != NULL); |
| 128 |
|
| 129 |
*buf = 0; |
| 130 |
for (i = 0; i < parc; i++) |
| 131 |
{ |
| 132 |
mowgli_strlcat(buf, parv[i], BUFSIZE); |
| 133 |
mowgli_strlcat(buf, " ", BUFSIZE); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static void cs_cmd_list(sourceinfo_t *si, int parc, char *parv[]) |
| 138 |
{ |
| 139 |
mychan_t *mc; |
| 140 |
metadata_t *md, *mdclosed; |
| 141 |
char *chanpattern = NULL, *markpattern = NULL, *closedpattern = NULL; |
| 142 |
char buf[BUFSIZE]; |
| 143 |
char criteriastr[BUFSIZE]; |
| 144 |
unsigned int matches = 0; |
| 145 |
unsigned int flagset = 0; |
| 146 |
int aclsize = 0; |
| 147 |
time_t age = 0, lastused = 0; |
| 148 |
bool closed = false, marked = false, markmatch, closedmatch; |
| 149 |
mowgli_patricia_iteration_state_t state; |
| 150 |
list_option_t optstable[] = { |
| 151 |
{"pattern", OPT_STRING, {.strval = &chanpattern}, 0}, |
| 152 |
{"mark-reason", OPT_STRING, {.strval = &markpattern}, 0}, |
| 153 |
{"close-reason", OPT_STRING, {.strval = &closedpattern}, 0}, |
| 154 |
{"noexpire", OPT_FLAG, {.flagval = &flagset}, MC_HOLD}, |
| 155 |
{"held", OPT_FLAG, {.flagval = &flagset}, MC_HOLD}, |
| 156 |
{"hold", OPT_FLAG, {.flagval = &flagset}, MC_HOLD}, |
| 157 |
{"noop", OPT_FLAG, {.flagval = &flagset}, MC_NOOP}, |
| 158 |
{"limitflags", OPT_FLAG, {.flagval = &flagset}, MC_LIMITFLAGS}, |
| 159 |
{"secure", OPT_FLAG, {.flagval = &flagset}, MC_SECURE}, |
| 160 |
{"nosync", OPT_FLAG, {.flagval = &flagset}, MC_NOSYNC}, |
| 161 |
{"verbose", OPT_FLAG, {.flagval = &flagset}, MC_VERBOSE}, |
| 162 |
{"restricted", OPT_FLAG, {.flagval = &flagset}, MC_RESTRICTED}, |
| 163 |
{"keeptopic", OPT_FLAG, {.flagval = &flagset}, MC_KEEPTOPIC}, |
| 164 |
{"verbose-ops", OPT_FLAG, {.flagval = &flagset}, MC_VERBOSE_OPS}, |
| 165 |
{"topiclock", OPT_FLAG, {.flagval = &flagset}, MC_TOPICLOCK}, |
| 166 |
{"guard", OPT_FLAG, {.flagval = &flagset}, MC_GUARD}, |
| 167 |
{"private", OPT_FLAG, {.flagval = &flagset}, MC_PRIVATE}, |
| 168 |
{"closed", OPT_BOOL, {.boolval = &closed}, 0}, |
| 169 |
{"marked", OPT_BOOL, {.boolval = &marked}, 0}, |
| 170 |
{"aclsize", OPT_INT, {.intval = &aclsize}, 0}, |
| 171 |
{"registered", OPT_AGE, {.ageval = &age}, 0}, |
| 172 |
{"lastused", OPT_AGE, {.ageval = &lastused}, 0}, |
| 173 |
}; |
| 174 |
|
| 175 |
process_parvarray(optstable, ARRAY_SIZE(optstable), parc, parv); |
| 176 |
build_criteriastr(criteriastr, parc, parv); |
| 177 |
|
| 178 |
command_success_nodata(si, _("Channels matching \2%s\2:"), criteriastr); |
| 179 |
|
| 180 |
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist) |
| 181 |
{ |
| 182 |
if (chanpattern != NULL && match(chanpattern, mc->name)) |
| 183 |
continue; |
| 184 |
|
| 185 |
if (markpattern) |
| 186 |
{ |
| 187 |
markmatch = false; |
| 188 |
md = metadata_find(mc, "private:mark:reason"); |
| 189 |
if (md != NULL && !match(markpattern, md->value)) |
| 190 |
markmatch = true; |
| 191 |
|
| 192 |
if (!markmatch) |
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
if (closedpattern) |
| 197 |
{ |
| 198 |
closedmatch = false; |
| 199 |
mdclosed = metadata_find(mc, "private:close:reason"); |
| 200 |
if (mdclosed != NULL && !match(closedpattern, mdclosed->value)) |
| 201 |
closedmatch = true; |
| 202 |
|
| 203 |
if (!closedmatch) |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
if (marked && !metadata_find(mc, "private:mark:setter")) |
| 208 |
continue; |
| 209 |
|
| 210 |
if (closed && !metadata_find(mc, "private:close:closer")) |
| 211 |
continue; |
| 212 |
|
| 213 |
if (flagset && (mc->flags & flagset) != flagset) |
| 214 |
continue; |
| 215 |
|
| 216 |
if (aclsize && MOWGLI_LIST_LENGTH(&mc->chanacs) < (unsigned int)aclsize) |
| 217 |
continue; |
| 218 |
|
| 219 |
if (age && (CURRTIME - mc->registered) < age) |
| 220 |
continue; |
| 221 |
|
| 222 |
if (lastused && (CURRTIME - mc->used) < lastused) |
| 223 |
continue; |
| 224 |
|
| 225 |
/* in the future we could add a LIMIT parameter */ |
| 226 |
*buf = '\0'; |
| 227 |
|
| 228 |
if (metadata_find(mc, "private:mark:setter")) { |
| 229 |
mowgli_strlcat(buf, "\2[marked]\2", BUFSIZE); |
| 230 |
} |
| 231 |
if (metadata_find(mc, "private:close:closer")) { |
| 232 |
if (*buf) |
| 233 |
mowgli_strlcat(buf, " ", BUFSIZE); |
| 234 |
|
| 235 |
mowgli_strlcat(buf, "\2[closed]\2", BUFSIZE); |
| 236 |
} |
| 237 |
if (mc->flags & MC_HOLD) { |
| 238 |
if (*buf) |
| 239 |
mowgli_strlcat(buf, " ", BUFSIZE); |
| 240 |
|
| 241 |
mowgli_strlcat(buf, "\2[held]\2", BUFSIZE); |
| 242 |
} |
| 243 |
|
| 244 |
command_success_nodata(si, "- %s (%s) %s", mc->name, mychan_founder_names(mc), buf); |
| 245 |
matches++; |
| 246 |
} |
| 247 |
|
| 248 |
logcommand(si, CMDLOG_ADMIN, "LIST: \2%s\2 (\2%d\2 matches)", criteriastr, matches); |
| 249 |
if (matches == 0) |
| 250 |
command_success_nodata(si, _("No channel matched criteria \2%s\2"), criteriastr); |
| 251 |
else |
| 252 |
command_success_nodata(si, ngettext(N_("\2%d\2 match for criteria \2%s\2"), N_("\2%d\2 matches for criteria \2%s\2"), matches), matches, criteriastr); |
| 253 |
} |
| 254 |
|
| 255 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 256 |
* vim:ts=8 |
| 257 |
* vim:sw=8 |
| 258 |
* vim:noexpandtab |
| 259 |
*/ |