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
use super::error::Error;
use super::spec;
use super::util;
use log::{error, warn};
use std::fmt;

const PASSWORD_REDACTED: &str = "REDACTED";

/// Fixed field with spec and value.
///
/// Since fixed fields have specific length requirements, a well-known
/// spec::FixedField is required
#[derive(PartialEq, Debug)]
pub struct FixedField {
    spec: &'static spec::FixedField,
    value: String,
}

impl FixedField {
    pub fn new(spec: &'static spec::FixedField, value: &str) -> Result<Self, Error> {
        if value.len() == spec.length {
            Ok(FixedField {
                spec,
                value: value.to_string(),
            })
        } else {
            Err(Error::FixedFieldLengthError)
        }
    }

    pub fn spec(&self) -> &'static spec::FixedField {
        self.spec
    }

    pub fn value(&self) -> &str {
        &self.value
    }

    pub fn set_value(&mut self, value: &str) -> Result<(), Error> {
        if value.len() == self.spec.length {
            self.value = value.to_string();
            Ok(())
        } else {
            Err(Error::FixedFieldLengthError)
        }
    }

    /// Translate a FixedField into a string which can be inserted into
    /// a SIP message.
    ///
    /// ```
    /// use sip2::FixedField;
    /// use sip2::spec;
    /// let ff = FixedField::new(&spec::FF_MAX_PRINT_WIDTH, "999").unwrap();
    /// assert_eq!(ff.to_sip(), "999");
    /// ```
    pub fn to_sip(&self) -> String {
        util::sip_string(&self.value)
    }
}

/// SIP Field with code and value.
///
/// To support passing field types that are not known at compile time,
/// store the message code instead of a ref to a well-known spec::Field.
#[derive(PartialEq, Debug)]
pub struct Field {
    /// 2-character code
    // Note we could link to the static spec::Field here, like
    // FixedField, instead of storing a copy of the code/label, but that
    // won't work with fields which are unknown until runtime.
    code: String,

    /// Field value
    value: String,
}

impl Field {
    pub fn new(code: &str, value: &str) -> Self {
        Field {
            code: code.to_string(),
            value: value.to_string(),
        }
    }

    /// value getter
    pub fn value(&self) -> &str {
        &self.value
    }

    pub fn set_value(&mut self, value: &str) {
        self.value = value.to_string();
    }

    /// code getter
    pub fn code(&self) -> &str {
        &self.code
    }

    /// Create a SIP string from a field
    ///
    /// String includes the trailing "|" delimiter.
    ///
    /// ```
    /// use sip2::Field;
    /// use sip2::spec;
    /// let f = Field::new(spec::F_LOGIN_UID.code, "sip_username");
    /// assert_eq!(f.to_sip(), "CNsip_username|");
    /// ```
    pub fn to_sip(&self) -> String {
        self.code.to_string() + &util::sip_string(&self.value) + &String::from("|")
    }
}

/// SIP message complete with message code, fixed fields, and fields.
#[derive(PartialEq, Debug)]
pub struct Message {
    /// Link to the specification for this message type
    spec: &'static spec::Message,

    /// List of fixed fields
    fixed_fields: Vec<FixedField>,

    /// List of fields
    fields: Vec<Field>,
}

impl Message {
    pub fn new(
        spec: &'static spec::Message,
        fixed_fields: Vec<FixedField>,
        fields: Vec<Field>,
    ) -> Self {
        let mut msg = Message {
            spec,
            fixed_fields,
            fields,
        };

        // Sorting fields allows for consistent message layout,
        // which is useful for debugging purposes.
        msg.sort_fields();

        msg
    }

    pub fn from_code(msg_code: &str) -> Result<Message, Error> {
        Message::from_ff_values(msg_code, &[])
    }

    /// Creates a new message from a set of fixed field values.
    ///
    /// Returns an error if the fixed field values provided are not
    /// the correct length for the specified message type.
    pub fn from_ff_values(msg_code: &str, fixed_fields: &[&str]) -> Result<Message, Error> {
        let msg_spec = match spec::Message::from_code(msg_code) {
            Some(s) => s,
            None => {
                log::error!("Unknown message code: {msg_code}");
                return Err(Error::UnknownMessageError);
            }
        };

        let mut ff: Vec<FixedField> = Vec::new();

        for (idx, ff_spec) in msg_spec.fixed_fields.iter().enumerate() {
            if let Some(v) = fixed_fields.get(idx) {
                ff.push(FixedField::new(ff_spec, v)?);
            }
        }

        if ff.len() != msg_spec.fixed_fields.len() {
            log::warn!(
                "SIP message {} contains incorrect number of fixed fields",
                msg_spec.code
            );
            return Err(Error::MessageFormatError);
        }

        Ok(Message {
            spec: msg_spec,
            fixed_fields: ff,
            fields: Vec::new(),
        })
    }

    /// Create a new message from a list of fixed field and field string values.
    pub fn from_values(
        msg_code: &str,
        fixed_fields: &[&str],
        fields: &[(&str, &str)],
    ) -> Result<Message, Error> {
        let mut msg = Message::from_ff_values(msg_code, fixed_fields)?;
        for field in fields {
            msg.add_field(field.0, field.1);
        }
        Ok(msg)
    }

    /// Keep fields sorted for consistent to_sip output.
    fn sort_fields(&mut self) {
        self.fields.sort_by(|a, b| a.code.cmp(&b.code));
    }

    /// Adds a Field to a message.
    ///
    /// ```
    /// use sip2::{Message, Field};
    /// use sip2::spec;
    ///
    /// let mut msg = Message::new(
    ///     &spec::M_LOGIN,
    ///     vec![],
    ///     vec![],
    /// );
    ///
    /// msg.add_field("ZZ", "ZZ is a value");
    /// assert_eq!(msg.fields()[0].code(), "ZZ");
    /// ```
    pub fn add_field(&mut self, code: &str, value: &str) {
        self.fields.push(Field::new(code, value));
        self.sort_fields();
    }

    /// Adds a field to a SIP message if the provided value is not None.
    pub fn maybe_add_field(&mut self, code: &str, value: Option<&str>) {
        if let Some(v) = value {
            self.fields.push(Field::new(code, v));
            self.sort_fields();
        }
    }

    /// Remove a field by its code.  If 'all' is true, remove all occurrences.
    pub fn remove_field(&mut self, code: &str, all: bool) -> usize {
        let mut count: usize = 0;

        loop {
            let pos = match self.fields.iter().position(|f| f.code().eq(code)) {
                Some(p) => p,
                None => return count, // got them all
            };

            self.fields.remove(pos);

            count += 1;

            if !all {
                return count;
            }
        }
    }

    /// Return the first value with the specified field code.
    pub fn get_field_value(&self, code: &str) -> Option<&str> {
        if let Some(f) = self.fields().iter().find(|f| f.code() == code) {
            Some(f.value.as_str())
        } else {
            None
        }
    }

    pub fn spec(&self) -> &'static spec::Message {
        self.spec
    }

    pub fn fields(&self) -> &Vec<Field> {
        &self.fields
    }

    pub fn fields_mut(&mut self) -> &mut Vec<Field> {
        &mut self.fields
    }

    pub fn fixed_fields(&self) -> &Vec<FixedField> {
        &self.fixed_fields
    }

    pub fn fixed_fields_mut(&mut self) -> &mut Vec<FixedField> {
        &mut self.fixed_fields
    }

    /// Create a SIP string of a message.
    ///
    /// ```
    /// use sip2::{Message, Field, FixedField};
    /// use sip2::spec;
    ///
    /// let msg = Message::new(
    ///     &spec::M_LOGIN,
    ///     vec![
    ///         FixedField::new(&spec::FF_UID_ALGO, "0").unwrap(),
    ///         FixedField::new(&spec::FF_PWD_ALGO, "0").unwrap(),
    ///     ],
    ///     vec![
    ///         Field::new(spec::F_LOGIN_UID.code, "sip_username"),
    ///         Field::new(spec::F_LOGIN_PWD.code, "sip_password"),
    ///     ]
    /// );
    ///
    /// assert_eq!(msg.to_sip(), "9300CNsip_username|COsip_password|");
    /// ```
    pub fn to_sip(&self) -> String {
        let mut s = self.spec.code.to_string();

        for ff in self.fixed_fields.iter() {
            s.push_str(&ff.to_sip());
        }

        for f in self.fields.iter() {
            s.push_str(&f.to_sip());
        }

        s
    }

    /// Same as to_sip() but replaces the patron password 'AD' value
    /// with redacted text.
    ///
    /// Useful for logging.
    pub fn to_sip_redacted(&self) -> String {
        let mut s = self.spec.code.to_string();

        for ff in self.fixed_fields.iter() {
            s.push_str(&ff.to_sip());
        }

        for f in self.fields.iter() {
            if f.code() == spec::F_PATRON_PWD.code {
                s += f.code();
                s += PASSWORD_REDACTED;
                s += "|";
            } else {
                s.push_str(&f.to_sip());
            }
        }

        s
    }

    /// Turns a SIP string into a Message
    ///
    /// Assumes the trailing message terminator character has been removed.
    ///
    /// Message types and Fixed Field types must be known in advance
    /// (see sip2::spec), but Field's do not necessarily have to match
    /// a known spec::Field.  Any value of 3 or more characters will be
    /// treated as a valid field.
    ///
    /// ```
    /// use sip2::{Message, Field, FixedField};
    /// let sip_text = "9300CNsip_username|COsip_password|";
    /// let msg = Message::from_sip(sip_text).unwrap();
    /// assert_eq!(msg.spec().code, "93");
    /// assert_eq!(msg.fields()[0].code(), "CN");
    /// assert_eq!(msg.fields()[1].value(), "sip_password");
    /// ```
    pub fn from_sip(text: &str) -> Result<Message, Error> {
        if text.len() < 2 {
            log::warn!("SIP message is incomplete: {text}");
            return Err(Error::MessageFormatError);
        }

        let msg_spec = match spec::Message::from_code(&text[0..2]) {
            Some(m) => m,
            None => {
                // Message spec must match a known value.
                error!("Unknown message type: {}", &text[0..2]);
                return Err(Error::MessageFormatError);
            }
        };

        let mut msg = Message {
            spec: msg_spec,
            fixed_fields: vec![],
            fields: vec![],
        };

        // Remove the message code
        let mut msg_text = &text[2..];

        for ff_spec in msg_spec.fixed_fields.iter() {
            if msg_text.len() < ff_spec.length {
                // Fixed Fields must match known values.

                warn!(
                    "Message has invalid fixed field: {} : {}",
                    ff_spec.label, msg_text
                );
                return Err(Error::MessageFormatError);
            }

            let value = &msg_text[0..ff_spec.length];
            msg_text = &msg_text[ff_spec.length..];

            // unwrap() is OK because we have confirmed the value has
            // the correct length above.
            msg.fixed_fields
                .push(FixedField::new(ff_spec, value).unwrap());
        }

        // Not all messages have fixed fields and/or fields
        if msg_text.is_empty() {
            return Ok(msg);
        }

        for part in msg_text.split('|') {
            if part.len() > 1 {
                let val = match part.len() > 2 {
                    true => &part[2..],
                    _ => "",
                };
                msg.fields.push(Field::new(&part[0..2], val));
            }
        }

        Ok(msg)
    }
}

/// Message display support for logging / debugging.
impl fmt::Display for Message {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{} {}", self.spec.code, self.spec.label)?;

        for ff in self.fixed_fields.iter() {
            writeln!(f, "   {:.<35} {}", ff.spec.label, ff.value)?;
        }

        for field in self.fields.iter() {
            if let Some(spec) = spec::Field::from_code(&field.code) {
                writeln!(f, "{} {:.<35} {}", spec.code, spec.label, field.value)?;
            } else {
                writeln!(f, "{} {:.<35} {}", field.code, "custom", field.value)?;
            }
        }

        write!(f, "")
    }
}