2016-02-10 06:08:06 +01:00
|
|
|
/*
|
|
|
|
* charybdis - an advanced ircd.
|
|
|
|
* Copyright (c) 2016 William Pitcock <nenolod@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
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_MSGBUF_H
|
2016-02-12 19:12:30 +01:00
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
#include "s_assert.h"
|
2016-02-10 06:08:06 +01:00
|
|
|
|
|
|
|
#define MAXPARA (15)
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
namespace ircd {
|
|
|
|
|
2016-02-10 06:08:06 +01:00
|
|
|
/* a key-value structure for each message tag. */
|
|
|
|
struct MsgTag {
|
2016-02-12 01:17:43 +01:00
|
|
|
const char *key; /* the key of the tag (must be set) */
|
|
|
|
const char *value; /* the value of the tag or NULL */
|
|
|
|
unsigned int capmask; /* the capability mask this tag belongs to (used only when sending) */
|
2016-02-10 06:08:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MsgBuf {
|
2016-02-12 01:17:43 +01:00
|
|
|
size_t n_tags; /* the number of tags in the MsgBuf */
|
|
|
|
struct MsgTag tags[MAXPARA]; /* the tags themselves, upto MAXPARA tags available */
|
2016-02-10 06:08:06 +01:00
|
|
|
|
2016-02-12 01:17:43 +01:00
|
|
|
const char *origin; /* the origin of the message (or NULL) */
|
2016-02-21 00:59:00 +01:00
|
|
|
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]) */
|
2016-02-10 06:08:06 +01:00
|
|
|
|
2016-02-12 01:17:43 +01:00
|
|
|
size_t parselen; /* the length of the message */
|
2016-02-21 00:59:00 +01:00
|
|
|
size_t n_para; /* the number of parameters (always at least 1 if a full message) */
|
2016-02-12 01:17:43 +01:00
|
|
|
const char *para[MAXPARA]; /* parameters vector (starting with cmd as para[0]) */
|
2016-02-10 06:08:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse a message into a MsgBuf.
|
|
|
|
* returns 0 on success, 1 on error.
|
|
|
|
*/
|
2016-02-10 07:08:58 +01:00
|
|
|
int msgbuf_parse(struct MsgBuf *msgbuf, char *line);
|
2016-02-10 06:08:06 +01:00
|
|
|
|
2016-02-10 06:19:13 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-02-20 18:21:12 +01:00
|
|
|
int msgbuf_unparse(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask);
|
2016-02-10 06:19:13 +01:00
|
|
|
|
2016-02-10 06:08:06 +01:00
|
|
|
/*
|
|
|
|
* unparse a MsgBuf header plus payload into a buffer.
|
|
|
|
* if origin is NULL, me.name will be used.
|
|
|
|
* cmd may not be NULL.
|
|
|
|
* returns 0 on success, 1 on error.
|
|
|
|
*/
|
2016-02-20 18:21:12 +01:00
|
|
|
int msgbuf_unparse_fmt(char *buf, size_t buflen, struct MsgBuf *head, unsigned int capmask, const char *fmt, ...) AFP(5, 6);
|
|
|
|
int msgbuf_vunparse_fmt(char *buf, size_t buflen, struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va);
|
2016-02-10 06:19:13 +01:00
|
|
|
|
2016-02-20 21:46:09 +01:00
|
|
|
void msgbuf_unparse_prefix(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask);
|
|
|
|
|
2016-02-10 06:19:13 +01:00
|
|
|
static inline void
|
|
|
|
msgbuf_init(struct MsgBuf *msgbuf)
|
|
|
|
{
|
2016-02-10 07:46:32 +01:00
|
|
|
memset(msgbuf, 0, sizeof(*msgbuf));
|
2016-02-10 06:19:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2016-02-13 07:19:23 +01:00
|
|
|
msgbuf_append_tag(struct MsgBuf *msgbuf, const char *key, const char *value, unsigned int capmask)
|
2016-02-10 06:19:13 +01:00
|
|
|
{
|
2016-02-12 01:17:43 +01:00
|
|
|
s_assert(msgbuf->n_tags < MAXPARA);
|
|
|
|
|
2016-02-10 06:19:13 +01:00
|
|
|
msgbuf->tags[msgbuf->n_tags].key = key;
|
|
|
|
msgbuf->tags[msgbuf->n_tags].value = value;
|
2016-02-13 07:19:23 +01:00
|
|
|
msgbuf->tags[msgbuf->n_tags].capmask = capmask;
|
2016-02-10 06:19:13 +01:00
|
|
|
msgbuf->n_tags++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
msgbuf_append_para(struct MsgBuf *msgbuf, const char *para)
|
|
|
|
{
|
2016-02-12 01:17:43 +01:00
|
|
|
s_assert(msgbuf->n_para < MAXPARA);
|
|
|
|
|
2016-02-10 06:19:13 +01:00
|
|
|
msgbuf->para[msgbuf->n_para] = para;
|
|
|
|
msgbuf->n_para++;
|
|
|
|
}
|
2016-02-10 06:08:06 +01:00
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
} // namespace ircd
|
|
|
|
#endif // __cplusplus
|