]>
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 | ||
22 | #ifndef CHARYBDIS__MSGBUF_H | |
23 | #define CHARYBDIS__MSGBUF_H | |
24 | ||
25 | #define MAXPARA (15) | |
26 | ||
27 | /* a key-value structure for each message tag. */ | |
28 | struct MsgTag { | |
9d517024 AC |
29 | const char *key; /* the key of the tag (must be set) */ |
30 | const char *value; /* the value of the tag or NULL */ | |
31 | unsigned int capmask; /* the capability mask this tag belongs to (used only when sending) */ | |
b830b641 AC |
32 | }; |
33 | ||
34 | struct MsgBuf { | |
9d517024 AC |
35 | size_t n_tags; /* the number of tags in the MsgBuf */ |
36 | struct MsgTag tags[MAXPARA]; /* the tags themselves, upto MAXPARA tags available */ | |
b830b641 | 37 | |
9d517024 AC |
38 | const char *origin; /* the origin of the message (or NULL) */ |
39 | const char *cmd; /* the cmd/verb of the message (also para[0]) */ | |
b830b641 | 40 | |
9d517024 AC |
41 | size_t parselen; /* the length of the message */ |
42 | size_t n_para; /* the number of parameters (always at least 1) */ | |
43 | const char *para[MAXPARA]; /* parameters vector (starting with cmd as para[0]) */ | |
b830b641 AC |
44 | }; |
45 | ||
46 | /* | |
47 | * parse a message into a MsgBuf. | |
48 | * returns 0 on success, 1 on error. | |
49 | */ | |
a8e69f5d | 50 | int msgbuf_parse(struct MsgBuf *msgbuf, char *line); |
b830b641 | 51 | |
88b427b6 AC |
52 | /* |
53 | * unparse a pure MsgBuf into a buffer. | |
54 | * if origin is NULL, me.name will be used. | |
55 | * cmd may not be NULL. | |
56 | * returns 0 on success, 1 on error. | |
57 | */ | |
9d517024 | 58 | int msgbuf_unparse(char *buf, struct MsgBuf *msgbuf, unsigned int capmask); |
88b427b6 | 59 | |
b830b641 AC |
60 | /* |
61 | * unparse a MsgBuf header plus payload into a buffer. | |
62 | * if origin is NULL, me.name will be used. | |
63 | * cmd may not be NULL. | |
64 | * returns 0 on success, 1 on error. | |
65 | */ | |
920e4849 | 66 | int msgbuf_unparse_fmt(char *buf, struct MsgBuf *head, unsigned int capmask, const char *fmt, ...) AFP(4, 5); |
9d517024 | 67 | int msgbuf_vunparse_fmt(char *buf, struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va); |
88b427b6 AC |
68 | |
69 | static inline void | |
70 | msgbuf_init(struct MsgBuf *msgbuf) | |
71 | { | |
269dd686 | 72 | memset(msgbuf, 0, sizeof(*msgbuf)); |
88b427b6 AC |
73 | } |
74 | ||
75 | static inline void | |
76 | msgbuf_append_tag(struct MsgBuf *msgbuf, const char *key, const char *value) | |
77 | { | |
9d517024 AC |
78 | s_assert(msgbuf->n_tags < MAXPARA); |
79 | ||
88b427b6 AC |
80 | msgbuf->tags[msgbuf->n_tags].key = key; |
81 | msgbuf->tags[msgbuf->n_tags].value = value; | |
82 | msgbuf->n_tags++; | |
83 | } | |
84 | ||
85 | static inline void | |
86 | msgbuf_append_para(struct MsgBuf *msgbuf, const char *para) | |
87 | { | |
9d517024 AC |
88 | s_assert(msgbuf->n_para < MAXPARA); |
89 | ||
88b427b6 AC |
90 | msgbuf->para[msgbuf->n_para] = para; |
91 | msgbuf->n_para++; | |
92 | } | |
b830b641 AC |
93 | |
94 | #endif |