1 // This file is under GNU General Public License 3.0
4 #ifdef ENIGMAIL_MAY_USE_THIS
6 #include "pEp_internal.h"
7 #include "message_api.h"
14 #include "aux_mime_msg.h"
17 static PEP_STATUS update_identity_recip_list(PEP_SESSION session,
18 identity_list* list) {
20 PEP_STATUS status = PEP_STATUS_OK;
23 return PEP_UNKNOWN_ERROR;
25 identity_list* id_list_ptr = NULL;
27 for (id_list_ptr = list; id_list_ptr; id_list_ptr = id_list_ptr->next) {
28 pEp_identity* curr_identity = id_list_ptr->ident;
30 if (!is_me(session, curr_identity)) {
31 char* name_bak = curr_identity->username;
32 curr_identity->username = NULL;
33 status = update_identity(session, curr_identity);
35 (EMPTYSTR(curr_identity->username) || strcmp(name_bak, curr_identity->username) != 0)) {
36 free(curr_identity->username);
37 curr_identity->username = name_bak;
41 status = _myself(session, curr_identity, false, false, true);
42 if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
50 DYNAMIC_API PEP_STATUS MIME_decrypt_message(
54 char** mime_plaintext,
55 stringlist_t **keylist,
57 PEP_decrypt_flags_t *flags,
62 assert(mime_plaintext);
68 if (!(mimetext && mime_plaintext && keylist && rating && flags && modified_src))
69 return PEP_ILLEGAL_VALUE;
71 PEP_STATUS status = PEP_STATUS_OK;
72 message* tmp_msg = NULL;
73 message* dec_msg = NULL;
74 *mime_plaintext = NULL;
76 status = mime_decode_message(mimetext, size, &tmp_msg, NULL);
77 if (status != PEP_STATUS_OK)
80 tmp_msg->dir = PEP_dir_incoming;
81 // MIME decode message delivers only addresses. We need more.
83 if (!is_me(session, tmp_msg->from))
84 status = update_identity(session, (tmp_msg->from));
86 status = _myself(session, tmp_msg->from, false, false, true);
88 if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
92 status = update_identity_recip_list(session, tmp_msg->to);
93 if (status != PEP_STATUS_OK)
96 status = update_identity_recip_list(session, tmp_msg->cc);
97 if (status != PEP_STATUS_OK)
100 status = update_identity_recip_list(session, tmp_msg->bcc);
101 if (status != PEP_STATUS_OK)
104 PEP_STATUS decrypt_status = decrypt_message(session,
112 if (!dec_msg && (decrypt_status == PEP_UNENCRYPTED || decrypt_status == PEP_VERIFIED)) {
113 dec_msg = message_dup(tmp_msg);
116 if (decrypt_status > PEP_CANNOT_DECRYPT_UNKNOWN || !dec_msg)
118 status = decrypt_status;
122 if (*flags & PEP_decrypt_flag_src_modified) {
123 mime_encode_message(tmp_msg, false, modified_src, false);
125 *flags &= (~PEP_decrypt_flag_src_modified);
126 decrypt_status = PEP_CANNOT_REENCRYPT; // Because we couldn't return it, I guess.
130 // FIXME: test with att
131 status = mime_encode_message(dec_msg, false, mime_plaintext, false);
133 if (status == PEP_STATUS_OK)
137 return decrypt_status;
141 free_message(tmp_msg);
142 free_message(dec_msg);
148 DYNAMIC_API PEP_STATUS MIME_encrypt_message(
150 const char *mimetext,
153 char** mime_ciphertext,
154 PEP_enc_format enc_format,
155 PEP_encrypt_flags_t flags
158 PEP_STATUS status = PEP_STATUS_OK;
159 PEP_STATUS tmp_status = PEP_STATUS_OK;
160 message* tmp_msg = NULL;
161 message* enc_msg = NULL;
162 message* ret_msg = NULL;
164 status = mime_decode_message(mimetext, size, &tmp_msg, NULL);
165 if (status != PEP_STATUS_OK)
168 // MIME decode message delivers only addresses. We need more.
171 status = get_default_own_userid(session, &own_id);
172 free(tmp_msg->from->user_id);
174 if (status != PEP_STATUS_OK || !own_id) {
175 tmp_msg->from->user_id = strdup(PEP_OWN_USERID);
178 tmp_msg->from->user_id = own_id; // ownership transfer
181 status = myself(session, tmp_msg->from);
182 if (status != PEP_STATUS_OK)
186 // Own identities can be retrieved here where they would otherwise
187 // fail because we lack all other information. This is ok and even
188 // desired. FIXME: IS it?
189 status = update_identity_recip_list(session, tmp_msg->to);
190 if (status != PEP_STATUS_OK)
193 status = update_identity_recip_list(session, tmp_msg->cc);
194 if (status != PEP_STATUS_OK)
197 status = update_identity_recip_list(session, tmp_msg->bcc);
198 if (status != PEP_STATUS_OK)
201 // This isn't incoming, though... so we need to reverse the direction
202 tmp_msg->dir = PEP_dir_outgoing;
203 status = encrypt_message(session,
210 if (status == PEP_STATUS_OK || status == PEP_UNENCRYPTED)
211 ret_msg = (status == PEP_STATUS_OK ? enc_msg : tmp_msg);
215 if (status == PEP_STATUS_OK && !enc_msg) {
216 status = PEP_UNKNOWN_ERROR;
220 tmp_status = mime_encode_message(ret_msg,
225 if (tmp_status != PEP_STATUS_OK)
229 free_message(tmp_msg);
230 free_message(enc_msg);
236 DYNAMIC_API PEP_STATUS MIME_encrypt_message_for_self(
238 pEp_identity* target_id,
239 const char *mimetext,
242 char** mime_ciphertext,
243 PEP_enc_format enc_format,
244 PEP_encrypt_flags_t flags
247 PEP_STATUS status = PEP_STATUS_OK;
248 message* tmp_msg = NULL;
249 message* enc_msg = NULL;
251 status = mime_decode_message(mimetext, size, &tmp_msg, NULL);
252 if (status != PEP_STATUS_OK)
255 // This isn't incoming, though... so we need to reverse the direction
256 tmp_msg->dir = PEP_dir_outgoing;
257 status = encrypt_message_for_self(session,
264 if (status != PEP_STATUS_OK)
268 status = PEP_UNKNOWN_ERROR;
272 status = mime_encode_message(enc_msg, false, mime_ciphertext, false);
275 free_message(tmp_msg);
276 free_message(enc_msg);
281 const int the_answer_my_friend = 42;