Import release 2.0beta1
This commit is contained in:
+215
-63
@@ -26,6 +26,8 @@
|
||||
|
||||
#include <KRun>
|
||||
#include <KUrl>
|
||||
#include <KShell>
|
||||
#include <KToolInvocation>
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QFile>
|
||||
@@ -42,7 +44,6 @@ QByteArray KMessShared::createHMACSha1 ( const QByteArray &keyForHash , const QB
|
||||
// The algorithm is definited into RFC 2104
|
||||
int blocksize = 64;
|
||||
QByteArray key( keyForHash );
|
||||
QByteArray data( secret );
|
||||
QByteArray opad( blocksize, 0x5c );
|
||||
QByteArray ipad( blocksize, 0x36 );
|
||||
|
||||
@@ -56,7 +57,6 @@ QByteArray KMessShared::createHMACSha1 ( const QByteArray &keyForHash , const QB
|
||||
if( key.size() < blocksize )
|
||||
{
|
||||
key += QByteArray( blocksize - key.size() , 0x00 );
|
||||
|
||||
}
|
||||
|
||||
// Compute the XOR operations
|
||||
@@ -67,14 +67,10 @@ QByteArray KMessShared::createHMACSha1 ( const QByteArray &keyForHash , const QB
|
||||
}
|
||||
|
||||
// Append the data to ipad
|
||||
ipad += data;
|
||||
ipad += secret;
|
||||
|
||||
// Compute result: hash sha1 of ipad
|
||||
QByteArray result;
|
||||
result = QCryptographicHash::hash( ipad, QCryptographicHash::Sha1 );
|
||||
|
||||
// Append the data to opad
|
||||
opad += result;
|
||||
// Compute result: hash sha1 of ipad and append the data to opad
|
||||
opad += QCryptographicHash::hash( ipad, QCryptographicHash::Sha1 );;
|
||||
|
||||
// Return array contains the result of HMACSha1
|
||||
return QCryptographicHash::hash( opad, QCryptographicHash::Sha1 );
|
||||
@@ -83,35 +79,55 @@ QByteArray KMessShared::createHMACSha1 ( const QByteArray &keyForHash , const QB
|
||||
|
||||
|
||||
// Create the html file to request the given service
|
||||
QString KMessShared::createRequestFile( const QString &folder )
|
||||
QString KMessShared::createRequestFile( const QString &mailto, const QString &folder )
|
||||
{
|
||||
CurrentAccount *currentAccount = CurrentAccount::instance();
|
||||
|
||||
// Use the method into passport to compute the token
|
||||
QString token = PassportLoginService::createHotmailToken( currentAccount->getPassportToken(),
|
||||
currentAccount->getProofToken(), folder );
|
||||
|
||||
// Use one file for hml page with post method
|
||||
QString filename = KMessConfig::instance()->getAccountDirectory( currentAccount-> getHandle() )
|
||||
+ "/hotmail.htm";
|
||||
QString filename( KMessConfig::instance()->getAccountDirectory( currentAccount-> getHandle() ) +
|
||||
"/hotmail.htm" );
|
||||
|
||||
QFile file( filename );
|
||||
if ( ! file.open( QIODevice::WriteOnly | QIODevice::Text ) )
|
||||
{
|
||||
return "";
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString command( folder );
|
||||
|
||||
// Check if the user wants to send an email
|
||||
if( ! mailto.isEmpty() )
|
||||
{
|
||||
command += "?mailto=1&to=" + mailto;
|
||||
}
|
||||
|
||||
// Use the method into passport to compute the token
|
||||
QString token( PassportLoginService::createHotmailToken( currentAccount->getToken( "Passport" ),
|
||||
currentAccount->getToken( "PassportProof" ),
|
||||
command ) );
|
||||
QTextStream outBuffer( &file );
|
||||
|
||||
QString pageHtml = "<html><head></head><body onload=\"document.pform.submit(); \"><form name=\"pform\""
|
||||
"action=\"https://login.live.com/ppsecure/sha1auth.srf?lc=" + currentAccount->getLanguageCode() +
|
||||
"\" method=\"POST\"><input type=\"hidden\" name=\"token\" value=\"";
|
||||
// Write the file contents
|
||||
outBuffer << "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
|
||||
" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
|
||||
"<html>\n"
|
||||
" <head></head>\n"
|
||||
" <body onload=\"document.pform.submit(); \">\n"
|
||||
" <form name=\"pform\""
|
||||
"action=\"https://login.live.com/ppsecure/sha1auth.srf?lc=" +
|
||||
currentAccount->getLanguageCode() +
|
||||
"\" method=\"POST\">\n"
|
||||
" <input type=\"hidden\" name=\"token\" value=\"" +
|
||||
token +
|
||||
"\">\n"
|
||||
" <noscript><p>Please enable JavaScript!</p>\n"
|
||||
" <input type=\"submit\" value=\"Click here to open Live Mail\"/>\n"
|
||||
" </noscript>\n"
|
||||
" </form>\n"
|
||||
" </body>\n"
|
||||
"</html>\n";
|
||||
|
||||
pageHtml += token + "\"><noscript>Enable yout javascript, please!<br /> <br /> <input type=\"submit\""
|
||||
"value=\"Click here to get redirected\"/></noscript></form></body></html>";
|
||||
|
||||
// Write and close the file
|
||||
outBuffer << pageHtml;
|
||||
// Then close the buffer
|
||||
file.close();
|
||||
|
||||
return filename;
|
||||
@@ -169,20 +185,20 @@ QByteArray KMessShared::deriveKey ( const QByteArray &keyToDerive, const QByteAr
|
||||
QString KMessShared::generateGUID()
|
||||
{
|
||||
// This code is based on Kopete, but much shorter.
|
||||
QString guid = "{"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "}";
|
||||
QString guid( "{"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "-"
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ QString::number((rand() & 0xAAFF) + 0x1111, 16)
|
||||
+ "}" );
|
||||
return guid.toUpper();
|
||||
}
|
||||
|
||||
@@ -195,7 +211,7 @@ QString KMessShared::generateGUID()
|
||||
*
|
||||
* @return A random value between 4 and 4294967295.
|
||||
*/
|
||||
unsigned long KMessShared::generateID()
|
||||
quint32 KMessShared::generateID()
|
||||
{
|
||||
return (rand() & 0x00FFFFFC);
|
||||
}
|
||||
@@ -222,6 +238,83 @@ QByteArray KMessShared::generateFileHash( const QString &fileName )
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Converts a string with HTML to one with escaped entities
|
||||
*
|
||||
* Only the main HTML control characters are escaped; the string is made suitable for
|
||||
* insertion within an HTML tag attribute.
|
||||
* Neither KDE nor Qt have escape/unescape methods this thorough, they only replace the <>&. Annoying.
|
||||
*
|
||||
* @param string The string to modify
|
||||
* @return A reference to the modified string
|
||||
*/
|
||||
QString &KMessShared::htmlEscape( QString &string )
|
||||
{
|
||||
string.replace( ";", ";" )
|
||||
.replace( "&", "&" )
|
||||
.replace( "&#59;", ";" ) // oh god :(
|
||||
.replace( "<", "<" )
|
||||
.replace( ">", ">" )
|
||||
.replace( "'", "'" )
|
||||
.replace( '"', """ ); // NOTE: by not using " this result is also usable for XML.
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Converts a string constant with HTML to one with escaped entities
|
||||
*
|
||||
* This version is suitable for use with string constants, as it creates a copy of the original string.
|
||||
*
|
||||
* @param string The string to modify
|
||||
* @return Another string with the escaped contents
|
||||
*/
|
||||
QString KMessShared::htmlEscape( const QString &string )
|
||||
{
|
||||
QString copy( string );
|
||||
return htmlEscape( copy );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Converts a string with escaped entities to one with HTML
|
||||
*
|
||||
* Only the main HTML control characters are unescaped; the string is reverted back to its
|
||||
* original state.
|
||||
* Neither KDE nor Qt have HTML to text decoding methods. Annoying.
|
||||
*
|
||||
* @param string The string to modify
|
||||
* @return A reference to the modified string
|
||||
*/
|
||||
QString &KMessShared::htmlUnescape( QString &string )
|
||||
{
|
||||
string.replace( """, "\"" )
|
||||
.replace( "'", "'" )
|
||||
.replace( ">", ">" )
|
||||
.replace( "<", "<" )
|
||||
.replace( ";", "&#59;" )
|
||||
.replace( "&", "&" )
|
||||
.replace( ";", ";" );
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string with escaped entities to one with HTML
|
||||
*
|
||||
* This version is suitable for use with string constants, as it creates a copy of the original string.
|
||||
*
|
||||
* @param string The string to modify
|
||||
* @return Another string with the escaped contents
|
||||
*/
|
||||
QString KMessShared::htmlUnescape( const QString &string )
|
||||
{
|
||||
QString copy( string );
|
||||
return htmlUnescape( copy );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Open the given URL respecting the user's preference
|
||||
@@ -289,13 +382,13 @@ void KMessShared::openBrowser( const KUrl &url )
|
||||
#ifdef KMESSDEBUG_SHARED_OPENEXTERNAL
|
||||
kDebug() << "KDE default browser selected.";
|
||||
#endif
|
||||
new KRun( url, mainWindow );
|
||||
KToolInvocation::invokeBrowser( url.url() );
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace the %u in the command line with the actual URL, and launch the browser.
|
||||
browser.replace( "%u", "\"" + url.url() + "\"", Qt::CaseInsensitive );
|
||||
KRun::run( browser, KUrl::List(), mainWindow );
|
||||
browser.replace( "%u", KShell::quoteArg( url.url() ), Qt::CaseInsensitive );
|
||||
KRun::runCommand( browser, mainWindow );
|
||||
}
|
||||
|
||||
|
||||
@@ -305,10 +398,11 @@ void KMessShared::openBrowser( const KUrl &url )
|
||||
*
|
||||
* The program which will open the URL is the one selected in the Global Settings
|
||||
*
|
||||
* @param mailto The address to compose a message to (can also be empty to open the folder instead).
|
||||
* @param folder The folder to open.
|
||||
* @param isHotmailAvailable Whether Hotmail can be used to access the e-mail for this account.
|
||||
* @param isHotmailAvailable Whether Hotmail can be used to access the email for this account.
|
||||
*/
|
||||
void KMessShared::openEmailClient( const QString folder, bool isHotmailAvailable )
|
||||
void KMessShared::openEmailClient( const QString &mailto, const QString &folder, bool isHotmailAvailable )
|
||||
{
|
||||
// Open the options
|
||||
KConfigGroup group = KMessConfig::instance()->getGlobalConfig( "General" );
|
||||
@@ -317,7 +411,7 @@ void KMessShared::openEmailClient( const QString folder, bool isHotmailAvailable
|
||||
if( isHotmailAvailable && group.readEntry( "useLiveMail", true ) )
|
||||
{
|
||||
// Create the request file
|
||||
QString filename( createRequestFile( folder ) );
|
||||
QString filename( createRequestFile( mailto, folder ) );
|
||||
if( ! filename.isEmpty() )
|
||||
{
|
||||
// Open the webmail in the browser
|
||||
@@ -377,38 +471,96 @@ void KMessShared::openEmailClient( const QString folder, bool isHotmailAvailable
|
||||
}
|
||||
|
||||
// Retrieve the mail address to open
|
||||
QString handleToSend;
|
||||
if( folder.contains( "?mailto=1&to=" ) )
|
||||
{
|
||||
handleToSend = folder.split( "?mailto=1&to=" )[1];
|
||||
}
|
||||
|
||||
if( mailClientChoice == "KDE" )
|
||||
{
|
||||
#ifdef KMESSDEBUG_SHARED_OPENEXTERNAL
|
||||
kDebug() << "KDE default mail client selected.";
|
||||
#endif
|
||||
new KRun( "mailto:" + handleToSend, mainWindow );
|
||||
KToolInvocation::invokeMailer( KUrl( "mailto:" + mailto ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace the %u in the command line with the contact address, and launch the application.
|
||||
mailClient.replace( "%u", "\"" + handleToSend + "\"", Qt::CaseInsensitive );
|
||||
KRun::run( mailClient, KUrl::List(), mainWindow );
|
||||
mailClient.replace( "%u", KShell::quoteArg( mailto ), Qt::CaseInsensitive );
|
||||
KRun::runCommand( mailClient, mainWindow );
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Open the give Profile
|
||||
void KMessShared::openProfile( const QString &handle )
|
||||
/**
|
||||
* Select the next available file.
|
||||
*
|
||||
* This method searches for numbered files and returns the last existing file,
|
||||
* and the first number available for file creation.
|
||||
*
|
||||
*
|
||||
* For example, calling selectNextFile( "/", "name", "ext", 0 ) returns:
|
||||
* - if "/name.ext" doesn't exists, returns false, startingNumber = 1
|
||||
* - if "/name.ext" exists, returns true, startingNumber = 1
|
||||
* - if "/name.1.ext" and "/name.2.ext" exist, returns true, fileName = "name.2",
|
||||
* startingNumber = 3
|
||||
*
|
||||
* @param directory Path where the files will be searched, followed by '/'.
|
||||
* May be relative.
|
||||
* @param baseName Common name for the files. Will be updated with the name
|
||||
* of the last existing numbered file.
|
||||
* @param suffix Append to the baseName to get the name of the last
|
||||
* existing numbered file.
|
||||
* @param startingNumber File number to start from, set to zero to start with
|
||||
* the common name alone. Gets updated with the number of
|
||||
* the first available file.
|
||||
* @return bool If the file in the updated fileName param exists or not.
|
||||
*/
|
||||
bool KMessShared::selectNextFile( const QString &directory, const QString &baseName, QString &suffix, const QString &extension, int &startingNumber )
|
||||
{
|
||||
QString folder = "http://members.msn.com/default.msnw?mem=" + handle + "&pgmarket";
|
||||
QString filename = createRequestFile( folder );
|
||||
if( filename.isEmpty() )
|
||||
QString path( directory + baseName + "." );
|
||||
|
||||
// "baseName.ext" doesn't exist, find the next available file
|
||||
if( startingNumber == 0 && ! QFile::exists( directory + baseName + "." + extension ) )
|
||||
{
|
||||
return;
|
||||
#ifdef KMESSDEBUG_SHAREDMETHODS
|
||||
kDebug() << "Initial file" << ( directory + baseName + "." + extension ) << "does not exist, finding next available.";
|
||||
#endif
|
||||
while( QFile::exists( path + QString::number( ++startingNumber ) + "." + extension ) ) ;
|
||||
#ifdef KMESSDEBUG_SHAREDMETHODS
|
||||
kDebug() << "Next available:" << path << startingNumber;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
// Open the profile in the browser
|
||||
openBrowser( KUrl( "file://" + filename ) );
|
||||
#ifdef KMESSDEBUG_SHAREDMETHODS
|
||||
kDebug() << "Initial file exists, finding the last numbered.";
|
||||
#endif
|
||||
|
||||
// "baseName.ext" exists, find the last existing numbered one
|
||||
|
||||
// Start the sequence number from 1
|
||||
if( startingNumber == 0 )
|
||||
{
|
||||
startingNumber = 1;
|
||||
}
|
||||
|
||||
while( QFile::exists( path + QString::number( startingNumber ) + "." + extension ) )
|
||||
{
|
||||
startingNumber++;
|
||||
}
|
||||
|
||||
// Found the first non-existing file
|
||||
if( startingNumber == 1 )
|
||||
{
|
||||
// Select the first numbered file
|
||||
suffix = QString();
|
||||
}
|
||||
else
|
||||
{
|
||||
suffix = "." + QString::number( startingNumber - 1 ); // That's the number of the last existing file
|
||||
}
|
||||
|
||||
#ifdef KMESSDEBUG_SHAREDMETHODS
|
||||
kDebug() << "Last existent file name:" << ( baseName + suffix );
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user