catgirl/chat.h

305 lines
7.5 KiB
C
Raw Normal View History

2020-02-01 06:18:01 +00:00
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Additional permission under GNU GPL version 3 section 7:
*
* If you modify this Program, or any covered work, by linking or
* combining it with LibreSSL (or a modified version of that library),
* containing parts covered by the terms of the OpenSSL License and the
* original SSLeay license, the licensors of this Program grant you
* additional permission to convey the resulting work. Corresponding
* Source for a non-source form of such a combination shall include the
* source code for the parts of LibreSSL used as well as that of the
* covered work.
2020-02-01 06:18:01 +00:00
*/
#include <assert.h>
2020-02-01 07:33:17 +00:00
#include <err.h>
#include <getopt.h>
#include <stdarg.h>
2020-02-01 06:18:01 +00:00
#include <stdbool.h>
2020-02-02 07:31:20 +00:00
#include <stdint.h>
#include <stdio.h>
2020-02-01 07:33:17 +00:00
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <wchar.h>
2020-02-01 06:18:01 +00:00
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
#define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit
typedef unsigned uint;
2020-02-01 06:18:01 +00:00
typedef unsigned char byte;
struct Cat {
char *buf;
size_t cap;
size_t len;
};
static inline void __attribute__((format(printf, 2, 3)))
catf(struct Cat *cat, const char *format, ...) {
va_list ap;
va_start(ap, format);
int len = vsnprintf(&cat->buf[cat->len], cat->cap - cat->len, format, ap);
assert(len >= 0);
va_end(ap);
cat->len += len;
if (cat->len >= cat->cap) cat->len = cat->cap - 1;
}
2020-02-02 08:27:50 +00:00
enum Color {
White, Black, Blue, Green, Red, Brown, Magenta, Orange,
Yellow, LightGreen, Cyan, LightCyan, LightBlue, Pink, Gray, LightGray,
Default = 99,
ColorCap,
2020-02-02 08:27:50 +00:00
};
2020-02-01 07:33:17 +00:00
enum { None, Debug, Network, IDCap = 256 };
extern char *idNames[IDCap];
2020-02-02 08:27:50 +00:00
extern enum Color idColors[IDCap];
extern uint idNext;
2020-02-01 07:33:17 +00:00
static inline uint idFind(const char *name) {
for (uint id = 0; id < idNext; ++id) {
2020-02-01 07:33:17 +00:00
if (!strcmp(idNames[id], name)) return id;
}
return None;
}
2020-02-02 08:27:50 +00:00
static inline uint idFor(const char *name) {
uint id = idFind(name);
2020-02-01 07:33:17 +00:00
if (id) return id;
if (idNext == IDCap) return Network;
2020-02-01 07:33:17 +00:00
idNames[idNext] = strdup(name);
2020-02-06 09:19:56 +00:00
idColors[idNext] = Default;
if (!idNames[idNext]) err(EX_OSERR, "strdup");
2020-02-01 07:33:17 +00:00
return idNext++;
}
2020-02-11 22:41:06 +00:00
extern uint32_t hashInit;
static inline enum Color hash(const char *str) {
if (*str == '~') str++;
uint32_t hash = hashInit;
for (; *str; ++str) {
hash = (hash << 5) | (hash >> 27);
hash ^= *str;
hash *= 0x27220A95;
}
return Blue + hash % 74;
2020-02-11 22:41:06 +00:00
}
extern struct Network {
char *name;
uint userLen;
uint hostLen;
char *chanTypes;
char *prefixes;
char *prefixModes;
char *listModes;
char *paramModes;
char *setParamModes;
char *channelModes;
char excepts;
char invex;
} network;
2020-02-01 06:18:01 +00:00
#define ENUM_CAP \
X("causal.agency/consumer", CapConsumer) \
2020-03-22 18:32:20 +00:00
X("chghost", CapChghost) \
X("extended-join", CapExtendedJoin) \
2020-02-15 02:36:58 +00:00
X("invite-notify", CapInviteNotify) \
2020-02-15 09:59:50 +00:00
X("multi-prefix", CapMultiPrefix) \
2020-02-01 06:18:01 +00:00
X("sasl", CapSASL) \
X("server-time", CapServerTime) \
X("userhost-in-names", CapUserhostInNames)
enum Cap {
#define X(name, id) BIT(id),
ENUM_CAP
#undef X
};
extern struct Self {
2020-02-01 07:26:35 +00:00
bool debug;
2020-02-12 03:39:29 +00:00
bool restricted;
size_t pos;
2020-02-01 06:18:01 +00:00
enum Cap caps;
char *plain;
char *join;
2020-02-01 06:18:01 +00:00
char *nick;
2020-02-05 05:40:24 +00:00
char *user;
2020-03-22 18:32:20 +00:00
char *host;
enum Color color;
2020-02-06 02:57:23 +00:00
char *quit;
2020-02-01 06:18:01 +00:00
} self;
static inline void set(char **field, const char *value) {
free(*field);
*field = strdup(value);
if (!*field) err(EX_OSERR, "strdup");
}
2020-02-01 06:18:01 +00:00
#define ENUM_TAG \
X("causal.agency/pos", TagPos) \
2020-02-01 06:18:01 +00:00
X("time", TagTime)
enum Tag {
#define X(name, id) id,
ENUM_TAG
#undef X
TagCap,
};
enum { ParamCap = 254 };
2020-02-01 06:18:01 +00:00
struct Message {
char *tags[TagCap];
char *nick;
char *user;
char *host;
char *cmd;
char *params[ParamCap];
};
void ircConfig(bool insecure, FILE *cert, FILE *priv);
int ircConnect(const char *bind, const char *host, const char *port);
2020-02-01 06:18:01 +00:00
void ircRecv(void);
void ircSend(const char *ptr, size_t len);
void ircFormat(const char *format, ...)
__attribute__((format(printf, 1, 2)));
void ircClose(void);
2020-02-01 06:18:01 +00:00
extern uint execID;
extern int execPipe[2];
extern int utilPipe[2];
2020-02-14 02:57:55 +00:00
2020-02-13 01:12:34 +00:00
enum { UtilCap = 16 };
struct Util {
uint argc;
2020-02-13 01:12:34 +00:00
const char *argv[UtilCap];
};
static inline void utilPush(struct Util *util, const char *arg) {
if (1 + util->argc < UtilCap) {
util->argv[util->argc++] = arg;
} else {
errx(EX_CONFIG, "too many utility arguments");
}
}
extern struct Replies {
uint away;
uint ban;
uint excepts;
uint invex;
uint join;
uint list;
2020-03-23 20:52:24 +00:00
uint mode;
uint names;
uint topic;
uint whois;
} replies;
void handle(struct Message *msg);
void command(uint id, char *input);
const char *commandIsPrivmsg(uint id, const char *input);
const char *commandIsNotice(uint id, const char *input);
const char *commandIsAction(uint id, const char *input);
2020-03-30 18:56:26 +00:00
void commandCompleteAdd(void);
2020-03-31 18:30:42 +00:00
enum Heat { Ice, Cold, Warm, Hot };
2020-02-13 01:12:34 +00:00
extern struct Util uiNotifyUtil;
void uiInit(void);
void uiShow(void);
void uiHide(void);
void uiDraw(void);
void uiShowID(uint id);
void uiShowNum(uint num);
void uiMoveID(uint id, uint num);
void uiCloseID(uint id);
void uiCloseNum(uint id);
2020-02-04 08:58:56 +00:00
void uiRead(void);
void uiWrite(uint id, enum Heat heat, const time_t *time, const char *str);
void uiFormat(
uint id, enum Heat heat, const time_t *time, const char *format, ...
) __attribute__((format(printf, 4, 5)));
2020-02-11 00:40:13 +00:00
void uiLoad(const char *name);
int uiSave(const char *name);
enum Edit {
EditHead,
EditTail,
EditPrev,
EditNext,
2020-02-09 09:20:07 +00:00
EditPrevWord,
EditNextWord,
2020-02-09 09:22:41 +00:00
EditDeleteHead,
EditDeleteTail,
EditDeletePrev,
EditDeleteNext,
2020-02-09 09:32:32 +00:00
EditDeletePrevWord,
EditDeleteNextWord,
EditPaste,
EditTranspose,
2020-04-07 14:48:44 +00:00
EditCollapse,
EditInsert,
2020-02-08 02:30:25 +00:00
EditComplete,
2020-03-30 18:56:26 +00:00
EditExpand,
EditEnter,
};
void edit(uint id, enum Edit op, wchar_t ch);
char *editBuffer(size_t *pos);
2020-03-30 18:56:26 +00:00
void editCompleteAdd(void);
const char *complete(uint id, const char *prefix);
2020-02-08 02:30:25 +00:00
void completeAccept(void);
void completeReject(void);
void completeAdd(uint id, const char *str, enum Color color);
void completeTouch(uint id, const char *str, enum Color color);
void completeReplace(uint id, const char *old, const char *new);
void completeRemove(uint id, const char *str);
void completeClear(uint id);
uint completeID(const char *str);
enum Color completeColor(uint id, const char *str);
2020-02-08 02:30:25 +00:00
extern struct Util urlOpenUtil;
extern struct Util urlCopyUtil;
void urlScan(uint id, const char *nick, const char *mesg);
void urlOpenCount(uint id, uint count);
void urlOpenMatch(uint id, const char *str);
void urlCopyMatch(uint id, const char *str);
2020-02-08 23:29:01 +00:00
2020-03-31 18:30:42 +00:00
enum { IgnoreCap = 256 };
extern struct Ignore {
size_t len;
char *patterns[IgnoreCap];
} ignore;
const char *ignoreAdd(const char *pattern);
bool ignoreRemove(const char *pattern);
enum Heat ignoreCheck(enum Heat heat, uint id, const struct Message *msg);
2020-03-31 18:30:42 +00:00
extern bool logEnable;
void logFormat(uint id, const time_t *time, const char *format, ...)
__attribute__((format(printf, 3, 4)));
void logClose(void);
FILE *configOpen(const char *path, const char *mode);
FILE *dataOpen(const char *path, const char *mode);
void dataMkdir(const char *path);
int getopt_config(
int argc, char *const *argv,
const char *optstring, const struct option *longopts, int *longindex
);