pEp_utility.cpp
author Volker Birk <vb@pep-project.org>
Fri, 01 Jul 2016 11:50:29 +0200
changeset 136 57e180b99366
parent 82 29ea9f52055d
child 157 ce82a4a45fc5
permissions -rw-r--r--
empty attachments
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;
vb@10
    19
            comm_type = (pEp_comm_type) _ident->comm_type;
vb@10
    20
            lang = _ident->lang;
vb@10
    21
        }
vb@10
    22
vb@10
    23
        pEp_identity_cpp::pEp_identity_cpp(const pEp_identity_s *_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);
vb@10
    30
            if (_ident->user_id)
vb@10
    31
                user_id = utf8_string(_ident->user_id);
vb@10
    32
            if (_ident->username)
vb@10
    33
                username = utf8_string(_ident->username);
vb@10
    34
            comm_type = _ident->comm_type;
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
vb@10
    59
        pEp_identity_s * pEp_identity_cpp::to_pEp_identity_s()
vb@10
    60
        {
vb@10
    61
            pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
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);
vb@10
    67
            _ident->comm_type = 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);
vb@40
    71
            _ident->user_id = utf16_bstr(this->user_id);
vb@10
    72
vb@10
    73
            return _ident;
vb@10
    74
        }
vb@10
    75
vb@10
    76
        void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
vb@10
    77
        {
vb@10
    78
            assert(ident_s);
vb@10
    79
vb@10
    80
            ::memset(ident_s, 0, sizeof(pEp_identity_s));
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)
vb@40
    87
                    ident_s->user_id = utf16_bstr(ident->user_id);
vb@10
    88
                if (ident->username)
vb@40
    89
                    ident_s->username = utf16_bstr(ident->username);
vb@10
    90
                ident_s->comm_type = (pEp_comm_type) 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
vb@10
    96
        ::pEp_identity *new_identity(const pEp_identity_s * 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
            }
vb@10
   120
            if (ident->user_id)
vb@10
   121
                _user_id = utf8_string(ident->user_id);
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
vb@10
   130
            _ident->comm_type = (PEP_comm_type) ident->comm_type;
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
vb@33
   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
vb@33
   177
            blob *_blob = new blob();
vb@33
   178
vb@33
   179
            _blob->value = sa.Detach();
vb@33
   180
            _blob->mime_type = 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
vb@41
   207
        void clear_identity_s(pEp_identity_s& 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);
vb@41
   213
            SysFreeString(ident.user_id);
vb@41
   214
vb@41
   215
            memset(&ident, 0, sizeof(pEp_identity_s));
vb@41
   216
        }
vb@41
   217
vb@33
   218
        template<> pEp_identity_s from_C< pEp_identity_s, pEp_identity >(pEp_identity *tl)
vb@33
   219
        {
vb@33
   220
            pEp_identity_s _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
vb@33
   228
        pEp_identity_s identity_s(pEp_identity *ident)
vb@33
   229
        {
vb@33
   230
            return from_C< pEp_identity_s, pEp_identity >(ident);
vb@33
   231
        }
vb@33
   232
vb@33
   233
        template<> pEp_identity_s *from_C< pEp_identity_s *, identity_list >(identity_list *il)
vb@33
   234
        {
vb@33
   235
            pEp_identity_s *ident = new pEp_identity_s();
vb@41
   236
            memset(ident, 0, sizeof(pEp_identity_s));
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
vb@33
   243
        template<> opt_field *from_C< opt_field *, stringpair_list_t >(stringpair_list_t * sp)
vb@33
   244
        {
vb@33
   245
            opt_field *fld = new opt_field();
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
vb@41
   268
        void clear_text_message(text_message *msg)
vb@41
   269
        {
vb@41
   270
            SysFreeString(msg->id);
vb@41
   271
            SysFreeString(msg->shortmsg);
vb@41
   272
            SysFreeString(msg->longmsg);
vb@41
   273
            SysFreeString(msg->longmsg_formatted);
vb@41
   274
            SafeArrayDestroy(msg->attachments);
vb@41
   275
            clear_identity_s(msg->from);
vb@41
   276
            SafeArrayDestroy(msg->to);
vb@41
   277
            clear_identity_s(msg->recv_by);
vb@41
   278
            SafeArrayDestroy(msg->cc);
vb@41
   279
            SafeArrayDestroy(msg->bcc);
vb@41
   280
            SafeArrayDestroy(msg->reply_to);
vb@41
   281
            SafeArrayDestroy(msg->references);
vb@41
   282
            SafeArrayDestroy(msg->keywords);
vb@41
   283
            SysFreeString(msg->comments);
vb@41
   284
            SafeArrayDestroy(msg->opt_fields);
vb@41
   285
vb@41
   286
            memset(msg, 0, sizeof(text_message));
vb@41
   287
        }
vb@41
   288
vb@74
   289
        void text_message_from_C(text_message *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
vb@33
   296
            msg2->dir = (pEp_msg_direction) 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);
vb@33
   300
            msg2->longmsg_formatted = bstr(msg->longmsg_formatted);
vb@33
   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);
vb@33
   307
            msg2->to = array_from_C<pEp_identity_s, identity_list>(msg->to);
vb@33
   308
            msg2->recv_by = identity_s(msg->recv_by);
vb@33
   309
            msg2->cc = array_from_C<pEp_identity_s, identity_list>(msg->cc);
vb@33
   310
            msg2->bcc = array_from_C<pEp_identity_s, identity_list>(msg->bcc);
vb@33
   311
            msg2->reply_to = array_from_C<pEp_identity_s, 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);
vb@33
   315
            msg2->opt_fields = array_from_C<opt_field, 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
vb@41
   328
        void clear_blob(blob& b)
vb@41
   329
        {
vb@41
   330
            SysFreeString(b.filename);
vb@41
   331
            SysFreeString(b.mime_type);
vb@41
   332
            SafeArrayDestroy(b.value);
vb@41
   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++) {
vb@33
   355
                blob b;
vb@38
   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
                }
vb@38
   382
                _bl = bloblist_add(_bl, buffer, size, str(b.mime_type), 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++) {
vb@33
   413
                pEp_identity_s s;
vb@41
   414
                memset(&s, 0, sizeof(pEp_identity_s));
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
vb@33
   438
        stringpair_t *new_stringpair(opt_field *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
vb@41
   447
        void clear_opt_field(opt_field& f)
vb@41
   448
        {
vb@41
   449
            SysFreeString(f.name);
vb@41
   450
            SysFreeString(f.value);
vb@41
   451
            memset(&f, 0, sizeof(opt_field));
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++) {
vb@33
   471
                opt_field s;
vb@37
   472
                memset(&s, 0, sizeof(opt_field));
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
        
vb@33
   496
        ::message * text_message_to_C(text_message *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);
vb@33
   507
            msg2->longmsg_formatted = str(msg->longmsg_formatted);
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);
vb@33
   513
            msg2->recv_by = new_identity(&msg->recv_by);
vb@33
   514
            msg2->cc = identities(msg->cc);
vb@33
   515
            msg2->bcc = identities(msg->bcc);
vb@33
   516
            msg2->reply_to = identities(msg->reply_to);
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);
vb@33
   520
            msg2->opt_fields = stringpair_list(msg->opt_fields);
vb@33
   521
vb@33
   522
            return msg2;
vb@33
   523
        }
vb@10
   524
    }
vb@10
   525
}