summaryrefslogtreecommitdiff
path: root/libkolabxml/lib/utils.cpp
blob: fb5f10bb2fa356e6dea66c098d40366cb6695dc0 (plain)
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
/*
 * Copyright (C) 2011  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "utils.h"
#include <iostream>
#include <string>

#include <boost/version.hpp>

#if BOOST_VERSION >= 104200
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#else
#include <uuid/uuid.h>
#endif

#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <time.h>
#include "base64.h"
#include "uricode.h"

namespace Kolab {
    
namespace Utils {

/**
 * We have to store our global variables thread-local to ensure thread-safety.
 */
struct Global {
    std::string createdUID;
    std::string productId;
    std::string xKolabVersion;
    
    ErrorSeverity errorBit;
    std::string errorMessage;
};

boost::thread_specific_ptr<Global> ptr;
class ThreadLocal
{
public:
    static Global &inst()
    {
        Global *t = ptr.get();
//         std::cout << "inst " << boost::this_thread::get_id() << std::endl;
        if (!t) {
//             std::cout << "initialize Global" << std::endl;
            t = new Global();
            ptr.reset(t);
        }
        return *t;
    }
};

void setKolabVersion(const std::string &s)
{
    ThreadLocal::inst().xKolabVersion = s;
}

std::string kolabVersion()
{
    return ThreadLocal::inst().xKolabVersion;
}

void setProductId(const std::string &s)
{
    ThreadLocal::inst().productId = s;
}

std::string productId()
{
    return ThreadLocal::inst().productId;
}


void setCreatedUid(const std::string &s)
{
    ThreadLocal::inst().createdUID = s;
}

std::string createdUid()
{
    return ThreadLocal::inst().createdUID;
}


std::string getUID(const std::string &s)
{
    if (s.empty()) {
#if BOOST_VERSION >= 104200
        boost::uuids::uuid u; // initialize uuid
        return boost::uuids::to_string(u);
#else
        uuid_t u;
        uuid_generate(u);
        char out[37];
        uuid_unparse(u,out);
        return std::string(out, 36); //We don't need the terminating \0
#endif
    }
    return s;
}

cDateTime getCurrentTime()
{
    time_t rawtime;
    struct tm * ptm;
    time(&rawtime);
    ptm = gmtime(&rawtime);
    return cDateTime(ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, true);
}


void logMessage(const std::string &m, ErrorSeverity s)
{
    switch (s) {
        case NoError:
            std::cout << "Debug: " << m << std::endl;
            break;
        case Warning:
            std::cerr << "Warning: " << m << std::endl;
            break;
        case Error:
            std::cerr << "Error: " << m << std::endl;
            if (ThreadLocal::inst().errorBit < Error) {
                ThreadLocal::inst().errorBit = Error;
                ThreadLocal::inst().errorMessage = m;
            }
            break;
        case Critical:
        default:
            std::cerr << "Critical: " << m << std::endl;
            if (ThreadLocal::inst().errorBit < Critical) {
                ThreadLocal::inst().errorBit = Critical;
                ThreadLocal::inst().errorMessage = m;
            }
    }
}

void logMessage(const std::string &message, const std::string &file, int line, ErrorSeverity s)
{
   logMessage(file+" "+boost::lexical_cast<std::string>(line)+": " + "  " + message, s);
}

void clearErrors()
{
    ThreadLocal::inst().errorBit = NoError;
    ThreadLocal::inst().errorMessage.clear();
}

ErrorSeverity getError()
{
    return ThreadLocal::inst().errorBit;
}

std::string getErrorMessage()
{
    return ThreadLocal::inst().errorMessage;
}


std::string uriInlineEncoding(const std::string &s, const std::string &mimetype)
{
    return std::string("data:")+mimetype+std::string(";base64,")+base64_encode(reinterpret_cast<const unsigned char*>(s.c_str()), s.length());
}

std::string uriInlineDecoding(const std::string &s, std::string &mimetype)
{
    if (s.substr(0, 5) != std::string("data:")) {
        ERROR("wrong picture encoding");
        std::cout << s <<  "      " << s.substr(0, 5) << std::endl;
        return std::string();
    }
    std::size_t pos = s.find(";",5);
    if (pos == std::string::npos) {
        ERROR("wrong picture encoding");
        std::cout << s <<  "      " << s.substr(0, 5) << std::endl;
        return std::string();
    }
    mimetype = s.substr(5, pos-5);
    if (s.substr(pos+1, 6) != std::string("base64")) {
        ERROR("wrong picture encoding");
        std::cout << s <<  "      " << s.substr(pos+1, 6) << std::endl;
        return std::string();
    }
    return base64_decode(s.substr(pos+8, s.size()-(pos+8)));
}

std::string toMailto(const std::string &email, const std::string &name)
{
    std::string mailto;
    if (!name.empty()) {
        mailto.append(name);
    }
    mailto.append("<");
    mailto.append(email);
    mailto.append(">");
    return std::string("mailto:")+UriEncode(mailto);
}

std::string fromMailto(const std::string &mailtoUri, std::string &name)
{
    const std::string &decoded = UriDecode(mailtoUri);
    if (decoded.substr(0, 7).compare("mailto:")) {
        WARNING("no mailto address");
        std::cout << decoded << std::endl;
        return mailtoUri;
    }
    std::size_t begin = decoded.find('<',7);
    if (begin == std::string::npos) {
        WARNING("no mailto address");
        std::cout << decoded << std::endl;
        return mailtoUri;
    }
    std::size_t end = decoded.find('>', begin);
    if (end == std::string::npos) {
        WARNING("no mailto address");
        std::cout << decoded << std::endl;
        return mailtoUri;
    }
    name = decoded.substr(7, begin-7);
    const std::string &email = decoded.substr(begin+1, end-begin-1);
    return email;
}

std::string fromMailto(const std::string &mailtoUri)
{
    std::string n;
    return fromMailto(mailtoUri, n);
}

} //Namespace

} //Namespace