27#include <sys/socket.h> 
   35#include <tqapplication.h> 
   39#include <tdemessagebox.h> 
   40#include <tdeconfigbase.h> 
   42#include <kstaticdeleter.h> 
   50Module *Module::kpgpObject = 0L;
 
   51static KStaticDeleter<Module> kpgpod;
 
   55    mPublicKeysCached(false),
 
   57    mSecretKeysCached(false),
 
   58    passphrase(TQString::null), 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);
 
  121Module::writeConfig(
bool sync)
 
  123  config->writeEntry(
"storePass", storePass);
 
  124  config->writeEntry(
"showEncryptionResult", showEncryptionResult);
 
  125  config->writeEntry( 
"showKeysForApproval", mShowKeyApprovalDlg );
 
  127  config->writeEntry(
"encryptToSelf", flagEncryptToSelf);
 
  140Module::setUser(
const KeyID& keyID)
 
  142  if (pgpUser != keyID) {
 
  149Module::user(
void)
 const 
  156Module::setEncryptToSelf(
bool flag)
 
  158  flagEncryptToSelf = flag;
 
  162Module::encryptToSelf(
void)
 const 
  164  return flagEncryptToSelf;
 
  169Module::setStorePassPhrase(
bool flag)
 
  175Module::storePassPhrase(
void)
 const 
  181Module::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 (passdlg.passphrase().length() >= 1024)
 
  213             errMsg = i18n(
"Passphrase is too long, it must contain fewer than 1024 characters.");
 
  215             errMsg = i18n(
"Out of memory.");
 
  228Module::wipePassPhrase(
bool freeMem)
 
  230  if (!passphrase.isEmpty()) {
 
  231    passphrase.fill(
' ');
 
  234    passphrase.truncate(0);
 
  236  havePassPhrase = 
false;
 
  240Module::verify( Block& block )
 
  244  if (0 == pgp) assignPGPBase();
 
  247  if( !prepare( 
false, &block ) )
 
  250  retval = pgp->verify( block );
 
  254    errMsg = pgp->lastErrorMessage();
 
  261Module::decrypt( Block& block )
 
  265  if (0 == pgp) assignPGPBase();
 
  270    if( prepare( 
true, &block ) != 1 )
 
  273    retval = pgp->decrypt( block, passphrase );
 
  275    if( retval & BADPHRASE ) {
 
  277      TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  278      int ret = KMessageBox::warningContinueCancel(0,
 
  279         i18n(
"You just entered an invalid passphrase.\n" 
  280          "Do you want to try again, or " 
  281          "cancel and view the message undecrypted?"),
 
  282         i18n(
"PGP Warning"), i18n(
"&Retry"));
 
  283      TQApplication::restoreOverrideCursor();
 
  284      if ( ret == KMessageBox::Cancel ) 
break;
 
  294    errMsg = pgp->lastErrorMessage();
 
  301Module::clearsign( Block& block,
 
  302                   const KeyID& keyId, 
const TQCString& charset )
 
  304  return encrypt( block, TQStringList(), keyId, 
true, charset );
 
  308Module::encrypt( Block& block,
 
  309                 const TQStringList& receivers, 
const KeyID& keyId,
 
  310                 bool sign, 
const TQCString& charset )
 
  312  KeyIDList encryptionKeyIds; 
 
  316  if( 0 == pgp ) assignPGPBase();
 
  320  if( !receivers.empty() ) {
 
  321    Kpgp::Result result = getEncryptionKeys( encryptionKeyIds, receivers,
 
  323    if( Kpgp::Ok != result ) {
 
  328  status = doEncSign( block, encryptionKeyIds, sign );
 
  330  if( status & CANCEL )
 
  331    return Kpgp::Canceled;
 
  334  while( status & BADPHRASE ) {
 
  336    TQString str = i18n(
"You entered an invalid passphrase.\n" 
  337                       "Do you want to try again, continue and leave the " 
  338                       "message unsigned, or cancel sending the message?");
 
  339    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  340    int ret = KMessageBox::warningYesNoCancel( 0, str,
 
  343                                               i18n(
"Send &Unsigned") );
 
  344    TQApplication::restoreOverrideCursor();
 
  345    if( ret == KMessageBox::Cancel ) {
 
  346      return Kpgp::Canceled;
 
  348    if( ret == KMessageBox::No ) {
 
  350      if( encryptionKeyIds.isEmpty() ) {
 
  359    status = doEncSign( block, encryptionKeyIds, sign );
 
  363  if( status & ERR_SIGNING ) {
 
  364    TQString str = i18n(
"%1 = 'signing failed' error message",
 
  365                       "%1\nDo you want to send the message unsigned, " 
  366                       "or cancel sending the message?")
 
  367                  .arg( pgp->lastErrorMessage() );
 
  368    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  369    int ret = KMessageBox::warningContinueCancel( 0, str,
 
  371                                                  i18n(
"Send &Unsigned") );
 
  372    TQApplication::restoreOverrideCursor();
 
  373    if( ret == KMessageBox::Cancel ) {
 
  374      return Kpgp::Canceled;
 
  377    status = doEncSign( block, encryptionKeyIds, sign );
 
  381  if( status & BADKEYS ) {
 
  382    TQString str = i18n(
"%1 = 'bad keys' error message",
 
  383                       "%1\nDo you want to encrypt anyway, leave the " 
  384                       "message as-is, or cancel sending the message?")
 
  385                  .arg( pgp->lastErrorMessage() );
 
  387    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  388    int ret = KMessageBox::warningYesNoCancel( 0, str,
 
  390                                               i18n(
"Send &Encrypted"),
 
  391                                               i18n(
"Send &Unencrypted") );
 
  392    TQApplication::restoreOverrideCursor();
 
  393    if( ret == KMessageBox::Cancel ) {
 
  394      return Kpgp::Canceled;
 
  396    if( ret == KMessageBox::No ) {
 
  399        doEncSign( block, KeyIDList(), sign );
 
  408  if( status & MISSINGKEY ) {
 
  409    TQString str = i18n(
"%1 = 'missing keys' error message",
 
  410                       "%1\nDo you want to leave the message as-is, " 
  411                       "or cancel sending the message?")
 
  412                  .arg( pgp->lastErrorMessage() );
 
  413    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  414    int ret = KMessageBox::warningContinueCancel( 0, str,
 
  416                                                  i18n(
"&Send As-Is") );
 
  417    TQApplication::restoreOverrideCursor();
 
  418    if( ret == KMessageBox::Cancel ) {
 
  419      return Kpgp::Canceled;
 
  425  if( status & ERROR ) {
 
  427    errMsg = i18n( 
"The following error occurred:\n%1" )
 
  428             .arg( pgp->lastErrorMessage() );
 
  429    TQString details = i18n( 
"This is the error message of %1:\n%2" )
 
  430                      .arg( ( pgpType == tGPG ) ? 
"GnuPG" : 
"PGP" )
 
  431                      .arg( block.error().data() );
 
  432    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  433    KMessageBox::detailedSorry( 0, errMsg, details );
 
  434    TQApplication::restoreOverrideCursor();
 
  435    return Kpgp::Failure;
 
  438  if( showCipherText() ) {
 
  440    CipherTextDialog *cipherTextDlg = 
new CipherTextDialog( block.text(), charset );
 
  441    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  442    bool result = ( cipherTextDlg->exec() == TQDialog::Accepted );
 
  443    TQApplication::restoreOverrideCursor();
 
  444    delete cipherTextDlg;
 
  445    return result == TQDialog::Accepted ? Kpgp::Ok : Kpgp::Canceled;
 
  451Module::doEncSign( Block& block,
 
  452                   const KeyIDList& recipientKeyIds, 
bool sign )
 
  456  if( 0 == pgp ) assignPGPBase();
 
  459  if( !havePgp ) 
return OK;
 
  462    int result = prepare( 
true, &block );
 
  469    retval = pgp->encsign( block, recipientKeyIds, passphrase );
 
  472    if( !prepare( 
false, &block ) ) 
return ERROR;
 
  473    retval = pgp->encrypt( block, recipientKeyIds );
 
  482Module::getEncryptionKeys( KeyIDList& encryptionKeyIds,
 
  483                           const TQStringList& recipients,
 
  486  if( recipients.empty() ) {
 
  487    encryptionKeyIds.clear();
 
  493  TQValueVector<KeyIDList> recipientKeyIds( recipients.count() + 1 );
 
  495  if( encryptToSelf() ) {
 
  496    recipientKeyIds[0] = KeyIDList( keyId );
 
  499    recipientKeyIds[0] = KeyIDList();
 
  501  bool showKeysForApproval = 
false;
 
  503  for( TQStringList::ConstIterator it = recipients.begin();
 
  504       it != recipients.end(); ++it, ++i ) {
 
  505    EncryptPref encrPref = encryptionPreference( *it );
 
  506    if( ( encrPref == UnknownEncryptPref ) || ( encrPref == NeverEncrypt ) )
 
  507      showKeysForApproval = 
true;
 
  509    KeyIDList keyIds = getEncryptionKeys( *it );
 
  510    if( keyIds.isEmpty() ) {
 
  511      showKeysForApproval = 
true;
 
  513    recipientKeyIds[i] = keyIds;
 
  516  kdDebug(5100) << 
"recipientKeyIds = (\n";
 
  517  TQValueVector<KeyIDList>::const_iterator kit;
 
  518  for( kit = recipientKeyIds.begin(); kit != recipientKeyIds.end(); ++kit ) {
 
  519    kdDebug(5100) << 
"( 0x" << (*kit).toStringList().join( 
", 0x" )
 
  522  kdDebug(5100) << 
")\n";
 
  524  if( showKeysForApproval || mShowKeyApprovalDlg ) {
 
  527    unsigned int allowedKeys = PublicKeys | EncryptionKeys | ValidKeys | TrustedKeys;
 
  530    if( pgpType != tGPG ) {
 
  532      allowedKeys |= TrustedKeys;
 
  536    KeyApprovalDialog dlg( recipients, recipientKeyIds, allowedKeys );
 
  538    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  539    int ret = dlg.exec();
 
  541    if( ret == TQDialog::Rejected ) {
 
  542        TQApplication::restoreOverrideCursor();
 
  543        return Kpgp::Canceled;
 
  546    recipientKeyIds = dlg.keys();
 
  547    TQApplication::restoreOverrideCursor();
 
  551  unsigned int emptyListCount = 0;
 
  552  for( TQValueVector<KeyIDList>::const_iterator it = recipientKeyIds.begin();
 
  553       it != recipientKeyIds.end(); ++it ) {
 
  554    if( (*it).isEmpty() ) {
 
  556      if( it != recipientKeyIds.begin() ) {
 
  561      for( KeyIDList::ConstIterator kit = (*it).begin();
 
  562           kit != (*it).end(); kit++ ) {
 
  563        encryptionKeyIds.append( *kit );
 
  572  if( recipientKeyIds.size() == emptyListCount + 1 ) { 
 
  573    TQString str = ( recipients.count() == 1 )
 
  574                  ? i18n(
"You did not select an encryption key for the " 
  575                         "recipient of this message; therefore, the message " 
  576                         "will not be encrypted.")
 
  577                  : i18n(
"You did not select an encryption key for any of the " 
  578                         "recipients of this message; therefore, the message " 
  579                         "will not be encrypted.");
 
  580    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  581    int ret = KMessageBox::warningContinueCancel( 0, str,
 
  583                                                  i18n(
"Send &Unencrypted") );
 
  584    TQApplication::restoreOverrideCursor();
 
  585    if( ret == KMessageBox::Cancel ) {
 
  586      return Kpgp::Canceled;
 
  589      encryptionKeyIds.clear();
 
  591  else if( emptyListCount > 0 ) {
 
  592    TQString str = ( emptyListCount == 1 )
 
  593                  ? i18n(
"You did not select an encryption key for one of " 
  594                         "the recipients; this person will not be able to " 
  595                         "decrypt the message if you encrypt it.")
 
  596                  : i18n(
"You did not select encryption keys for some of " 
  597                         "the recipients; these persons will not be able to " 
  598                         "decrypt the message if you encrypt it." );
 
  599    TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
  600    int ret = KMessageBox::warningYesNoCancel( 0, str,
 
  602                                               i18n(
"Send &Encrypted"),
 
  603                                               i18n(
"Send &Unencrypted") );
 
  604    TQApplication::restoreOverrideCursor();
 
  605    if( ret == KMessageBox::Cancel ) {
 
  606      return Kpgp::Canceled;
 
  608    else if( ret == KMessageBox::No ) {
 
  610      encryptionKeyIds.clear();
 
  618Module::encryptionPossible( 
const TQStringList& recipients )
 
  620  if( 0 == pgp ) assignPGPBase();
 
  625  if( recipients.empty() )
 
  628  int noKey = 0, never = 0, unknown = 0, always = 0, aip = 0, ask = 0,
 
  630  for( TQStringList::ConstIterator it = recipients.begin();
 
  631       it != recipients.end(); ++it) {
 
  632    if( haveTrustedEncryptionKey( *it ) ) {
 
  633      EncryptPref encrPref = encryptionPreference( *it );
 
  638        case UnknownEncryptPref:
 
  644        case AlwaysEncryptIfPossible:
 
  647        case AlwaysAskForEncryption:
 
  650        case AskWheneverPossible:
 
  660  if( ( always+aip > 0 ) && ( never+unknown+ask+askwp+noKey == 0 ) ) {
 
  664  if( ( unknown+ask+askwp > 0 ) && ( never+noKey == 0 ) ) {
 
  668  if( ( never+noKey > 0 ) && ( always+ask == 0 ) ) {
 
  676Module::signKey(
const KeyID& keyId)
 
  678  if (0 == pgp) assignPGPBase();
 
  680  if( prepare( 
true ) != 1 )
 
  682  if(pgp->signKey(keyId, passphrase) & ERROR)
 
  684    errMsg = pgp->lastErrorMessage();
 
  694  if (0 == pgp) assignPGPBase();
 
  696  if (!prepare()) 
return KeyList();
 
  698  if( !mPublicKeysCached ) {
 
  709  if (0 == pgp) assignPGPBase();
 
  711  if (!prepare()) 
return KeyList();
 
  713  if( !mSecretKeysCached ) {
 
  722Module::publicKey(
const KeyID& keyID)
 
  726  for( KeyListIterator it( mPublicKeys ); (*it); ++it )
 
  727    if( keyID == (*it)->primaryKeyID() ||
 
  728    keyID == (*it)->primaryFingerprint() )
 
  735Module::publicKey( 
const TQString& userID )
 
  739  for( KeyListIterator it( mPublicKeys ); (*it); ++it )
 
  740    if( (*it)->matchesUserID( userID ) )
 
  747Module::secretKey(
const KeyID& keyID)
 
  751  for( KeyListIterator it( mSecretKeys ); (*it); ++it )
 
  752    if( keyID == (*it)->primaryKeyID() ||
 
  753    keyID == (*it)->primaryFingerprint() )
 
  760Module::keyTrust( 
const KeyID& keyID )
 
  762  Key *key = publicKey( keyID );
 
  764  if( ( 0 == key ) || ( key->keyTrust() == KPGP_VALIDITY_UNKNOWN ) )
 
  766    key = rereadKey( keyID, 
true );
 
  768      return KPGP_VALIDITY_UNKNOWN;
 
  771  return key->keyTrust();
 
  775Module::keyTrust( 
const TQString& userID )
 
  777  Key *key = publicKey( userID );
 
  780    return KPGP_VALIDITY_UNKNOWN;
 
  782  if( key->keyTrust() == KPGP_VALIDITY_UNKNOWN )
 
  784    key = rereadKey( key->primaryKeyID(), 
true );
 
  786      return KPGP_VALIDITY_UNKNOWN;
 
  789  return key->keyTrust();
 
  793Module::isTrusted( 
const KeyID& keyID )
 
  795  return ( keyTrust( keyID ) >= KPGP_VALIDITY_MARGINAL );
 
  799Module::rereadKey( 
const KeyID& keyID, 
const bool readTrust  )
 
  801  if( 0 == pgp ) assignPGPBase();
 
  804  Key* oldKey = publicKey( keyID );
 
  806  Key* newKey = pgp->readPublicKey( keyID, readTrust, oldKey );
 
  808  if( ( 0 == oldKey ) && ( 0 != newKey ) )
 
  810    mPublicKeys.inSort( newKey );
 
  811    kdDebug(5100) << 
"New public key 0x" << newKey->primaryKeyID() << 
" (" 
  812                  << newKey->primaryUserID() << 
").\n";
 
  814  else if( ( 0 != oldKey ) && ( 0 == newKey ) )
 
  816    kdDebug(5100) << 
"Public key 0x" << oldKey->primaryKeyID() << 
" (" 
  817                  << oldKey->primaryUserID() << 
") will be removed.\n";
 
  818    mPublicKeys.removeRef( oldKey );
 
  825Module::getAsciiPublicKey(
const KeyID& keyID)
 
  827  if (0 == pgp) assignPGPBase();
 
  829  return pgp->getAsciiPublicKey(keyID);
 
  833bool Module::setPassPhrase(
const TQString &aPass)
 
  839  if (!aPass.isEmpty())
 
  841    if (aPass.length() >= 1024) {
 
  848    havePassPhrase = 
true;
 
  854Module::changePassPhrase()
 
  857  KMessageBox::information(0,i18n(
"This feature is\nstill missing"));
 
  862Module::clear(
const bool erasePassPhrase)
 
  865    wipePassPhrase(
true);
 
  869Module::lastErrorMsg(
void)
 const 
  875Module::havePGP(
void)
 const 
  881Module::setShowCipherText(
const bool flag)
 
  883  showEncryptionResult = flag;
 
  887Module::showCipherText(
void)
 const 
  889  return showEncryptionResult;
 
  893Module::selectSecretKey( 
const TQString& title,
 
  894                         const TQString& text,
 
  902    return selectKey( secretKeys(), title, text, keyId, SecretKeys );
 
  905    KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed " 
  906                                "or you chose not to use GnuPG/PGP.") );
 
  912Module::selectPublicKey( 
const TQString& title,
 
  913                         const TQString& text ,
 
  914                         const KeyID& oldKeyId ,
 
  915                         const TQString& address ,
 
  916                         const unsigned int allowedKeys  )
 
  925    if( address.isEmpty() ) {
 
  926      keyId = selectKey( publicKeys(), title, text, oldKeyId, allowedKeys );
 
  930      keyId = selectKey( rememberChoice, publicKeys(), title, text, oldKeyId,
 
  932      if( !keyId.isEmpty() && rememberChoice ) {
 
  933        setKeysForAddress( address, KeyIDList( keyId ) );
 
  940    KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed " 
  941                                "or you chose not to use GnuPG/PGP.") );
 
  948Module::selectPublicKeys( 
const TQString& title,
 
  949                          const TQString& text ,
 
  950                          const KeyIDList& oldKeyIds ,
 
  951                          const TQString& address ,
 
  952                          const unsigned int allowedKeys  )
 
  961    if( address.isEmpty() ) {
 
  962      keyIds = selectKeys( publicKeys(), title, text, oldKeyIds, allowedKeys );
 
  966      keyIds = selectKeys( rememberChoice, publicKeys(), title, text,
 
  967                           oldKeyIds, allowedKeys );
 
  968      if( !keyIds.isEmpty() && rememberChoice ) {
 
  969        setKeysForAddress( address, keyIds );
 
  976    KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed " 
  977                                "or you chose not to use GnuPG/PGP.") );
 
  990    kpgpObject = 
new Module();
 
  999  return getKpgp()->config;
 
 1004Module::prepareMessageForDecryption( 
const TQCString& msg,
 
 1005                                     TQPtrList<Block>& pgpBlocks,
 
 1006                                     TQStrList& nonPgpBlocks )
 
 1008  BlockType pgpBlock = NoPgpBlock;
 
 1012  pgpBlocks.setAutoDelete( 
true );
 
 1014  nonPgpBlocks.setAutoDelete( 
true );
 
 1015  nonPgpBlocks.clear();
 
 1019    nonPgpBlocks.append( 
"" );
 
 1023  if( !strncmp( msg.data(), 
"-----BEGIN PGP ", 15 ) )
 
 1027    start = msg.find( 
"\n-----BEGIN PGP" ) + 1;
 
 1030      nonPgpBlocks.append( msg );
 
 1035  while( start != -1 )
 
 1037    int nextEnd, nextStart;
 
 1040    if( !strncmp( msg.data() + start + 15, 
"SIGNED", 6 ) )
 
 1041      pgpBlock = ClearsignedBlock;
 
 1043      pgpBlock = UnknownBlock;
 
 1045    nextEnd = msg.find( 
"\n-----END PGP", start + 15 );
 
 1048      nonPgpBlocks.append( msg.mid( lastEnd+1 ) );
 
 1051    nextStart = msg.find( 
"\n-----BEGIN PGP", start + 15 );
 
 1053    if( ( nextStart == -1 ) || ( nextEnd < nextStart ) ||
 
 1054        ( pgpBlock == ClearsignedBlock ) )
 
 1057      nonPgpBlocks.append( msg.mid( lastEnd+1, start-lastEnd-1 ) );
 
 1058      lastEnd = msg.find( 
"\n", nextEnd + 14 );
 
 1061        pgpBlocks.append( 
new Block( msg.mid( start ) ) );
 
 1062        nonPgpBlocks.append( 
"" );
 
 1067        pgpBlocks.append( 
new Block( msg.mid( start, lastEnd+1-start ) ) );
 
 1068        if( ( nextStart != -1 ) && ( nextEnd > nextStart ) )
 
 1069          nextStart = msg.find( 
"\n-----BEGIN PGP", lastEnd+1 );
 
 1075      nonPgpBlocks.append( msg.mid( lastEnd+1 ) );
 
 1080  return ( !pgpBlocks.isEmpty() );
 
 1087Module::haveTrustedEncryptionKey( 
const TQString& person )
 
 1089  if( 0 == pgp ) assignPGPBase();
 
 1091  if( !usePGP() ) 
return false;
 
 1095  TQString address = canonicalAddress( person ).lower();
 
 1098  KeyIDList keyIds = keysForAddress( address );
 
 1099  if( !keyIds.isEmpty() ) {
 
 1101    for( KeyIDList::ConstIterator it = keyIds.begin();
 
 1102         it != keyIds.end(); ++it ) {
 
 1105      Key *key = publicKey( *it );
 
 1106      if( key && ( key->isValidEncryptionKey() ) &&
 
 1107          ( key->keyTrust() >= KPGP_VALIDITY_MARGINAL ) )
 
 1113  KeyListIterator it( mPublicKeys );
 
 1116  for( it.toFirst(); (*it); ++it ) {
 
 1118    if( (*it)->matchesUserID( person, 
false ) ) {
 
 1119      keyTrust( (*it)->primaryKeyID() ); 
 
 1121      if( ( (*it)->isValidEncryptionKey() ) &&
 
 1122          ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
 
 1130  for( it.toFirst(); (*it); ++it ) {
 
 1132    if( (*it)->matchesUserID( address, 
false ) ) {
 
 1133      keyTrust( (*it)->primaryKeyID() ); 
 
 1135      if( ( (*it)->isValidEncryptionKey() ) &&
 
 1136          ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
 
 1147Module::getEncryptionKeys( 
const TQString& person )
 
 1149  if( 0 == pgp ) assignPGPBase();
 
 1151  if( !usePGP() ) 
return KeyIDList();
 
 1155  TQString address = canonicalAddress( person ).lower();
 
 1159  unsigned int allowedKeys = PublicKeys | EncryptionKeys | ValidKeys | TrustedKeys;
 
 1162  if( pgpType != tGPG ) {
 
 1164    allowedKeys |= TrustedKeys;
 
 1169  KeyIDList keyIds = keysForAddress( address );
 
 1170  if( !keyIds.isEmpty() ) {
 
 1171    kdDebug(5100) << 
"Using encryption keys 0x" 
 1172                  << keyIds.toStringList().join( 
", 0x" )
 
 1173                  << 
" for " << person << endl;
 
 1176    for( KeyIDList::ConstIterator it = keyIds.begin();
 
 1177         it != keyIds.end(); ++it ) {
 
 1180      Key *key = publicKey( *it );
 
 1181      if( !( key && ( key->isValidEncryptionKey() ) &&
 
 1182             ( key->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) )
 
 1189      bool rememberChoice;
 
 1190      keyIds = selectKeys( rememberChoice, mPublicKeys,
 
 1191                           i18n(
"Encryption Key Selection"),
 
 1192                           i18n(
"if in your language something like " 
 1193                                "'key(s)' isn't possible please " 
 1194                                "use the plural in the translation",
 
 1195                                "There is a problem with the " 
 1196                                "encryption key(s) for \"%1\".\n\n" 
 1197                                "Please re-select the key(s) which should " 
 1198                                "be used for this recipient." 
 1202      if( !keyIds.isEmpty() ) {
 
 1203        if( rememberChoice ) {
 
 1204          setKeysForAddress( person, keyIds );
 
 1212  KeyListIterator it( mPublicKeys );
 
 1213  KeyList matchingKeys;
 
 1216  kdDebug(5100) << 
"Looking for keys matching " << person << 
" ...\n";
 
 1217  for( it.toFirst(); (*it); ++it ) {
 
 1219    if( (*it)->matchesUserID( person, 
false ) ) {
 
 1220      keyTrust( (*it)->primaryKeyID() ); 
 
 1222      if( ( (*it)->isValidEncryptionKey() ) &&
 
 1223          ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
 
 1224        kdDebug(5100) << 
"Matching trusted key found: " 
 1225                      << (*it)->primaryKeyID() << endl;
 
 1226        matchingKeys.append( *it );
 
 1233  kdDebug(5100) << 
"Looking for keys matching " << address << 
" ...\n";
 
 1234  if( matchingKeys.isEmpty() ) {
 
 1235    for ( it.toFirst(); (*it); ++it ) {
 
 1237      if( (*it)->matchesUserID( address, 
false ) ) {
 
 1238        keyTrust( (*it)->primaryKeyID() ); 
 
 1240        if( ( (*it)->isValidEncryptionKey() ) &&
 
 1241            ( (*it)->keyTrust() >= KPGP_VALIDITY_MARGINAL ) ) {
 
 1242          kdDebug(5100) << 
"Matching trusted key found: " 
 1243                        << (*it)->primaryKeyID() << endl;
 
 1244          matchingKeys.append( *it );
 
 1251  if( matchingKeys.isEmpty() ) {
 
 1253    bool rememberChoice;
 
 1254    KeyIDList keyIds = selectKeys( rememberChoice, mPublicKeys,
 
 1255                                   i18n(
"Encryption Key Selection"),
 
 1256                                   i18n(
"if in your language something like " 
 1257                                        "'key(s)' isn't possible please " 
 1258                                        "use the plural in the translation",
 
 1259                                        "No valid and trusted OpenPGP key was " 
 1260                                        "found for \"%1\".\n\n" 
 1261                                        "Select the key(s) which should " 
 1262                                        "be used for this recipient." 
 1266    if( !keyIds.isEmpty() ) {
 
 1267      if( rememberChoice ) {
 
 1268        setKeysForAddress( person, keyIds );
 
 1274  else if( matchingKeys.count() == 1 ) {
 
 1275    return KeyIDList( matchingKeys.getFirst()->primaryKeyID() );
 
 1279    bool rememberChoice;
 
 1280    KeyIDList keyIds = selectKeys( rememberChoice, matchingKeys,
 
 1281                                   i18n(
"Encryption Key Selection"),
 
 1282                                   i18n(
"if in your language something like " 
 1283                                        "'key(s)' isn't possible please " 
 1284                                        "use the plural in the translation",
 
 1285                                        "More than one key matches \"%1\".\n\n" 
 1286                                        "Select the key(s) which should " 
 1287                                        "be used for this recipient." 
 1291    if( !keyIds.isEmpty() ) {
 
 1292      if( rememberChoice ) {
 
 1293        setKeysForAddress( person, keyIds );
 
 1305Module::checkForPGP(
void)
 
 1309  TQStrList pSearchPaths;
 
 1315  path = getenv(
"PATH");
 
 1316  while((index = path.find(
":",lastindex+1)) != -1)
 
 1318    pSearchPaths.append(path.mid(lastindex+1,index-lastindex-1));
 
 1321  if(lastindex != (
int)path.length() - 1)
 
 1322    pSearchPaths.append( path.mid(lastindex+1,path.length()-lastindex) );
 
 1324  TQStrListIterator it(pSearchPaths);
 
 1329  for ( it.toFirst() ; it.current() ; ++it )
 
 1333    if ( !access( path, X_OK ) )
 
 1335      kdDebug(5100) << 
"Kpgp: gpg found" << endl;
 
 1344  for ( it.toFirst() ; it.current() ; ++it )
 
 1348    if ( !access( path, X_OK ) )
 
 1350      kdDebug(5100) << 
"Kpgp: pgp 5 found" << endl;
 
 1359    for ( it.toFirst() ; it.current() ; ++it )
 
 1361      path = it.current();
 
 1363      if ( !access( path, X_OK ) )
 
 1365    kdDebug(5100) << 
"Kpgp: pgp 2 or 6 found" << endl;
 
 1374    kdDebug(5100) << 
"Kpgp: no pgp found" << endl;
 
 1381Module::assignPGPBase(
void)
 
 1391        kdDebug(5100) << 
"Kpgp: assign pgp - gpg" << endl;
 
 1396        kdDebug(5100) << 
"Kpgp: assign pgp - pgp 2" << endl;
 
 1401        kdDebug(5100) << 
"Kpgp: assign pgp - pgp 5" << endl;
 
 1406        kdDebug(5100) << 
"Kpgp: assign pgp - pgp 6" << endl;
 
 1412        kdDebug(5100) << 
"Kpgp: pgpBase is dummy " << endl;
 
 1417        kdDebug(5100) << 
"Kpgp: assign pgp - auto" << endl;
 
 1420        kdDebug(5100) << 
"Kpgp: assign pgp - default" << endl;
 
 1423          kdDebug(5100) << 
"Kpgp: pgpBase is gpg " << endl;
 
 1429          kdDebug(5100) << 
"Kpgp: pgpBase is pgp 5" << endl;
 
 1435          Base6 *pgp_v6 = 
new Base6();
 
 1436          if (!pgp_v6->isVersion6())
 
 1438            kdDebug(5100) << 
"Kpgp: pgpBase is pgp 2 " << endl;
 
 1445            kdDebug(5100) << 
"Kpgp: pgpBase is pgp 6 " << endl;
 
 1455    kdDebug(5100) << 
"Kpgp: pgpBase is dummy " << endl;
 
 1462Module::canonicalAddress( 
const TQString& _adress )
 
 1466  TQString address = _adress.simplifyWhiteSpace();
 
 1467  address = address.stripWhiteSpace();
 
 1470  if((index = address.find(
"<")) != -1)
 
 1471    if((index2 = address.find(
"@",index+1)) != -1)
 
 1472      if((index2 = address.find(
">",index2+1)) != -1)
 
 1473    return address.mid(index,index2-index+1);
 
 1475  if((index = address.find(
"@")) == -1)
 
 1481    return "<" + address + 
"@localdomain>";
 
 1485    int index1 = address.findRev(
" ",index);
 
 1486    int index2 = address.find(
" ",index);
 
 1487    if(index2 == -1) index2 = address.length();
 
 1488    return "<" + address.mid(index1+1 ,index2-index1-1) + 
">";
 
 1493Module::readPublicKeys( 
bool reread )
 
 1495  if( 0 == pgp ) assignPGPBase();
 
 1499    mPublicKeys.clear();
 
 1500    mPublicKeysCached = 
false;
 
 1504  if( !mPublicKeysCached || reread )
 
 1506    if( mPublicKeys.isEmpty() )
 
 1508      mPublicKeys = pgp->publicKeys();
 
 1512      KeyList newPublicKeyList = pgp->publicKeys();
 
 1517      KeyListIterator it( newPublicKeyList );
 
 1518      for( it.toFirst(); (*it); ++it )
 
 1520        Key* oldKey = publicKey( (*it)->primaryKeyID() );
 
 1523          (*it)->cloneKeyTrust( oldKey );
 
 1527      mPublicKeys = newPublicKeyList;
 
 1530    mPublicKeysCached = 
true;
 
 1531    mPublicKeys.setAutoDelete( 
true );
 
 1536Module::readSecretKeys( 
bool reread )
 
 1538  if( 0 == pgp ) assignPGPBase();
 
 1542    mSecretKeys.clear();
 
 1543    mSecretKeysCached = 
false;
 
 1547  if( mSecretKeys.isEmpty() || reread )
 
 1549    if( mSecretKeys.isEmpty() )
 
 1551      mSecretKeys = pgp->secretKeys();
 
 1555      KeyList newSecretKeyList = pgp->secretKeys();
 
 1560      KeyListIterator it( newSecretKeyList );
 
 1561      for( it.toFirst(); (*it); ++it )
 
 1563        Key* oldKey = secretKey( (*it)->primaryKeyID() );
 
 1566          (*it)->cloneKeyTrust( oldKey );
 
 1570      mSecretKeys = newSecretKeyList;
 
 1573    mSecretKeysCached = 
true;
 
 1574    mSecretKeys.setAutoDelete( 
true );
 
 1579Module::selectKey( 
const KeyList& keys,
 
 1580                   const TQString& title,
 
 1581                   const TQString& text  ,
 
 1582                   const KeyID& keyId  ,
 
 1583                   const unsigned int allowedKeys  )
 
 1585  KeyID retval = KeyID();
 
 1587  KeySelectionDialog dlg( keys, title, text, KeyIDList( keyId ), 
false,
 
 1588                          allowedKeys, 
false );
 
 1590  TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
 1591  bool rej = ( dlg.exec() == TQDialog::Rejected );
 
 1592  TQApplication::restoreOverrideCursor();
 
 1602Module::selectKeys( 
const KeyList& keys,
 
 1603                    const TQString& title,
 
 1604                    const TQString& text  ,
 
 1605                    const KeyIDList& keyIds  ,
 
 1606                    const unsigned int allowedKeys  )
 
 1608  KeyIDList retval = KeyIDList();
 
 1610  KeySelectionDialog dlg( keys, title, text, keyIds, 
false, allowedKeys,
 
 1613  TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
 1614  bool rej = ( dlg.exec() == TQDialog::Rejected );
 
 1615  TQApplication::restoreOverrideCursor();
 
 1618    retval = dlg.keys();
 
 1626Module::selectKey( 
bool& rememberChoice,
 
 1627                   const KeyList& keys,
 
 1628                   const TQString& title,
 
 1629                   const TQString& text  ,
 
 1630                   const KeyID& keyId  ,
 
 1631                   const unsigned int allowedKeys  )
 
 1633  KeyID retval = KeyID();
 
 1635  KeySelectionDialog dlg( keys, title, text, KeyIDList( keyId ), 
false,
 
 1636                          allowedKeys, 
false );
 
 1638  TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
 1639  bool rej = ( dlg.exec() == TQDialog::Rejected );
 
 1640  TQApplication::restoreOverrideCursor();
 
 1644    rememberChoice = dlg.rememberSelection();
 
 1647    rememberChoice = 
false;
 
 1654Module::selectKeys( 
bool& rememberChoice,
 
 1655                    const KeyList& keys,
 
 1656                    const TQString& title,
 
 1657                    const TQString& text  ,
 
 1658                    const KeyIDList& keyIds  ,
 
 1659                    const unsigned int allowedKeys  )
 
 1661  KeyIDList retval = KeyIDList();
 
 1663  KeySelectionDialog dlg( keys, title, text, keyIds, 
true, allowedKeys,
 
 1666  TQApplication::setOverrideCursor( TQCursor(TQCursor::ArrowCursor) );
 
 1667  bool rej = ( dlg.exec() == TQDialog::Rejected );
 
 1668  TQApplication::restoreOverrideCursor();
 
 1671    retval = dlg.keys();
 
 1672    rememberChoice = dlg.rememberSelection();
 
 1675    rememberChoice = 
false;
 
 1682Module::keysForAddress( 
const TQString& address )
 
 1684  if( address.isEmpty() ) {
 
 1687  TQString addr = canonicalAddress( address ).lower();
 
 1688  if( addressDataDict.contains( addr ) ) {
 
 1689    return addressDataDict[addr].keyIds;
 
 1697Module::setKeysForAddress( 
const TQString& address, 
const KeyIDList& keyIds )
 
 1699  if( address.isEmpty() ) {
 
 1702  TQString addr = canonicalAddress( address ).lower();
 
 1703  if( addressDataDict.contains( addr ) ) {
 
 1704    addressDataDict[addr].keyIds = keyIds;
 
 1708    data.encrPref = UnknownEncryptPref;
 
 1709    data.keyIds = keyIds;
 
 1710    addressDataDict.insert( addr, data );
 
 1717Module::readAddressData()
 
 1722  TDEConfigGroup general( config, 
"General" );
 
 1723  int num = general.readNumEntry( 
"addressEntries", 0 );
 
 1725  addressDataDict.clear();
 
 1726  for( 
int i=1; i<=num; i++ ) {
 
 1727    TDEConfigGroup addrGroup( config, TQString(
"Address #%1").arg(i).local8Bit() );
 
 1728    address = addrGroup.readEntry( 
"Address" );
 
 1729    data.keyIds = KeyIDList::fromStringList( addrGroup.readListEntry( 
"Key IDs" ) );
 
 1730    data.encrPref = (EncryptPref) addrGroup.readNumEntry( 
"EncryptionPreference",
 
 1731                                                          UnknownEncryptPref );
 
 1735    if ( !address.isEmpty() ) {
 
 1736      addressDataDict.insert( address, data );
 
 1742Module::writeAddressData()
 
 1744  TDEConfigGroup general( config, 
"General" );
 
 1745  general.writeEntry( 
"addressEntries", addressDataDict.count() );
 
 1748  AddressDataDict::Iterator it;
 
 1749  for ( i=1, it = addressDataDict.begin();
 
 1750        it != addressDataDict.end();
 
 1752    TDEConfigGroup addrGroup( config, TQString(
"Address #%1").arg(i).local8Bit() );
 
 1753    addrGroup.writeEntry( 
"Address", it.key() );
 
 1754    addrGroup.writeEntry( 
"Key IDs", it.data().keyIds.toStringList() );
 
 1755    addrGroup.writeEntry( 
"EncryptionPreference", it.data().encrPref );
 
 1762Module::encryptionPreference( 
const TQString& address )
 
 1764  TQString addr = canonicalAddress( address ).lower();
 
 1765  if( addressDataDict.contains( addr ) ) {
 
 1766    return addressDataDict[addr].encrPref;
 
 1769    return UnknownEncryptPref;
 
 1774Module::setEncryptionPreference( 
const TQString& address,
 
 1775                                 const EncryptPref pref )
 
 1777  if( address.isEmpty() ) {
 
 1780  TQString addr = canonicalAddress( address ).lower();
 
 1781  if( addressDataDict.contains( addr ) ) {
 
 1782    addressDataDict[addr].encrPref = pref;
 
 1786    data.encrPref = pref;
 
 1787    addressDataDict.insert( addr, data );