]> jfr.im git - solanum.git/blame - ircd/substitution.c
Show account name in cliconn snotes when SASL is used (#135)
[solanum.git] / ircd / substitution.c
CommitLineData
92fb5c31 1/*
a6f63a82 2 * Solanum: a slightly advanced ircd
92fb5c31
AC
3 * substitution.c: parses substitution-keyword expansions
4 *
3fc0499e 5 * Copyright (c) 2006-2007 Ariadne Conill <ariadne@dereferenced.org>
92fb5c31
AC
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
92fb5c31
AC
32 */
33
34#include "stdinc.h"
92fb5c31 35#include "s_user.h"
8c23bb2e 36#include "snomask.h"
4562c604 37#include "match.h"
2e819b6b 38#include "substitution.h"
77d3d2db 39#include "s_assert.h"
92fb5c31
AC
40
41/*
42 * Simple mappings for $foo -> 'bar'.
43 * Everything is a string, so typing doesn't really matter too
44 * horribly much right now.
45 */
46struct substitution_variable
47{
48 char *name;
49 char *value;
50};
51
52/*
53 * substitution_append_var
54 *
330fc5c1 55 * Inputs - A variable list (rb_dlink_list), name -> value for mapping to make
92fb5c31
AC
56 * Output - none
57 * Side Effects - Adds a name->value mapping to a list.
58 */
330fc5c1 59void substitution_append_var(rb_dlink_list *varlist, const char *name, const char *value)
92fb5c31 60{
eddc2ab6 61 struct substitution_variable *tmp = rb_malloc(sizeof(struct substitution_variable));
92fb5c31 62
47a03750
VY
63 tmp->name = rb_strdup(name);
64 tmp->value = rb_strdup(value);
92fb5c31 65
330fc5c1 66 rb_dlinkAddAlloc(tmp, varlist);
92fb5c31
AC
67}
68
69/*
70 * substitution_free
71 *
330fc5c1 72 * Inputs - A rb_dlink_list of markup variables to free.
92fb5c31
AC
73 * Outputs - none
74 * Side Effects - Empties a list of markup variables.
75 */
330fc5c1 76void substitution_free(rb_dlink_list *varlist)
92fb5c31 77{
330fc5c1 78 rb_dlink_node *nptr, *nptr2;
92fb5c31 79
5cefa1d6 80 RB_DLINK_FOREACH_SAFE(nptr, nptr2, varlist->head)
92fb5c31
AC
81 {
82 struct substitution_variable *tmp = (struct substitution_variable *) nptr->data;
83
55d5f797 84 rb_dlinkDestroy(nptr, varlist);
637c4932
VY
85 rb_free(tmp->name);
86 rb_free(tmp->value);
87 rb_free(tmp);
92fb5c31
AC
88 }
89}
90
91/*
92 * substitution_parse
93 *
330fc5c1 94 * Inputs - A markup string, rb_dlink-list of markup values
92fb5c31
AC
95 * Output - A string which has been markup-replaced.
96 * Side Effects - Strings larger than BUFSIZE are terminated.
97 */
330fc5c1 98char *substitution_parse(const char *fmt, rb_dlink_list *varlist)
92fb5c31
AC
99{
100 static char buf[BUFSIZE];
101 const char *ptr;
102 char *bptr = buf;
103
42124941
SA
104 for (ptr = fmt; *ptr != '\0' && bptr - buf < BUFSIZE; ptr++) {
105 if (*ptr != '$') {
92fb5c31 106 *bptr++ = *ptr;
42124941
SA
107 } else if (*(ptr + 1) == '{') {
108 char varname[BUFSIZE] = { 0 };
92fb5c31 109 char *vptr = varname;
330fc5c1 110 rb_dlink_node *nptr;
92fb5c31 111
92fb5c31 112 /* break out ${var} */
42124941
SA
113 for (ptr += 2; *ptr != '\0'; ptr++) {
114 if (*ptr == '$') {
115 ptr--;
1dfb0808 116 break;
42124941 117 } else if (*ptr == '}') {
92fb5c31 118 break;
42124941
SA
119 } else if (vptr < &varname[sizeof(varname) - 1]) {
120 *vptr++ = *ptr;
92fb5c31
AC
121 }
122 }
123
42124941 124 RB_DLINK_FOREACH(nptr, varlist->head) {
92fb5c31
AC
125 struct substitution_variable *val = (struct substitution_variable *) nptr->data;
126
42124941 127 if (!rb_strcasecmp(varname, val->name)) {
fb81421f 128 rb_strlcpy(bptr, val->value, sizeof(buf) - (bptr - buf));
92fb5c31 129 bptr += strlen(val->value);
fb81421f
SA
130 if (bptr >= &buf[sizeof(buf)]) {
131 bptr = &buf[sizeof(buf) - 1];
132 }
92fb5c31
AC
133 break;
134 }
135 }
1dfb0808
SA
136
137 /* don't increment ptr into a following string if the '}' is missing */
138 if (*ptr == '\0')
139 break;
92fb5c31 140 }
42124941 141 }
92fb5c31
AC
142
143 *bptr = '\0';
144 return buf;
145}