508 msg_idn += 1U + (unsigned int) (9.0 * (rand() / (RAND_MAX + 1.0))); |
508 msg_idn += 1U + (unsigned int) (9.0 * (rand() / (RAND_MAX + 1.0))); |
509 return g_strdup_printf("%u%d", msg_idn, (int)(now%10L)); |
509 return g_strdup_printf("%u%d", msg_idn, (int)(now%10L)); |
510 } |
510 } |
511 |
511 |
512 // jb_send_msg(jid, test, type, subject, msgid, *encrypted) |
512 // jb_send_msg(jid, test, type, subject, msgid, *encrypted) |
513 // When encrypted is not NULL, the function set *encrypted to TRUE if the |
513 // When encrypted is not NULL, the function set *encrypted to 1 if the |
514 // message has been PGP-encrypted. |
514 // message has been PGP-encrypted. If encryption enforcement is set and |
|
515 // encryption fails, *encrypted is set to -1. |
515 void jb_send_msg(const char *fjid, const char *text, int type, |
516 void jb_send_msg(const char *fjid, const char *text, int type, |
516 const char *subject, const char *msgid, guint *encrypted) |
517 const char *subject, const char *msgid, gint *encrypted) |
517 { |
518 { |
518 xmlnode x; |
519 xmlnode x; |
519 gchar *strtype; |
520 gchar *strtype; |
520 #if defined HAVE_GPGME || defined JEP0022 || defined JEP0085 |
521 #if defined HAVE_GPGME || defined JEP0022 || defined JEP0085 |
521 char *rname, *barejid; |
522 char *rname, *barejid; |
551 if (rname) |
552 if (rname) |
552 rname++; |
553 rname++; |
553 |
554 |
554 #ifdef HAVE_GPGME |
555 #ifdef HAVE_GPGME |
555 if (type == ROSTER_TYPE_USER && sl_buddy && gpg_enabled()) { |
556 if (type == ROSTER_TYPE_USER && sl_buddy && gpg_enabled()) { |
556 if (!settings_pgp_getdisabled(barejid)) { // disabled for this contact? |
557 if (!settings_pgp_getdisabled(barejid)) { // not disabled for this contact? |
|
558 guint force; |
557 struct pgp_data *res_pgpdata; |
559 struct pgp_data *res_pgpdata; |
|
560 force = settings_pgp_getforce(barejid); |
558 res_pgpdata = buddy_resource_pgp(sl_buddy->data, rname); |
561 res_pgpdata = buddy_resource_pgp(sl_buddy->data, rname); |
559 if (res_pgpdata && res_pgpdata->sign_keyid) { |
562 if (force || (res_pgpdata && res_pgpdata->sign_keyid)) { |
560 /* Remote client has PGP support (we have a signature). |
563 /* Remote client has PGP support (we have a signature) |
|
564 * OR encryption is enforced (force = TRUE). |
561 * If the contact has a specific KeyId, we'll use it; |
565 * If the contact has a specific KeyId, we'll use it; |
562 * if not, we'll use the key used for the signature. |
566 * if not, we'll use the key used for the signature. |
563 * Both keys should match, in theory (cf. XEP-0027). */ |
567 * Both keys should match, in theory (cf. XEP-0027). */ |
564 const char *key; |
568 const char *key; |
565 key = settings_pgp_getkeyid(barejid); |
569 key = settings_pgp_getkeyid(barejid); |
566 if (!key) |
570 if (!key && res_pgpdata) |
567 key = res_pgpdata->sign_keyid; |
571 key = res_pgpdata->sign_keyid; |
568 enc = gpg_encrypt(text, key); |
572 if (key) |
|
573 enc = gpg_encrypt(text, key); |
|
574 if (!enc && force) { |
|
575 if (encrypted) |
|
576 *encrypted = -1; |
|
577 g_free(barejid); |
|
578 return; |
|
579 } |
569 } |
580 } |
570 } |
581 } |
571 } |
582 } |
572 #endif // HAVE_GPGME |
583 #endif // HAVE_GPGME |
573 |
584 |
585 xmlnode y; |
596 xmlnode y; |
586 y = xmlnode_insert_tag(x, "x"); |
597 y = xmlnode_insert_tag(x, "x"); |
587 xmlnode_put_attrib(y, "xmlns", NS_ENCRYPTED); |
598 xmlnode_put_attrib(y, "xmlns", NS_ENCRYPTED); |
588 xmlnode_insert_cdata(y, enc, (unsigned) -1); |
599 xmlnode_insert_cdata(y, enc, (unsigned) -1); |
589 if (encrypted) |
600 if (encrypted) |
590 *encrypted = TRUE; |
601 *encrypted = 1; |
591 g_free(enc); |
602 g_free(enc); |
592 } |
603 } |
593 |
604 |
594 #if defined JEP0022 || defined JEP0085 |
605 #if defined JEP0022 || defined JEP0085 |
595 // If typing notifications are disabled, we can skip all this stuff... |
606 // If typing notifications are disabled, we can skip all this stuff... |