Subversion Repositories tpanel

Rev

Rev 446 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tprjresources.h"
459 andreas 20
#include "tbytearray.h"
446 andreas 21
#include "terror.h"
22
 
23
using std::string;
24
using std::vector;
25
 
26
TPrjResources::TPrjResources(vector<RESOURCE_LIST_T>& list)
27
        : mResources(list)
28
{
29
    DECL_TRACER("TPrjResources::TPrjResources(vector<RESOURCE_LIST_T>& list)");
30
}
31
 
32
TPrjResources::~TPrjResources()
33
{
34
    DECL_TRACER("TPrjResources::~TPrjResources()");
35
}
36
 
37
RESOURCE_LIST_T TPrjResources::findResourceType(const std::string& type)
38
{
39
    DECL_TRACER("TPrjResources::findResourceType(const std::string& type)");
40
 
41
    if (mResources.size() == 0)
42
        return RESOURCE_LIST_T();
43
 
44
    vector<RESOURCE_LIST_T>::iterator iter;
45
 
46
    for (iter = mResources.begin(); iter != mResources.end(); iter++)
47
    {
48
        if (iter->type.compare(type) == 0)
49
            return *iter;
50
    }
51
 
52
    return RESOURCE_LIST_T();
53
}
54
 
55
RESOURCE_T TPrjResources::findResource(int idx, const std::string& name)
56
{
57
    DECL_TRACER("TPrjResources::findResource(int idx, const std::string& name)");
58
 
59
    if (idx < 1 || mResources.size() < (size_t)idx)
60
    {
61
        MSG_ERROR("Invalid index " << idx << "!");
62
        return RESOURCE_T();
63
    }
64
 
65
    RESOURCE_LIST_T list = mResources[idx-1];
66
 
67
    if (list.type.empty())
68
    {
69
        MSG_ERROR("Resource list " << idx << " is empty!");
70
        return RESOURCE_T();
71
    }
72
 
73
    vector<RESOURCE_T>::iterator iter;
74
 
75
    for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
76
    {
77
        MSG_DEBUG("Resource: " << iter->name);
78
        if (iter->name.compare(name) == 0)
79
            return *iter;
80
    }
81
 
82
    MSG_WARNING("Resource " << name << " not found!");
83
    return RESOURCE_T();
84
}
85
 
86
RESOURCE_T TPrjResources::findResource(const std::string& type, const std::string& name)
87
{
88
    DECL_TRACER("TPrjResources::findResource(const std::string& type, const std::string& name)");
89
 
90
    RESOURCE_LIST_T list = findResourceType(type);
91
 
92
    if (list.type.empty())
93
        return RESOURCE_T();
94
 
95
    vector<RESOURCE_T>::iterator iter;
96
 
97
    for (iter = list.ressource.begin(); iter != list.ressource.end(); iter++)
98
    {
99
        if (iter->name.compare(name) == 0)
100
            return *iter;
101
    }
102
 
103
    return RESOURCE_T();
104
}
105
 
106
RESOURCE_T TPrjResources::findResource(const std::string& name)
107
{
108
    DECL_TRACER("TPrjResources::findResource(const std::string& name)");
109
 
110
    if (mResources.size() == 0 || mResources[0].ressource.size() == 0)
111
        return RESOURCE_T();
112
 
113
    vector<RESOURCE_T> list = mResources[0].ressource;
114
    vector<RESOURCE_T>::iterator iter;
115
 
116
    for (iter = list.begin(); iter != list.end(); iter++)
117
    {
118
        if (iter->name.compare(name) == 0)
119
            return *iter;
120
    }
121
 
122
    return RESOURCE_T();
123
}
124
 
125
size_t TPrjResources::getResourceIndex(const string& type)
126
{
127
    DECL_TRACER("TPrjResources::getResourceIndex(const string& type)");
128
 
129
    if (mResources.size() == 0)
130
        return npos;
131
 
132
    vector<RESOURCE_LIST_T>::iterator iter;
133
    size_t idx = 0;
134
 
135
    for (iter = mResources.begin(); iter != mResources.end(); iter++)
136
    {
137
        idx++;
138
 
139
        if (iter->type.compare(type) == 0)
140
            return idx;
141
    }
142
 
143
    return npos;
144
}
145
 
146
bool TPrjResources::setResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)
147
{
148
    DECL_TRACER("TPrjResources::setResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)");
149
 
150
    if (mResources.size() == 0)
151
        return false;
152
 
153
    vector<RESOURCE_LIST_T>::iterator itRlist;
154
 
155
    for (itRlist = mResources.begin(); itRlist != mResources.end(); ++itRlist)
156
    {
157
        if (itRlist->type.compare("image") == 0)
158
            break;
159
    }
160
 
161
    if (itRlist == mResources.end())
162
    {
163
        MSG_ERROR("There was no resource type \"image\" found in the resources!");
164
        return false;
165
    }
166
 
167
    vector<RESOURCE_T> list = itRlist->ressource;
168
    vector<RESOURCE_T>::iterator iter;
169
 
170
    for (iter = list.begin(); iter != list.end(); iter++)
171
    {
172
        if (iter->name.compare(name) == 0)
173
        {
174
            RESOURCE_T res = *iter;
175
 
176
            if (!scheme.empty())
177
                res.protocol = scheme;
178
 
179
            if (!host.empty())
180
                res.host = host;
181
 
182
            if (!path.empty())
183
                res.path = path;
184
 
185
            if (!file.empty())
186
                res.file = file;
187
 
188
            if (!user.empty())
189
                res.user = user;
190
 
191
            if (!pw.empty())
192
                res.password = pw;
193
 
194
            if (refresh >= 0)
195
                res.refresh = refresh;
196
 
197
            list.erase(iter);
198
            list.push_back(res);
199
 
200
            itRlist->ressource.clear();
201
            itRlist->ressource = list;
202
            return true;
203
        }
204
    }
205
 
206
    return false;
207
}
208
 
209
bool TPrjResources::addResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)
210
{
211
    DECL_TRACER("TPrjResources::addResource(const string& name, const string& scheme, const string& host, const string& path, const string& file, const string& user, const string& pw, int refresh)");
212
 
213
    vector<RESOURCE_LIST_T>::iterator itRlist;
214
 
215
    if (mResources.size() == 0)
216
    {
217
        RESOURCE_LIST_T rl;
218
        rl.type = "image";
219
        mResources.push_back(rl);
220
        itRlist = mResources.begin();
221
    }
222
    else
223
    {
224
        // Find the resource container
225
        for (itRlist = mResources.begin(); itRlist != mResources.end(); ++itRlist)
226
        {
227
            if (itRlist->type.compare("image") == 0)
228
                break;
229
        }
230
    }
231
 
232
    if (itRlist == mResources.end())
233
    {
234
        MSG_ERROR("There is no resouce container called \"image\"!");
235
        return false;
236
    }
237
 
238
    RESOURCE_T r;
239
    r.name = name;
240
    r.protocol = scheme;
241
    r.host = host;
242
    r.path = path;
243
    r.file = file;
244
    r.user = user;
245
    r.password = pw;
246
    r.refresh = refresh;
247
 
248
    // Make sure the resource does not already exist
249
    if (itRlist->ressource.size() == 0)
250
    {
251
        itRlist->ressource.push_back(r);
252
        return true;
253
    }
254
 
255
    vector<RESOURCE_T>::iterator iter;
256
 
257
    for (iter = itRlist->ressource.begin(); iter != itRlist->ressource.end(); iter++)
258
    {
259
        if (iter->name == name)
260
        {
261
            iter->protocol = scheme;
262
            iter->host = host;
263
            iter->path = path;
264
            iter->file = file;
265
            iter->user = user;
266
            iter->password = pw;
267
            iter->refresh = refresh;
268
            return true;
269
        }
270
    }
271
 
272
    itRlist->ressource.push_back(r);
273
    return true;
274
}
459 andreas 275
 
276
string TPrjResources::decryptPassword(const string& pw)
277
{
278
    DECL_TRACER("TPrjResources::decryptPassword(const string& pw)");
279
 
280
    string erg;
281
    size_t len;
282
    unsigned char *bytes = TByteArray::hexStrToByte(pw, &len);
283
 
284
    if (bytes == nullptr || (len % 16) != 0)
285
        return erg;
286
 
287
    // TODO: Implement DES-CBC algorithm to decrypt
288
    erg.assign(reinterpret_cast<char *>(bytes), len);
289
    return erg;
290
}