]>
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 | 38 | const char *origin; /* the origin of the message (or NULL) */ |
71c875fb AC |
39 | const char *target; /* the target of the message (either NULL, or custom defined) */ |
40 | const char *cmd; /* the cmd/verb of the message (either NULL, or para[0]) */ | |
4e14f9a4 | 41 | char *endp; /* one past the end of the original array */ |
b830b641 | 42 | |
71c875fb | 43 | size_t n_para; /* the number of parameters (always at least 1 if a full message) */ |
9d517024 | 44 | const char *para[MAXPARA]; /* parameters vector (starting with cmd as para[0]) */ |
b830b641 AC |
45 | }; |
46 | ||
4b1cce65 SA |
47 | struct MsgBuf_str_data { |
48 | const struct MsgBuf *msgbuf; | |
49 | unsigned int caps; | |
50 | }; | |
51 | ||
52 | #define MSGBUF_CACHE_SIZE 32 | |
53 | ||
54 | struct MsgBuf_cache_entry { | |
55 | unsigned int caps; | |
56 | buf_head_t linebuf; | |
57 | struct MsgBuf_cache_entry *next; | |
58 | }; | |
59 | ||
60 | struct MsgBuf_cache { | |
61 | const struct MsgBuf *msgbuf; | |
62 | char message[DATALEN + 1]; | |
63 | unsigned int overall_capmask; | |
64 | ||
65 | /* Fixed maximum size linked list, new entries are allocated at the end | |
66 | * of the array but are accessed through the "next" pointers. | |
67 | * | |
68 | * This does not use rb dlink to avoid unnecessary individual allocations. | |
69 | */ | |
70 | struct MsgBuf_cache_entry entry[MSGBUF_CACHE_SIZE]; | |
71 | struct MsgBuf_cache_entry *head; /* LRU cache head */ | |
72 | }; | |
73 | ||
b830b641 AC |
74 | /* |
75 | * parse a message into a MsgBuf. | |
76 | * returns 0 on success, 1 on error. | |
77 | */ | |
a8e69f5d | 78 | int msgbuf_parse(struct MsgBuf *msgbuf, char *line); |
b830b641 | 79 | |
4e14f9a4 EK |
80 | /* |
81 | * Unparse the tail of a msgbuf perfectly, preserving framing details | |
82 | * msgbuf->para[n] will reach to the end of the line | |
83 | */ | |
84 | void msgbuf_reconstruct_tail(struct MsgBuf *msgbuf, size_t n); | |
85 | ||
88b427b6 AC |
86 | /* |
87 | * unparse a pure MsgBuf into a buffer. | |
88 | * if origin is NULL, me.name will be used. | |
89 | * cmd may not be NULL. | |
90 | * returns 0 on success, 1 on error. | |
91 | */ | |
f3564f47 | 92 | int msgbuf_unparse(char *buf, size_t buflen, const struct MsgBuf *msgbuf, unsigned int capmask); |
88b427b6 | 93 | |
b830b641 AC |
94 | /* |
95 | * unparse a MsgBuf header plus payload into a buffer. | |
96 | * if origin is NULL, me.name will be used. | |
97 | * cmd may not be NULL. | |
98 | * returns 0 on success, 1 on error. | |
99 | */ | |
f3564f47 SA |
100 | int msgbuf_unparse_fmt(char *buf, size_t buflen, const struct MsgBuf *head, unsigned int capmask, const char *fmt, ...) AFP(5, 6); |
101 | int msgbuf_vunparse_fmt(char *buf, size_t buflen, const struct MsgBuf *head, unsigned int capmask, const char *fmt, va_list va); | |
88b427b6 | 102 | |
4b1cce65 SA |
103 | int msgbuf_unparse_linebuf_tags(char *buf, size_t buflen, void *data); |
104 | int msgbuf_unparse_prefix(char *buf, size_t *buflen, const struct MsgBuf *msgbuf, unsigned int capmask); | |
105 | ||
106 | void msgbuf_cache_init(struct MsgBuf_cache *cache, const struct MsgBuf *msgbuf, const rb_strf_t *message); | |
107 | void msgbuf_cache_initf(struct MsgBuf_cache *cache, const struct MsgBuf *msgbuf, const rb_strf_t *message, const char *format, ...) AFP(4, 5); | |
108 | buf_head_t *msgbuf_cache_get(struct MsgBuf_cache *cache, unsigned int caps); | |
109 | void msgbuf_cache_free(struct MsgBuf_cache *cache); | |
33085472 | 110 | |
88b427b6 AC |
111 | static inline void |
112 | msgbuf_init(struct MsgBuf *msgbuf) | |
113 | { | |
269dd686 | 114 | memset(msgbuf, 0, sizeof(*msgbuf)); |
88b427b6 AC |
115 | } |
116 | ||
117 | static inline void | |
d670fe52 | 118 | msgbuf_append_tag(struct MsgBuf *msgbuf, const char *key, const char *value, unsigned int capmask) |
88b427b6 | 119 | { |
169a1c35 SA |
120 | if (msgbuf->n_tags < MAXPARA) { |
121 | msgbuf->tags[msgbuf->n_tags].key = key; | |
122 | msgbuf->tags[msgbuf->n_tags].value = value; | |
123 | msgbuf->tags[msgbuf->n_tags].capmask = capmask; | |
124 | msgbuf->n_tags++; | |
125 | } | |
88b427b6 AC |
126 | } |
127 | ||
b830b641 | 128 | #endif |