27 #include <sys/socket.h>
28 #include <sys/types.h>
35 #include <tqapplication.h>
38 #include <tdelocale.h>
39 #include <tdemessagebox.h>
40 #include <tdeconfigbase.h>
41 #include <tdeconfig.h>
42 #include <kstaticdeleter.h>
50 Module *Module::kpgpObject = 0L;
51 static KStaticDeleter<Module> kpgpod;
55 mPublicKeysCached(false),
57 mSecretKeysCached(false),
58 passphrase(0), passphrase_buffer_len(0), havePassPhrase(false)
61 kdDebug(5100) <<
"creating new pgp object" << endl;
63 kpgpObject=kpgpod.setObject(Module::kpgpObject,
this);
66 config =
new TDEConfig(
"kpgprc");
75 if (kpgpObject ==
this) kpgpObject = kpgpod.setObject( Module::kpgpObject, 0,
false );
110 storePass = config->readBoolEntry(
"storePass",
false);
111 showEncryptionResult = config->readBoolEntry(
"showEncryptionResult",
true);
112 mShowKeyApprovalDlg = config->readBoolEntry(
"showKeysForApproval",
true );
117 flagEncryptToSelf = config->readBoolEntry(
"encryptToSelf",
true);
121 Module::writeConfig(
bool sync)
123 config->writeEntry(
"storePass", storePass);
124 config->writeEntry(
"showEncryptionResult", showEncryptionResult);
125 config->writeEntry(
"showKeysForApproval", mShowKeyApprovalDlg );
127 config->writeEntry(
"encryptToSelf", flagEncryptToSelf);
140 Module::setUser(
const KeyID& keyID)
142 if (pgpUser != keyID) {
149 Module::user(
void)
const
156 Module::setEncryptToSelf(
bool flag)
158 flagEncryptToSelf = flag;
162 Module::encryptToSelf(
void)
const
164 return flagEncryptToSelf;
169 Module::setStorePassPhrase(
bool flag)
175 Module::storePassPhrase(
void)
const
181 Module::prepare(
bool needPassPhrase, Block* block )
183 if (0 == pgp) assignPGPBase();
187 errMsg = i18n(
"Could not find PGP executable.\n"
188 "Please check your PATH is set correctly.");
192 if( block && ( block->status() & NO_SEC_KEY ) )
195 if(needPassPhrase && !havePassPhrase) {
196 if( ( tGPG == pgpType ) && ( 0 != getenv(
"GPG_AGENT_INFO") ) ) {
198 kdDebug(5100) <<
"user uses gpg-agent -> don't ask for passphrase\n";
200 setPassPhrase(
"dummy" );
205 ID = block->requiredUserId();
206 PassphraseDialog passdlg(0, i18n(
"OpenPGP Security Check"),
true, ID);
207 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
208 int passdlgResult = passdlg.exec();
209 TQApplication::restoreOverrideCursor();
210 if (passdlgResult == TQDialog::Accepted) {
211 if (!setPassPhrase(passdlg.passphrase())) {
212 if (strlen(passdlg.passphrase()) >= 1024)
213 errMsg = i18n(
"Passphrase is too long, it must contain fewer than 1024 characters.");
215 errMsg = i18n(
"Out of memory.");
228 Module::wipePassPhrase(
bool freeMem)
231 if ( passphrase_buffer_len )
232 memset( passphrase, 0x00, passphrase_buffer_len );
234 kdDebug(5100) <<
"wipePassPhrase: passphrase && !passphrase_buffer_len ???" << endl;
238 if ( freeMem && passphrase ) {
241 passphrase_buffer_len = 0;
243 havePassPhrase =
false;
247 Module::verify( Block& block )
251 if (0 == pgp) assignPGPBase();
254 if( !prepare(
false, &block ) )
257 retval = pgp->verify( block );
261 errMsg = pgp->lastErrorMessage();
268 Module::decrypt( Block& block )
272 if (0 == pgp) assignPGPBase();
277 if( prepare(
true, &block ) != 1 )
280 retval = pgp->decrypt( block, passphrase );
282 if( retval & BADPHRASE ) {
284 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
285 int ret = KMessageBox::warningContinueCancel(0,
286 i18n(
"You just entered an invalid passphrase.\n"
287 "Do you want to try again, or "
288 "cancel and view the message undecrypted?"),
289 i18n(
"PGP Warning"), i18n(
"&Retry"));
290 TQApplication::restoreOverrideCursor();
291 if ( ret == KMessageBox::Cancel )
break;
301 errMsg = pgp->lastErrorMessage();
308 Module::clearsign( Block& block,
309 const KeyID& keyId,
const TQCString& charset )
311 return encrypt( block, TQStringList(), keyId,
true, charset );
315 Module::encrypt( Block& block,
316 const TQStringList& receivers,
const KeyID& keyId,
317 bool sign,
const TQCString& charset )
319 KeyIDList encryptionKeyIds;
323 if( 0 == pgp ) assignPGPBase();
327 if( !receivers.empty() ) {
328 Kpgp::Result result = getEncryptionKeys( encryptionKeyIds, receivers,
330 if( Kpgp::Ok != result ) {
335 status = doEncSign( block, encryptionKeyIds, sign );
337 if( status & CANCEL )
338 return Kpgp::Canceled;
341 while( status & BADPHRASE ) {
343 TQString str = i18n(
"You entered an invalid passphrase.\n"
344 "Do you want to try again, continue and leave the "
345 "message unsigned, or cancel sending the message?");
346 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
347 int ret = KMessageBox::warningYesNoCancel( 0, str,
350 i18n(
"Send &Unsigned") );
351 TQApplication::restoreOverrideCursor();
352 if( ret == KMessageBox::Cancel ) {
353 return Kpgp::Canceled;
355 if( ret == KMessageBox::No ) {
357 if( encryptionKeyIds.isEmpty() ) {
366 status = doEncSign( block, encryptionKeyIds, sign );
370 if( status & ERR_SIGNING ) {
371 TQString str = i18n(
"%1 = 'signing failed' error message",
372 "%1\nDo you want to send the message unsigned, "
373 "or cancel sending the message?")
374 .arg( pgp->lastErrorMessage() );
375 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
376 int ret = KMessageBox::warningContinueCancel( 0, str,
378 i18n(
"Send &Unsigned") );
379 TQApplication::restoreOverrideCursor();
380 if( ret == KMessageBox::Cancel ) {
381 return Kpgp::Canceled;
384 status = doEncSign( block, encryptionKeyIds, sign );
388 if( status & BADKEYS ) {
389 TQString str = i18n(
"%1 = 'bad keys' error message",
390 "%1\nDo you want to encrypt anyway, leave the "
391 "message as-is, or cancel sending the message?")
392 .arg( pgp->lastErrorMessage() );
394 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
395 int ret = KMessageBox::warningYesNoCancel( 0, str,
397 i18n(
"Send &Encrypted"),
398 i18n(
"Send &Unencrypted") );
399 TQApplication::restoreOverrideCursor();
400 if( ret == KMessageBox::Cancel ) {
401 return Kpgp::Canceled;
403 if( ret == KMessageBox::No ) {
406 doEncSign( block, KeyIDList(), sign );
415 if( status & MISSINGKEY ) {
416 TQString str = i18n(
"%1 = 'missing keys' error message",
417 "%1\nDo you want to leave the message as-is, "
418 "or cancel sending the message?")
419 .arg( pgp->lastErrorMessage() );
420 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
421 int ret = KMessageBox::warningContinueCancel( 0, str,
423 i18n(
"&Send As-Is") );
424 TQApplication::restoreOverrideCursor();
425 if( ret == KMessageBox::Cancel ) {
426 return Kpgp::Canceled;
432 if( status & ERROR ) {
434 errMsg = i18n(
"The following error occurred:\n%1" )
435 .arg( pgp->lastErrorMessage() );
436 TQString details = i18n(
"This is the error message of %1:\n%2" )
437 .arg( ( pgpType == tGPG ) ?
"GnuPG" :
"PGP" )
438 .arg( block.error().data() );
439 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
440 KMessageBox::detailedSorry( 0, errMsg, details );
441 TQApplication::restoreOverrideCursor();
442 return Kpgp::Failure;
445 if( showCipherText() ) {
447 CipherTextDialog *cipherTextDlg =
new CipherTextDialog( block.text(), charset );
448 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
449 bool result = ( cipherTextDlg->exec() == TQDialog::Accepted );
450 TQApplication::restoreOverrideCursor();
451 delete cipherTextDlg;
452 return result == TQDialog::Accepted ? Kpgp::Ok : Kpgp::Canceled;
458 Module::doEncSign( Block& block,
459 const KeyIDList& recipientKeyIds,
bool sign )
463 if( 0 == pgp ) assignPGPBase();
466 if( !havePgp )
return OK;
469 int result = prepare(
true, &block );
476 retval = pgp->encsign( block, recipientKeyIds, passphrase );
479 if( !prepare(
false, &block ) )
return ERROR;
480 retval = pgp->encrypt( block, recipientKeyIds );
489 Module::getEncryptionKeys( KeyIDList& encryptionKeyIds,
490 const TQStringList& recipients,
493 if( recipients.empty() ) {
494 encryptionKeyIds.clear();
500 TQValueVector<KeyIDList> recipientKeyIds( recipients.count() + 1 );
502 if( encryptToSelf() ) {
503 recipientKeyIds[0] = KeyIDList( keyId );
506 recipientKeyIds[0] = KeyIDList();
508 bool showKeysForApproval =
false;
510 for( TQStringList::ConstIterator it = recipients.begin();
511 it != recipients.end(); ++it, ++i ) {
512 EncryptPref encrPref = encryptionPreference( *it );
513 if( ( encrPref == UnknownEncryptPref ) || ( encrPref == NeverEncrypt ) )
514 showKeysForApproval =
true;
516 KeyIDList keyIds = getEncryptionKeys( *it );
517 if( keyIds.isEmpty() ) {
518 showKeysForApproval =
true;
520 recipientKeyIds[i] = keyIds;
523 kdDebug(5100) <<
"recipientKeyIds = (\n";
524 TQValueVector<KeyIDList>::const_iterator kit;
525 for( kit = recipientKeyIds.begin(); kit != recipientKeyIds.end(); ++kit ) {
526 kdDebug(5100) <<
"( 0x" << (*kit).toStringList().join(
", 0x" )
529 kdDebug(5100) <<
")\n";
531 if( showKeysForApproval || mShowKeyApprovalDlg ) {
534 unsigned int allowedKeys = PublicKeys | EncryptionKeys | ValidKeys | TrustedKeys;
537 if( pgpType != tGPG ) {
539 allowedKeys |= TrustedKeys;
543 KeyApprovalDialog dlg( recipients, recipientKeyIds, allowedKeys );
545 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
546 int ret = dlg.exec();
548 if( ret == TQDialog::Rejected ) {
549 TQApplication::restoreOverrideCursor();
550 return Kpgp::Canceled;
553 recipientKeyIds = dlg.keys();
554 TQApplication::restoreOverrideCursor();
558 unsigned int emptyListCount = 0;
559 for( TQValueVector<KeyIDList>::const_iterator it = recipientKeyIds.begin();
560 it != recipientKeyIds.end(); ++it ) {
561 if( (*it).isEmpty() ) {
563 if( it != recipientKeyIds.begin() ) {
568 for( KeyIDList::ConstIterator kit = (*it).begin();
569 kit != (*it).end(); kit++ ) {
570 encryptionKeyIds.append( *kit );
579 if( recipientKeyIds.size() == emptyListCount + 1 ) {
580 TQString str = ( recipients.count() == 1 )
581 ? i18n(
"You did not select an encryption key for the "
582 "recipient of this message; therefore, the message "
583 "will not be encrypted.")
584 : i18n(
"You did not select an encryption key for any of the "
585 "recipients of this message; therefore, the message "
586 "will not be encrypted.");
587 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
588 int ret = KMessageBox::warningContinueCancel( 0, str,
590 i18n(
"Send &Unencrypted") );
591 TQApplication::restoreOverrideCursor();
592 if( ret == KMessageBox::Cancel ) {
593 return Kpgp::Canceled;
596 encryptionKeyIds.clear();
598 else if( emptyListCount > 0 ) {
599 TQString str = ( emptyListCount == 1 )
600 ? i18n(
"You did not select an encryption key for one of "
601 "the recipients; this person will not be able to "
602 "decrypt the message if you encrypt it.")
603 : i18n(
"You did not select encryption keys for some of "
604 "the recipients; these persons will not be able to "
605 "decrypt the message if you encrypt it." );
606 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
607 int ret = KMessageBox::warningYesNoCancel( 0, str,
609 i18n(
"Send &Encrypted"),
610 i18n(
"Send &Unencrypted") );
611 TQApplication::restoreOverrideCursor();
612 if( ret == KMessageBox::Cancel ) {
613 return Kpgp::Canceled;
615 else if( ret == KMessageBox::No ) {
617 encryptionKeyIds.clear();
625 Module::encryptionPossible(
const TQStringList& recipients )
627 if( 0 == pgp ) assignPGPBase();
632 if( recipients.empty() )
635 int noKey = 0, never = 0, unknown = 0, always = 0, aip = 0, ask = 0,
637 for( TQStringList::ConstIterator it = recipients.begin();
638 it != recipients.end(); ++it) {
639 if( haveTrustedEncryptionKey( *it ) ) {
640 EncryptPref encrPref = encryptionPreference( *it );
645 case UnknownEncryptPref:
651 case AlwaysEncryptIfPossible:
654 case AlwaysAskForEncryption:
657 case AskWheneverPossible:
667 if( ( always+aip > 0 ) && ( never+unknown+ask+askwp+noKey == 0 ) ) {
671 if( ( unknown+ask+askwp > 0 ) && ( never+noKey == 0 ) ) {
675 if( ( never+noKey > 0 ) && ( always+ask == 0 ) ) {
683 Module::signKey(
const KeyID& keyId)
685 if (0 == pgp) assignPGPBase();
687 if( prepare(
true ) != 1 )
689 if(pgp->signKey(keyId, passphrase) & ERROR)
691 errMsg = pgp->lastErrorMessage();
701 if (0 == pgp) assignPGPBase();
703 if (!prepare())
return KeyList();
705 if( !mPublicKeysCached ) {
716 if (0 == pgp) assignPGPBase();
718 if (!prepare())
return KeyList();
720 if( !mSecretKeysCached ) {
729 Module::publicKey(
const KeyID& keyID)
733 for( KeyListIterator it( mPublicKeys ); (*it); ++it )
734 if( keyID == (*it)->primaryKeyID() ||
735 keyID == (*it)->primaryFingerprint() )
742 Module::publicKey(
const TQString& userID )
746 for( KeyListIterator it( mPublicKeys ); (*it); ++it )
747 if( (*it)->matchesUserID( userID ) )
754 Module::secretKey(
const KeyID& keyID)
758 for( KeyListIterator it( mSecretKeys ); (*it); ++it )
759 if( keyID == (*it)->primaryKeyID() ||
760 keyID == (*it)->primaryFingerprint() )
767 Module::keyTrust(
const KeyID& keyID )
769 Key *key = publicKey( keyID );
771 if( ( 0 == key ) || ( key->keyTrust() == KPGP_VALIDITY_UNKNOWN ) )
773 key = rereadKey( keyID,
true );
775 return KPGP_VALIDITY_UNKNOWN;
778 return key->keyTrust();
782 Module::keyTrust(
const TQString& userID )
784 Key *key = publicKey( userID );
787 return KPGP_VALIDITY_UNKNOWN;
789 if( key->keyTrust() == KPGP_VALIDITY_UNKNOWN )
791 key = rereadKey( key->primaryKeyID(),
true );
793 return KPGP_VALIDITY_UNKNOWN;
796 return key->keyTrust();
800 Module::isTrusted(
const KeyID& keyID )
802 return ( keyTrust( keyID ) >= KPGP_VALIDITY_MARGINAL );
806 Module::rereadKey(
const KeyID& keyID,
const bool readTrust )
808 if( 0 == pgp ) assignPGPBase();
811 Key* oldKey = publicKey( keyID );
813 Key* newKey = pgp->readPublicKey( keyID, readTrust, oldKey );
815 if( ( 0 == oldKey ) && ( 0 != newKey ) )
817 mPublicKeys.inSort( newKey );
818 kdDebug(5100) <<
"New public key 0x" << newKey->primaryKeyID() <<
" ("
819 << newKey->primaryUserID() <<
").\n";
821 else if( ( 0 != oldKey ) && ( 0 == newKey ) )
823 kdDebug(5100) <<
"Public key 0x" << oldKey->primaryKeyID() <<
" ("
824 << oldKey->primaryUserID() <<
") will be removed.\n";
825 mPublicKeys.removeRef( oldKey );
832 Module::getAsciiPublicKey(
const KeyID& keyID)
834 if (0 == pgp) assignPGPBase();
836 return pgp->getAsciiPublicKey(keyID);
840 bool Module::setPassPhrase(
const char * aPass)
848 size_t newlen = strlen( aPass );
849 if ( newlen >= 1024 ) {
855 if ( passphrase_buffer_len < newlen + 1 ) {
860 passphrase_buffer_len = (newlen + 1 + 15) & ~0xF;
861 passphrase = (
char*)malloc( passphrase_buffer_len );
863 passphrase_buffer_len = 0;
867 memcpy( passphrase, aPass, newlen + 1 );
868 havePassPhrase =
true;
874 Module::changePassPhrase()
877 KMessageBox::information(0,i18n(
"This feature is\nstill missing"));
882 Module::clear(
const bool erasePassPhrase)
885 wipePassPhrase(
true);
889 Module::lastErrorMsg(
void)
const
895 Module::havePGP(
void)
const
901 Module::setShowCipherText(
const bool flag)
903 showEncryptionResult = flag;
907 Module::showCipherText(
void)
const
909 return showEncryptionResult;
913 Module::selectSecretKey(
const TQString& title,
914 const TQString& text,
922 return selectKey( secretKeys(), title, text, keyId, SecretKeys );
925 KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed "
926 "or you chose not to use GnuPG/PGP.") );
932 Module::selectPublicKey(
const TQString& title,
933 const TQString& text ,
934 const KeyID& oldKeyId ,
935 const TQString& address ,
936 const unsigned int allowedKeys )
945 if( address.isEmpty() ) {
946 keyId = selectKey( publicKeys(), title, text, oldKeyId, allowedKeys );
950 keyId = selectKey( rememberChoice, publicKeys(), title, text, oldKeyId,
952 if( !keyId.isEmpty() && rememberChoice ) {
953 setKeysForAddress( address, KeyIDList( keyId ) );
960 KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed "
961 "or you chose not to use GnuPG/PGP.") );
968 Module::selectPublicKeys(
const TQString& title,
969 const TQString& text ,
970 const KeyIDList& oldKeyIds ,
971 const TQString& address ,
972 const unsigned int allowedKeys )
981 if( address.isEmpty() ) {
982 keyIds = selectKeys( publicKeys(), title, text, oldKeyIds, allowedKeys );
986 keyIds = selectKeys( rememberChoice, publicKeys(), title, text,
987 oldKeyIds, allowedKeys );
988 if( !keyIds.isEmpty() && rememberChoice ) {
989 setKeysForAddress( address, keyIds );
996 KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed "
997 "or you chose not to use GnuPG/PGP.") );
1010 kpgpObject =
new Module();
1019 return getKpgp()->config;
1024 Module::prepareMessageForDecryption(
const TQCString& msg,
1025 TQPtrList<Block>& pgpBlocks,
1026 TQStrList& nonPgpBlocks )
1028 BlockType pgpBlock = NoPgpBlock;
1032 pgpBlocks.setAutoDelete(
true );
1034 nonPgpBlocks.setAutoDelete(
true );
1035 nonPgpBlocks.clear();
1039 nonPgpBlocks.append(
"" );
1043 if( !strncmp( msg.data(),
"-----BEGIN PGP ", 15 ) )
1047 start = msg.find(
"\n-----BEGIN PGP" ) + 1;
1050 nonPgpBlocks.append( msg );
1055 while( start != -1 )
1057 int nextEnd, nextStart;
1060 if( !strncmp( msg.data() + start + 15,
"SIGNED", 6 ) )
1061 pgpBlock = ClearsignedBlock;
1063 pgpBlock = UnknownBlock;
1065 nextEnd = msg.find(
"\n-----END PGP", start + 15 );
1068 nonPgpBlocks.append( msg.mid( lastEnd+1 ) );
1071 nextStart = msg.find(
"\n-----BEGIN PGP", start + 15 );
1073 if( ( nextStart == -1 ) || ( nextEnd < nextStart ) ||
1074 ( pgpBlock == ClearsignedBlock ) )
1077 nonPgpBlocks.append( msg.mid( lastEnd+1, start-lastEnd-1 ) );
1078 lastEnd = msg.find(
"\n", nextEnd + 14 );
1081 pgpBlocks.append(
new Block( msg.mid( start ) ) );
1082 nonPgpBlocks.append(
"" );
1087 pgpBlocks.append(
new Block( msg.mid( start, lastEnd+1-start ) ) );
1088 if( ( nextStart != -1 ) && ( nextEnd > nextStart ) )
1089 nextStart = msg.find(
"\n-----BEGIN PGP", lastEnd+1 );
1095 nonPgpBlocks.append( msg.mid( lastEnd+1 ) );
1100 return ( !pgpBlocks.isEmpty() );
1107 Module::haveTrustedEncryptionKey(
const TQString& person )
1109 if( 0 == pgp ) assignPGPBase();
1111 if( !usePGP() )
return false;
1115 TQString address = canonicalAddress( person ).lower();
1118 KeyIDList keyIds = keysForAddress( address );
1119 if( !keyIds.isEmpty() ) {
1121 for( KeyIDList::ConstIterator it = keyIds.begin();
1122 it != keyIds.end(); ++it ) {
1125 Key *key = publicKey( *it );
1126 if( key && ( key->isValidEncryptionKey() ) &&
1127 ( key->keyTrust() >= KPGP_VALIDITY_MARGINAL ) )
1133 KeyListIterator it( mPublicKeys );
1136 for( it.toFirst(); (*it); ++it ) {
1138 if( (*it)->matchesUserID( person,
false ) ) {
1139 keyTrust( (*it)->primaryKeyID() );
1141 if( ( (*it)->isValidEncryptionKey() ) &&
1142 ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
1150 for( it.toFirst(); (*it); ++it ) {
1152 if( (*it)->matchesUserID( address,
false ) ) {
1153 keyTrust( (*it)->primaryKeyID() );
1155 if( ( (*it)->isValidEncryptionKey() ) &&
1156 ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
1167 Module::getEncryptionKeys(
const TQString& person )
1169 if( 0 == pgp ) assignPGPBase();
1171 if( !usePGP() )
return KeyIDList();
1175 TQString address = canonicalAddress( person ).lower();
1179 unsigned int allowedKeys = PublicKeys | EncryptionKeys | ValidKeys | TrustedKeys;
1182 if( pgpType != tGPG ) {
1184 allowedKeys |= TrustedKeys;
1189 KeyIDList keyIds = keysForAddress( address );
1190 if( !keyIds.isEmpty() ) {
1191 kdDebug(5100) <<
"Using encryption keys 0x"
1192 << keyIds.toStringList().join(
", 0x" )
1193 <<
" for " << person << endl;
1196 for( KeyIDList::ConstIterator it = keyIds.begin();
1197 it != keyIds.end(); ++it ) {
1200 Key *key = publicKey( *it );
1201 if( !( key && ( key->isValidEncryptionKey() ) &&
1202 ( key->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) )
1209 bool rememberChoice;
1210 keyIds = selectKeys( rememberChoice, mPublicKeys,
1211 i18n(
"Encryption Key Selection"),
1212 i18n(
"if in your language something like "
1213 "'key(s)' isn't possible please "
1214 "use the plural in the translation",
1215 "There is a problem with the "
1216 "encryption key(s) for \"%1\".\n\n"
1217 "Please re-select the key(s) which should "
1218 "be used for this recipient."
1222 if( !keyIds.isEmpty() ) {
1223 if( rememberChoice ) {
1224 setKeysForAddress( person, keyIds );
1232 KeyListIterator it( mPublicKeys );
1233 KeyList matchingKeys;
1236 kdDebug(5100) <<
"Looking for keys matching " << person <<
" ...\n";
1237 for( it.toFirst(); (*it); ++it ) {
1239 if( (*it)->matchesUserID( person,
false ) ) {
1240 keyTrust( (*it)->primaryKeyID() );
1242 if( ( (*it)->isValidEncryptionKey() ) &&
1243 ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
1244 kdDebug(5100) <<
"Matching trusted key found: "
1245 << (*it)->primaryKeyID() << endl;
1246 matchingKeys.append( *it );
1253 kdDebug(5100) <<
"Looking for keys matching " << address <<
" ...\n";
1254 if( matchingKeys.isEmpty() ) {
1255 for ( it.toFirst(); (*it); ++it ) {
1257 if( (*it)->matchesUserID( address,
false ) ) {
1258 keyTrust( (*it)->primaryKeyID() );
1260 if( ( (*it)->isValidEncryptionKey() ) &&
1261 ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
1262 kdDebug(5100) <<
"Matching trusted key found: "
1263 << (*it)->primaryKeyID() << endl;
1264 matchingKeys.append( *it );
1271 if( matchingKeys.isEmpty() ) {
1273 bool rememberChoice;
1274 KeyIDList keyIds = selectKeys( rememberChoice, mPublicKeys,
1275 i18n(
"Encryption Key Selection"),
1276 i18n(
"if in your language something like "
1277 "'key(s)' isn't possible please "
1278 "use the plural in the translation",
1279 "No valid and trusted OpenPGP key was "
1280 "found for \"%1\".\n\n"
1281 "Select the key(s) which should "
1282 "be used for this recipient."
1286 if( !keyIds.isEmpty() ) {
1287 if( rememberChoice ) {
1288 setKeysForAddress( person, keyIds );
1294 else if( matchingKeys.count() == 1 ) {
1295 return KeyIDList( matchingKeys.getFirst()->primaryKeyID() );
1299 bool rememberChoice;
1300 KeyIDList keyIds = selectKeys( rememberChoice, matchingKeys,
1301 i18n(
"Encryption Key Selection"),
1302 i18n(
"if in your language something like "
1303 "'key(s)' isn't possible please "
1304 "use the plural in the translation",
1305 "More than one key matches \"%1\".\n\n"
1306 "Select the key(s) which should "
1307 "be used for this recipient."
1311 if( !keyIds.isEmpty() ) {
1312 if( rememberChoice ) {
1313 setKeysForAddress( person, keyIds );
1325 Module::checkForPGP(
void)
1329 TQStrList pSearchPaths;
1335 path = getenv(
"PATH");
1336 while((index = path.find(
":",lastindex+1)) != -1)
1338 pSearchPaths.append(path.mid(lastindex+1,index-lastindex-1));
1341 if(lastindex != (
int)path.length() - 1)
1342 pSearchPaths.append( path.mid(lastindex+1,path.length()-lastindex) );
1344 TQStrListIterator it(pSearchPaths);
1349 for ( it.toFirst() ; it.current() ; ++it )
1353 if ( !access( path, X_OK ) )
1355 kdDebug(5100) <<
"Kpgp: gpg found" << endl;
1364 for ( it.toFirst() ; it.current() ; ++it )
1368 if ( !access( path, X_OK ) )
1370 kdDebug(5100) <<
"Kpgp: pgp 5 found" << endl;
1379 for ( it.toFirst() ; it.current() ; ++it )
1381 path = it.current();
1383 if ( !access( path, X_OK ) )
1385 kdDebug(5100) <<
"Kpgp: pgp 2 or 6 found" << endl;
1394 kdDebug(5100) <<
"Kpgp: no pgp found" << endl;
1401 Module::assignPGPBase(
void)
1411 kdDebug(5100) <<
"Kpgp: assign pgp - gpg" << endl;
1416 kdDebug(5100) <<
"Kpgp: assign pgp - pgp 2" << endl;
1421 kdDebug(5100) <<
"Kpgp: assign pgp - pgp 5" << endl;
1426 kdDebug(5100) <<
"Kpgp: assign pgp - pgp 6" << endl;
1432 kdDebug(5100) <<
"Kpgp: pgpBase is dummy " << endl;
1437 kdDebug(5100) <<
"Kpgp: assign pgp - auto" << endl;
1440 kdDebug(5100) <<
"Kpgp: assign pgp - default" << endl;
1443 kdDebug(5100) <<
"Kpgp: pgpBase is gpg " << endl;
1449 kdDebug(5100) <<
"Kpgp: pgpBase is pgp 5" << endl;
1455 Base6 *pgp_v6 =
new Base6();
1456 if (!pgp_v6->isVersion6())
1458 kdDebug(5100) <<
"Kpgp: pgpBase is pgp 2 " << endl;
1465 kdDebug(5100) <<
"Kpgp: pgpBase is pgp 6 " << endl;
1475 kdDebug(5100) <<
"Kpgp: pgpBase is dummy " << endl;
1482 Module::canonicalAddress(
const TQString& _adress )
1486 TQString address = _adress.simplifyWhiteSpace();
1487 address = address.stripWhiteSpace();
1490 if((index = address.find(
"<")) != -1)
1491 if((index2 = address.find(
"@",index+1)) != -1)
1492 if((index2 = address.find(
">",index2+1)) != -1)
1493 return address.mid(index,index2-index+1);
1495 if((index = address.find(
"@")) == -1)
1501 return "<" + address +
"@localdomain>";
1505 int index1 = address.findRev(
" ",index);
1506 int index2 = address.find(
" ",index);
1507 if(index2 == -1) index2 = address.length();
1508 return "<" + address.mid(index1+1 ,index2-index1-1) +
">";
1513 Module::readPublicKeys(
bool reread )
1515 if( 0 == pgp ) assignPGPBase();
1519 mPublicKeys.clear();
1520 mPublicKeysCached =
false;
1524 if( !mPublicKeysCached || reread )
1526 if( mPublicKeys.isEmpty() )
1528 mPublicKeys = pgp->publicKeys();
1532 KeyList newPublicKeyList = pgp->publicKeys();
1537 KeyListIterator it( newPublicKeyList );
1538 for( it.toFirst(); (*it); ++it )
1540 Key* oldKey = publicKey( (*it)->primaryKeyID() );
1543 (*it)->cloneKeyTrust( oldKey );
1547 mPublicKeys = newPublicKeyList;
1550 mPublicKeysCached =
true;
1551 mPublicKeys.setAutoDelete(
true );
1556 Module::readSecretKeys(
bool reread )
1558 if( 0 == pgp ) assignPGPBase();
1562 mSecretKeys.clear();
1563 mSecretKeysCached =
false;
1567 if( mSecretKeys.isEmpty() || reread )
1569 if( mSecretKeys.isEmpty() )
1571 mSecretKeys = pgp->secretKeys();
1575 KeyList newSecretKeyList = pgp->secretKeys();
1580 KeyListIterator it( newSecretKeyList );
1581 for( it.toFirst(); (*it); ++it )
1583 Key* oldKey = secretKey( (*it)->primaryKeyID() );
1586 (*it)->cloneKeyTrust( oldKey );
1590 mSecretKeys = newSecretKeyList;
1593 mSecretKeysCached =
true;
1594 mSecretKeys.setAutoDelete(
true );
1599 Module::selectKey(
const KeyList& keys,
1600 const TQString& title,
1601 const TQString& text ,
1602 const KeyID& keyId ,
1603 const unsigned int allowedKeys )
1605 KeyID retval = KeyID();
1607 KeySelectionDialog dlg( keys, title, text, KeyIDList( keyId ),
false,
1608 allowedKeys,
false );
1610 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
1611 bool rej = ( dlg.exec() == TQDialog::Rejected );
1612 TQApplication::restoreOverrideCursor();
1622 Module::selectKeys(
const KeyList& keys,
1623 const TQString& title,
1624 const TQString& text ,
1625 const KeyIDList& keyIds ,
1626 const unsigned int allowedKeys )
1628 KeyIDList retval = KeyIDList();
1630 KeySelectionDialog dlg( keys, title, text, keyIds,
false, allowedKeys,
1633 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
1634 bool rej = ( dlg.exec() == TQDialog::Rejected );
1635 TQApplication::restoreOverrideCursor();
1638 retval = dlg.keys();
1646 Module::selectKey(
bool& rememberChoice,
1647 const KeyList& keys,
1648 const TQString& title,
1649 const TQString& text ,
1650 const KeyID& keyId ,
1651 const unsigned int allowedKeys )
1653 KeyID retval = KeyID();
1655 KeySelectionDialog dlg( keys, title, text, KeyIDList( keyId ),
false,
1656 allowedKeys,
false );
1658 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
1659 bool rej = ( dlg.exec() == TQDialog::Rejected );
1660 TQApplication::restoreOverrideCursor();
1664 rememberChoice = dlg.rememberSelection();
1667 rememberChoice =
false;
1674 Module::selectKeys(
bool& rememberChoice,
1675 const KeyList& keys,
1676 const TQString& title,
1677 const TQString& text ,
1678 const KeyIDList& keyIds ,
1679 const unsigned int allowedKeys )
1681 KeyIDList retval = KeyIDList();
1683 KeySelectionDialog dlg( keys, title, text, keyIds,
true, allowedKeys,
1686 TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
1687 bool rej = ( dlg.exec() == TQDialog::Rejected );
1688 TQApplication::restoreOverrideCursor();
1691 retval = dlg.keys();
1692 rememberChoice = dlg.rememberSelection();
1695 rememberChoice =
false;
1702 Module::keysForAddress(
const TQString& address )
1704 if( address.isEmpty() ) {
1707 TQString addr = canonicalAddress( address ).lower();
1708 if( addressDataDict.contains( addr ) ) {
1709 return addressDataDict[addr].keyIds;
1717 Module::setKeysForAddress(
const TQString& address,
const KeyIDList& keyIds )
1719 if( address.isEmpty() ) {
1722 TQString addr = canonicalAddress( address ).lower();
1723 if( addressDataDict.contains( addr ) ) {
1724 addressDataDict[addr].keyIds = keyIds;
1728 data.encrPref = UnknownEncryptPref;
1729 data.keyIds = keyIds;
1730 addressDataDict.insert( addr, data );
1737 Module::readAddressData()
1742 TDEConfigGroup general( config,
"General" );
1743 int num = general.readNumEntry(
"addressEntries", 0 );
1745 addressDataDict.clear();
1746 for(
int i=1; i<=num; i++ ) {
1747 TDEConfigGroup addrGroup( config, TQString(
"Address #%1").arg(i).local8Bit() );
1748 address = addrGroup.readEntry(
"Address" );
1749 data.keyIds = KeyIDList::fromStringList( addrGroup.readListEntry(
"Key IDs" ) );
1750 data.encrPref = (EncryptPref) addrGroup.readNumEntry(
"EncryptionPreference",
1751 UnknownEncryptPref );
1755 if ( !address.isEmpty() ) {
1756 addressDataDict.insert( address, data );
1762 Module::writeAddressData()
1764 TDEConfigGroup general( config,
"General" );
1765 general.writeEntry(
"addressEntries", addressDataDict.count() );
1768 AddressDataDict::Iterator it;
1769 for ( i=1, it = addressDataDict.begin();
1770 it != addressDataDict.end();
1772 TDEConfigGroup addrGroup( config, TQString(
"Address #%1").arg(i).local8Bit() );
1773 addrGroup.writeEntry(
"Address", it.key() );
1774 addrGroup.writeEntry(
"Key IDs", it.data().keyIds.toStringList() );
1775 addrGroup.writeEntry(
"EncryptionPreference", it.data().encrPref );
1782 Module::encryptionPreference(
const TQString& address )
1784 TQString addr = canonicalAddress( address ).lower();
1785 if( addressDataDict.contains( addr ) ) {
1786 return addressDataDict[addr].encrPref;
1789 return UnknownEncryptPref;
1794 Module::setEncryptionPreference(
const TQString& address,
1795 const EncryptPref pref )
1797 if( address.isEmpty() ) {
1800 TQString addr = canonicalAddress( address ).lower();
1801 if( addressDataDict.contains( addr ) ) {
1802 addressDataDict[addr].encrPref = pref;
1806 data.encrPref = pref;
1807 addressDataDict.insert( addr, data );