예제 #1
0
파일: gnupg.cpp 프로젝트: psi-plus/plugins
bool GnuPG::incomingStanza(int account, const QDomElement& stanza)
{
    if (!_enabled) {
        return false;
    }

    if (!_optionHost->getPluginOption("auto-import", true).toBool()) {
        return false;
    }

    if (stanza.tagName() != "message" && stanza.attribute("type") != "chat") {
        return false;
    }

    QString body = stanza.firstChildElement("body").text();

    int start = body.indexOf("-----BEGIN PGP PUBLIC KEY BLOCK-----");
    if (start == -1) {
        return false;
    }

    int end = body.indexOf("-----END PGP PUBLIC KEY BLOCK-----", start);
    if (end == -1) {
        return false;
    }

    QString key = body.mid(start, end - start);

    GpgProcess gpg;
    QStringList arguments;
    arguments << "--batch"
              << "--import";
    gpg.start(arguments);
    gpg.waitForStarted();
    gpg.write(key.toUtf8());
    gpg.closeWriteChannel();
    gpg.waitForFinished();

    QString from = stanza.attribute("from");
    // Cut trash from gpg command output
    QString res = QString::fromUtf8(gpg.readAllStandardError());
    res = _stanzaSending->escape(res.mid(0, res.indexOf('\n')));
    _accountHost->appendSysMsg(account, from, res);

    // Don't hide message if an error occurred
    if (gpg.exitCode()) {
        return false;
    }

    if (!_optionHost->getPluginOption("hide-key-message", true).toBool()) {
        return false;
    }
    else {
        return true;
    }
}
예제 #2
0
void Options::addKey()
{
	AddKeyDlg dlg(this);
	if (dlg.exec() == QDialog::Rejected) {
		return;
	}

	QString key;
	QString type, stype, length, name, comment, email, expiration, pass;
	switch (dlg.type()) {
	case 0: type = stype = "RSA"; break;
	case 1: type = "DSA"; stype = "ELG-E"; break;
	case 2: type = "DSA"; break;
	case 3: type = "RSA"; break;
	}

	length = QString::number(dlg.length());
	name = dlg.name();
	comment = dlg.comment();
	email = dlg.email();
	expiration = dlg.expiration().isValid() ? dlg.expiration().toString(Qt::ISODate) : "0";
	pass = dlg.pass();

	key += QString("Key-Type: %1\n").arg(type);
	key += QString("Key-Length: %2\n").arg(length);
	if (!stype.isEmpty()) {
		key += QString("Subkey-Type: %1\n").arg(stype);
		key += QString("Subkey-Length: %2\n").arg(length);
	}

	if (!name.isEmpty()) {
		key += QString("Name-Real: %1\n").arg(name);
	}

	if (!comment.isEmpty()) {
		key += QString("Name-Comment: %1\n").arg(comment);
	}

	if (!email.isEmpty()) {
		key += QString("Name-Email: %1\n").arg(email);
	}

	key += QString("Expire-Date: %1\n").arg(expiration);

	if (!pass.isEmpty()) {
		key += QString("Passphrase: %1\n").arg(pass);
	}

	key += "%commit\n";

	QProgressDialog waitingDlg("", trUtf8("Cancel"), 0, 0, this);

	QLabel progressTextLabel(trUtf8(
"<b>Please wait!</b><br/>"
"We need to generate a lot of random bytes. It is a good idea to perform "
"some other action (type on the keyboard, move the mouse, utilize the "
"disks) during the prime generation; this gives the random number "
"generator a better chance to gain enough entropy."), &waitingDlg);
	progressTextLabel.setAlignment(Qt::AlignHCenter);
	progressTextLabel.setWordWrap(true);

	waitingDlg.setLabel(&progressTextLabel);

	QProgressBar progressBar(&waitingDlg);
	progressBar.setAlignment(Qt::AlignHCenter);
	progressBar.setMinimum(0);
	progressBar.setMaximum(0);

	waitingDlg.setBar(&progressBar);

	waitingDlg.setWindowModality(Qt::WindowModal);
	waitingDlg.setWindowTitle(trUtf8("Key pair generating"));
	waitingDlg.show();

	GpgProcess gpg;
	QStringList arguments;
	arguments << "--batch"
			  << "--gen-key";

	gpg.start(arguments);
	gpg.waitForStarted();
	gpg.write(key.toUtf8());
	gpg.closeWriteChannel();
	while (gpg.state() == QProcess::Running) {
		gpg.waitForFinished(1);
		if (waitingDlg.wasCanceled()) {
			gpg.terminate();
			break;
		}
		qApp->processEvents();
	}

	updateKeys();
}