1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use serde_yaml::{Mapping, Value};
use std::borrow::{Borrow, Cow};
use std::collections::VecDeque;
use std::io::Read;

#[derive(Debug)]
pub enum Error {
    IO(std::io::Error),
    SerDe(serde_yaml::Error),
    UTF8(std::str::Utf8Error),
    ParseInt(std::num::ParseIntError),
    ParseError,
    InvalidEscapeSequence,
    MissingArgument,
    MissingConfigValue(String),
}
impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Error::IO(e)
    }
}
impl From<std::str::Utf8Error> for Error {
    fn from(e: std::str::Utf8Error) -> Self {
        Error::UTF8(e)
    }
}
impl From<std::num::ParseIntError> for Error {
    fn from(e: std::num::ParseIntError) -> Self {
        Error::ParseInt(e)
    }
}
impl From<serde_yaml::Error> for Error {
    fn from(e: serde_yaml::Error) -> Self {
        Error::SerDe(e)
    }
}
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}
impl std::error::Error for Error {}

#[derive(Debug, Clone)]
pub struct EscapePattern<'a>(pub Cow<'a, [u8]>, pub Cow<'a, [u8]>);
impl Default for EscapePattern<'static> {
    fn default() -> Self {
        EscapePattern(b"{{"[..].into(), b"}}"[..].into())
    }
}
impl<'a> std::str::FromStr for EscapePattern<'a> {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut split = s.split("var");
        let res = EscapePattern(
            split
                .next()
                .ok_or(Error::InvalidEscapeSequence)?
                .as_bytes()
                .to_owned()
                .into(),
            split
                .next()
                .ok_or(Error::InvalidEscapeSequence)?
                .as_bytes()
                .to_owned()
                .into(),
        );
        if res.0 == res.1 {
            Err(Error::InvalidEscapeSequence)
        } else {
            Ok(res)
        }
    }
}
impl<'a> std::fmt::Display for EscapePattern<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}var{}",
            std::str::from_utf8(self.0.borrow()).unwrap(),
            std::str::from_utf8(self.1.borrow()).unwrap(),
        )
    }
}

pub fn get_val_from_config_seq(seq: &[Value], key: &str) -> Option<Value> {
    let mut seg_iter = key.splitn(2, ".");
    let seg = seg_iter.next()?;
    let val = seq.get(seg.parse::<usize>().ok()?)?;
    match (val, seg_iter.next()) {
        (Value::Mapping(m), Some(rest)) => get_val_from_config_map(&m, rest),
        (Value::Sequence(s), Some(rest)) => get_val_from_config_seq(s.as_slice(), rest),
        (Value::Null, _) => None,
        (_, None) => Some(val.clone()),
        _ => None,
    }
}

pub fn get_val_from_config_map(map: &Mapping, key: &str) -> Option<Value> {
    let mut seg_iter = key.splitn(2, ".");
    let seg = seg_iter.next()?;
    let val = map.get(&Value::String(seg.to_owned()))?;
    match (val, seg_iter.next()) {
        (Value::Mapping(m), Some(rest)) => get_val_from_config_map(&m, rest),
        (Value::Sequence(s), Some(rest)) => get_val_from_config_seq(s.as_slice(), rest),
        (Value::Null, _) => None,
        (_, None) => Some(val.clone()),
        _ => None,
    }
}

pub fn set_val_in_config_seq(seq: &mut [Value], key: &str, value: Value) -> Option<()> {
    let mut seg_iter = key.splitn(2, ".");
    let seg = seg_iter.next()?;
    let val = seq.get_mut(seg.parse::<usize>().ok()?)?;
    match (val, seg_iter.next()) {
        (Value::Mapping(m), Some(rest)) => set_val_in_config_map(m, rest, value),
        (Value::Sequence(s), Some(rest)) => set_val_in_config_seq(s.as_mut_slice(), rest, value),
        (val, None) => {
            *val = value;
            Some(())
        }
        _ => None,
    }
}

pub fn set_val_in_config_map(map: &mut Mapping, key: &str, value: Value) -> Option<()> {
    let mut seg_iter = key.splitn(2, ".");
    let seg = seg_iter.next()?;
    let val = map.get_mut(&Value::String(seg.to_owned()))?;
    match (val, seg_iter.next()) {
        (Value::Mapping(m), Some(rest)) => set_val_in_config_map(m, rest, value),
        (Value::Sequence(s), Some(rest)) => set_val_in_config_seq(s.as_mut_slice(), rest, value),
        (val, None) => {
            *val = value;
            Some(())
        }
        _ => None,
    }
}

pub fn val_to_string(val: Value) -> String {
    match val {
        Value::Bool(b) => format!("{}", b),
        Value::Mapping(_) => "{map}".to_owned(),
        Value::Null => "null".to_owned(),
        Value::Number(n) => format!("{}", n),
        Value::Sequence(_) => "[list]".to_owned(),
        Value::String(s) => s,
    }
}

pub fn val_is_truthy(val: &Value) -> bool {
    match val {
        Value::Bool(false) => false,
        Value::Null => false,
        _ => true,
    }
}

/// rpcpassword={{rpcpassword}}
/// {{#IF rpcauth
/// rpcuser={{rpcauth.rpcuser}}
/// rpcpassword={{rpcauth.rpcpassword}}
/// }}
/// {{#IF listen
/// listen=1
/// bind=0.0.0.0:8333
/// }}
/// {{#IFNOT listen
/// listen=0
/// }}
/// {{#FOREACH rpcallowip
/// rpcallowip={{rpcallowip}}
/// }}
pub fn eval(
    map: &Mapping,
    expr: &str,
    escape: &EscapePattern,
    unescape: u8,
) -> Result<String, Error> {
    let trimmed = expr.trim_start();
    if trimmed.starts_with("#IF ") {
        let mut split = trimmed[4..].splitn(2, "\n");
        let expr = split.next().ok_or_else(|| Error::ParseError)?.trim();
        let rest = split.next().ok_or_else(|| Error::ParseError)?;
        if expr.contains("!=") {
            let mut split = expr.splitn(2, "!=");
            let if_var = split.next().ok_or_else(|| Error::ParseError)?.trim();
            let target = split.next().ok_or_else(|| Error::ParseError)?.trim();
            match get_val_from_config_map(map, if_var) {
                Some(Value::String(ref s)) => {
                    if !target.starts_with("\"") || !target.ends_with("\"") {
                        eprintln!("{}", target);
                        return Err(Error::ParseError);
                    }
                    if format!("{:?}", s) != target {
                        let mut ret = String::new();
                        TemplatingReader::new(
                            std::io::Cursor::new(rest.as_bytes()),
                            map,
                            escape,
                            unescape,
                        )
                        .read_to_string(&mut ret)?;
                        Ok(ret)
                    } else {
                        Ok("".to_owned())
                    }
                }
                Some(Value::Number(n)) => {
                    if format!("{}", n) != target {
                        let mut ret = String::new();
                        TemplatingReader::new(
                            std::io::Cursor::new(rest.as_bytes()),
                            map,
                            escape,
                            unescape,
                        )
                        .read_to_string(&mut ret)?;
                        Ok(ret)
                    } else {
                        Ok("".to_owned())
                    }
                }
                _ => Ok("".to_owned()),
            }
        } else if expr.contains("=") {
            let mut split = expr.splitn(2, "=");
            let if_var = split.next().ok_or_else(|| Error::ParseError)?.trim();
            let target = split.next().ok_or_else(|| Error::ParseError)?.trim();
            match get_val_from_config_map(map, if_var) {
                Some(Value::String(ref s)) => {
                    if !target.starts_with("\"") || !target.ends_with("\"") {
                        eprintln!("{}", target);
                        return Err(Error::ParseError);
                    }
                    if format!("{:?}", s) == target {
                        let mut ret = String::new();
                        TemplatingReader::new(
                            std::io::Cursor::new(rest.as_bytes()),
                            map,
                            escape,
                            unescape,
                        )
                        .read_to_string(&mut ret)?;
                        Ok(ret)
                    } else {
                        Ok("".to_owned())
                    }
                }
                Some(Value::Number(n)) => {
                    if format!("{}", n) == target {
                        let mut ret = String::new();
                        TemplatingReader::new(
                            std::io::Cursor::new(rest.as_bytes()),
                            map,
                            escape,
                            unescape,
                        )
                        .read_to_string(&mut ret)?;
                        Ok(ret)
                    } else {
                        Ok("".to_owned())
                    }
                }
                _ => Ok("".to_owned()),
            }
        } else if expr.starts_with("!") {
            let if_var = &expr[1..];
            if !get_val_from_config_map(map, if_var)
                .map(|a| val_is_truthy(&a))
                .unwrap_or(false)
            {
                let mut ret = String::new();
                TemplatingReader::new(std::io::Cursor::new(rest.as_bytes()), map, escape, unescape)
                    .read_to_string(&mut ret)?;
                Ok(ret)
            } else {
                Ok("".to_owned())
            }
        } else {
            let if_var = expr;
            if get_val_from_config_map(map, if_var)
                .map(|a| val_is_truthy(&a))
                .unwrap_or(false)
            {
                let mut ret = String::new();
                TemplatingReader::new(std::io::Cursor::new(rest.as_bytes()), map, escape, unescape)
                    .read_to_string(&mut ret)?;
                Ok(ret)
            } else {
                Ok("".to_owned())
            }
        }
    } else if trimmed.starts_with("#FOREACH ") {
        let mut split = trimmed[9..].splitn(2, "\n");
        let for_var = split.next().ok_or_else(|| Error::ParseError)?.trim();
        let rest = split.next().ok_or_else(|| Error::ParseError)?;
        match get_val_from_config_map(map, for_var) {
            Some(Value::Sequence(s)) => {
                let mut ret = String::new();
                let mut new_map = map.clone();
                for item in s {
                    set_val_in_config_map(&mut new_map, for_var, item)
                        .ok_or_else(|| Error::MissingConfigValue(for_var.to_owned()))?;
                    TemplatingReader::new(
                        std::io::Cursor::new(rest.as_bytes()),
                        &new_map,
                        escape,
                        unescape,
                    )
                    .read_to_string(&mut ret)?;
                }
                Ok(ret)
            }
            Some(ref a) if val_is_truthy(a) => {
                let mut ret = String::new();
                TemplatingReader::new(std::io::Cursor::new(rest.as_bytes()), map, escape, unescape)
                    .read_to_string(&mut ret)?;
                Ok(ret)
            }
            _ => Ok("".to_owned()),
        }
    } else {
        get_val_from_config_map(map, trimmed)
            .map(val_to_string)
            .ok_or_else(|| Error::MissingConfigValue(trimmed.to_owned()))
    }
}

#[derive(Debug)]
pub struct TemplatingReader<'a, 'b, 'c, R: Read> {
    inner: R,
    mapping: &'a Mapping,
    escape: &'b EscapePattern<'c>,
    unescape: u8,
    unescapable: bool,
    count_start: usize,
    count_end: usize,
    depth: usize,
    var: Vec<u8>,
    buf: VecDeque<u8>,
}
impl<'a, 'b, 'c, R> TemplatingReader<'a, 'b, 'c, R>
where
    R: Read,
{
    pub fn new(
        reader: R,
        mapping: &'a Mapping,
        escape: &'b EscapePattern<'c>,
        unescape: u8,
    ) -> Self {
        TemplatingReader {
            inner: reader,
            mapping,
            escape,
            unescape,
            unescapable: false,
            count_start: 0,
            count_end: 0,
            depth: 0,
            var: Vec::new(),
            buf: VecDeque::new(),
        }
    }
}

fn to_io_error<E: std::error::Error + Send + Sync + 'static>(e: E) -> std::io::Error {
    std::io::Error::new(std::io::ErrorKind::Other, e)
}

impl<'a, 'b, 'c, R> Read for TemplatingReader<'a, 'b, 'c, R>
where
    R: Read,
{
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let mut written = 0;
        while written == 0 {
            let in_bytes = self.inner.read(buf)?;
            for byte in &buf[..in_bytes] {
                let mut write_byte = true;
                let byte_arr = [];
                let mut to_extend: &[u8] = &byte_arr;
                if self.unescapable && byte == &self.unescape {
                    self.depth -= 1;
                    self.count_start = 0;
                    to_extend = &*self.escape.0;
                    write_byte = false;
                }
                self.unescapable = false;
                if byte == &self.escape.0[self.count_start] {
                    self.count_start += 1;
                    if self.depth == 0 {
                        write_byte = false;
                    }
                } else if self.count_start != 0 {
                    to_extend = &self.escape.0[..self.count_start];
                    self.count_start = 0;
                }
                if self.depth > 0 && byte == &self.escape.1[self.count_end] {
                    self.count_end += 1;
                    if self.depth == 1 {
                        write_byte = false;
                    }
                } else if self.count_end != 0 {
                    to_extend = &self.escape.1[..self.count_end];
                    self.count_end = 0;
                }
                if self.count_start == self.escape.0.len() {
                    self.depth += 1;
                    self.count_start = 0;
                    self.unescapable = true;
                }
                if self.count_end == self.escape.0.len() {
                    self.depth -= 1;
                    self.count_end = 0;
                    if self.depth == 0 {
                        self.buf.extend(
                            eval(
                                self.mapping,
                                std::str::from_utf8(&self.var).map_err(to_io_error)?,
                                self.escape,
                                self.unescape,
                            )
                            .map_err(to_io_error)?
                            .as_bytes(),
                        );
                        self.var.clear();
                    }
                }
                if self.depth == 0 {
                    self.buf.extend(to_extend);
                    if write_byte {
                        self.buf.push_back(*byte);
                    }
                } else {
                    self.var.extend_from_slice(to_extend);
                    if write_byte {
                        self.var.push(*byte);
                    }
                }
            }
            written = std::cmp::min(buf.len(), self.buf.len());
            for (i, elem) in self.buf.drain(0..written).enumerate() {
                buf[i] = elem;
            }
            if in_bytes == 0 {
                break;
            }
        }
        Ok(written)
    }
}