]>
Commit | Line | Data |
---|---|---|
b830b641 AC |
1 | /* |
2 | * charybdis - an advanced ircd. | |
3 | * Copyright (c) 2016 William Pitcock <nenolod@dereferenced.org>. | |
4 | * | |
5 | * Permission to use, copy, modify, and/or distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice is present in all copies. | |
8 | * | |
9 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
10 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
11 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
12 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
13 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
14 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
15 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
16 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
17 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
18 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
19 | * POSSIBILITY OF SUCH DAMAGE. | |
20 | */ | |
21 | ||
b4993fe8 AC |
22 | #include "s_assert.h" |
23 | ||
b830b641 AC |
24 | #ifndef CHARYBDIS__MSGBUF_H |
25 | #define CHARYBDIS__MSGBUF_H | |
26 | ||
27 | #define MAXPARA (15) | |
28 | ||
29 | /* a key-value structure for each message tag. */ | |
30 | struct MsgTag { | |
9d517024 AC |
31 | const char *key; /* the key of the tag (must be set) */ |
32 | const char *value; /* the value of the tag or NULL */ | |
33 | unsigned int capmask; /* the capability mask this tag belongs to (used only when sending) */ | |
b830b641 AC |
34 | }; |
35 | ||
36 | struct MsgBuf { | |
9d517024 AC |
37 | size_t n_tags; /* the number of tags in the MsgBuf */ |
38 | struct MsgTag tags[MAXPARA]; /* the tags themselves, upto MAXPARA tags available */ | |
b830b641 | 39 | |
9d517024 AC |
40 | const char *origin; /* the origin of the message (or NULL) */ |
41 | const char *cmd; /* the cmd/verb of the message (also para[0]) */ | |
b830b641 | 42 | |
9d517024 AC |
43 | size_t parselen; /* the length of the message */ |
44 | size_t n_para; /* the number of parameters (always at least 1) */ | |
45 | const char *para[MAXPARA]; /* parameters vector (starting with cmd as para[0]) */ | |
b830b641 AC |
46 | }; |
47 | ||
48 | /* | |
49 | * parse a message into a MsgBuf. | |
50 | * returns 0 on success, 1 on error. | |
51 | */ | |
a8e69f5d | 52 | int msgbuf_parse(struct MsgBuf *msgbuf, char *line); |
b830b641 | 53 | |
88b427b6 AC |
54 | /* |
55 | * unparse a pure MsgBuf into a buffer. | |
56 | * if origin is NULL, me.name will be used. | |
57 | * cmd may not be NULL. | |
58 | * returns 0 on success, 1 on error. | |
59 | */ | |
4a13e3f1 | 60 | int msgbuf_unparse(char *buf, size_t buflen, struct MsgBuf *msgbuf, unsigned int capmask); |
88b427b6 | 61 | |
b830b641 AC |
62 | /* |
63 | * unparse a MsgBuf header plus payload into a buffer. | |
64 | * if origin is NULL, me.name will be used. | |
65 | * cmd may not be NULL. | |
66 | * returns 0 on success, 1 on error. | |
67 | */ | |
4a13e3f1 AC |
68 | int msgbuf_unparse_fmt(char *buf, size_t buflen, struct MsgBuf *head, unsigned int capmask, const char *fmt, ...) AFP(5, 6); |
69 | int msgbuf_vunparse_fmt(char *buf, size_t buflen, struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va); | |
88b427b6 AC |
70 | |
71 | static inline void | |
72 | msgbuf_init(struct MsgBuf *msgbuf) | |
73 | { | |
269dd686 | 74 | memset(msgbuf, 0, sizeof(*msgbuf)); |
88b427b6 AC |
75 | } |
76 | ||
77 | static inline void | |
d670fe52 | 78 | msgbuf_append_tag(struct MsgBuf *msgbuf, const char *key, const char *value, unsigned int capmask) |
88b427b6 | 79 | { |
9d517024 AC |
80 | s_assert(msgbuf->n_tags < MAXPARA); |
81 | ||
88b427b6 AC |
82 | msgbuf->tags[msgbuf->n_tags].key = key; |
83 | msgbuf->tags[msgbuf->n_tags].value = value; | |
d670fe52 | 84 | msgbuf->tags[msgbuf->n_tags].capmask = capmask; |
88b427b6 AC |
85 | msgbuf->n_tags++; |
86 | } | |
87 | ||
88 | static inline void | |
89 | msgbuf_append_para(struct MsgBuf *msgbuf, const char *para) | |
90 | { | |
9d517024 AC |
91 | s_assert(msgbuf->n_para < MAXPARA); |
92 | ||
88b427b6 AC |
93 | msgbuf->para[msgbuf->n_para] = para; |
94 | msgbuf->n_para++; | |
95 | } | |
b830b641 AC |
96 | |
97 | #endif |