summaryrefslogtreecommitdiffstats
path: root/knode/knserverinfo.cpp
blob: 30f0b863dd30fe14317961bc3c4d75682a54063d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
    KNode, the KDE newsreader
    Copyright (c) 1999-2005 the KNode authors.
    See file AUTHORS for details

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/


#include <kmessagebox.h>
#include <kconfig.h>
#include <klocale.h>
#include <kdebug.h>
#include <kwallet.h>
using namespace KWallet;

#include "knglobals.h"
#include "knserverinfo.h"
#include "knaccountmanager.h"
#include "utilities.h"

#include <tqwidget.h>

KNServerInfo::KNServerInfo() :
  t_ype(STnntp), i_d(-1), p_ort(119), h_old(300),
  t_imeout(60), n_eedsLogon(false), p_assDirty(false),
  mPassLoaded( false ),
  mEncryption( None )
{
}



KNServerInfo::~KNServerInfo()
{
}



void KNServerInfo::readConf(TDEConfig *conf)
{
  s_erver=conf->readEntry("server", "localhost");

  if(t_ype==STnntp)
    p_ort=conf->readNumEntry("port", 119);
  else
    p_ort=conf->readNumEntry("port", 25);

  h_old=conf->readNumEntry("holdTime", 300);

  if(h_old < 0) h_old=0;

  t_imeout=conf->readNumEntry("timeout", 60);

  if(t_imeout < 15) t_imeout=15;

  if(t_ype==STnntp)
    i_d=conf->readNumEntry("id", -1);

  n_eedsLogon=conf->readBoolEntry("needsLogon",false);
  u_ser=conf->readEntry("user");
  p_ass = KNHelper::decryptStr(conf->readEntry("pass"));

  // migration to KWallet
  if (Wallet::isEnabled() && !p_ass.isEmpty()) {
    conf->deleteEntry( "pass" );
    p_assDirty = true;
  }

  // if the wallet is open, no need to delay the password loading
  if (Wallet::isOpen( Wallet::NetworkWallet() ))
    readPassword();

  TQString encStr = conf->readEntry( "encryption", "None" );
  if ( encStr.contains( "SSL", false ) )
    mEncryption = SSL;
  else if ( encStr.contains( "TLS", false ) )
    mEncryption = TLS;
  else
    mEncryption = None;
}


void KNServerInfo::saveConf(TDEConfig *conf)
{
  conf->writeEntry("server", s_erver);
  if ( p_ort == 0 ) p_ort = 119;
  conf->writeEntry("port", p_ort);
  conf->writeEntry("holdTime", h_old);
  conf->writeEntry("timeout", t_imeout);
  if (t_ype==STnntp)
    conf->writeEntry("id", i_d);

  conf->writeEntry("needsLogon", n_eedsLogon);
  conf->writeEntry("user", u_ser);
  // open wallet for storing only if the user actually changed the password
  if (n_eedsLogon && p_assDirty) {
    Wallet *wallet = KNAccountManager::wallet();
    if (!wallet || wallet->writePassword(TQString::number(i_d), p_ass)) {
      if ( KMessageBox::warningYesNo( 0,
            i18n("KWallet is not available. It is strongly recommended to use "
                "KWallet for managing your passwords.\n"
                "However, KNode can store the password in its configuration "
                "file instead. The password is stored in an obfuscated format, "
                "but should not be considered secure from decryption efforts "
                "if access to the configuration file is obtained.\n"
                "Do you want to store the password for server '%1' in the "
                "configuration file?").arg( server() ),
            i18n("KWallet Not Available"),
            KGuiItem( i18n("Store Password") ),
            KGuiItem( i18n("Do Not Store Password") ) )
            == KMessageBox::Yes ) {
        conf->writeEntry( "pass", KNHelper::encryptStr( p_ass ) );
      }
    }
    p_assDirty = false;
  }

  switch ( mEncryption ) {
    case SSL:
      conf->writeEntry( "encryption", "SSL" );
      break;
    case TLS:
      conf->writeEntry( "encryption", "TLS" );
      break;
    default:
      conf->writeEntry( "encryption", "None" );
  }
}



bool KNServerInfo::operator==(const KNServerInfo &s)
{
  return (  (t_ype==s.t_ype)  &&
            (s_erver==s.s_erver) &&
            (p_ort==s.p_ort) &&
            (h_old==s.h_old) &&
            (t_imeout==s.t_imeout) &&
            (n_eedsLogon==s.n_eedsLogon) &&
            (u_ser==s.u_ser) &&
            (p_ass==s.p_ass) &&
            (mEncryption == s.mEncryption)
         );
}


const TQString &KNServerInfo::pass()
{
  // if we need to load the password, load all of them
  if (n_eedsLogon && !mPassLoaded && p_ass.isEmpty() )
    knGlobals.accountManager()->loadPasswords();

  return p_ass;
}

void KNServerInfo::setPass(const TQString &s)
{
  if (p_ass != s) {
    p_ass = s;
    p_assDirty = true;
  }
}


void KNServerInfo::readPassword()
{
  // no need to load a password if the account doesn't require auth
  if (!n_eedsLogon)
    return;
  mPassLoaded = true;

  // check wether there is a chance to find our password at all
  if (Wallet::folderDoesNotExist(Wallet::NetworkWallet(), "knode") ||
      Wallet::keyDoesNotExist(Wallet::NetworkWallet(), "knode", TQString::number(i_d)))
    return;

  // finally try to open the wallet and read the password
  KWallet::Wallet *wallet = KNAccountManager::wallet();
  if ( wallet )
    wallet->readPassword( TQString::number(i_d), p_ass );
}