]> jfr.im git - solanum.git/blobdiff - include/msgbuf.h
Keep propagated bans in a dictionary, not a list
[solanum.git] / include / msgbuf.h
index b060fafb6eda074d23eef8d1df867ae97ba41343..27e6d75d653e54bff6f6ba43cfad0d5ddce296a1 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * charybdis - an advanced ircd.
- * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>.
+ * solanum - an advanced ircd.
+ * Copyright (c) 2016 Ariadne Conill <ariadne@dereferenced.org>.
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "s_assert.h"
-
-#ifndef CHARYBDIS__MSGBUF_H
-#define CHARYBDIS__MSGBUF_H
+#ifndef SOLANUM__MSGBUF_H
+#define SOLANUM__MSGBUF_H
 
 #define MAXPARA                (15)
 
@@ -38,26 +36,60 @@ struct MsgBuf {
        struct MsgTag tags[MAXPARA];    /* the tags themselves, upto MAXPARA tags available */
 
        const char *origin;             /* the origin of the message (or NULL) */
-       const char *cmd;                /* the cmd/verb of the message (also para[0]) */
+       const char *target;             /* the target of the message (either NULL, or custom defined) */
+       const char *cmd;                /* the cmd/verb of the message (either NULL, or para[0]) */
+       char *endp;                     /* one past the end of the original array */
 
-       size_t parselen;                /* the length of the message */
-       size_t n_para;                  /* the number of parameters (always at least 1) */
+       size_t n_para;                  /* the number of parameters (always at least 1 if a full message) */
        const char *para[MAXPARA];      /* parameters vector (starting with cmd as para[0]) */
 };
 
+struct MsgBuf_str_data {
+       const struct MsgBuf *msgbuf;
+       unsigned int caps;
+};
+
+#define MSGBUF_CACHE_SIZE 32
+
+struct MsgBuf_cache_entry {
+       unsigned int caps;
+       buf_head_t linebuf;
+       struct MsgBuf_cache_entry *next;
+};
+
+struct MsgBuf_cache {
+       const struct MsgBuf *msgbuf;
+       char message[DATALEN + 1];
+       unsigned int overall_capmask;
+
+       /* Fixed maximum size linked list, new entries are allocated at the end
+        * of the array but are accessed through the "next" pointers.
+        *
+        * This does not use rb dlink to avoid unnecessary individual allocations.
+        */
+       struct MsgBuf_cache_entry entry[MSGBUF_CACHE_SIZE];
+       struct MsgBuf_cache_entry *head; /* LRU cache head */
+};
+
 /*
  * parse a message into a MsgBuf.
  * returns 0 on success, 1 on error.
  */
 int msgbuf_parse(struct MsgBuf *msgbuf, char *line);
 
+/*
+ * Unparse the tail of a msgbuf perfectly, preserving framing details
+ * msgbuf->para[n] will reach to the end of the line
+ */
+void msgbuf_reconstruct_tail(struct MsgBuf *msgbuf, size_t n);
+
 /*
  * unparse a pure MsgBuf into a buffer.
  * if origin is NULL, me.name will be used.
  * cmd may not be NULL.
  * returns 0 on success, 1 on error.
  */
-int msgbuf_unparse(char *buf, struct MsgBuf *msgbuf, unsigned int capmask);
+int msgbuf_unparse(char *buf, size_t buflen, const struct MsgBuf *msgbuf, unsigned int capmask);
 
 /*
  * unparse a MsgBuf header plus payload into a buffer.
@@ -65,8 +97,16 @@ int msgbuf_unparse(char *buf, struct MsgBuf *msgbuf, unsigned int capmask);
  * cmd may not be NULL.
  * returns 0 on success, 1 on error.
  */
-int msgbuf_unparse_fmt(char *buf, struct MsgBuf *head, unsigned int capmask, const char *fmt, ...) AFP(4, 5);
-int msgbuf_vunparse_fmt(char *buf, struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va);
+int msgbuf_unparse_fmt(char *buf, size_t buflen, const struct MsgBuf *head, unsigned int capmask, const char *fmt, ...) AFP(5, 6);
+int msgbuf_vunparse_fmt(char *buf, size_t buflen, const struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va);
+
+int msgbuf_unparse_linebuf_tags(char *buf, size_t buflen, void *data);
+int msgbuf_unparse_prefix(char *buf, size_t *buflen, const struct MsgBuf *msgbuf, unsigned int capmask);
+
+void msgbuf_cache_init(struct MsgBuf_cache *cache, const struct MsgBuf *msgbuf, const rb_strf_t *message);
+void msgbuf_cache_initf(struct MsgBuf_cache *cache, const struct MsgBuf *msgbuf, const rb_strf_t *message, const char *format, ...) AFP(4, 5);
+buf_head_t *msgbuf_cache_get(struct MsgBuf_cache *cache, unsigned int caps);
+void msgbuf_cache_free(struct MsgBuf_cache *cache);
 
 static inline void
 msgbuf_init(struct MsgBuf *msgbuf)
@@ -77,21 +117,12 @@ msgbuf_init(struct MsgBuf *msgbuf)
 static inline void
 msgbuf_append_tag(struct MsgBuf *msgbuf, const char *key, const char *value, unsigned int capmask)
 {
-       s_assert(msgbuf->n_tags < MAXPARA);
-
-       msgbuf->tags[msgbuf->n_tags].key = key;
-       msgbuf->tags[msgbuf->n_tags].value = value;
-       msgbuf->tags[msgbuf->n_tags].capmask = capmask;
-       msgbuf->n_tags++;
-}
-
-static inline void
-msgbuf_append_para(struct MsgBuf *msgbuf, const char *para)
-{
-       s_assert(msgbuf->n_para < MAXPARA);
-
-       msgbuf->para[msgbuf->n_para] = para;
-       msgbuf->n_para++;
+       if (msgbuf->n_tags < MAXPARA) {
+               msgbuf->tags[msgbuf->n_tags].key = key;
+               msgbuf->tags[msgbuf->n_tags].value = value;
+               msgbuf->tags[msgbuf->n_tags].capmask = capmask;
+               msgbuf->n_tags++;
+       }
 }
 
 #endif