Add licence details

Insert copyright statements

Implemented CRUD for connections

Refactor C++ connection side, create debugview and connect C++ to UI

Implement SSL verification, reconnection, import moment.js, refactor UI logic

Storing SSL certificate for later use now works

Add missing comments

Follow the end of a TextListComponent if view was already at bottom

Initial protocol stubs for WeeChat protocol

Add README
This commit is contained in:
Mikko Ahlroth 2014-01-29 11:33:37 +02:00 committed by Mikko Ahlroth
parent 0233e130a1
commit 840a50e54b
62 changed files with 4879 additions and 292 deletions

7
LICENCE Normal file
View file

@ -0,0 +1,7 @@
Copyright © 2014 Mikko Ahlroth
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

33
README.md Normal file
View file

@ -0,0 +1,33 @@
# WeeCRApp
WeeCRApp, or WeeChat Relay App, is a WeeChat remote GUI client for Sailfish. It
uses the buffer relay protocol to talk to a WeeChat instance running on some
server and displays the open channels and chat history. It is not an IRC client
in itself and cannot function without an accessible WeeChat instance.
Note: The client is in the stages of early development, so features are scarce
and revisions may not compile.
## Possible future features
* Connect to WeeChat on a server, optionally with SSL (done)
* Store SSL certificates for later (done)
* Debug window (done)
* Display all open buffers with possibility to change buffer with a side swipe
* Display buffer list and nick list with some appropriate GUI
* Autoconnect on start
* Automatic reconnect
* OS notifications for highlights and other interesting stuff
* Automatic URL recognition
* IRC shortcuts (nick completion, aliases etc.)
* Image upload from device camera and pasting link to channel
## Licence
WeeCRApp is licenced with the MIT Expat licence. See the LICENCE file for more
details.
## Thanks
The development of WeeCRApp is graciously sponsored by
[Vincit Oy](http://www.vincit.fi/).

View file

@ -11,18 +11,67 @@ TARGET = harbour-weechatrelay
CONFIG += sailfishapp c++11
SOURCES += src/harbour-weechatrelay.cpp \
src/relayconnection.cpp
src/relayconnection.cpp \
src/sslrelayconnection.cpp \
src/connectionhandler.cpp \
src/weechatprotocolhandler.cpp \
src/protocolhandler.cpp \
src/qsslcertificateinfo.cpp \
src/weechatproto/protocoltype.cpp \
src/weechatproto/hashtable.cpp \
src/weechatproto/hdata.cpp \
src/weechatproto/info.cpp \
src/weechatproto/infolist.cpp \
src/weechatproto/array.cpp \
src/weechatproto/time.cpp \
src/weechatproto/pointer.cpp \
src/weechatproto/buffer.cpp \
src/weechatproto/string.cpp \
src/weechatproto/long.cpp \
src/weechatproto/integer.cpp \
src/weechatproto/char.cpp \
src/weechatproto/protocoltypeoverwriteexception.cpp
OTHER_FILES += qml/harbour-weechatrelay.qml \
qml/cover/CoverPage.qml \
qml/pages/FirstPage.qml \
qml/pages/SecondPage.qml \
rpm/harbour-weechatrelay.spec \
rpm/harbour-weechatrelay.yaml \
harbour-weechatrelay.desktop
harbour-weechatrelay.desktop \
qml/js/storage.js \
qml/pages/ConnectionList.qml \
qml/cover/DefaultCover.qml \
qml/pages/AddConnection.qml \
qml/pages/DebugView.qml \
qml/pages/TextListComponent.qml \
qml/pages/SslVerifyDialog.qml \
qml/js/utils.js \
qml/js/moment.js \
qml/js/connection.js \
qml/js/debug.js
HEADERS += \
src/relayconnection.h
src/relayconnection.h \
src/sslrelayconnection.h \
src/connectionhandler.h \
src/protocolhandler.h \
src/weechatprotocolhandler.h \
src/connectresolver.h \
src/qsslcertificateinfo.h \
src/weechatproto/protocoltype.h \
src/weechatproto/hashtable.h \
src/weechatproto/hdata.h \
src/weechatproto/info.h \
src/weechatproto/infolist.h \
src/weechatproto/array.h \
src/weechatproto/time.h \
src/weechatproto/pointer.h \
src/weechatproto/buffer.h \
src/weechatproto/string.h \
src/weechatproto/long.h \
src/weechatproto/integer.h \
src/weechatproto/char.h \
src/weechatproto/protocoltypeoverwriteexception.h
QT += network
INCLUDEPATH += /Users/nicd/SailfishOS/mersdk/targets/SailfishOS-armv7hl/usr/include/c++/4.6.4

View file

@ -1,54 +0,0 @@
/*
Copyright (C) 2013 Jolla Ltd.
Contact: Thomas Perl <thomas.perl@jollamobile.com>
All rights reserved.
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Jolla Ltd nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
CoverBackground {
Label {
id: label
anchors.centerIn: parent
text: "My Cover"
}
CoverActionList {
id: coverAction
CoverAction {
iconSource: "image://theme/icon-cover-next"
}
CoverAction {
iconSource: "image://theme/icon-cover-pause"
}
}
}

View file

@ -0,0 +1,20 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
CoverBackground {
id: defaultCover
Label {
id: label
anchors.centerIn: parent
text: "WeeCRApp"
}
}

View file

@ -1,41 +1,34 @@
/*
Copyright (C) 2013 Jolla Ltd.
Contact: Thomas Perl <thomas.perl@jollamobile.com>
All rights reserved.
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Jolla Ltd nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import "pages"
import "js/connection.js" as C
import harbour.weechatrelay.connectionhandler 1.0
ApplicationWindow
{
initialPage: Component { FirstPage { } }
cover: Qt.resolvedUrl("cover/CoverPage.qml")
initialPage: Component { ConnectionList { } }
cover: Qt.resolvedUrl("cover/DefaultCover.qml")
Component.onCompleted: {
C.init(connectionHandler, pageStack);
// Connect signals to UI logic
connectionHandler.connected.connect(C.onConnected);
connectionHandler.disconnected.connect(C.onDisconnected);
connectionHandler.displayDebugData.connect(C.onDisplayDebugData);
connectionHandler.sslError.connect(C.onSslError);
}
ConnectionHandler {
id: connectionHandler
}
}

157
qml/js/connection.js Normal file
View file

@ -0,0 +1,157 @@
.pragma library
.import harbour.weechatrelay.connectionhandler 1.0 as CH
.import harbour.weechatrelay.qsslcertificateinfo 1.0 as QSCI
.import "storage.js" as S
.import "debug.js" as D
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
/*
* This file contains the main UI logic in the handling of a relay connection.
* It will create the necessary views and pass messages to them.
*/
// State variables
var connected = false;
var connection = null; // A Connection object
var handler = null; // The underlying ConnectionHandler
var ps = null; // The PageStack
// Views
var debugViewPage = null;
var sslVerifyDialog = null;
// Set initial variables before using any other functions
function init(connectionHandler, pageStack) {
handler = connectionHandler;
ps = pageStack;
}
// Signal handlers for incoming data from connection
function onDisplayDebugData(str) {
debugViewPage.display(str);
}
function onConnected() {
connected = true;
debugViewPage.connected();
debug("Connected");
}
function onDisconnected() {
connected = false;
debugViewPage.disconnected();
debug("Disconnected");
}
function onSslError(errorStrings,
issuerInfo,
startDate,
expiryDate,
digest) {
debug("SSL verification error when connecting");
sslVerifyDialog = ps.push("../pages/SslVerifyDialog.qml",
{
"errorList": errorStrings,
"issuer": issuerInfo,
"startDate": startDate,
"expiryDate": expiryDate,
"digest": digest
});
}
// Public API
function connect(connObj) {
debugViewPage = ps.replace("../pages/DebugView.qml");
debug("Connecting...");
var connType = (connObj.type === "ssl")
? CH.ConnectionHandler.SSL
: CH.ConnectionHandler.NONE;
// Use any stored certificates
if ('cert' in connObj.options
&& 'digest' in connObj.options.cert
&& connObj.options.cert.digest.length > 0) {
var qmlSpec = "import harbour.weechatrelay.qsslcertificateinfo 1.0; QSslCertificateInfo {}";
var info = Qt.createQmlObject(qmlSpec, handler, '');
info.effectiveDate = connObj.options.cert.effectiveDate;
info.expiryDate = connObj.options.cert.expiryDate;
info.digest = connObj.options.cert.digest;
handler.acceptCertificate(info);
}
handler.connect(CH.ConnectionHandler.WEECHAT,
connType,
connObj.host,
connObj.port,
connObj.password);
connection = new S.Connection(connObj);
}
function disconnect() {
connected = false;
handler.disconnect();
}
function reconnect() {
debug("Reconnecting...");
handler.reconnect();
}
// Reconnect, accepting the previously failed certificate
function reconnectWithFailed() {
debug("Reconnecting...");
handler.reconnectWithFailed();
}
// Store the failed certificate information so that it will be accepted automatically
// on the next connection
function storeFailedCertificate() {
var cert = handler.getFailedCertificate();
var storedInfo = {
effectiveDate: cert.effectiveDate,
expiryDate: cert.expiryDate,
digest: cert.digest
};
connection.options['cert'] = storedInfo;
S.storeConnection(S.connect(), connection);
}
function clearConnection() {
connection = null;
connected = false;
handler.clearData();
}
// Write a line to the debug view (will go to console if debug view isn't open)
function debug(str) {
if (debugViewPage !== null) {
debugViewPage.display(str);
}
else {
console.log(str);
}
}
// Private API

28
qml/js/debug.js Normal file
View file

@ -0,0 +1,28 @@
.pragma library
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
function d(o) {
console.log(o);
}
function e(o) {
enumerate(o);
}
function enumerate(o) {
d("Enumerating " + o);
d("---");
var keys = Object.keys(o);
for (var i = 0; i < keys.length; ++i) {
d(keys[i] + ": " + o[keys[i]]);
}
d("");
}

2498
qml/js/moment.js Normal file

File diff suppressed because it is too large Load diff

182
qml/js/storage.js Normal file
View file

@ -0,0 +1,182 @@
.pragma library
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
.import QtQuick.LocalStorage 2.0 as LS
var db_inst = null;
function connect() {
// Connect if not already connected, otherwise just return instance
if (db_inst === null) {
db_inst = LS.LocalStorage.openDatabaseSync("WeeCRApp", "1.0", "StorageDatabase", 10240);
db_inst.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS settings \
(key TEXT PRIMARY KEY, \
value TEXT);");
tx.executeSql("CREATE TABLE IF NOT EXISTS connections \
(id INTEGER PRIMARY KEY, \
name TEXT, \
host TEXT, \
port INTEGER, \
password TEXT, \
type TEXT, \
options TEXT \
);");
});
}
return db_inst;
}
function readSetting(db, key, defVal) {
var setting = null;
db.readTransaction(function(tx) {
var rows = tx.executeSql("SELECT value AS val FROM settings WHERE key=?;", [key]);
if (rows.rows.length !== 1) {
setting = null;
}
else {
setting = rows.rows.item(0).val;
}
});
if (setting === 'true') {
setting = true;
}
else if (setting === 'false') {
setting = false;
}
// If setting has never been read (doesn't exist), use default value
else if (setting === null) {
setting = defVal;
}
return setting;
}
function storeSetting(db, key, value) {
if (value === true) {
value = 'true';
}
else if (value === false) {
value = 'false';
}
db.transaction(function(tx) {
tx.executeSql("INSERT OR REPLACE INTO settings VALUES (?, ?);", [key, value]);
tx.executeSql("COMMIT;");
});
}
// Connection class
function Connection(infodict) {
this.id = infodict.id;
this.name = infodict.name;
this.host = infodict.host;
this.port = infodict.port;
this.password = infodict.password;
this.type = infodict.type;
// If infodict.options is not an array, try to parse it as JSON
if (infodict.options === null) {
this.options = {};
}
else if (typeof infodict.options === 'object') {
this.options = infodict.options;
}
else {
this.options = JSON.parse(infodict.options);
}
}
// Convert connection back to object suitable for storage in database
Connection.prototype.toStorageDict = function() {
return {
"id": this.id,
"name": this.name,
"host": this.host,
"port": this.port,
"password": this.password,
"type": this.type,
"options": JSON.stringify(this.options)
};
};
function readAllConnections(db) {
var connlist = [];
var ids = [];
db.readTransaction(function(tx) {
var rows = tx.executeSql("SELECT id FROM connections;");
for (var i = 0; i < rows.rows.length; ++i) {
ids.push(rows.rows.item(i).id);
}
});
for (var i = 0; i < ids.length; ++i) {
connlist.push(readConnection(db, ids[i]));
}
return connlist;
}
function readConnection(db, id) {
var infodict = null;
db.readTransaction(function(tx) {
var rows = tx.executeSql("SELECT id, name, host, port, password, type, options \
FROM connections WHERE id = ?;", [id]);
if (rows.rows.length === 1) {
infodict = rows.rows.item(0);
}
});
if (infodict !== null) {
return new Connection(infodict);
}
return null;
}
function storeConnection(db, connection) {
var infodict = connection.toStorageDict();
db.transaction(function(tx) {
tx.executeSql("INSERT OR REPLACE INTO connections \
(id, name, host, port, password, type, options) \
VALUES (?, ?, ?, ?, ?, ?, ?);",
[
infodict.id,
infodict.name,
infodict.host,
infodict.port,
infodict.password,
infodict.type,
infodict.options
]);
tx.executeSql("COMMIT;");
});
}
function deleteConnection(db, id) {
db.transaction(function(tx) {
tx.executeSql("DELETE FROM connections WHERE id = ?;",
[
id
]);
tx.executeSql("COMMIT;");
});
}

13
qml/js/utils.js Normal file
View file

@ -0,0 +1,13 @@
// Miscellaneous functions for generic use
// Escape strings for Text.StyledText
function escapeStyled(string) {
string = string.replace('&', '&amp;');
string = string.replace('<', '&lt;');
return string.replace('>', '&gt;');
}
// Return StyledText in a specified color
function colored(color, string) {
return '<font color="' + color + '">' + string + '</font>';
}

173
qml/pages/AddConnection.qml Normal file
View file

@ -0,0 +1,173 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import "../js/storage.js" as Storage
Dialog {
id: addConnectionDialog
canAccept: false
property SilicaListView connList;
property int oldId: -1;
// Custom properties for saving connection info when modifying
property var oldinfo: null;
function updateFields(infodict) {
nameField.text = infodict.name;
hostField.text = infodict.host;
portField.text = infodict.port;
passwordField.text = "FAKE PASS";
switch (infodict.type) {
case "plain":
securityField.currentIndex = 0;
break;
case "ssl":
securityField.currentIndex = 1;
}
oldinfo = infodict;
}
function saveFields() {
var id = null;
var pass = passwordField.text;
var type = "";
if (oldinfo !== null) {
id = oldinfo.id;
if (passwordField.text === "FAKE PASS") {
pass = oldinfo.password;
}
}
switch (securityField.currentIndex) {
case 0:
type = "plain";
break;
case 1:
type = "ssl";
}
return {
"id": id,
"name": nameField.text,
"host": hostField.text,
"port": portField.text,
"password": pass,
"type": type,
"options": {}
};
}
function setCanAccept() {
addConnectionDialog.canAccept = (nameField.text.length !== 0
&& hostField.text.length !== 0
&& portField.text.length !== 0);
}
Component.onCompleted: {
// Load old data if available
if (oldId === -1) return;
var db = Storage.connect();
var infodict = Storage.readConnection(db, oldId);
updateFields(infodict);
}
onAccepted: {
var infodict = saveFields();
var connection = new Storage.Connection(infodict);
var db = Storage.connect();
Storage.storeConnection(db, connection);
connList.updateList();
}
SilicaFlickable {
id: addConnectionFlickable
anchors.fill: parent
contentHeight: addConnectionColumn.height
VerticalScrollDecorator { flickable: addConnectionFlickable }
Column {
id: addConnectionColumn
width: addConnectionDialog.width
spacing: Theme.paddingLarge
DialogHeader {
title: "Save"
}
TextField {
id: nameField
placeholderText: "Connection name"
width: parent.width
onTextChanged: setCanAccept();
}
Row {
spacing: Theme.paddingSmall
width: parent.width
TextField {
id: hostField
width: parent.width * 0.75
placeholderText: "Hostname"
inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
onTextChanged: setCanAccept();
}
TextField {
id: portField
width: parent.width * 0.25
placeholderText: "Port"
inputMethodHints: Qt.ImhDigitsOnly
validator: IntValidator {
bottom: 1
top: 65535
}
onTextChanged: setCanAccept();
}
}
TextField {
id: passwordField
placeholderText: "Password"
width: parent.width
echoMode: TextInput.Password
onTextChanged: setCanAccept();
}
ComboBox {
id: securityField
label: "Security"
menu: ContextMenu {
MenuItem {
text: "None"
}
MenuItem {
text: "SSL"
}
}
}
}
}
}

View file

@ -0,0 +1,135 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import "../js/storage.js" as S
import "../js/connection.js" as C
import "."
Page {
id: connectionListPage
onVisibleChanged: connectionList.updateList();
PageHeader {
title: "Connections"
}
SilicaListView {
id: connectionList
model: ListModel { id: connectionModel }
anchors.fill: parent
delegate: ListItem {
id: listItem
contentHeight: Theme.itemSizeLarge
menu: contextMenu
ListView.onRemove: animateRemoval(listItem);
function remove() {
remorseAction("Removing connection",
function() {
S.deleteConnection(S.connect(),
connectionModel.get(index).id);
connectionModel.remove(index);
});
}
Column {
anchors.verticalCenter: parent.verticalCenter
Label {
text: name
color: listItem.highlighted ? Theme.highlightColor
: Theme.primaryColor
anchors {
left: parent.left
right: parent.right
margins: Theme.paddingLarge
}
}
Label {
text: host + ":" + port
color: listItem.highlighted ? Theme.secondaryHighlightColor
: Theme.secondaryColor
anchors {
left: parent.left
right: parent.right
margins: Theme.paddingLarge
}
}
}
Component {
id: contextMenu
ContextMenu {
MenuItem {
text: "Edit"
onClicked: pageStack.push(Qt.resolvedUrl("AddConnection.qml"),
{
"connList": connectionList,
"oldId": connectionModel.get(index).id
});
}
MenuItem {
text: "Remove"
onClicked: remove();
}
}
}
onClicked: {
C.connect(connectionModel.get(index));
}
}
VerticalScrollDecorator { flickable: connectionList }
Component.onCompleted: updateList();
function updateList() {
connectionModel.clear();
var db = S.connect();
var connList = S.readAllConnections(db);
for (var i = 0; i < connList.length; ++i) {
connectionModel.append(connList[i]);
}
}
PullDownMenu {
MenuItem {
text: "About"
onClicked: pageStack.push(Qt.resolvedUrl("."));
}
MenuItem {
text: "Settings"
onClicked: pageStack.push(Qt.resolvedUrl("."));
}
MenuItem {
text: "Add connection"
onClicked: pageStack.push(Qt.resolvedUrl("AddConnection.qml"), { "connList": connectionList });
}
}
PushUpMenu {
MenuItem {
text: "Go to top"
onClicked: connectionList.scrollToTop();
}
}
}
}

54
qml/pages/DebugView.qml Normal file
View file

@ -0,0 +1,54 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import harbour.weechatrelay.connectionhandler 1.0
import "."
import "../js/connection.js" as C
Page {
id: debugViewPage
property bool isConnected: false;
function display(str) {
debugList.add(new Date(), str);
}
function connected() {
isConnected = true;
}
function disconnected() {
isConnected = false;
}
TextListComponent {
id: debugList
anchors.fill: parent
PushUpMenu {
MenuItem {
text: isConnected? "Disconnect" : "Close";
onClicked: {
if (isConnected) {
C.disconnect();
isConnected = false;
}
else {
C.clearConnection();
pageStack.replace("ConnectionList.qml");
}
}
}
}
VerticalScrollDecorator { flickable: debugList }
}
}

View file

@ -1,73 +0,0 @@
/*
Copyright (C) 2013 Jolla Ltd.
Contact: Thomas Perl <thomas.perl@jollamobile.com>
All rights reserved.
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Jolla Ltd nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
Page {
id: page
// To enable PullDownMenu, place our content in a SilicaFlickable
SilicaFlickable {
anchors.fill: parent
// PullDownMenu and PushUpMenu must be declared in SilicaFlickable, SilicaListView or SilicaGridView
PullDownMenu {
MenuItem {
text: "Show Page 2"
onClicked: pageStack.push(Qt.resolvedUrl("SecondPage.qml"))
}
}
// Tell SilicaFlickable the height of its content.
contentHeight: column.height
// Place our content in a Column. The PageHeader is always placed at the top
// of the page, followed by our content.
Column {
id: column
width: page.width
spacing: Theme.paddingLarge
PageHeader {
title: "UI Template"
}
Label {
x: Theme.paddingLarge
text: "Hello Sailors"
color: Theme.secondaryHighlightColor
font.pixelSize: Theme.fontSizeExtraLarge
}
}
}
}

View file

@ -1,62 +0,0 @@
/*
Copyright (C) 2013 Jolla Ltd.
Contact: Thomas Perl <thomas.perl@jollamobile.com>
All rights reserved.
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Jolla Ltd nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
Page {
id: page
SilicaListView {
id: listView
model: 20
anchors.fill: parent
header: PageHeader {
title: "Nested Page"
}
delegate: BackgroundItem {
id: delegate
Label {
x: Theme.paddingLarge
text: "Item " + index
anchors.verticalCenter: parent.verticalCenter
color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
}
onClicked: console.log("Clicked " + index)
}
VerticalScrollDecorator {}
}
}

View file

@ -0,0 +1,151 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import "../js/utils.js" as U
import "../js/moment.js" as M
import "../js/connection.js" as C
Dialog {
id: sslVerifyDialog
property string errorList;
property string issuer;
property var startDate;
property var expiryDate;
property string startDateText;
property string expiryDateText;
property string digest;
canAccept: acceptSwitch.checked;
onAccepted: {
if (saveSwitch.checked) {
C.storeFailedCertificate();
}
C.reconnectWithFailed();
}
SilicaFlickable {
id: sslVerifyFlickable
anchors.fill: parent
contentHeight: sslVerifyColumn.height + Theme.paddingLarge
VerticalScrollDecorator { flickable: sslVerifyFlickable }
Component.onCompleted: {
startDate = M.moment(startDate);
expiryDate = M.moment(expiryDate);
startDateText = startDate.format();
expiryDateText = expiryDate.format();
}
Column {
id: sslVerifyColumn
spacing: Theme.paddingMedium
DialogHeader {
id: connectHeader
title: "Connect"
cancelText: "Disconnect"
width: sslVerifyFlickable.width
}
Column {
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: Theme.paddingLarge
anchors.rightMargin: Theme.paddingLarge
spacing: Theme.paddingMedium
Label {
font.pixelSize: Theme.fontSizeLarge
text: "SSL verification failed"
wrapMode: Text.Wrap
color: Theme.highlightColor
width: parent.width
}
Label {
text: "The following errors occurred while attempting to connect "
+ "to the remote host:"
wrapMode: Text.Wrap
color: Theme.highlightColor
width: parent.width
}
Label {
text: errorList
wrapMode: Text.Wrap
color: Theme.highlightColor
font.weight: Font.Bold
width: parent.width
}
Label {
text: "This can mean that someone is trying to eavesdrop on your "
+ "connection or that the certificate is invalid or self-signed. "
+ "Please read through the errors and the information provided "
+ "below. Only continue connecting if you are certain of the "
+ "identity of the remote host. You can only accept the dialog "
+ "once you have scrolled fully down."
wrapMode: Text.Wrap
color: Theme.highlightColor
width: parent.width
}
PageHeader {
title: "Certificate information"
}
Label {
text: U.colored(Theme.secondaryHighlightColor, "Issuer:<br>") + issuer
wrapMode: Text.Wrap
color: Theme.highlightColor
width: parent.width
textFormat: Text.StyledText
}
Label {
text: U.colored(Theme.secondaryHighlightColor, "Valid from:<br>") + startDateText
wrapMode: Text.Wrap
color: Theme.highlightColor
width: parent.width
textFormat: Text.StyledText
}
Label {
text: U.colored(Theme.secondaryHighlightColor, "Valid to:<br>") + expiryDateText
wrapMode: Text.Wrap
color: Theme.highlightColor
width: parent.width
textFormat: Text.StyledText
}
Label {
text: U.colored(Theme.secondaryHighlightColor, "SHA-256 digest:<br>") + digest
wrapMode: Text.Wrap
color: Theme.highlightColor
width: parent.width
textFormat: Text.StyledText
}
Label { text: " " }
TextSwitch {
id: acceptSwitch
text: "I have verified this certificate is correct and wish to continue"
}
TextSwitch {
id: saveSwitch
text: "Automatically accept this certificate in the future"
}
}
}
}
}

View file

@ -0,0 +1,58 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import "../js/utils.js" as U
SilicaListView {
id: textList
function add(datetime, str) {
var snap = false;
if (textList.atYEnd) {
snap = true;
}
textListModel.append({ "time": datetime, "str": str });
if (snap) {
textList.positionViewAtEnd();
}
}
function formatTime(datetime) {
return U.colored(Theme.secondaryHighlightColor,
padTime(datetime.getHours()) + ':'
+ padTime(datetime.getMinutes()) + ':'
+ padTime(datetime.getSeconds()));
}
function padTime(timeInt) {
if (timeInt < 10) {
return '0' + timeInt;
}
return '' + timeInt;
}
model: ListModel { id: textListModel }
delegate: ListItem {
width: parent.width
contentHeight: messageLabel.height
Label {
id: messageLabel
text: formatTime(time) + " " + U.escapeStyled(str)
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
color: Theme.highlightColor
anchors.left: parent.left
anchors.right: parent.right
anchors.leftMargin: Theme.paddingMedium
anchors.rightMargin: Theme.paddingMedium
textFormat: Text.StyledText
}
}
}

View file

@ -12,11 +12,11 @@ Name: harbour-weechatrelay
%{!?qtc_qmake5:%define qtc_qmake5 %qmake5}
%{!?qtc_make:%define qtc_make make}
%{?qtc_builddir:%define _builddir %qtc_builddir}
Summary: WeeChat Relay
Summary: WeeCRApp
Version: 0.1
Release: 1
Group: Qt/Qt
License: LICENSE
License: MIT Expat licence
URL: http://example.org/
Source0: %{name}-%{version}.tar.bz2
Source100: harbour-weechatrelay.yaml
@ -28,8 +28,7 @@ BuildRequires: pkgconfig(sailfishapp) >= 0.0.10
BuildRequires: desktop-file-utils
%description
Short description of my SailfishOS Application
WeeCRApp (WeeChat Relay App) is a WeeChat relay client app for Sailfish OS.
%prep
%setup -q -n %{name}-%{version}

View file

@ -4,10 +4,11 @@ Version: 0.1
Release: 1
Group: Qt/Qt
URL: http://example.org/
License: LICENSE
License: MIT Expat licence
Sources:
- '%{name}-%{version}.tar.bz2'
Description: ""
Description: "WeeCRApp (WeeChat Relay App) is a WeeChat relay client app for Sailfish
OS."
Configure: none
Builder: qtc5
PkgConfigBR:

187
src/connectionhandler.cpp Normal file
View file

@ -0,0 +1,187 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#include <QTcpSocket>
#include <QSslSocket>
#include <QMetaObject>
#include <QDateTime>
#include "connectionhandler.h"
#include "sslrelayconnection.h"
#include "weechatprotocolhandler.h"
ConnectionHandler::ConnectionHandler(QObject* parent) :
QObject(parent),
connection(0),
handler(0),
protocol(WEECHAT),
security(NONE),
host(),
port(0),
password(),
acceptInfo(0),
failedCert()
{
}
void ConnectionHandler::connect(ProtocolType type,
ConnectionSecurity security,
QString host,
uint port,
QString password)
{
this->host = host;
this->port = port;
this->password = password;
this->protocol = type;
this->security = security;
if (type == ProtocolType::WEECHAT) {
handler = new WeeChatProtocolHandler(this);
}
if (security == ConnectionSecurity::NONE) {
QTcpSocket* socket = new QTcpSocket(this);
connection = new RelayConnection(socket, 0);
}
else if (security == ConnectionSecurity::SSL) {
QSslSocket* socket = new QSslSocket(this);
SSLRelayConnection* sslConnection = new SSLRelayConnection(socket, 0);
sslConnection->setHandler(this);
connection = sslConnection;
}
connectSignals();
// Move connection to another thread to prevent blocking the rest of the app
//connection->moveToThread(&connectionThread);
//connectionThread.start();
QMetaObject::invokeMethod(connection,
"connect",
Qt::AutoConnection,
Q_ARG(QString, host),
Q_ARG(uint, port));
}
void ConnectionHandler::disconnect()
{
connection->disconnect();
delete handler;
handler = 0;
}
void ConnectionHandler::reconnect()
{
connect(protocol, security, host, port, password);
}
// Reconnect with the certificate that failed to verify the last time
void ConnectionHandler::reconnectWithFailed()
{
acceptInfo = new QSslCertificateInfo(failedCert, this);
reconnect();
}
// Return the failed certificate for storage in PEM form
QSslCertificateInfo* ConnectionHandler::getFailedCertificate()
{
return new QSslCertificateInfo(failedCert);
}
// Parse and accept the given certificate on next connection
// Note: Will not use the given certificate if the dates are wrong
void ConnectionHandler::acceptCertificate(QSslCertificateInfo* info)
{
// Check that the current date is between the start and end dates
QDateTime now = QDateTime::currentDateTime();
if (now >= info->getEffectiveDate() && now <= info->getExpiryDate())
{
info->setParent(this);
acceptInfo = info;
}
else
{
emit displayDebugData("Stored SSL certificate skipped due to invalid dates.");
}
}
// Clear all meaningful data left over by previous connection attempts
void ConnectionHandler::clearData()
{
protocol = WEECHAT;
security = NONE;
host = "";
port = 0;
password = "";
acceptInfo = 0;
failedCert = QSslCertificate();
}
void ConnectionHandler::relayConnected()
{
emit connected();
handler->initialize(password);
}
void ConnectionHandler::relayDisconnected()
{
emit disconnected();
}
bool ConnectionHandler::relaySslErrors(QStringList errorStrings, QSslCertificate remoteCert)
{
// The SSL verification failed, check if we can accept the certificate anyway
if (acceptInfo != 0)
{
QString remoteDigest(remoteCert.digest(QCryptographicHash::Sha3_512).toHex());
if (acceptInfo->getEffectiveDate() == remoteCert.effectiveDate()
&& acceptInfo->getExpiryDate() == remoteCert.expiryDate()
&& acceptInfo->getDigest() == remoteDigest)
{
return true;
}
}
QStringList issuerInfoList = remoteCert.issuerInfo(QSslCertificate::Organization);
failedCert = remoteCert;
emit sslError(errorStrings.join("\n"), issuerInfoList.join("\n"),
remoteCert.effectiveDate(), remoteCert.expiryDate(),
remoteCert.digest(QCryptographicHash::Sha256).toHex());
return false;
}
void ConnectionHandler::handleDebugData(QString hexstring)
{
emit displayDebugData(hexstring);
}
void ConnectionHandler::sendDebugData(QString string)
{
}
void ConnectionHandler::connectSignals()
{
QObject::connect(connection, &RelayConnection::connected, this, &ConnectionHandler::relayConnected);
QObject::connect(connection, &RelayConnection::disconnected, this, &ConnectionHandler::relayDisconnected);
QObject::connect(connection, &RelayConnection::newDataAvailable, [this]() {
this->handler->handleNewData(connection);
});
QObject::connect(handler, &ProtocolHandler::debugData, this, &ConnectionHandler::handleDebugData);
QObject::connect(handler, &ProtocolHandler::sendData, connection, &RelayConnection::write);
}

82
src/connectionhandler.h Normal file
View file

@ -0,0 +1,82 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#ifndef CONNECTIONHANDLER_H
#define CONNECTIONHANDLER_H
#include <QObject>
#include <QThread>
#include <QDateTime>
#include <QString>
#include <QStringList>
#include <QSslCertificate>
#include <QByteArray>
#include "relayconnection.h"
#include "protocolhandler.h"
#include "qsslcertificateinfo.h"
class ConnectionHandler : public QObject
{
Q_OBJECT
public:
enum ConnectionSecurity { NONE, SSL };
enum ProtocolType { WEECHAT };
Q_ENUMS(ConnectionSecurity ProtocolType)
explicit ConnectionHandler(QObject* parent = 0);
Q_INVOKABLE void connect(ProtocolType protocol,
ConnectionSecurity security,
QString host,
uint port,
QString password);
Q_INVOKABLE void disconnect();
Q_INVOKABLE void reconnect();
Q_INVOKABLE void reconnectWithFailed();
Q_INVOKABLE QSslCertificateInfo* getFailedCertificate();
Q_INVOKABLE void acceptCertificate(QSslCertificateInfo* info);
Q_INVOKABLE void clearData();
signals:
void connected();
void disconnected();
void displayDebugData(QString data);
void sslError(QString errorStrings, QString issuerInfo,
QDateTime startDate, QDateTime expiryDate,
QString digest);
public slots:
void relayConnected();
void relayDisconnected();
bool relaySslErrors(QStringList errorStrings, QSslCertificate remoteCert);
void handleDebugData(QString hexstring);
void sendDebugData(QString string);
private:
RelayConnection* connection;
ProtocolHandler* handler;
ProtocolType protocol;
ConnectionSecurity security;
QString host;
uint port;
QString password;
// Accept SSL certificates with this info when connecting
QSslCertificateInfo* acceptInfo;
// The SSL certificate that failed verification on last connect
QSslCertificate failedCert;
void connectSignals();
};
#endif // CONNECTIONHANDLER_H

18
src/connectresolver.h Normal file
View file

@ -0,0 +1,18 @@
/*
* This snippet helps when connecting to overloaded signals using the new
* connect syntax.
*
* See http://stackoverflow.com/a/16795664
*/
#ifndef CONNECTRESOLVER_H
#define CONNECTRESOLVER_H
template<typename... Args> struct SELECT {
template<typename C, typename R>
static constexpr auto OVERLOAD_OF( R (C::*pmf)(Args...) ) -> decltype(pmf) {
return pmf;
}
};
#endif // CONNECTRESOLVER_H

View file

@ -1,40 +1,22 @@
/*
Copyright (C) 2013 Jolla Ltd.
Contact: Thomas Perl <thomas.perl@jollamobile.com>
All rights reserved.
You may use this file under the terms of BSD license as follows:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Jolla Ltd nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#ifdef QT_QML_DEBUG
#include <QtQuick>
#endif
#include <sailfishapp.h>
#include <QCoreApplication>
#include <QGuiApplication>
#include <QtQml>
int main(int argc, char *argv[])
#include "connectionhandler.h"
#include "qsslcertificateinfo.h"
int main(int argc, char* argv[])
{
// SailfishApp::main() will display "qml/template.qml", if you need more
// control over initialization, you can use:
@ -45,6 +27,14 @@ int main(int argc, char *argv[])
//
// To display the view, call "show()" (will show fullscreen on device).
QCoreApplication::setApplicationName("WeeCRApp");
QCoreApplication::setOrganizationName("Nytsoi Inc.");
QCoreApplication::setOrganizationDomain("nytsoi.net");
// Register custom types to be accessible from QML
qmlRegisterType<ConnectionHandler>("harbour.weechatrelay.connectionhandler", 1, 0, "ConnectionHandler");
qmlRegisterType<QSslCertificateInfo>("harbour.weechatrelay.qsslcertificateinfo", 1, 0, "QSslCertificateInfo");
return SailfishApp::main(argc, argv);
}

26
src/protocolhandler.cpp Normal file
View file

@ -0,0 +1,26 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#include "protocolhandler.h"
ProtocolHandler::ProtocolHandler(QObject* parent):
QObject(parent),
bytesNeeded(0)
{
}
void ProtocolHandler::emitData(QString data)
{
QByteArray bytes;
bytes.append(data);
emit sendData(bytes);
}
QDataStream* ProtocolHandler::read(RelayConnection* connection, quint64 bytes)
{
QByteArray data = connection->read(bytes);
return new QDataStream(data);
}

46
src/protocolhandler.h Normal file
View file

@ -0,0 +1,46 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#ifndef PROTOCOLHANDLER_H
#define PROTOCOLHANDLER_H
#include <QObject>
#include <QByteArray>
#include <QString>
#include <QDataStream>
#include "relayconnection.h"
class ProtocolHandler : public QObject
{
Q_OBJECT
public:
explicit ProtocolHandler(QObject* parent = 0);
virtual ~ProtocolHandler() {}
// Initalize protocol connection and login with password
virtual void initialize(QString password) = 0;
signals:
void debugData(QString hexString);
// ProtocolHandler wants to send data to the associated network stream
void sendData(QByteArray data);
public slots:
virtual void handleNewData(RelayConnection* connection) = 0;
virtual void sendDebugData(QString string) = 0;
protected:
void emitData(QString data);
QDataStream* read(RelayConnection* connection, quint64 bytes);
// How many bytes we need to be available until we execute the next read
qint64 bytesNeeded;
};
#endif // PROTOCOLHANDLER_H

View file

@ -0,0 +1,49 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#include "qsslcertificateinfo.h"
QSslCertificateInfo::QSslCertificateInfo(QSslCertificate cert, QObject* parent) :
QObject(parent)
{
effectiveDate = cert.effectiveDate();
expiryDate = cert.expiryDate();
digest = cert.digest(QCryptographicHash::Sha3_512).toHex();
}
QSslCertificateInfo::QSslCertificateInfo():
QObject(0)
{}
QDateTime QSslCertificateInfo::getEffectiveDate() const
{
return effectiveDate;
}
QDateTime QSslCertificateInfo::getExpiryDate() const
{
return expiryDate;
}
QString QSslCertificateInfo::getDigest() const
{
return digest;
}
void QSslCertificateInfo::setEffectiveDate(QDateTime effectiveDate)
{
this->effectiveDate = effectiveDate;
}
void QSslCertificateInfo::setExpiryDate(QDateTime expiryDate)
{
this->expiryDate = expiryDate;
}
void QSslCertificateInfo::setDigest(QString digest)
{
this->digest = digest;
}

42
src/qsslcertificateinfo.h Normal file
View file

@ -0,0 +1,42 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#ifndef QSSLCERTIFICATEINFO_H
#define QSSLCERTIFICATEINFO_H
#include <QObject>
#include <QSslCertificate>
#include <QDateTime>
#include <QString>
// This class is used as a struct to pass certificate information to the UI
class QSslCertificateInfo : public QObject
{
Q_OBJECT
public:
explicit QSslCertificateInfo(QSslCertificate cert, QObject* parent = 0);
QSslCertificateInfo();
Q_PROPERTY(QDateTime effectiveDate READ getEffectiveDate WRITE setEffectiveDate)
Q_PROPERTY(QDateTime expiryDate READ getExpiryDate WRITE setExpiryDate)
Q_PROPERTY(QString digest READ getDigest WRITE setDigest)
QDateTime getEffectiveDate() const;
QDateTime getExpiryDate() const;
QString getDigest() const;
void setEffectiveDate(QDateTime effectiveDate);
void setExpiryDate(QDateTime expiryDate);
void setDigest(QString digest);
private:
QDateTime effectiveDate;
QDateTime expiryDate;
QString digest;
};
#endif // QSSLCERTIFICATEINFO_H

View file

@ -1,36 +1,71 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#include "relayconnection.h"
#include <QDebug>
/*QSslSocket socket;
socket.connectToHostEncrypted("nytsoi.net", 5555);
if (!socket.waitForEncrypted()) {
qDebug() << socket.errorString();
QSslCertificate cert = socket.peerCertificate();
QStringList info = cert.issuerInfo(QSslCertificate::Organization);
for (QString str : info)
{
qDebug() << "Info: " << str;
}
qDebug() << cert.effectiveDate().toString();
qDebug() << cert.expiryDate().toString();
qDebug() << cert.publicKey().toPem();
qDebug() << "SHA-256 digest: " << cert.digest(QCryptographicHash::Sha256).toHex();
return 1;
}*/
RelayConnection::RelayConnection(QTcpSocket* socket):
socket(socket)
RelayConnection::RelayConnection(QTcpSocket* socket, QObject* parent):
QObject(parent), socket(socket)
{
socket->setParent(this);
QObject::connect(socket, &QTcpSocket::connected, this, &RelayConnection::socketConnected);
QObject::connect(socket, &QTcpSocket::readyRead, this, &RelayConnection::socketReadyRead);
QObject::connect(socket, &QTcpSocket::disconnected, this, &RelayConnection::socketDisconnected);
}
void RelayConnection::readForever()
QByteArray RelayConnection::read(quint64 bytes)
{
while (true)
char* data = new char[bytes];
qint64 errorStatus = socket->read(data, bytes);
if (errorStatus < 0)
{
QByteArray readBytes = this->socket->readAll();
QByteArray pe = readBytes.toPercentEncoding();
qDebug(pe.constData());
// Reading from socket failed, disconnect automatically
disconnect();
emit disconnected();
return QByteArray("");
}
return QByteArray(data, bytes);
}
qint64 RelayConnection::bytesAvailable() const
{
return socket->bytesAvailable();
}
void RelayConnection::connect(QString host, uint port)
{
socket->connectToHost(host, port);
}
void RelayConnection::disconnect()
{
socket->disconnectFromHost();
}
void RelayConnection::write(QByteArray data)
{
socket->write(data);
}
// A successful connection has been initiated
void RelayConnection::socketConnected()
{
emit connected();
}
void RelayConnection::socketDisconnected()
{
emit disconnected();
}
// There is new data available for reading on the socket
void RelayConnection::socketReadyRead()
{
emit newDataAvailable();
}

View file

@ -1,18 +1,41 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#ifndef RELAYCONNECTION_H
#define RELAYCONNECTION_H
#include <QObject>
#include <QTcpSocket>
#include <QByteArray>
class RelayConnection
class RelayConnection : public QObject
{
Q_OBJECT
public:
RelayConnection(QTcpSocket* socket);
explicit RelayConnection(QTcpSocket* socket, QObject* parent = 0);
void readForever();
QByteArray read(quint64 bytes);
qint64 bytesAvailable() const;
public slots:
void connect(QString host, uint port);
void disconnect();
void write(QByteArray data);
void socketConnected();
void socketDisconnected();
void socketReadyRead();
private:
signals:
void connected();
void disconnected();
void newDataAvailable();
protected:
QTcpSocket* socket;
};

View file

@ -0,0 +1,54 @@
#include "sslrelayconnection.h"
#include "connectresolver.h"
#include <QStringList>
#include <QSslCertificate>
#include <QSslKey>
#include <QMetaObject>
SSLRelayConnection::SSLRelayConnection(QSslSocket* socket, QObject* parent) :
RelayConnection(socket, parent), sslSocket(socket)
{
QObject::connect(socket, SELECT<const QList<QSslError>&>::OVERLOAD_OF(&QSslSocket::sslErrors),
this, &SSLRelayConnection::socketSslErrors);
}
void SSLRelayConnection::setHandler(ConnectionHandler* handler)
{
this->handler = handler;
}
void SSLRelayConnection::connect(QString host, uint port)
{
sslSocket->connectToHostEncrypted(host, port);
}
void SSLRelayConnection::socketSslErrors(const QList<QSslError>& errors)
{
QStringList errorStrings;
for (QSslError error : errors)
{
errorStrings.append(error.errorString());
}
QSslCertificate cert = sslSocket->peerCertificate();
// Let the user know about the errors and then fail the connection if the certificate
// wasn't accepted
bool accepted = false;
QMetaObject::invokeMethod(handler,
"relaySslErrors",
Qt::AutoConnection,
Q_RETURN_ARG(bool, accepted),
Q_ARG(QStringList, errorStrings),
Q_ARG(QSslCertificate, cert));
if (accepted)
{
sslSocket->ignoreSslErrors(errors);
}
}

40
src/sslrelayconnection.h Normal file
View file

@ -0,0 +1,40 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#ifndef SSLRELAYCONNECTION_H
#define SSLRELAYCONNECTION_H
#include <QSslSocket>
#include <QString>
#include <QList>
#include <QSslError>
#include "relayconnection.h"
#include "connectionhandler.h"
class SSLRelayConnection : public RelayConnection
{
Q_OBJECT
public:
explicit SSLRelayConnection(QSslSocket* socket, QObject* parent = 0);
void setHandler(ConnectionHandler* handler);
signals:
void newData(QByteArray* data);
public slots:
void connect(QString host, uint port);
void socketSslErrors(const QList<QSslError>& errors);
private:
QSslSocket* sslSocket;
ConnectionHandler* handler;
};
#endif // SSLRELAYCONNECTION_H

View file

@ -0,0 +1,5 @@
#include "array.h"
Array::Array()
{
}

12
src/weechatproto/array.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef ARRAY_H
#define ARRAY_H
#include "protocoltype.h"
class Array : public ProtocolType
{
public:
Array();
};
#endif // ARRAY_H

View file

@ -0,0 +1,28 @@
#include "buffer.h"
Buffer::Buffer(): data(0)
{
}
void Buffer::readFrom(QDataStream* data)
{
ProtocolType::markRead();
// Buffer is the same as string, but just bytes
quint32 length;
*data >> length;
this->data = new QByteArray();
quint8 current;
for (quint32 i = 0; i < length; ++i)
{
*data >> current;
this->data->append(static_cast<char>(current));
}
}
const QByteArray* Buffer::getBytes() const
{
return this->data;
}

17
src/weechatproto/buffer.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef BUFFER_H
#define BUFFER_H
#include "protocoltype.h"
class Buffer : public ProtocolType
{
public:
Buffer();
void readFrom(QDataStream* data);
const QByteArray* getBytes() const;
private:
QByteArray* data;
};
#endif // BUFFER_H

24
src/weechatproto/char.cpp Normal file
View file

@ -0,0 +1,24 @@
#include "char.h"
Char::Char(): data(' ')
{
}
void Char::readFrom(QDataStream* data)
{
ProtocolType::markRead();
quint8 c;
*data >> c;
this->data = static_cast<char>(c);
}
char Char::getChar() const
{
return this->data;
}
std::size_t Char::hash() const
{
return data * 2654435761 % 2^32;
}

20
src/weechatproto/char.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef CHAR_H
#define CHAR_H
#include "protocoltype.h"
class Char : public ProtocolType
{
public:
Char();
void readFrom(QDataStream* data);
char getChar() const;
std::size_t hash() const;
private:
char data;
};
#endif // CHAR_H

View file

@ -0,0 +1,10 @@
#include "hashtable.h"
HashTable::HashTable()
{
}
void HashTable::readFrom(QDataStream* data)
{
ProtocolType::markRead();
}

View file

@ -0,0 +1,17 @@
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include "protocoltype.h"
#include <unordered_map>
class HashTable : public ProtocolType
{
public:
HashTable();
void readFrom(QDataStream* data);
private:
};
#endif // HASHTABLE_H

View file

@ -0,0 +1,5 @@
#include "hdata.h"
HData::HData()
{
}

12
src/weechatproto/hdata.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef HDATA_H
#define HDATA_H
#include "protocoltype.h"
class HData : public ProtocolType
{
public:
HData();
};
#endif // HDATA_H

View file

@ -0,0 +1,5 @@
#include "info.h"
Info::Info()
{
}

12
src/weechatproto/info.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef INFO_H
#define INFO_H
#include "protocoltype.h"
class Info : public ProtocolType
{
public:
Info();
};
#endif // INFO_H

View file

@ -0,0 +1,5 @@
#include "infolist.h"
InfoList::InfoList()
{
}

View file

@ -0,0 +1,12 @@
#ifndef INFOLIST_H
#define INFOLIST_H
#include "protocoltype.h"
class InfoList : public ProtocolType
{
public:
InfoList();
};
#endif // INFOLIST_H

View file

@ -0,0 +1,17 @@
#include "integer.h"
Integer::Integer(): data(0)
{
}
void Integer::readFrom(QDataStream* data)
{
ProtocolType::markRead();
*data >> this->data;
}
qint32 Integer::getInt() const
{
return this->data;
}

View file

@ -0,0 +1,17 @@
#ifndef INTEGER_H
#define INTEGER_H
#include "protocoltype.h"
class Integer : public ProtocolType
{
public:
Integer();
void readFrom(QDataStream* data);
qint32 getInt() const;
private:
qint32 data;
};
#endif // INTEGER_H

50
src/weechatproto/long.cpp Normal file
View file

@ -0,0 +1,50 @@
#include "long.h"
Long::Long(): data(0)
{
}
void Long::readFrom(QDataStream* data)
{
ProtocolType::markRead();
// Long is stored as a string, with one byte showing the length
quint8 length;
*data >> length;
quint8 current;
qint8 magnitude = 1;
for (quint8 i = 0; i < length; ++i)
{
*data >> current;
if (i == 0)
{
if (current == 45) // ASCII '-'
{
magnitude = -1;
}
}
else
{
this->data *= 10;
}
this->data += magnitude * static_cast<qint64>(current - '0');
}
}
qint64 Long::getLong() const
{
return this->data;
}
std::size_t Long::hash() const
{
return data * 2654435761 % 2^32;
}
bool Long::operator==(const Long& other) const
{
return (other.getLong() == this->data);
}

19
src/weechatproto/long.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef LONG_H
#define LONG_H
#include "protocoltype.h"
class Long : public ProtocolType
{
public:
Long();
void readFrom(QDataStream* data);
qint64 getLong() const;
std::size_t hash() const;
bool operator==(const Long& other) const;
private:
qint64 data;
};
#endif // LONG_H

View file

@ -0,0 +1,28 @@
#include "pointer.h"
Pointer::Pointer(): data(0)
{
}
void Pointer::readFrom(QDataStream* data)
{
ProtocolType::markRead();
// Pointers are stored as length (1 byte) + data as hex string
quint8 length;
*data >> length;
this->data = new QString();
quint8 current;
for (quint8 i = 0; i < length; ++i)
{
*data >> current;
this->data->append(static_cast<char>(current));
}
}
const QString* Pointer::getPointer() const
{
return this->data;
}

View file

@ -0,0 +1,17 @@
#ifndef POINTER_H
#define POINTER_H
#include "protocoltype.h"
class Pointer : public ProtocolType
{
public:
Pointer();
void readFrom(QDataStream* data);
const QString* getPointer() const;
private:
QString* data;
};
#endif // POINTER_H

View file

@ -0,0 +1,19 @@
#include "protocoltype.h"
#include "protocoltypeoverwriteexception.h"
ProtocolType::ProtocolType(): isRead(false)
{
}
// Mark this object as read, so that the values can't
// be overwritten. A second call to this method will throw
// an exception.
void ProtocolType::markRead()
{
if (isRead)
{
throw new ProtocolTypeOverWriteException("Cannot overwrite ProtocolType.");
}
isRead = true;
}

View file

@ -0,0 +1,22 @@
#ifndef PROTOCOLTYPE_H
#define PROTOCOLTYPE_H
#include <QDataStream>
#include <cstddef>
class ProtocolType
{
public:
ProtocolType();
virtual void readFrom(QDataStream* data) = 0;
void markRead();
// For hashtable
virtual std::size_t hash() const = 0;
private:
bool isRead;
};
#endif // PROTOCOLTYPE_H

View file

@ -0,0 +1,6 @@
#include "protocoltypeoverwriteexception.h"
ProtocolTypeOverWriteException::ProtocolTypeOverWriteException(const std::string& reason):
std::runtime_error(reason)
{
}

View file

@ -0,0 +1,13 @@
#ifndef PROTOCOLTYPEOVERWRITEEXCEPTION_H
#define PROTOCOLTYPEOVERWRITEEXCEPTION_H
#include <stdexcept>
#include <string>
class ProtocolTypeOverWriteException : public std::runtime_error
{
public:
explicit ProtocolTypeOverWriteException(const std::string& reason);
};
#endif // PROTOCOLTYPEOVERWRITEEXCEPTION_H

View file

@ -0,0 +1,28 @@
#include "string.h"
String::String(): data(0)
{
}
void String::readFrom(QDataStream* data)
{
ProtocolType::markRead();
// Strings are stored as length (4 bytes) + data (no \0)
quint32 length;
*data >> length;
this->data = new QString();
quint8 current;
for (quint32 i = 0; i < length; ++i)
{
*data >> current;
this->data->append(static_cast<char>(current));
}
}
const QString* String::getString() const
{
return this->data;
}

17
src/weechatproto/string.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef STRING_H
#define STRING_H
#include "protocoltype.h"
class String : public ProtocolType
{
public:
String();
void readFrom(QDataStream* data);
const QString* getString() const;
private:
QString* data;
};
#endif // STRING_H

25
src/weechatproto/time.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "time.h"
#include "long.h"
Time::Time(): data(0)
{
}
void Time::readFrom(QDataStream* data)
{
ProtocolType::markRead();
// Time is stored as length (1 byte) + data (unix time)
// It's the same format as a Long so we will read it that way
Long* timelong = new Long();
timelong->readFrom(data);
quint64 timestamp = timelong->getLong();
this->data = new QDateTime();
this->data->fromTime_t(timestamp);
}
const QDateTime* Time::getTime() const
{
return this->data;
}

19
src/weechatproto/time.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef TIME_H
#define TIME_H
#include "protocoltype.h"
#include <QDateTime>
class Time : public ProtocolType
{
public:
Time();
void readFrom(QDataStream* data);
const QDateTime* getTime() const;
private:
QDateTime* data;
};
#endif // TIME_H

View file

@ -0,0 +1,99 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#include <QDebug>
#include <QDataStream>
#include "weechatprotocolhandler.h"
WeeChatProtocolHandler::WeeChatProtocolHandler(QObject* parent) :
ProtocolHandler(parent),
readingBody(false)
{
bytesNeeded = HEADER_BYTES;
}
void WeeChatProtocolHandler::initialize(QString password)
{
emitData("init compression=off,password=" + password);
emitData("test");
emitData("sync");
}
// Handle incoming data from the socket and emit the appropriate signals
void WeeChatProtocolHandler::handleNewData(RelayConnection* connection)
{
// If we cannot read the amount of bytes we want, skip and retry next time
if (connection->bytesAvailable() < bytesNeeded)
{
qDebug() << connection->bytesAvailable() << " was lower than " << bytesNeeded << "... Skipping.";
return;
}
QDataStream* data = read(connection, bytesNeeded);
if (!readingBody)
{
// The header contains the length of the message and compression specifier
quint32 length;
quint8 compression;
(*data) >> length;
(*data) >> compression;
qDebug() << length << " " << compression;
// The length includes the header
readingBody = true;
bytesNeeded = length - HEADER_BYTES;
}
else
{
handleBody(data);
readingBody = false;
bytesNeeded = HEADER_BYTES;
}
delete data;
data = 0;
emit debugData("");
}
void WeeChatProtocolHandler::sendDebugData(QString string)
{
emitData(string);
}
void WeeChatProtocolHandler::emitData(QString data)
{
ProtocolHandler::emitData(data + "\n");
}
void WeeChatProtocolHandler::handleBody(QDataStream* data)
{
// The first element is the id which will affect how we interpret the rest
char* idData = 0;
(*data) >> idData;
qDebug() << "Id: " << idData;
QString id(idData);
if (id == "_buffer_line_added")
{
handleBufferLineAdded(data);
}
delete idData;
}
void WeeChatProtocolHandler::handleBufferLineAdded(QDataStream* data)
{
}

View file

@ -0,0 +1,53 @@
/*
* © Mikko Ahlroth 2014
* WeeCRApp is open source software. For licensing information, please check
* the LICENCE file.
*/
#ifndef WEECHATPROTOCOLHANDLER_H
#define WEECHATPROTOCOLHANDLER_H
#include <QObject>
#include <QDateTime>
#include "protocolhandler.h"
class WeeChatProtocolHandler : public ProtocolHandler
{
Q_OBJECT
public:
explicit WeeChatProtocolHandler(QObject* parent = 0);
void initialize(QString password);
// How many bytes at least each message contains
static const qint64 HEADER_BYTES = 5;
signals:
public slots:
void handleNewData(RelayConnection* connection);
void sendDebugData(QString string);
protected:
void emitData(QString data);
void handleBody(QDataStream* data);
// WeeChat event types
void handleBufferLineAdded(QDataStream* data);
void handleUnknown(QDataStream* data);
// Individual object types
char handleChar(QDataStream* data);
qint32 handleInt(QDataStream* data);
qint64 handleLong(QDataStream* data);
QString* handleString(QDataStream* data);
QByteArray* handleBuffer(QDataStream* data);
QString* handlePointer(QDataStream* data);
QDateTime* handleTime(QDataStream* data);
bool readingBody;
};
#endif // WEECHATPROTOCOLHANDLER_H