pEp_utility.cpp
author Markus Schaber <markus@pep-security.net>
Tue, 04 Oct 2016 23:07:42 +0200
branchkeysync
changeset 178 9f30e2a5f72c
parent 177 4d197f1c3abb
child 180 51f843f18dd2
permissions -rw-r--r--
COM-30 COM-24: Refactorings
Drop unused enum.
vb@10
     1
#include "stdafx.h"
vb@33
     2
#include "pEp_utility.h"
vb@33
     3
vb@33
     4
using namespace ATL;
vb@10
     5
vb@10
     6
namespace pEp {
vb@10
     7
    namespace utility {
vb@10
     8
        pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
vb@10
     9
            : me(false)
vb@10
    10
        {
vb@10
    11
            if (_ident->address)
vb@10
    12
                address = _ident->address;
vb@10
    13
            if (_ident->fpr)
vb@10
    14
                fpr = _ident->fpr;
vb@10
    15
            if (_ident->user_id)
vb@10
    16
                user_id = _ident->user_id;
vb@10
    17
            if (_ident->username)
vb@10
    18
                username = _ident->username;
markus@172
    19
            comm_type = (pEpComType) _ident->comm_type;
vb@10
    20
            lang = _ident->lang;
vb@10
    21
        }
vb@10
    22
markus@172
    23
        pEp_identity_cpp::pEp_identity_cpp(const pEpIdentity *_ident)
vb@10
    24
            : me(false)
vb@10
    25
        {
vb@10
    26
            if (_ident->address)
vb@10
    27
                address = utf8_string(_ident->address);
vb@10
    28
            if (_ident->fpr)
vb@10
    29
                fpr = utf8_string(_ident->fpr);
markus@177
    30
            if (_ident->userId)
markus@177
    31
                user_id = utf8_string(_ident->userId);
vb@10
    32
            if (_ident->username)
vb@10
    33
                username = utf8_string(_ident->username);
markus@177
    34
            comm_type = _ident->commType;
vb@10
    35
            if (_ident->lang)
vb@10
    36
                lang = utf8_string(_ident->lang);
vb@10
    37
        }
vb@10
    38
vb@10
    39
        pEp_identity * pEp_identity_cpp::to_pEp_identity()
vb@10
    40
        {
vb@10
    41
            ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
vb@10
    42
            assert(_ident);
vb@10
    43
            if (_ident == NULL)
vb@10
    44
                throw bad_alloc();
vb@10
    45
vb@10
    46
            _ident->comm_type = (::PEP_comm_type) this->comm_type;
vb@10
    47
            _ident->me = this->me;
vb@10
    48
vb@10
    49
            assert(this->lang.size() == 0 || this->lang.size() == 2);
vb@10
    50
vb@10
    51
            if (this->lang.size()) {
vb@10
    52
                _ident->lang[0] = this->lang[0];
vb@10
    53
                _ident->lang[1] = this->lang[1];
vb@10
    54
            }
vb@10
    55
vb@10
    56
            return _ident;
vb@10
    57
        }
vb@10
    58
markus@172
    59
        pEpIdentity * pEp_identity_cpp::to_pEp_identity_s()
vb@10
    60
        {
markus@172
    61
            pEpIdentity *_ident = (pEpIdentity *) calloc(1, sizeof(pEpIdentity));
vb@10
    62
            assert(_ident);
vb@10
    63
            if (_ident == NULL)
vb@10
    64
                throw bad_alloc();
vb@10
    65
vb@40
    66
            _ident->address = utf16_bstr(this->address);
markus@177
    67
            _ident->commType = this->comm_type;
vb@40
    68
            _ident->fpr = utf16_bstr(this->fpr);
vb@40
    69
            _ident->lang = utf16_bstr(this->lang);
vb@40
    70
            _ident->username = utf16_bstr(this->username);
markus@177
    71
            _ident->userId = utf16_bstr(this->user_id);
vb@10
    72
vb@10
    73
            return _ident;
vb@10
    74
        }
vb@10
    75
markus@172
    76
        void copy_identity(pEpIdentity * ident_s, const pEp_identity * ident)
vb@10
    77
        {
vb@10
    78
            assert(ident_s);
vb@10
    79
markus@172
    80
            ::memset(ident_s, 0, sizeof(pEpIdentity));
vb@10
    81
            if (ident) {
vb@10
    82
                if (ident->address)
vb@40
    83
                    ident_s->address = utf16_bstr(ident->address);
vb@10
    84
                if (ident->fpr)
vb@40
    85
                    ident_s->fpr = utf16_bstr(ident->fpr);
vb@10
    86
                if (ident->user_id)
markus@177
    87
                    ident_s->userId = utf16_bstr(ident->user_id);
vb@10
    88
                if (ident->username)
vb@40
    89
                    ident_s->username = utf16_bstr(ident->username);
markus@177
    90
                ident_s->commType = (pEpComType) ident->comm_type;
vb@10
    91
                if (ident->lang)
vb@40
    92
                    ident_s->lang = utf16_bstr(ident->lang);
vb@10
    93
            }
vb@10
    94
        }
vb@10
    95
markus@172
    96
        ::pEp_identity *new_identity(const pEpIdentity * ident)
vb@10
    97
        {
vb@37
    98
            if (ident == NULL)
vb@37
    99
                return NULL;
vb@37
   100
vb@10
   101
            ::pEp_identity *_ident;
vb@10
   102
vb@10
   103
            string _address;
vb@10
   104
            string _fpr;
vb@10
   105
            string _user_id;
vb@10
   106
            string _username;
vb@10
   107
vb@10
   108
            if (ident->address)
vb@10
   109
                _address = utf8_string(ident->address);
vb@10
   110
            if (ident->fpr) {
vb@10
   111
                _fpr = utf8_string(ident->fpr);
vb@10
   112
                for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
vb@10
   113
                    if (*p >= 'A' && *p <= 'Z')
vb@10
   114
                        continue;
vb@10
   115
                    if (*p >= '0' && *p <= '9')
vb@10
   116
                        continue;
vb@10
   117
                    throw invalid_argument("invalid hex digits in fingerprint");
vb@10
   118
                }
vb@10
   119
            }
markus@177
   120
            if (ident->userId)
markus@177
   121
                _user_id = utf8_string(ident->userId);
vb@10
   122
            if (ident->username)
vb@10
   123
                _username = utf8_string(ident->username);
vb@10
   124
vb@10
   125
            _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
vb@10
   126
            assert(_ident);
vb@10
   127
            if (_ident == NULL)
vb@10
   128
                throw bad_alloc();
vb@10
   129
markus@177
   130
            _ident->comm_type = (PEP_comm_type) ident->commType;
vb@10
   131
vb@10
   132
            if (ident->lang) {
vb@10
   133
                string _lang = utf8_string(ident->lang);
vb@10
   134
                if (_lang.length() != 0) {
vb@10
   135
                    if (_lang.length() != 2) {
vb@10
   136
                        ::free_identity(_ident);
vb@10
   137
                        throw invalid_argument("invalid language code");
vb@10
   138
                    }
vb@10
   139
                    if (_lang[0] < 'a' || _lang[0] > 'z') {
vb@10
   140
                        ::free_identity(_ident);
vb@10
   141
                        throw invalid_argument("invalid language code");
vb@10
   142
                    }
vb@10
   143
                    if (_lang[1] < 'a' || _lang[1] > 'z') {
vb@10
   144
                        ::free_identity(_ident);
vb@10
   145
                        throw invalid_argument("invalid language code");
vb@10
   146
                    }
vb@10
   147
                    _ident->lang[0] = _lang[0];
vb@10
   148
                    _ident->lang[1] = _lang[1];
vb@10
   149
                }
vb@10
   150
            }
vb@10
   151
vb@10
   152
            return _ident;
vb@10
   153
        }
vb@33
   154
vb@33
   155
        template< class T2, class T > T2 from_C(T *tl);
vb@33
   156
vb@41
   157
        BSTR bstr(char *s)
vb@33
   158
        {
vb@42
   159
            if (s == NULL)
vb@33
   160
                return _bstr_t(L"").Detach();
vb@42
   161
vb@42
   162
            return utf16_bstr(s);
vb@33
   163
        }
vb@33
   164
markus@172
   165
        template<> Blob *from_C< Blob *, bloblist_t >(bloblist_t *tl)
vb@33
   166
        {
vb@33
   167
            CComSafeArray<BYTE> sa;
vb@33
   168
            sa.Create(tl->size);
vb@33
   169
vb@136
   170
            if (tl->size) {
vb@136
   171
                char *data;
vb@136
   172
                SafeArrayAccessData(sa, (void **) &data);
vb@136
   173
                memcpy(data, tl->value, tl->size);
vb@136
   174
                SafeArrayUnaccessData(sa);
vb@136
   175
            }
vb@33
   176
markus@172
   177
            Blob *_blob = new Blob();
vb@33
   178
vb@33
   179
            _blob->value = sa.Detach();
markus@177
   180
            _blob->mimeType = bstr(tl->mime_type);
vb@33
   181
            _blob->filename = bstr(tl->filename);
vb@33
   182
vb@33
   183
            return _blob;
vb@33
   184
        }
vb@33
   185
vb@33
   186
        template< class T > int length(T *);
vb@33
   187
vb@33
   188
        template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
vb@33
   189
        {
vb@39
   190
            if (tl == NULL)
vb@39
   191
                return newSafeArray<T2>(0);
vb@39
   192
vb@33
   193
            int len = length<T>(tl);
vb@33
   194
vb@33
   195
            LPSAFEARRAY sa = newSafeArray<T2>(len);
vb@33
   196
            LONG lbound, ubound;
vb@33
   197
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   198
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   199
vb@33
   200
            T *_tl = tl;
vb@38
   201
            for (LONG i = lbound; i <= ubound; _tl = _tl->next, i++)
vb@33
   202
                SafeArrayPutElement(sa, &i, from_C<T2 *, T>(_tl));
vb@33
   203
vb@33
   204
            return sa;
vb@33
   205
        }
vb@33
   206
markus@172
   207
        void clear_identity_s(pEpIdentity& ident)
vb@41
   208
        {
vb@41
   209
            SysFreeString(ident.address);
vb@41
   210
            SysFreeString(ident.fpr);
vb@41
   211
            SysFreeString(ident.lang);
vb@41
   212
            SysFreeString(ident.username);
markus@177
   213
            SysFreeString(ident.userId);
vb@41
   214
markus@172
   215
            memset(&ident, 0, sizeof(pEpIdentity));
vb@41
   216
        }
vb@41
   217
markus@172
   218
        template<> pEpIdentity from_C< pEpIdentity, pEp_identity >(pEp_identity *tl)
vb@33
   219
        {
markus@172
   220
            pEpIdentity _ident;
vb@41
   221
            memset(&_ident, 0, sizeof(_ident));
vb@41
   222
vb@39
   223
            if (tl)
vb@39
   224
                copy_identity(&_ident, tl);
vb@33
   225
            return _ident;
vb@33
   226
        }
vb@33
   227
markus@172
   228
        pEpIdentity identity_s(pEp_identity *ident)
vb@33
   229
        {
markus@172
   230
            return from_C< pEpIdentity, pEp_identity >(ident);
vb@33
   231
        }
vb@33
   232
markus@172
   233
        template<> pEpIdentity *from_C< pEpIdentity *, identity_list >(identity_list *il)
vb@33
   234
        {
markus@172
   235
            pEpIdentity *ident = new pEpIdentity();
markus@172
   236
            memset(ident, 0, sizeof(pEpIdentity));
vb@41
   237
vb@39
   238
            if (il)
vb@39
   239
                copy_identity(ident, il->ident);
vb@33
   240
            return ident;
vb@33
   241
        }
vb@33
   242
markus@171
   243
        template<> StringPair *from_C< StringPair *, stringpair_list_t >(stringpair_list_t * sp)
vb@33
   244
        {
markus@171
   245
            StringPair *fld = new StringPair();
vb@39
   246
            if (sp) {
vb@39
   247
                fld->name = bstr(sp->value->key);
vb@39
   248
                fld->value = bstr(sp->value->value);
vb@39
   249
            }
vb@33
   250
            return fld;
vb@33
   251
        }
vb@33
   252
vb@33
   253
        template<> int length<identity_list>(identity_list *tl)
vb@33
   254
        {
vb@33
   255
            return identity_list_length(tl);
vb@33
   256
        }
vb@33
   257
vb@33
   258
        template<> int length<bloblist_t>(bloblist_t *tl)
vb@33
   259
        {
vb@33
   260
            return bloblist_length(tl);
vb@33
   261
        }
vb@33
   262
vb@33
   263
        template<> int length<stringpair_list_t>(stringpair_list_t *tl)
vb@33
   264
        {
vb@33
   265
            return stringpair_list_length(tl);
vb@33
   266
        }
vb@33
   267
markus@172
   268
        void clear_text_message(TextMessage *msg)
vb@41
   269
        {
vb@41
   270
            SysFreeString(msg->id);
vb@41
   271
            SysFreeString(msg->shortmsg);
vb@41
   272
            SysFreeString(msg->longmsg);
markus@177
   273
            SysFreeString(msg->longmsgFormatted);
vb@41
   274
            SafeArrayDestroy(msg->attachments);
vb@41
   275
            clear_identity_s(msg->from);
vb@41
   276
            SafeArrayDestroy(msg->to);
markus@177
   277
            clear_identity_s(msg->recvBy);
vb@41
   278
            SafeArrayDestroy(msg->cc);
vb@41
   279
            SafeArrayDestroy(msg->bcc);
markus@177
   280
            SafeArrayDestroy(msg->replyTo);
vb@41
   281
            SafeArrayDestroy(msg->references);
vb@41
   282
            SafeArrayDestroy(msg->keywords);
vb@41
   283
            SysFreeString(msg->comments);
markus@177
   284
            SafeArrayDestroy(msg->optFields);
vb@41
   285
markus@172
   286
            memset(msg, 0, sizeof(TextMessage));
vb@41
   287
        }
vb@41
   288
markus@172
   289
        void text_message_from_C(TextMessage *msg2, const ::message *msg)
vb@33
   290
        {
vb@37
   291
            assert(msg2);
vb@33
   292
            assert(msg);
vb@33
   293
vb@82
   294
            clear_text_message(msg2);
vb@41
   295
markus@172
   296
            msg2->dir = (pEpMsgDirection) msg->dir;
vb@33
   297
            msg2->id = bstr(msg->id);
vb@33
   298
            msg2->shortmsg = bstr(msg->shortmsg);
vb@33
   299
            msg2->longmsg = bstr(msg->longmsg);
markus@177
   300
            msg2->longmsgFormatted = bstr(msg->longmsg_formatted);
markus@172
   301
            msg2->attachments = array_from_C<Blob, bloblist_t>(msg->attachments);
vb@39
   302
            if (msg->sent)
vb@39
   303
                msg2->sent = mktime(msg->sent);
vb@39
   304
            if (msg->recv)
vb@39
   305
                msg2->recv = mktime(msg->recv);
vb@33
   306
            msg2->from = identity_s(msg->from);
markus@172
   307
            msg2->to = array_from_C<pEpIdentity, identity_list>(msg->to);
markus@177
   308
            msg2->recvBy = identity_s(msg->recv_by);
markus@172
   309
            msg2->cc = array_from_C<pEpIdentity, identity_list>(msg->cc);
markus@172
   310
            msg2->bcc = array_from_C<pEpIdentity, identity_list>(msg->bcc);
markus@177
   311
            msg2->replyTo = array_from_C<pEpIdentity, identity_list>(msg->reply_to);
vb@40
   312
            msg2->references = string_array(msg->references);
vb@40
   313
            msg2->keywords = string_array(msg->keywords);
vb@33
   314
            msg2->comments = bstr(msg->comments);
markus@177
   315
            msg2->optFields = array_from_C<StringPair, stringpair_list_t>(msg->opt_fields);
vb@33
   316
        }
vb@33
   317
vb@37
   318
        char * str(BSTR s)
vb@33
   319
        {
vb@37
   320
            string str = utf8_string(s);
vb@37
   321
            char *_s = _strdup(str.c_str());
vb@33
   322
            if (_s == NULL)
vb@33
   323
                throw bad_alloc();
vb@33
   324
vb@37
   325
            return _s;
vb@33
   326
        }
vb@33
   327
markus@172
   328
        void clear_blob(Blob& b)
vb@41
   329
        {
vb@41
   330
            SysFreeString(b.filename);
markus@177
   331
            SysFreeString(b.mimeType);
vb@41
   332
            SafeArrayDestroy(b.value);
markus@172
   333
            memset(&b, 0, sizeof(Blob));
vb@41
   334
        }
vb@41
   335
vb@33
   336
        bloblist_t *bloblist(SAFEARRAY *sa)
vb@33
   337
        {
vb@37
   338
            if (sa == NULL)
vb@37
   339
                return NULL;
vb@37
   340
vb@33
   341
            LONG lbound, ubound;
vb@33
   342
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   343
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   344
vb@33
   345
            size_t size = ubound - lbound + 1;
vb@33
   346
            if (size <= 0)
vb@33
   347
                return NULL;
vb@33
   348
vb@33
   349
            bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
vb@33
   350
            if (bl == NULL)
vb@33
   351
                throw bad_alloc();
vb@33
   352
vb@33
   353
            bloblist_t *_bl = bl;
vb@33
   354
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   355
                Blob b;
markus@172
   356
                memset(&b, 0, sizeof(Blob));
vb@33
   357
                SafeArrayGetElement(sa, &i, &b);
vb@33
   358
vb@38
   359
                LONG _lbound, _ubound;
vb@38
   360
                SafeArrayGetLBound(b.value, 1, &_lbound);
vb@38
   361
                SafeArrayGetUBound(b.value, 1, &_ubound);
vb@38
   362
                size_t size = _ubound - _lbound + 1;
vb@38
   363
vb@136
   364
                char *buffer;
vb@136
   365
                if (size) {
vb@136
   366
                    buffer = (char *) malloc(size);
vb@136
   367
                    if (buffer == NULL)
vb@136
   368
                        throw bad_alloc();
vb@33
   369
vb@136
   370
                    char *data;
vb@33
   371
vb@136
   372
                    SafeArrayAccessData(b.value, (void **) &data);
vb@136
   373
                    memcpy(buffer, data, size);
vb@136
   374
                    SafeArrayUnaccessData(sa);
vb@136
   375
                }
vb@136
   376
                else {
vb@136
   377
                    buffer = _strdup("");
vb@136
   378
                    if (buffer == NULL)
vb@136
   379
                        throw bad_alloc();
vb@33
   380
vb@136
   381
                }
markus@177
   382
                _bl = bloblist_add(_bl, buffer, size, str(b.mimeType), str(b.filename));
vb@33
   383
                if (_bl == NULL) {
vb@42
   384
                    free(buffer);
vb@41
   385
                    clear_blob(b);
vb@33
   386
                    free_bloblist(bl);
vb@33
   387
                    throw bad_alloc();
vb@33
   388
                }
vb@33
   389
vb@41
   390
                clear_blob(b);
vb@33
   391
            }
vb@33
   392
vb@33
   393
            return bl;
vb@33
   394
        }
vb@33
   395
vb@33
   396
        identity_list *identities(SAFEARRAY * sa)
vb@33
   397
        {
vb@37
   398
            if (sa == NULL)
vb@37
   399
                return NULL;
vb@37
   400
vb@33
   401
            LONG lbound, ubound;
vb@33
   402
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   403
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   404
vb@33
   405
            size_t size = ubound - lbound + 1;
vb@33
   406
            if (size <= 0)
vb@33
   407
                return NULL;
vb@33
   408
vb@33
   409
            identity_list *il = new_identity_list(NULL);
vb@33
   410
vb@33
   411
            identity_list *_il = il;
vb@33
   412
            for (LONG i = lbound; i <= ubound; i++) {
markus@172
   413
                pEpIdentity s;
markus@172
   414
                memset(&s, 0, sizeof(pEpIdentity));
vb@33
   415
                SafeArrayGetElement(sa, &i, &s);
vb@33
   416
vb@33
   417
                pEp_identity *ident;
vb@33
   418
                try {
vb@33
   419
                    ident = new_identity(&s);
vb@33
   420
                }
vb@33
   421
                catch (bad_alloc&) {
vb@41
   422
                    clear_identity_s(s);
vb@33
   423
                    throw bad_alloc();
vb@33
   424
                }
vb@33
   425
vb@41
   426
                clear_identity_s(s);
vb@33
   427
vb@33
   428
                _il = identity_list_add(_il, ident);
vb@33
   429
                if (_il == NULL) {
vb@33
   430
                    free_identity_list(il);
vb@33
   431
                    throw bad_alloc();
vb@33
   432
                }
vb@33
   433
            }
vb@33
   434
vb@33
   435
            return il;
vb@33
   436
        }
vb@33
   437
markus@171
   438
        stringpair_t *new_stringpair(StringPair *fld)
vb@33
   439
        {
vb@33
   440
            stringpair_t *pair = ::new_stringpair(str(fld->name), str(fld->value));
vb@33
   441
            if (pair == NULL)
vb@33
   442
                throw bad_alloc();
vb@33
   443
vb@33
   444
            return pair;
vb@33
   445
        }
vb@33
   446
markus@171
   447
        void clear_opt_field(StringPair& f)
vb@41
   448
        {
vb@41
   449
            SysFreeString(f.name);
vb@41
   450
            SysFreeString(f.value);
markus@171
   451
            memset(&f, 0, sizeof(StringPair));
vb@41
   452
        }
vb@41
   453
vb@33
   454
        stringpair_list_t *stringpair_list(SAFEARRAY * sa)
vb@33
   455
        {
vb@37
   456
            if (sa == NULL)
vb@37
   457
                return NULL;
vb@37
   458
vb@33
   459
            LONG lbound, ubound;
vb@33
   460
            SafeArrayGetLBound(sa, 1, &lbound);
vb@33
   461
            SafeArrayGetUBound(sa, 1, &ubound);
vb@33
   462
vb@33
   463
            size_t size = ubound - lbound + 1;
vb@33
   464
            if (size <= 0)
vb@33
   465
                return NULL;
vb@33
   466
vb@33
   467
            stringpair_list_t *il = new_stringpair_list(NULL);
vb@33
   468
vb@33
   469
            stringpair_list_t *_il = il;
vb@33
   470
            for (LONG i = lbound; i <= ubound; i++) {
markus@171
   471
                StringPair s;
markus@171
   472
                memset(&s, 0, sizeof(StringPair));
vb@33
   473
                SafeArrayGetElement(sa, &i, &s);
vb@33
   474
vb@33
   475
                stringpair_t *pair;
vb@33
   476
                try {
vb@33
   477
                    pair = new_stringpair(&s);
vb@33
   478
                }
vb@33
   479
                catch (bad_alloc&) {
vb@41
   480
                    clear_opt_field(s);
vb@33
   481
                    throw bad_alloc();
vb@33
   482
                }
vb@33
   483
vb@41
   484
                clear_opt_field(s);
vb@33
   485
vb@33
   486
                _il = stringpair_list_add(_il, pair);
vb@33
   487
                if (_il == NULL) {
vb@33
   488
                    free_stringpair_list(il);
vb@33
   489
                    throw bad_alloc();
vb@33
   490
                }
vb@33
   491
            }
vb@33
   492
vb@33
   493
            return il;
vb@33
   494
        }
vb@33
   495
        
markus@172
   496
        ::message * text_message_to_C(TextMessage *msg)
vb@33
   497
        {
vb@33
   498
            assert(msg);
vb@33
   499
vb@39
   500
            ::message * msg2 = new_message((PEP_msg_direction) msg->dir);
vb@37
   501
            if (msg2 == NULL)
vb@37
   502
                throw bad_alloc();
vb@33
   503
vb@33
   504
            msg2->id = str(msg->id);
vb@33
   505
            msg2->shortmsg = str(msg->shortmsg);
vb@33
   506
            msg2->longmsg = str(msg->longmsg);
markus@177
   507
            msg2->longmsg_formatted = str(msg->longmsgFormatted);
vb@33
   508
            msg2->attachments = bloblist(msg->attachments);
vb@33
   509
            msg2->sent = new_timestamp(msg->sent);
vb@33
   510
            msg2->recv = new_timestamp(msg->recv);
vb@33
   511
            msg2->from = new_identity(&msg->from);
vb@33
   512
            msg2->to = identities(msg->to);
markus@177
   513
            msg2->recv_by = new_identity(&msg->recvBy);
vb@33
   514
            msg2->cc = identities(msg->cc);
vb@33
   515
            msg2->bcc = identities(msg->bcc);
markus@177
   516
            msg2->reply_to = identities(msg->replyTo);
vb@33
   517
            msg2->references = new_stringlist(msg->references);
vb@33
   518
            msg2->keywords = new_stringlist(msg->keywords);
vb@33
   519
            msg2->comments = str(msg->comments);
markus@177
   520
            msg2->opt_fields = stringpair_list(msg->optFields);
vb@33
   521
vb@33
   522
            return msg2;
vb@33
   523
        }
markus@177
   524
krista@157
   525
		void opt_field_array_from_C(stringpair_list_t* spair_list, LPSAFEARRAY* pair_list_out) {
krista@157
   526
			assert(spair_list);
krista@157
   527
			assert(pair_list_out);
krista@157
   528
			
krista@157
   529
			if (!spair_list)
krista@157
   530
				return;
krista@157
   531
markus@171
   532
			*pair_list_out = array_from_C<StringPair, stringpair_list_t>(spair_list);
krista@157
   533
		}
markus@177
   534
krista@158
   535
		void clear_opt_field_array(LPSAFEARRAY* opt_field_array) {
krista@158
   536
			SafeArrayDestroy(*opt_field_array);
krista@158
   537
			//memset(*opt_field_array, 0, sizeof(SAFEARRAY*));
krista@158
   538
			*opt_field_array = NULL;
krista@158
   539
		}
vb@10
   540
    }
vb@10
   541
}