2 #include "pEp_utility.h"
8 pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
12 address = _ident->address;
16 user_id = _ident->user_id;
18 username = _ident->username;
19 comm_type = (pEp_comm_type) _ident->comm_type;
23 pEp_identity_cpp::pEp_identity_cpp(const pEp_identity_s *_ident)
27 address = utf8_string(_ident->address);
29 fpr = utf8_string(_ident->fpr);
31 user_id = utf8_string(_ident->user_id);
33 username = utf8_string(_ident->username);
34 comm_type = _ident->comm_type;
36 lang = utf8_string(_ident->lang);
39 pEp_identity * pEp_identity_cpp::to_pEp_identity()
41 ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
46 _ident->comm_type = (::PEP_comm_type) this->comm_type;
47 _ident->me = this->me;
49 assert(this->lang.size() == 0 || this->lang.size() == 2);
51 if (this->lang.size()) {
52 _ident->lang[0] = this->lang[0];
53 _ident->lang[1] = this->lang[1];
59 pEp_identity_s * pEp_identity_cpp::to_pEp_identity_s()
61 pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
66 _ident->address = utf16_bstr(this->address).Detach();
67 _ident->comm_type = this->comm_type;
68 _ident->fpr = utf16_bstr(this->fpr).Detach();
69 _ident->lang = utf16_bstr(this->lang).Detach();
70 _ident->username = utf16_bstr(this->username).Detach();
71 _ident->user_id = utf16_bstr(this->user_id).Detach();
76 void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
80 ::memset(ident_s, 0, sizeof(pEp_identity_s));
83 ident_s->address = utf16_bstr(ident->address).Detach();
85 ident_s->fpr = utf16_bstr(ident->fpr).Detach();
87 ident_s->user_id = utf16_bstr(ident->user_id).Detach();
89 ident_s->username = utf16_bstr(ident->username).Detach();
90 ident_s->comm_type = (pEp_comm_type) ident->comm_type;
92 ident_s->lang = utf16_bstr(ident->lang).Detach();
96 ::pEp_identity *new_identity(const pEp_identity_s * ident)
101 ::pEp_identity *_ident;
109 _address = utf8_string(ident->address);
111 _fpr = utf8_string(ident->fpr);
112 for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
113 if (*p >= 'A' && *p <= 'Z')
115 if (*p >= '0' && *p <= '9')
117 throw invalid_argument("invalid hex digits in fingerprint");
121 _user_id = utf8_string(ident->user_id);
123 _username = utf8_string(ident->username);
125 _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
130 _ident->comm_type = (PEP_comm_type) ident->comm_type;
133 string _lang = utf8_string(ident->lang);
134 if (_lang.length() != 0) {
135 if (_lang.length() != 2) {
136 ::free_identity(_ident);
137 throw invalid_argument("invalid language code");
139 if (_lang[0] < 'a' || _lang[0] > 'z') {
140 ::free_identity(_ident);
141 throw invalid_argument("invalid language code");
143 if (_lang[1] < 'a' || _lang[1] > 'z') {
144 ::free_identity(_ident);
145 throw invalid_argument("invalid language code");
147 _ident->lang[0] = _lang[0];
148 _ident->lang[1] = _lang[1];
155 template< class T2, class T > T2 from_C(T *tl);
157 template<> BSTR from_C< BSTR, char >(char *s)
160 return utf16_bstr(s).Detach();
162 return _bstr_t(L"").Detach();
165 inline BSTR bstr(char *s)
167 return from_C< BSTR, char >(s);
170 template<> blob *from_C< blob *, bloblist_t >(bloblist_t *tl)
172 assert(tl && tl->value);
174 CComSafeArray<BYTE> sa;
177 memcpy(sa.m_psa->pvData, tl->value, tl->size);
179 blob *_blob = new blob();
181 _blob->value = sa.Detach();
182 _blob->mime_type = bstr(tl->mime_type);
183 _blob->filename = bstr(tl->filename);
188 template< class T > int length(T *);
190 template< class T2, class T > SAFEARRAY * array_from_C(T *tl)
193 return newSafeArray<T2>(0);
195 int len = length<T>(tl);
197 LPSAFEARRAY sa = newSafeArray<T2>(len);
199 SafeArrayGetLBound(sa, 1, &lbound);
200 SafeArrayGetUBound(sa, 1, &ubound);
203 for (LONG i = lbound; i <= ubound; _tl = _tl->next, i++)
204 SafeArrayPutElement(sa, &i, from_C<T2 *, T>(_tl));
209 template<> pEp_identity_s from_C< pEp_identity_s, pEp_identity >(pEp_identity *tl)
211 pEp_identity_s _ident;
213 copy_identity(&_ident, tl);
217 pEp_identity_s identity_s(pEp_identity *ident)
219 return from_C< pEp_identity_s, pEp_identity >(ident);
222 template<> pEp_identity_s *from_C< pEp_identity_s *, identity_list >(identity_list *il)
224 pEp_identity_s *ident = new pEp_identity_s();
226 copy_identity(ident, il->ident);
230 template<> opt_field *from_C< opt_field *, stringpair_list_t >(stringpair_list_t * sp)
232 opt_field *fld = new opt_field();
234 fld->name = bstr(sp->value->key);
235 fld->value = bstr(sp->value->value);
240 template<> int length<identity_list>(identity_list *tl)
242 return identity_list_length(tl);
245 template<> int length<bloblist_t>(bloblist_t *tl)
247 return bloblist_length(tl);
250 template<> int length<stringpair_list_t>(stringpair_list_t *tl)
252 return stringpair_list_length(tl);
255 void text_message_from_C(text_message *msg2, ::message *msg)
260 msg2->dir = (pEp_msg_direction) msg->dir;
261 msg2->id = bstr(msg->id);
262 msg2->shortmsg = bstr(msg->shortmsg);
263 msg2->longmsg = bstr(msg->longmsg);
264 msg2->longmsg_formatted = bstr(msg->longmsg_formatted);
265 msg2->attachments = array_from_C<blob, bloblist_t>(msg->attachments);
267 msg2->sent = mktime(msg->sent);
269 msg2->recv = mktime(msg->recv);
270 msg2->from = identity_s(msg->from);
271 msg2->to = array_from_C<pEp_identity_s, identity_list>(msg->to);
272 msg2->recv_by = identity_s(msg->recv_by);
273 msg2->cc = array_from_C<pEp_identity_s, identity_list>(msg->cc);
274 msg2->bcc = array_from_C<pEp_identity_s, identity_list>(msg->bcc);
275 msg2->reply_to = array_from_C<pEp_identity_s, identity_list>(msg->reply_to);
276 msg2->references = string_array(msg->references).Detach();
277 msg2->keywords = string_array(msg->keywords).Detach();
278 msg2->comments = bstr(msg->comments);
279 msg2->opt_fields = array_from_C<opt_field, stringpair_list_t>(msg->opt_fields);
284 string str = utf8_string(s);
285 char *_s = _strdup(str.c_str());
292 bloblist_t *bloblist(SAFEARRAY *sa)
298 SafeArrayGetLBound(sa, 1, &lbound);
299 SafeArrayGetUBound(sa, 1, &ubound);
301 size_t size = ubound - lbound + 1;
305 bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
309 bloblist_t *_bl = bl;
310 for (LONG i = lbound; i <= ubound; i++) {
312 memset(&b, 0, sizeof(blob));
313 SafeArrayGetElement(sa, &i, &b);
315 LONG _lbound, _ubound;
316 SafeArrayGetLBound(b.value, 1, &_lbound);
317 SafeArrayGetUBound(b.value, 1, &_ubound);
318 size_t size = _ubound - _lbound + 1;
320 char *buffer = (char *) malloc(size);
326 SafeArrayAccessData(b.value, (void **) &data);
327 memcpy(buffer, data, size);
328 SafeArrayUnaccessData(sa);
330 _bl = bloblist_add(_bl, buffer, size, str(b.mime_type), str(b.filename));
332 IRecordInfo *ri = getRecordInfo<blob>();
340 IRecordInfo *ri = getRecordInfo<blob>();
347 identity_list *identities(SAFEARRAY * sa)
353 SafeArrayGetLBound(sa, 1, &lbound);
354 SafeArrayGetUBound(sa, 1, &ubound);
356 size_t size = ubound - lbound + 1;
360 identity_list *il = new_identity_list(NULL);
362 identity_list *_il = il;
363 for (LONG i = lbound; i <= ubound; i++) {
365 memset(&s, 0, sizeof(s));
366 SafeArrayGetElement(sa, &i, &s);
370 ident = new_identity(&s);
373 IRecordInfo *ri = getRecordInfo<pEp_identity_s>();
379 IRecordInfo *ri = getRecordInfo<pEp_identity_s>();
382 _il = identity_list_add(_il, ident);
384 free_identity_list(il);
392 stringpair_t *new_stringpair(opt_field *fld)
394 stringpair_t *pair = ::new_stringpair(str(fld->name), str(fld->value));
401 stringpair_list_t *stringpair_list(SAFEARRAY * sa)
407 SafeArrayGetLBound(sa, 1, &lbound);
408 SafeArrayGetUBound(sa, 1, &ubound);
410 size_t size = ubound - lbound + 1;
414 stringpair_list_t *il = new_stringpair_list(NULL);
416 stringpair_list_t *_il = il;
417 for (LONG i = lbound; i <= ubound; i++) {
419 memset(&s, 0, sizeof(opt_field));
420 SafeArrayGetElement(sa, &i, &s);
424 pair = new_stringpair(&s);
427 IRecordInfo *ri = getRecordInfo<opt_field>();
433 IRecordInfo *ri = getRecordInfo<opt_field>();
436 _il = stringpair_list_add(_il, pair);
438 free_stringpair_list(il);
446 ::message * text_message_to_C(text_message *msg)
450 ::message * msg2 = new_message((PEP_msg_direction) msg->dir);
454 msg2->id = str(msg->id);
455 msg2->shortmsg = str(msg->shortmsg);
456 msg2->longmsg = str(msg->longmsg);
457 msg2->longmsg_formatted = str(msg->longmsg_formatted);
458 msg2->attachments = bloblist(msg->attachments);
459 msg2->sent = new_timestamp(msg->sent);
460 msg2->recv = new_timestamp(msg->recv);
461 msg2->from = new_identity(&msg->from);
462 msg2->to = identities(msg->to);
463 msg2->recv_by = new_identity(&msg->recv_by);
464 msg2->cc = identities(msg->cc);
465 msg2->bcc = identities(msg->bcc);
466 msg2->reply_to = identities(msg->reply_to);
467 msg2->references = new_stringlist(msg->references);
468 msg2->keywords = new_stringlist(msg->keywords);
469 msg2->comments = str(msg->comments);
470 msg2->opt_fields = stringpair_list(msg->opt_fields);