Merge branch 'master' of feistymeow.org:feisty_meow
[feisty_meow.git] / octopi / library / tentacles / security_infoton.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : security_infoton                                                  *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 2002-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "security_infoton.h"
16
17 #include <basis/byte_array.h>
18 #include <basis/functions.h>
19 #include <basis/mutex.h>
20 #include <structures/string_array.h>
21 #include <structures/static_memory_gremlin.h>
22 #include <octopus/tentacle.h>
23
24 using namespace basis;
25 using namespace structures;
26 //using namespace textual;
27
28 namespace octopi {
29
30 security_infoton::security_infoton()
31 : infoton(security_classifier()),
32   _mode(LI_LOGIN),
33   _success(tentacle::NOT_FOUND),
34   _verification(new byte_array)
35 {}
36
37 security_infoton::security_infoton(login_modes mode, const outcome &success,
38     const byte_array &verification)
39 : infoton(security_classifier()),
40   _mode(mode),
41   _success(success),
42   _verification(new byte_array(verification))
43 {}
44
45 security_infoton::security_infoton(const security_infoton &to_copy)
46 : root_object(),
47   infoton(to_copy),
48   _mode(to_copy._mode),
49   _success(to_copy._success),
50   _verification(new byte_array(*to_copy._verification))
51 {
52 }
53
54 security_infoton::~security_infoton()
55 { WHACK(_verification); }
56
57 clonable *security_infoton::clone() const
58 { return cloner<security_infoton>(*this); }
59
60 security_infoton &security_infoton::operator =(const security_infoton &to_copy)
61 {
62   if (this == &to_copy) return *this;
63   set_classifier(to_copy.classifier());
64   _mode = to_copy._mode;
65   _success = to_copy._success;
66   *_verification = *to_copy._verification;
67   return *this;
68 }
69
70 const byte_array &security_infoton::verification() const
71 { return *_verification; }
72
73 byte_array &security_infoton::verification() { return *_verification; }
74
75 const astring login_classifier[] = { "#octsec" };
76
77 SAFE_STATIC_CONST(string_array, security_infoton::security_classifier,
78     (1, login_classifier))
79
80 int security_infoton::packed_size() const
81 {
82   return sizeof(_mode)
83       + sizeof(int)  // packed outcome.
84       + _verification->length() + sizeof(int);
85 }
86
87 void security_infoton::pack(byte_array &packed_form) const
88 {
89   structures::attach(packed_form, int(_mode));
90   attach(packed_form, _success.value());
91   structures::attach(packed_form, *_verification);
92 }
93
94 bool security_infoton::unpack(byte_array &packed_form)
95 {
96   int int_hold;
97   if (!structures::detach(packed_form, int_hold)) return false;
98   _mode = login_modes(int_hold);
99   int value;
100   if (!detach(packed_form, value)) return false;
101   _success = outcome(value);
102   if (!structures::detach(packed_form, *_verification)) return false;
103   return true;
104 }
105
106 } //namespace.
107