Rename ignore code to filter

master
C. McEnroe 2021-01-16 13:30:59 -05:00
parent 6160b3f18c
commit 5a490945ea
7 changed files with 68 additions and 65 deletions

View File

@ -13,8 +13,8 @@ OBJS += command.o
OBJS += complete.o OBJS += complete.o
OBJS += config.o OBJS += config.o
OBJS += edit.o OBJS += edit.o
OBJS += filter.o
OBJS += handle.o OBJS += handle.o
OBJS += ignore.o
OBJS += irc.o OBJS += irc.o
OBJS += log.o OBJS += log.o
OBJS += ui.o OBJS += ui.o

View File

@ -1,4 +1,4 @@
.Dd November 11, 2020 .Dd January 16, 2021
.Dt README 7 .Dt README 7
.Os "Causal Agency" .Os "Causal Agency"
.\" To view this file, run: man ./README.7 .\" To view this file, run: man ./README.7
@ -167,7 +167,7 @@ line editing
tab complete tab complete
.It Pa url.c .It Pa url.c
URL detection URL detection
.It Pa ignore.c .It Pa filter.c
message filtering message filtering
.It Pa log.c .It Pa log.c
chat logging chat logging

2
chat.c
View File

@ -243,7 +243,7 @@ int main(int argc, char *argv[]) {
break; case 'e': sasl = true; break; case 'e': sasl = true;
break; case 'g': genCert(optarg); break; case 'g': genCert(optarg);
break; case 'h': host = optarg; break; case 'h': host = optarg;
break; case 'i': ignoreAdd(optarg); break; case 'i': filterAdd(Ice, optarg);
break; case 'j': self.join = optarg; break; case 'j': self.join = optarg;
break; case 'k': priv = optarg; break; case 'k': priv = optarg;
break; case 'l': logEnable = true; break; case 'l': logEnable = true;

15
chat.h
View File

@ -361,17 +361,18 @@ void urlCopyMatch(uint id, const char *str);
int urlSave(FILE *file); int urlSave(FILE *file);
void urlLoad(FILE *file, size_t version); void urlLoad(FILE *file, size_t version);
enum { IgnoreCap = 64 }; enum { FilterCap = 64 };
extern struct Ignore { extern struct Filter {
enum Heat heat;
char *mask; char *mask;
char *cmd; char *cmd;
char *chan; char *chan;
char *mesg; char *mesg;
} ignores[IgnoreCap]; } filters[FilterCap];
struct Ignore ignoreParse(char *pattern); struct Filter filterParse(enum Heat heat, char *pattern);
struct Ignore ignoreAdd(const char *pattern); struct Filter filterAdd(enum Heat heat, const char *pattern);
bool ignoreRemove(struct Ignore ignore); bool filterRemove(struct Filter filter);
enum Heat ignoreCheck(enum Heat heat, uint id, const struct Message *msg); enum Heat filterCheck(enum Heat heat, uint id, const struct Message *msg);
extern bool logEnable; extern bool logEnable;
void logFormat(uint id, const time_t *time, const char *format, ...) void logFormat(uint id, const time_t *time, const char *format, ...)

View File

@ -388,19 +388,20 @@ static void commandCopy(uint id, char *params) {
static void commandIgnore(uint id, char *params) { static void commandIgnore(uint id, char *params) {
if (params) { if (params) {
struct Ignore ignore = ignoreAdd(params); struct Filter filter = filterAdd(Ice, params);
uiFormat( uiFormat(
id, Cold, NULL, "Ignoring \3%02d%s %s %s %s", id, Cold, NULL, "Ignoring \3%02d%s %s %s %s",
Brown, ignore.mask, Brown, filter.mask,
(ignore.cmd ?: ""), (ignore.chan ?: ""), (ignore.mesg ?: "") (filter.cmd ?: ""), (filter.chan ?: ""), (filter.mesg ?: "")
); );
} else { } else {
for (size_t i = 0; i < IgnoreCap && ignores[i].mask; ++i) { for (size_t i = 0; i < FilterCap && filters[i].mask; ++i) {
if (filters[i].heat != Ice) continue;
uiFormat( uiFormat(
Network, Warm, NULL, "Ignoring \3%02d%s %s %s %s", Network, Warm, NULL, "Ignoring \3%02d%s %s %s %s",
Brown, ignores[i].mask, Brown, filters[i].mask,
(ignores[i].cmd ?: ""), (ignores[i].chan ?: ""), (filters[i].cmd ?: ""), (filters[i].chan ?: ""),
(ignores[i].mesg ?: "") (filters[i].mesg ?: "")
); );
} }
} }
@ -408,12 +409,12 @@ static void commandIgnore(uint id, char *params) {
static void commandUnignore(uint id, char *params) { static void commandUnignore(uint id, char *params) {
if (!params) return; if (!params) return;
struct Ignore ignore = ignoreParse(params); struct Filter filter = filterParse(Ice, params);
bool found = ignoreRemove(ignore); bool found = filterRemove(filter);
uiFormat( uiFormat(
id, Cold, NULL, "%s ignoring \3%02d%s %s %s %s", id, Cold, NULL, "%s ignoring \3%02d%s %s %s %s",
(found ? "No longer" : "Not"), Brown, ignore.mask, (found ? "No longer" : "Not"), Brown, filter.mask,
(ignore.cmd ?: ""), (ignore.chan ?: ""), (ignore.mesg ?: "") (filter.cmd ?: ""), (filter.chan ?: ""), (filter.mesg ?: "")
); );
} }

View File

@ -35,20 +35,20 @@
#include "chat.h" #include "chat.h"
struct Ignore ignores[IgnoreCap]; struct Filter filters[FilterCap];
static size_t len; static size_t len;
struct Ignore ignoreParse(char *pattern) { struct Filter filterParse(enum Heat heat, char *pattern) {
struct Ignore ignore = {0}; struct Filter filter = { .heat = heat };
ignore.mask = strsep(&pattern, " "); filter.mask = strsep(&pattern, " ");
ignore.cmd = strsep(&pattern, " "); filter.cmd = strsep(&pattern, " ");
ignore.chan = strsep(&pattern, " "); filter.chan = strsep(&pattern, " ");
ignore.mesg = pattern; filter.mesg = pattern;
return ignore; return filter;
} }
struct Ignore ignoreAdd(const char *pattern) { struct Filter filterAdd(enum Heat heat, const char *pattern) {
if (len == IgnoreCap) errx(EX_CONFIG, "ignore limit exceeded"); if (len == FilterCap) errx(EX_CONFIG, "filter limit exceeded");
char *own; char *own;
if (!strchr(pattern, '!') && !strchr(pattern, ' ')) { if (!strchr(pattern, '!') && !strchr(pattern, ' ')) {
int n = asprintf(&own, "%s!*@*", pattern); int n = asprintf(&own, "%s!*@*", pattern);
@ -57,48 +57,49 @@ struct Ignore ignoreAdd(const char *pattern) {
own = strdup(pattern); own = strdup(pattern);
if (!own) err(EX_OSERR, "strdup"); if (!own) err(EX_OSERR, "strdup");
} }
struct Ignore ignore = ignoreParse(own); struct Filter filter = filterParse(heat, own);
ignores[len++] = ignore; filters[len++] = filter;
return ignore; return filter;
} }
bool ignoreRemove(struct Ignore ignore) { bool filterRemove(struct Filter filter) {
bool found = false; bool found = false;
for (size_t i = len - 1; i < len; --i) { for (size_t i = len - 1; i < len; --i) {
if (!ignores[i].cmd != !ignore.cmd) continue; if (filters[i].heat != filter.heat) continue;
if (!ignores[i].chan != !ignore.chan) continue; if (!filters[i].cmd != !filter.cmd) continue;
if (!ignores[i].mesg != !ignore.mesg) continue; if (!filters[i].chan != !filter.chan) continue;
if (strcasecmp(ignores[i].mask, ignore.mask)) continue; if (!filters[i].mesg != !filter.mesg) continue;
if (ignore.cmd && strcasecmp(ignores[i].cmd, ignore.cmd)) continue; if (strcasecmp(filters[i].mask, filter.mask)) continue;
if (ignore.chan && strcasecmp(ignores[i].chan, ignore.chan)) continue; if (filter.cmd && strcasecmp(filters[i].cmd, filter.cmd)) continue;
if (ignore.mesg && strcasecmp(ignores[i].mesg, ignore.mesg)) continue; if (filter.chan && strcasecmp(filters[i].chan, filter.chan)) continue;
free(ignores[i].mask); if (filter.mesg && strcasecmp(filters[i].mesg, filter.mesg)) continue;
ignores[i] = ignores[--len]; free(filters[i].mask);
ignores[len] = (struct Ignore) {0}; filters[i] = filters[--len];
filters[len] = (struct Filter) {0};
found = true; found = true;
} }
return found; return found;
} }
static bool ignoreTest( static bool filterTest(
struct Ignore ignore, const char *mask, uint id, const struct Message *msg struct Filter filter, const char *mask, uint id, const struct Message *msg
) { ) {
if (fnmatch(ignore.mask, mask, FNM_CASEFOLD)) return false; if (fnmatch(filter.mask, mask, FNM_CASEFOLD)) return false;
if (!ignore.cmd) return true; if (!filter.cmd) return true;
if (fnmatch(ignore.cmd, msg->cmd, FNM_CASEFOLD)) return false; if (fnmatch(filter.cmd, msg->cmd, FNM_CASEFOLD)) return false;
if (!ignore.chan) return true; if (!filter.chan) return true;
if (fnmatch(ignore.chan, idNames[id], FNM_CASEFOLD)) return false; if (fnmatch(filter.chan, idNames[id], FNM_CASEFOLD)) return false;
if (!ignore.mesg) return true; if (!filter.mesg) return true;
if (!msg->params[1]) return false; if (!msg->params[1]) return false;
return !fnmatch(ignore.mesg, msg->params[1], FNM_CASEFOLD); return !fnmatch(filter.mesg, msg->params[1], FNM_CASEFOLD);
} }
enum Heat ignoreCheck(enum Heat heat, uint id, const struct Message *msg) { enum Heat filterCheck(enum Heat heat, uint id, const struct Message *msg) {
if (!len) return heat; if (!len) return heat;
char mask[512]; char mask[512];
snprintf(mask, sizeof(mask), "%s!%s@%s", msg->nick, msg->user, msg->host); snprintf(mask, sizeof(mask), "%s!%s@%s", msg->nick, msg->user, msg->host);
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
if (ignoreTest(ignores[i], mask, id, msg)) return Ice; if (filterTest(filters[i], mask, id, msg)) return filters[i].heat;
} }
return heat; return heat;
} }

View File

@ -343,7 +343,7 @@ static void handleJoin(struct Message *msg) {
msg->params[2] = NULL; msg->params[2] = NULL;
} }
uiFormat( uiFormat(
id, ignoreCheck(Cold, id, msg), tagTime(msg), id, filterCheck(Cold, id, msg), tagTime(msg),
"\3%02d%s\3\t%s%s%sarrives in \3%02d%s\3", "\3%02d%s\3\t%s%s%sarrives in \3%02d%s\3",
hash(msg->user), msg->nick, hash(msg->user), msg->nick,
(msg->params[2] ? "(" : ""), (msg->params[2] ? "(" : ""),
@ -373,7 +373,7 @@ static void handlePart(struct Message *msg) {
completeClear(id); completeClear(id);
} }
completeRemove(id, msg->nick); completeRemove(id, msg->nick);
enum Heat heat = ignoreCheck(Cold, id, msg); enum Heat heat = filterCheck(Cold, id, msg);
if (heat > Ice) urlScan(id, msg->nick, msg->params[1]); if (heat > Ice) urlScan(id, msg->nick, msg->params[1]);
uiFormat( uiFormat(
id, heat, tagTime(msg), id, heat, tagTime(msg),
@ -423,7 +423,7 @@ static void handleNick(struct Message *msg) {
set(&idNames[id], msg->params[0]); set(&idNames[id], msg->params[0]);
} }
uiFormat( uiFormat(
id, ignoreCheck(Cold, id, msg), tagTime(msg), id, filterCheck(Cold, id, msg), tagTime(msg),
"\3%02d%s\3\tis now known as \3%02d%s\3", "\3%02d%s\3\tis now known as \3%02d%s\3",
hash(msg->user), msg->nick, hash(msg->user), msg->params[0] hash(msg->user), msg->nick, hash(msg->user), msg->params[0]
); );
@ -440,7 +440,7 @@ static void handleSetname(struct Message *msg) {
require(msg, true, 1); require(msg, true, 1);
for (uint id; (id = completeID(msg->nick));) { for (uint id; (id = completeID(msg->nick));) {
uiFormat( uiFormat(
id, ignoreCheck(Cold, id, msg), tagTime(msg), id, filterCheck(Cold, id, msg), tagTime(msg),
"\3%02d%s\3\tis now known as \3%02d%s\3 (%s)", "\3%02d%s\3\tis now known as \3%02d%s\3 (%s)",
hash(msg->user), msg->nick, hash(msg->user), msg->nick, hash(msg->user), msg->nick, hash(msg->user), msg->nick,
msg->params[0] msg->params[0]
@ -451,7 +451,7 @@ static void handleSetname(struct Message *msg) {
static void handleQuit(struct Message *msg) { static void handleQuit(struct Message *msg) {
require(msg, true, 0); require(msg, true, 0);
for (uint id; (id = completeID(msg->nick));) { for (uint id; (id = completeID(msg->nick));) {
enum Heat heat = ignoreCheck(Cold, id, msg); enum Heat heat = filterCheck(Cold, id, msg);
if (heat > Ice) urlScan(id, msg->nick, msg->params[0]); if (heat > Ice) urlScan(id, msg->nick, msg->params[0]);
uiFormat( uiFormat(
id, heat, tagTime(msg), id, heat, tagTime(msg),
@ -473,7 +473,7 @@ static void handleInvite(struct Message *msg) {
require(msg, true, 2); require(msg, true, 2);
if (!strcmp(msg->params[0], self.nick)) { if (!strcmp(msg->params[0], self.nick)) {
uiFormat( uiFormat(
Network, ignoreCheck(Hot, Network, msg), tagTime(msg), Network, filterCheck(Hot, Network, msg), tagTime(msg),
"\3%02d%s\3\tinvites you to \3%02d%s\3", "\3%02d%s\3\tinvites you to \3%02d%s\3",
hash(msg->user), msg->nick, hash(msg->params[1]), msg->params[1] hash(msg->user), msg->nick, hash(msg->params[1]), msg->params[1]
); );
@ -1199,7 +1199,7 @@ static void handlePrivmsg(struct Message *msg) {
bool notice = (msg->cmd[0] == 'N'); bool notice = (msg->cmd[0] == 'N');
bool action = isAction(msg); bool action = isAction(msg);
bool mention = !mine && isMention(msg); bool mention = !mine && isMention(msg);
enum Heat heat = ignoreCheck((mention || query ? Hot : Warm), id, msg); enum Heat heat = filterCheck((mention || query ? Hot : Warm), id, msg);
if (!notice && !mine && heat > Ice) { if (!notice && !mine && heat > Ice) {
completeTouch(id, msg->nick, hash(msg->user)); completeTouch(id, msg->nick, hash(msg->user));
} }
@ -1212,7 +1212,7 @@ static void handlePrivmsg(struct Message *msg) {
logFormat(id, tagTime(msg), "-%s- %s", msg->nick, msg->params[1]); logFormat(id, tagTime(msg), "-%s- %s", msg->nick, msg->params[1]);
} }
uiFormat( uiFormat(
id, ignoreCheck(Warm, id, msg), tagTime(msg), id, filterCheck(Warm, id, msg), tagTime(msg),
"\3%d-%s-\3%d\t%s", "\3%d-%s-\3%d\t%s",
hash(msg->user), msg->nick, LightGray, msg->params[1] hash(msg->user), msg->nick, LightGray, msg->params[1]
); );