diff --git a/LICENCE b/LICENCE new file mode 100644 index 0000000..9746a15 --- /dev/null +++ b/LICENCE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..58b8582 --- /dev/null +++ b/README.md @@ -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/). diff --git a/harbour-weechatrelay.pro b/harbour-weechatrelay.pro index 933b184..854a8ff 100644 --- a/harbour-weechatrelay.pro +++ b/harbour-weechatrelay.pro @@ -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 + diff --git a/qml/cover/CoverPage.qml b/qml/cover/CoverPage.qml deleted file mode 100644 index 6f8e4a4..0000000 --- a/qml/cover/CoverPage.qml +++ /dev/null @@ -1,54 +0,0 @@ -/* - Copyright (C) 2013 Jolla Ltd. - Contact: Thomas Perl - 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" - } - } -} - - diff --git a/qml/cover/DefaultCover.qml b/qml/cover/DefaultCover.qml new file mode 100644 index 0000000..28bfa6d --- /dev/null +++ b/qml/cover/DefaultCover.qml @@ -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" + } +} + + diff --git a/qml/harbour-weechatrelay.qml b/qml/harbour-weechatrelay.qml index bb031a4..5995d1f 100644 --- a/qml/harbour-weechatrelay.qml +++ b/qml/harbour-weechatrelay.qml @@ -1,41 +1,34 @@ /* - Copyright (C) 2013 Jolla Ltd. - Contact: Thomas Perl - 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 + } } diff --git a/qml/js/connection.js b/qml/js/connection.js new file mode 100644 index 0000000..6eb0c1d --- /dev/null +++ b/qml/js/connection.js @@ -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 + + + + + diff --git a/qml/js/debug.js b/qml/js/debug.js new file mode 100644 index 0000000..68987c9 --- /dev/null +++ b/qml/js/debug.js @@ -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(""); +} diff --git a/qml/js/moment.js b/qml/js/moment.js new file mode 100644 index 0000000..b04a39e --- /dev/null +++ b/qml/js/moment.js @@ -0,0 +1,2498 @@ +.pragma library + +//! moment.js +//! version : 2.6.0 +//! authors : Tim Wood, Iskren Chernev, Moment.js contributors +//! license : MIT +//! momentjs.com + +var moment = null; +var initialized = false; + +if (!initialized) { + init(); +} + +function init(undefined) { + + /************************************ + Constants + ************************************/ + + var moment, + VERSION = "2.6.0", + // the global-scope this is NOT the global object in Node.js + globalScope = typeof global !== 'undefined' ? global : this, + oldGlobalMoment, + round = Math.round, + i, + + YEAR = 0, + MONTH = 1, + DATE = 2, + HOUR = 3, + MINUTE = 4, + SECOND = 5, + MILLISECOND = 6, + + // internal storage for language config files + languages = {}, + + // moment internal properties + momentProperties = { + _isAMomentObject: null, + _i : null, + _f : null, + _l : null, + _strict : null, + _isUTC : null, + _offset : null, // optional. Combine with _isUTC + _pf : null, + _lang : null // optional + }, + + // check for nodeJS + hasModule = (typeof module !== 'undefined' && module.exports), + + // ASP.NET json date format regex + aspNetJsonRegex = /^\/?Date\((\-?\d+)/i, + aspNetTimeSpanJsonRegex = /(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/, + + // from http://docs.closure-library.googlecode.com/git/closure_goog_date_date.js.source.html + // somewhat more in line with 4.4.3.2 2004 spec, but allows decimal anywhere + isoDurationRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/, + + // format tokens + formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g, + localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g, + + // parsing token regexes + parseTokenOneOrTwoDigits = /\d\d?/, // 0 - 99 + parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999 + parseTokenOneToFourDigits = /\d{1,4}/, // 0 - 9999 + parseTokenOneToSixDigits = /[+\-]?\d{1,6}/, // -999,999 - 999,999 + parseTokenDigits = /\d+/, // nonzero number of digits + parseTokenWord = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i, // any word (or two) characters or numbers including two/three word month in arabic. + parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/gi, // +00:00 -00:00 +0000 -0000 or Z + parseTokenT = /T/i, // T (ISO separator) + parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123 + parseTokenOrdinal = /\d{1,2}/, + + //strict parsing regexes + parseTokenOneDigit = /\d/, // 0 - 9 + parseTokenTwoDigits = /\d\d/, // 00 - 99 + parseTokenThreeDigits = /\d{3}/, // 000 - 999 + parseTokenFourDigits = /\d{4}/, // 0000 - 9999 + parseTokenSixDigits = /[+-]?\d{6}/, // -999,999 - 999,999 + parseTokenSignedNumber = /[+-]?\d+/, // -inf - inf + + // iso 8601 regex + // 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000 or +00) + isoRegex = /^\s*(?:[+-]\d{6}|\d{4})-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d+)?)?)?)?([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?$/, + + isoFormat = 'YYYY-MM-DDTHH:mm:ssZ', + + isoDates = [ + ['YYYYYY-MM-DD', /[+-]\d{6}-\d{2}-\d{2}/], + ['YYYY-MM-DD', /\d{4}-\d{2}-\d{2}/], + ['GGGG-[W]WW-E', /\d{4}-W\d{2}-\d/], + ['GGGG-[W]WW', /\d{4}-W\d{2}/], + ['YYYY-DDD', /\d{4}-\d{3}/] + ], + + // iso time formats and regexes + isoTimes = [ + ['HH:mm:ss.SSSS', /(T| )\d\d:\d\d:\d\d\.\d+/], + ['HH:mm:ss', /(T| )\d\d:\d\d:\d\d/], + ['HH:mm', /(T| )\d\d:\d\d/], + ['HH', /(T| )\d\d/] + ], + + // timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"] + parseTimezoneChunker = /([\+\-]|\d\d)/gi, + + // getter and setter names + proxyGettersAndSetters = 'Date|Hours|Minutes|Seconds|Milliseconds'.split('|'), + unitMillisecondFactors = { + 'Milliseconds' : 1, + 'Seconds' : 1e3, + 'Minutes' : 6e4, + 'Hours' : 36e5, + 'Days' : 864e5, + 'Months' : 2592e6, + 'Years' : 31536e6 + }, + + unitAliases = { + ms : 'millisecond', + s : 'second', + m : 'minute', + h : 'hour', + d : 'day', + D : 'date', + w : 'week', + W : 'isoWeek', + M : 'month', + Q : 'quarter', + y : 'year', + DDD : 'dayOfYear', + e : 'weekday', + E : 'isoWeekday', + gg: 'weekYear', + GG: 'isoWeekYear' + }, + + camelFunctions = { + dayofyear : 'dayOfYear', + isoweekday : 'isoWeekday', + isoweek : 'isoWeek', + weekyear : 'weekYear', + isoweekyear : 'isoWeekYear' + }, + + // format function strings + formatFunctions = {}, + + // tokens to ordinalize and pad + ordinalizeTokens = 'DDD w W M D d'.split(' '), + paddedTokens = 'M D H h m s w W'.split(' '), + + formatTokenFunctions = { + M : function () { + return this.month() + 1; + }, + MMM : function (format) { + return this.lang().monthsShort(this, format); + }, + MMMM : function (format) { + return this.lang().months(this, format); + }, + D : function () { + return this.date(); + }, + DDD : function () { + return this.dayOfYear(); + }, + d : function () { + return this.day(); + }, + dd : function (format) { + return this.lang().weekdaysMin(this, format); + }, + ddd : function (format) { + return this.lang().weekdaysShort(this, format); + }, + dddd : function (format) { + return this.lang().weekdays(this, format); + }, + w : function () { + return this.week(); + }, + W : function () { + return this.isoWeek(); + }, + YY : function () { + return leftZeroFill(this.year() % 100, 2); + }, + YYYY : function () { + return leftZeroFill(this.year(), 4); + }, + YYYYY : function () { + return leftZeroFill(this.year(), 5); + }, + YYYYYY : function () { + var y = this.year(), sign = y >= 0 ? '+' : '-'; + return sign + leftZeroFill(Math.abs(y), 6); + }, + gg : function () { + return leftZeroFill(this.weekYear() % 100, 2); + }, + gggg : function () { + return leftZeroFill(this.weekYear(), 4); + }, + ggggg : function () { + return leftZeroFill(this.weekYear(), 5); + }, + GG : function () { + return leftZeroFill(this.isoWeekYear() % 100, 2); + }, + GGGG : function () { + return leftZeroFill(this.isoWeekYear(), 4); + }, + GGGGG : function () { + return leftZeroFill(this.isoWeekYear(), 5); + }, + e : function () { + return this.weekday(); + }, + E : function () { + return this.isoWeekday(); + }, + a : function () { + return this.lang().meridiem(this.hours(), this.minutes(), true); + }, + A : function () { + return this.lang().meridiem(this.hours(), this.minutes(), false); + }, + H : function () { + return this.hours(); + }, + h : function () { + return this.hours() % 12 || 12; + }, + m : function () { + return this.minutes(); + }, + s : function () { + return this.seconds(); + }, + S : function () { + return toInt(this.milliseconds() / 100); + }, + SS : function () { + return leftZeroFill(toInt(this.milliseconds() / 10), 2); + }, + SSS : function () { + return leftZeroFill(this.milliseconds(), 3); + }, + SSSS : function () { + return leftZeroFill(this.milliseconds(), 3); + }, + Z : function () { + var a = -this.zone(), + b = "+"; + if (a < 0) { + a = -a; + b = "-"; + } + return b + leftZeroFill(toInt(a / 60), 2) + ":" + leftZeroFill(toInt(a) % 60, 2); + }, + ZZ : function () { + var a = -this.zone(), + b = "+"; + if (a < 0) { + a = -a; + b = "-"; + } + return b + leftZeroFill(toInt(a / 60), 2) + leftZeroFill(toInt(a) % 60, 2); + }, + z : function () { + return this.zoneAbbr(); + }, + zz : function () { + return this.zoneName(); + }, + X : function () { + return this.unix(); + }, + Q : function () { + return this.quarter(); + } + }, + + lists = ['months', 'monthsShort', 'weekdays', 'weekdaysShort', 'weekdaysMin']; + + function defaultParsingFlags() { + // We need to deep clone this object, and es5 standard is not very + // helpful. + return { + empty : false, + unusedTokens : [], + unusedInput : [], + overflow : -2, + charsLeftOver : 0, + nullInput : false, + invalidMonth : null, + invalidFormat : false, + userInvalidated : false, + iso: false + }; + } + + function deprecate(msg, fn) { + var firstTime = true; + function printMsg() { + if (moment.suppressDeprecationWarnings === false && + typeof console !== 'undefined' && console.warn) { + console.warn("Deprecation warning: " + msg); + } + } + return extend(function () { + if (firstTime) { + printMsg(); + firstTime = false; + } + return fn.apply(this, arguments); + }, fn); + } + + function padToken(func, count) { + return function (a) { + return leftZeroFill(func.call(this, a), count); + }; + } + function ordinalizeToken(func, period) { + return function (a) { + return this.lang().ordinal(func.call(this, a), period); + }; + } + + while (ordinalizeTokens.length) { + i = ordinalizeTokens.pop(); + formatTokenFunctions[i + 'o'] = ordinalizeToken(formatTokenFunctions[i], i); + } + while (paddedTokens.length) { + i = paddedTokens.pop(); + formatTokenFunctions[i + i] = padToken(formatTokenFunctions[i], 2); + } + formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3); + + + /************************************ + Constructors + ************************************/ + + function Language() { + + } + + // Moment prototype object + function Moment(config) { + checkOverflow(config); + extend(this, config); + } + + // Duration Constructor + function Duration(duration) { + var normalizedInput = normalizeObjectUnits(duration), + years = normalizedInput.year || 0, + quarters = normalizedInput.quarter || 0, + months = normalizedInput.month || 0, + weeks = normalizedInput.week || 0, + days = normalizedInput.day || 0, + hours = normalizedInput.hour || 0, + minutes = normalizedInput.minute || 0, + seconds = normalizedInput.second || 0, + milliseconds = normalizedInput.millisecond || 0; + + // representation for dateAddRemove + this._milliseconds = +milliseconds + + seconds * 1e3 + // 1000 + minutes * 6e4 + // 1000 * 60 + hours * 36e5; // 1000 * 60 * 60 + // Because of dateAddRemove treats 24 hours as different from a + // day when working around DST, we need to store them separately + this._days = +days + + weeks * 7; + // It is impossible translate months into days without knowing + // which months you are are talking about, so we have to store + // it separately. + this._months = +months + + quarters * 3 + + years * 12; + + this._data = {}; + + this._bubble(); + } + + /************************************ + Helpers + ************************************/ + + + function extend(a, b) { + for (var i in b) { + if (b.hasOwnProperty(i)) { + a[i] = b[i]; + } + } + + if (b.hasOwnProperty("toString")) { + a.toString = b.toString; + } + + if (b.hasOwnProperty("valueOf")) { + a.valueOf = b.valueOf; + } + + return a; + } + + function cloneMoment(m) { + var result = {}, i; + for (i in m) { + if (m.hasOwnProperty(i) && momentProperties.hasOwnProperty(i)) { + result[i] = m[i]; + } + } + + return result; + } + + function absRound(number) { + if (number < 0) { + return Math.ceil(number); + } else { + return Math.floor(number); + } + } + + // left zero fill a number + // see http://jsperf.com/left-zero-filling for performance comparison + function leftZeroFill(number, targetLength, forceSign) { + var output = '' + Math.abs(number), + sign = number >= 0; + + while (output.length < targetLength) { + output = '0' + output; + } + return (sign ? (forceSign ? '+' : '') : '-') + output; + } + + // helper function for _.addTime and _.subtractTime + function addOrSubtractDurationFromMoment(mom, duration, isAdding, updateOffset) { + var milliseconds = duration._milliseconds, + days = duration._days, + months = duration._months; + updateOffset = updateOffset == null ? true : updateOffset; + + if (milliseconds) { + mom._d.setTime(+mom._d + milliseconds * isAdding); + } + if (days) { + rawSetter(mom, 'Date', rawGetter(mom, 'Date') + days * isAdding); + } + if (months) { + rawMonthSetter(mom, rawGetter(mom, 'Month') + months * isAdding); + } + if (updateOffset) { + moment.updateOffset(mom, days || months); + } + } + + // check if is an array + function isArray(input) { + return Object.prototype.toString.call(input) === '[object Array]'; + } + + function isDate(input) { + return Object.prototype.toString.call(input) === '[object Date]' || + input instanceof Date; + } + + // compare two arrays, return the number of differences + function compareArrays(array1, array2, dontConvert) { + var len = Math.min(array1.length, array2.length), + lengthDiff = Math.abs(array1.length - array2.length), + diffs = 0, + i; + for (i = 0; i < len; i++) { + if ((dontConvert && array1[i] !== array2[i]) || + (!dontConvert && toInt(array1[i]) !== toInt(array2[i]))) { + diffs++; + } + } + return diffs + lengthDiff; + } + + function normalizeUnits(units) { + if (units) { + var lowered = units.toLowerCase().replace(/(.)s$/, '$1'); + units = unitAliases[units] || camelFunctions[lowered] || lowered; + } + return units; + } + + function normalizeObjectUnits(inputObject) { + var normalizedInput = {}, + normalizedProp, + prop; + + for (prop in inputObject) { + if (inputObject.hasOwnProperty(prop)) { + normalizedProp = normalizeUnits(prop); + if (normalizedProp) { + normalizedInput[normalizedProp] = inputObject[prop]; + } + } + } + + return normalizedInput; + } + + function makeList(field) { + var count, setter; + + if (field.indexOf('week') === 0) { + count = 7; + setter = 'day'; + } + else if (field.indexOf('month') === 0) { + count = 12; + setter = 'month'; + } + else { + return; + } + + moment[field] = function (format, index) { + var i, getter, + method = moment.fn._lang[field], + results = []; + + if (typeof format === 'number') { + index = format; + format = undefined; + } + + getter = function (i) { + var m = moment().utc().set(setter, i); + return method.call(moment.fn._lang, m, format || ''); + }; + + if (index != null) { + return getter(index); + } + else { + for (i = 0; i < count; i++) { + results.push(getter(i)); + } + return results; + } + }; + } + + function toInt(argumentForCoercion) { + var coercedNumber = +argumentForCoercion, + value = 0; + + if (coercedNumber !== 0 && isFinite(coercedNumber)) { + if (coercedNumber >= 0) { + value = Math.floor(coercedNumber); + } else { + value = Math.ceil(coercedNumber); + } + } + + return value; + } + + function daysInMonth(year, month) { + return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); + } + + function weeksInYear(year, dow, doy) { + return weekOfYear(moment([year, 11, 31 + dow - doy]), dow, doy).week; + } + + function daysInYear(year) { + return isLeapYear(year) ? 366 : 365; + } + + function isLeapYear(year) { + return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; + } + + function checkOverflow(m) { + var overflow; + if (m._a && m._pf.overflow === -2) { + overflow = + m._a[MONTH] < 0 || m._a[MONTH] > 11 ? MONTH : + m._a[DATE] < 1 || m._a[DATE] > daysInMonth(m._a[YEAR], m._a[MONTH]) ? DATE : + m._a[HOUR] < 0 || m._a[HOUR] > 23 ? HOUR : + m._a[MINUTE] < 0 || m._a[MINUTE] > 59 ? MINUTE : + m._a[SECOND] < 0 || m._a[SECOND] > 59 ? SECOND : + m._a[MILLISECOND] < 0 || m._a[MILLISECOND] > 999 ? MILLISECOND : + -1; + + if (m._pf._overflowDayOfYear && (overflow < YEAR || overflow > DATE)) { + overflow = DATE; + } + + m._pf.overflow = overflow; + } + } + + function isValid(m) { + if (m._isValid == null) { + m._isValid = !isNaN(m._d.getTime()) && + m._pf.overflow < 0 && + !m._pf.empty && + !m._pf.invalidMonth && + !m._pf.nullInput && + !m._pf.invalidFormat && + !m._pf.userInvalidated; + + if (m._strict) { + m._isValid = m._isValid && + m._pf.charsLeftOver === 0 && + m._pf.unusedTokens.length === 0; + } + } + return m._isValid; + } + + function normalizeLanguage(key) { + return key ? key.toLowerCase().replace('_', '-') : key; + } + + // Return a moment from input, that is local/utc/zone equivalent to model. + function makeAs(input, model) { + return model._isUTC ? moment(input).zone(model._offset || 0) : + moment(input).local(); + } + + /************************************ + Languages + ************************************/ + + + extend(Language.prototype, { + + set : function (config) { + var prop, i; + for (i in config) { + prop = config[i]; + if (typeof prop === 'function') { + this[i] = prop; + } else { + this['_' + i] = prop; + } + } + }, + + _months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"), + months : function (m) { + return this._months[m.month()]; + }, + + _monthsShort : "Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"), + monthsShort : function (m) { + return this._monthsShort[m.month()]; + }, + + monthsParse : function (monthName) { + var i, mom, regex; + + if (!this._monthsParse) { + this._monthsParse = []; + } + + for (i = 0; i < 12; i++) { + // make the regex if we don't have it already + if (!this._monthsParse[i]) { + mom = moment.utc([2000, i]); + regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, ''); + this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i'); + } + // test the regex + if (this._monthsParse[i].test(monthName)) { + return i; + } + } + }, + + _weekdays : "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"), + weekdays : function (m) { + return this._weekdays[m.day()]; + }, + + _weekdaysShort : "Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"), + weekdaysShort : function (m) { + return this._weekdaysShort[m.day()]; + }, + + _weekdaysMin : "Su_Mo_Tu_We_Th_Fr_Sa".split("_"), + weekdaysMin : function (m) { + return this._weekdaysMin[m.day()]; + }, + + weekdaysParse : function (weekdayName) { + var i, mom, regex; + + if (!this._weekdaysParse) { + this._weekdaysParse = []; + } + + for (i = 0; i < 7; i++) { + // make the regex if we don't have it already + if (!this._weekdaysParse[i]) { + mom = moment([2000, 1]).day(i); + regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, ''); + this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i'); + } + // test the regex + if (this._weekdaysParse[i].test(weekdayName)) { + return i; + } + } + }, + + _longDateFormat : { + LT : "h:mm A", + L : "MM/DD/YYYY", + LL : "MMMM D YYYY", + LLL : "MMMM D YYYY LT", + LLLL : "dddd, MMMM D YYYY LT" + }, + longDateFormat : function (key) { + var output = this._longDateFormat[key]; + if (!output && this._longDateFormat[key.toUpperCase()]) { + output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) { + return val.slice(1); + }); + this._longDateFormat[key] = output; + } + return output; + }, + + isPM : function (input) { + // IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays + // Using charAt should be more compatible. + return ((input + '').toLowerCase().charAt(0) === 'p'); + }, + + _meridiemParse : /[ap]\.?m?\.?/i, + meridiem : function (hours, minutes, isLower) { + if (hours > 11) { + return isLower ? 'pm' : 'PM'; + } else { + return isLower ? 'am' : 'AM'; + } + }, + + _calendar : { + sameDay : '[Today at] LT', + nextDay : '[Tomorrow at] LT', + nextWeek : 'dddd [at] LT', + lastDay : '[Yesterday at] LT', + lastWeek : '[Last] dddd [at] LT', + sameElse : 'L' + }, + calendar : function (key, mom) { + var output = this._calendar[key]; + return typeof output === 'function' ? output.apply(mom) : output; + }, + + _relativeTime : { + future : "in %s", + past : "%s ago", + s : "a few seconds", + m : "a minute", + mm : "%d minutes", + h : "an hour", + hh : "%d hours", + d : "a day", + dd : "%d days", + M : "a month", + MM : "%d months", + y : "a year", + yy : "%d years" + }, + relativeTime : function (number, withoutSuffix, string, isFuture) { + var output = this._relativeTime[string]; + return (typeof output === 'function') ? + output(number, withoutSuffix, string, isFuture) : + output.replace(/%d/i, number); + }, + pastFuture : function (diff, output) { + var format = this._relativeTime[diff > 0 ? 'future' : 'past']; + return typeof format === 'function' ? format(output) : format.replace(/%s/i, output); + }, + + ordinal : function (number) { + return this._ordinal.replace("%d", number); + }, + _ordinal : "%d", + + preparse : function (string) { + return string; + }, + + postformat : function (string) { + return string; + }, + + week : function (mom) { + return weekOfYear(mom, this._week.dow, this._week.doy).week; + }, + + _week : { + dow : 0, // Sunday is the first day of the week. + doy : 6 // The week that contains Jan 1st is the first week of the year. + }, + + _invalidDate: 'Invalid date', + invalidDate: function () { + return this._invalidDate; + } + }); + + // Loads a language definition into the `languages` cache. The function + // takes a key and optionally values. If not in the browser and no values + // are provided, it will load the language file module. As a convenience, + // this function also returns the language values. + function loadLang(key, values) { + values.abbr = key; + if (!languages[key]) { + languages[key] = new Language(); + } + languages[key].set(values); + return languages[key]; + } + + // Remove a language from the `languages` cache. Mostly useful in tests. + function unloadLang(key) { + delete languages[key]; + } + + // Determines which language definition to use and returns it. + // + // With no parameters, it will return the global language. If you + // pass in a language key, such as 'en', it will return the + // definition for 'en', so long as 'en' has already been loaded using + // moment.lang. + function getLangDefinition(key) { + var i = 0, j, lang, next, split, + get = function (k) { + if (!languages[k] && hasModule) { + try { + require('./lang/' + k); + } catch (e) { } + } + return languages[k]; + }; + + if (!key) { + return moment.fn._lang; + } + + if (!isArray(key)) { + //short-circuit everything else + lang = get(key); + if (lang) { + return lang; + } + key = [key]; + } + + //pick the language from the array + //try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each + //substring from most specific to least, but move to the next array item if it's a more specific variant than the current root + while (i < key.length) { + split = normalizeLanguage(key[i]).split('-'); + j = split.length; + next = normalizeLanguage(key[i + 1]); + next = next ? next.split('-') : null; + while (j > 0) { + lang = get(split.slice(0, j).join('-')); + if (lang) { + return lang; + } + if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) { + //the next array item is better than a shallower substring of this one + break; + } + j--; + } + i++; + } + return moment.fn._lang; + } + + /************************************ + Formatting + ************************************/ + + + function removeFormattingTokens(input) { + if (input.match(/\[[\s\S]/)) { + return input.replace(/^\[|\]$/g, ""); + } + return input.replace(/\\/g, ""); + } + + function makeFormatFunction(format) { + var array = format.match(formattingTokens), i, length; + + for (i = 0, length = array.length; i < length; i++) { + if (formatTokenFunctions[array[i]]) { + array[i] = formatTokenFunctions[array[i]]; + } else { + array[i] = removeFormattingTokens(array[i]); + } + } + + return function (mom) { + var output = ""; + for (i = 0; i < length; i++) { + output += array[i] instanceof Function ? array[i].call(mom, format) : array[i]; + } + return output; + }; + } + + // format date using native date object + function formatMoment(m, format) { + + if (!m.isValid()) { + return m.lang().invalidDate(); + } + + format = expandFormat(format, m.lang()); + + if (!formatFunctions[format]) { + formatFunctions[format] = makeFormatFunction(format); + } + + return formatFunctions[format](m); + } + + function expandFormat(format, lang) { + var i = 5; + + function replaceLongDateFormatTokens(input) { + return lang.longDateFormat(input) || input; + } + + localFormattingTokens.lastIndex = 0; + while (i >= 0 && localFormattingTokens.test(format)) { + format = format.replace(localFormattingTokens, replaceLongDateFormatTokens); + localFormattingTokens.lastIndex = 0; + i -= 1; + } + + return format; + } + + + /************************************ + Parsing + ************************************/ + + + // get the regex to find the next token + function getParseRegexForToken(token, config) { + var a, strict = config._strict; + switch (token) { + case 'Q': + return parseTokenOneDigit; + case 'DDDD': + return parseTokenThreeDigits; + case 'YYYY': + case 'GGGG': + case 'gggg': + return strict ? parseTokenFourDigits : parseTokenOneToFourDigits; + case 'Y': + case 'G': + case 'g': + return parseTokenSignedNumber; + case 'YYYYYY': + case 'YYYYY': + case 'GGGGG': + case 'ggggg': + return strict ? parseTokenSixDigits : parseTokenOneToSixDigits; + case 'S': + if (strict) { return parseTokenOneDigit; } + /* falls through */ + case 'SS': + if (strict) { return parseTokenTwoDigits; } + /* falls through */ + case 'SSS': + if (strict) { return parseTokenThreeDigits; } + /* falls through */ + case 'DDD': + return parseTokenOneToThreeDigits; + case 'MMM': + case 'MMMM': + case 'dd': + case 'ddd': + case 'dddd': + return parseTokenWord; + case 'a': + case 'A': + return getLangDefinition(config._l)._meridiemParse; + case 'X': + return parseTokenTimestampMs; + case 'Z': + case 'ZZ': + return parseTokenTimezone; + case 'T': + return parseTokenT; + case 'SSSS': + return parseTokenDigits; + case 'MM': + case 'DD': + case 'YY': + case 'GG': + case 'gg': + case 'HH': + case 'hh': + case 'mm': + case 'ss': + case 'ww': + case 'WW': + return strict ? parseTokenTwoDigits : parseTokenOneOrTwoDigits; + case 'M': + case 'D': + case 'd': + case 'H': + case 'h': + case 'm': + case 's': + case 'w': + case 'W': + case 'e': + case 'E': + return parseTokenOneOrTwoDigits; + case 'Do': + return parseTokenOrdinal; + default : + a = new RegExp(regexpEscape(unescapeFormat(token.replace('\\', '')), "i")); + return a; + } + } + + function timezoneMinutesFromString(string) { + string = string || ""; + var possibleTzMatches = (string.match(parseTokenTimezone) || []), + tzChunk = possibleTzMatches[possibleTzMatches.length - 1] || [], + parts = (tzChunk + '').match(parseTimezoneChunker) || ['-', 0, 0], + minutes = +(parts[1] * 60) + toInt(parts[2]); + + return parts[0] === '+' ? -minutes : minutes; + } + + // function to convert string input to date + function addTimeToArrayFromToken(token, input, config) { + var a, datePartArray = config._a; + + switch (token) { + // QUARTER + case 'Q': + if (input != null) { + datePartArray[MONTH] = (toInt(input) - 1) * 3; + } + break; + // MONTH + case 'M' : // fall through to MM + case 'MM' : + if (input != null) { + datePartArray[MONTH] = toInt(input) - 1; + } + break; + case 'MMM' : // fall through to MMMM + case 'MMMM' : + a = getLangDefinition(config._l).monthsParse(input); + // if we didn't find a month name, mark the date as invalid. + if (a != null) { + datePartArray[MONTH] = a; + } else { + config._pf.invalidMonth = input; + } + break; + // DAY OF MONTH + case 'D' : // fall through to DD + case 'DD' : + if (input != null) { + datePartArray[DATE] = toInt(input); + } + break; + case 'Do' : + if (input != null) { + datePartArray[DATE] = toInt(parseInt(input, 10)); + } + break; + // DAY OF YEAR + case 'DDD' : // fall through to DDDD + case 'DDDD' : + if (input != null) { + config._dayOfYear = toInt(input); + } + + break; + // YEAR + case 'YY' : + datePartArray[YEAR] = moment.parseTwoDigitYear(input); + break; + case 'YYYY' : + case 'YYYYY' : + case 'YYYYYY' : + datePartArray[YEAR] = toInt(input); + break; + // AM / PM + case 'a' : // fall through to A + case 'A' : + config._isPm = getLangDefinition(config._l).isPM(input); + break; + // 24 HOUR + case 'H' : // fall through to hh + case 'HH' : // fall through to hh + case 'h' : // fall through to hh + case 'hh' : + datePartArray[HOUR] = toInt(input); + break; + // MINUTE + case 'm' : // fall through to mm + case 'mm' : + datePartArray[MINUTE] = toInt(input); + break; + // SECOND + case 's' : // fall through to ss + case 'ss' : + datePartArray[SECOND] = toInt(input); + break; + // MILLISECOND + case 'S' : + case 'SS' : + case 'SSS' : + case 'SSSS' : + datePartArray[MILLISECOND] = toInt(('0.' + input) * 1000); + break; + // UNIX TIMESTAMP WITH MS + case 'X': + config._d = new Date(parseFloat(input) * 1000); + break; + // TIMEZONE + case 'Z' : // fall through to ZZ + case 'ZZ' : + config._useUTC = true; + config._tzm = timezoneMinutesFromString(input); + break; + case 'w': + case 'ww': + case 'W': + case 'WW': + case 'd': + case 'dd': + case 'ddd': + case 'dddd': + case 'e': + case 'E': + token = token.substr(0, 1); + /* falls through */ + case 'gg': + case 'gggg': + case 'GG': + case 'GGGG': + case 'GGGGG': + token = token.substr(0, 2); + if (input) { + config._w = config._w || {}; + config._w[token] = input; + } + break; + } + } + + // convert an array to a date. + // the array should mirror the parameters below + // note: all values past the year are optional and will default to the lowest possible value. + // [year, month, day , hour, minute, second, millisecond] + function dateFromConfig(config) { + var i, date, input = [], currentDate, + yearToUse, fixYear, w, temp, lang, weekday, week; + + if (config._d) { + return; + } + + currentDate = currentDateArray(config); + + //compute day of the year from weeks and weekdays + if (config._w && config._a[DATE] == null && config._a[MONTH] == null) { + fixYear = function (val) { + var intVal = parseInt(val, 10); + return val ? + (val.length < 3 ? (intVal > 68 ? 1900 + intVal : 2000 + intVal) : intVal) : + (config._a[YEAR] == null ? moment().weekYear() : config._a[YEAR]); + }; + + w = config._w; + if (w.GG != null || w.W != null || w.E != null) { + temp = dayOfYearFromWeeks(fixYear(w.GG), w.W || 1, w.E, 4, 1); + } + else { + lang = getLangDefinition(config._l); + weekday = w.d != null ? parseWeekday(w.d, lang) : + (w.e != null ? parseInt(w.e, 10) + lang._week.dow : 0); + + week = parseInt(w.w, 10) || 1; + + //if we're parsing 'd', then the low day numbers may be next week + if (w.d != null && weekday < lang._week.dow) { + week++; + } + + temp = dayOfYearFromWeeks(fixYear(w.gg), week, weekday, lang._week.doy, lang._week.dow); + } + + config._a[YEAR] = temp.year; + config._dayOfYear = temp.dayOfYear; + } + + //if the day of the year is set, figure out what it is + if (config._dayOfYear) { + yearToUse = config._a[YEAR] == null ? currentDate[YEAR] : config._a[YEAR]; + + if (config._dayOfYear > daysInYear(yearToUse)) { + config._pf._overflowDayOfYear = true; + } + + date = makeUTCDate(yearToUse, 0, config._dayOfYear); + config._a[MONTH] = date.getUTCMonth(); + config._a[DATE] = date.getUTCDate(); + } + + // Default to current date. + // * if no year, month, day of month are given, default to today + // * if day of month is given, default month and year + // * if month is given, default only year + // * if year is given, don't default anything + for (i = 0; i < 3 && config._a[i] == null; ++i) { + config._a[i] = input[i] = currentDate[i]; + } + + // Zero out whatever was not defaulted, including time + for (; i < 7; i++) { + config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i]; + } + + // add the offsets to the time to be parsed so that we can have a clean array for checking isValid + input[HOUR] += toInt((config._tzm || 0) / 60); + input[MINUTE] += toInt((config._tzm || 0) % 60); + + config._d = (config._useUTC ? makeUTCDate : makeDate).apply(null, input); + } + + function dateFromObject(config) { + var normalizedInput; + + if (config._d) { + return; + } + + normalizedInput = normalizeObjectUnits(config._i); + config._a = [ + normalizedInput.year, + normalizedInput.month, + normalizedInput.day, + normalizedInput.hour, + normalizedInput.minute, + normalizedInput.second, + normalizedInput.millisecond + ]; + + dateFromConfig(config); + } + + function currentDateArray(config) { + var now = new Date(); + if (config._useUTC) { + return [ + now.getUTCFullYear(), + now.getUTCMonth(), + now.getUTCDate() + ]; + } else { + return [now.getFullYear(), now.getMonth(), now.getDate()]; + } + } + + // date from string and format string + function makeDateFromStringAndFormat(config) { + + config._a = []; + config._pf.empty = true; + + // This array is used to make a Date, either with `new Date` or `Date.UTC` + var lang = getLangDefinition(config._l), + string = '' + config._i, + i, parsedInput, tokens, token, skipped, + stringLength = string.length, + totalParsedInputLength = 0; + + tokens = expandFormat(config._f, lang).match(formattingTokens) || []; + + for (i = 0; i < tokens.length; i++) { + token = tokens[i]; + parsedInput = (string.match(getParseRegexForToken(token, config)) || [])[0]; + if (parsedInput) { + skipped = string.substr(0, string.indexOf(parsedInput)); + if (skipped.length > 0) { + config._pf.unusedInput.push(skipped); + } + string = string.slice(string.indexOf(parsedInput) + parsedInput.length); + totalParsedInputLength += parsedInput.length; + } + // don't parse if it's not a known token + if (formatTokenFunctions[token]) { + if (parsedInput) { + config._pf.empty = false; + } + else { + config._pf.unusedTokens.push(token); + } + addTimeToArrayFromToken(token, parsedInput, config); + } + else if (config._strict && !parsedInput) { + config._pf.unusedTokens.push(token); + } + } + + // add remaining unparsed input length to the string + config._pf.charsLeftOver = stringLength - totalParsedInputLength; + if (string.length > 0) { + config._pf.unusedInput.push(string); + } + + // handle am pm + if (config._isPm && config._a[HOUR] < 12) { + config._a[HOUR] += 12; + } + // if is 12 am, change hours to 0 + if (config._isPm === false && config._a[HOUR] === 12) { + config._a[HOUR] = 0; + } + + dateFromConfig(config); + checkOverflow(config); + } + + function unescapeFormat(s) { + return s.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function (matched, p1, p2, p3, p4) { + return p1 || p2 || p3 || p4; + }); + } + + // Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript + function regexpEscape(s) { + return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); + } + + // date from string and array of format strings + function makeDateFromStringAndArray(config) { + var tempConfig, + bestMoment, + + scoreToBeat, + i, + currentScore; + + if (config._f.length === 0) { + config._pf.invalidFormat = true; + config._d = new Date(NaN); + return; + } + + for (i = 0; i < config._f.length; i++) { + currentScore = 0; + tempConfig = extend({}, config); + tempConfig._pf = defaultParsingFlags(); + tempConfig._f = config._f[i]; + makeDateFromStringAndFormat(tempConfig); + + if (!isValid(tempConfig)) { + continue; + } + + // if there is any input that was not parsed add a penalty for that format + currentScore += tempConfig._pf.charsLeftOver; + + //or tokens + currentScore += tempConfig._pf.unusedTokens.length * 10; + + tempConfig._pf.score = currentScore; + + if (scoreToBeat == null || currentScore < scoreToBeat) { + scoreToBeat = currentScore; + bestMoment = tempConfig; + } + } + + extend(config, bestMoment || tempConfig); + } + + // date from iso format + function makeDateFromString(config) { + var i, l, + string = config._i, + match = isoRegex.exec(string); + + if (match) { + config._pf.iso = true; + for (i = 0, l = isoDates.length; i < l; i++) { + if (isoDates[i][1].exec(string)) { + // match[5] should be "T" or undefined + config._f = isoDates[i][0] + (match[6] || " "); + break; + } + } + for (i = 0, l = isoTimes.length; i < l; i++) { + if (isoTimes[i][1].exec(string)) { + config._f += isoTimes[i][0]; + break; + } + } + if (string.match(parseTokenTimezone)) { + config._f += "Z"; + } + makeDateFromStringAndFormat(config); + } + else { + moment.createFromInputFallback(config); + } + } + + function makeDateFromInput(config) { + var input = config._i, + matched = aspNetJsonRegex.exec(input); + + if (input === undefined) { + config._d = new Date(); + } else if (matched) { + config._d = new Date(+matched[1]); + } else if (typeof input === 'string') { + makeDateFromString(config); + } else if (isArray(input)) { + config._a = input.slice(0); + dateFromConfig(config); + } else if (isDate(input)) { + config._d = new Date(+input); + } else if (typeof(input) === 'object') { + dateFromObject(config); + } else if (typeof(input) === 'number') { + // from milliseconds + config._d = new Date(input); + } else { + moment.createFromInputFallback(config); + } + } + + function makeDate(y, m, d, h, M, s, ms) { + //can't just apply() to create a date: + //http://stackoverflow.com/questions/181348/instantiating-a-javascript-object-by-calling-prototype-constructor-apply + var date = new Date(y, m, d, h, M, s, ms); + + //the date constructor doesn't accept years < 1970 + if (y < 1970) { + date.setFullYear(y); + } + return date; + } + + function makeUTCDate(y) { + var date = new Date(Date.UTC.apply(null, arguments)); + if (y < 1970) { + date.setUTCFullYear(y); + } + return date; + } + + function parseWeekday(input, language) { + if (typeof input === 'string') { + if (!isNaN(input)) { + input = parseInt(input, 10); + } + else { + input = language.weekdaysParse(input); + if (typeof input !== 'number') { + return null; + } + } + } + return input; + } + + /************************************ + Relative Time + ************************************/ + + + // helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize + function substituteTimeAgo(string, number, withoutSuffix, isFuture, lang) { + return lang.relativeTime(number || 1, !!withoutSuffix, string, isFuture); + } + + function relativeTime(milliseconds, withoutSuffix, lang) { + var seconds = round(Math.abs(milliseconds) / 1000), + minutes = round(seconds / 60), + hours = round(minutes / 60), + days = round(hours / 24), + years = round(days / 365), + args = seconds < 45 && ['s', seconds] || + minutes === 1 && ['m'] || + minutes < 45 && ['mm', minutes] || + hours === 1 && ['h'] || + hours < 22 && ['hh', hours] || + days === 1 && ['d'] || + days <= 25 && ['dd', days] || + days <= 45 && ['M'] || + days < 345 && ['MM', round(days / 30)] || + years === 1 && ['y'] || ['yy', years]; + args[2] = withoutSuffix; + args[3] = milliseconds > 0; + args[4] = lang; + return substituteTimeAgo.apply({}, args); + } + + + /************************************ + Week of Year + ************************************/ + + + // firstDayOfWeek 0 = sun, 6 = sat + // the day of the week that starts the week + // (usually sunday or monday) + // firstDayOfWeekOfYear 0 = sun, 6 = sat + // the first week is the week that contains the first + // of this day of the week + // (eg. ISO weeks use thursday (4)) + function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) { + var end = firstDayOfWeekOfYear - firstDayOfWeek, + daysToDayOfWeek = firstDayOfWeekOfYear - mom.day(), + adjustedMoment; + + + if (daysToDayOfWeek > end) { + daysToDayOfWeek -= 7; + } + + if (daysToDayOfWeek < end - 7) { + daysToDayOfWeek += 7; + } + + adjustedMoment = moment(mom).add('d', daysToDayOfWeek); + return { + week: Math.ceil(adjustedMoment.dayOfYear() / 7), + year: adjustedMoment.year() + }; + } + + //http://en.wikipedia.org/wiki/ISO_week_date#Calculating_a_date_given_the_year.2C_week_number_and_weekday + function dayOfYearFromWeeks(year, week, weekday, firstDayOfWeekOfYear, firstDayOfWeek) { + var d = makeUTCDate(year, 0, 1).getUTCDay(), daysToAdd, dayOfYear; + + weekday = weekday != null ? weekday : firstDayOfWeek; + daysToAdd = firstDayOfWeek - d + (d > firstDayOfWeekOfYear ? 7 : 0) - (d < firstDayOfWeek ? 7 : 0); + dayOfYear = 7 * (week - 1) + (weekday - firstDayOfWeek) + daysToAdd + 1; + + return { + year: dayOfYear > 0 ? year : year - 1, + dayOfYear: dayOfYear > 0 ? dayOfYear : daysInYear(year - 1) + dayOfYear + }; + } + + /************************************ + Top Level Functions + ************************************/ + + function makeMoment(config) { + var input = config._i, + format = config._f; + + if (input === null || (format === undefined && input === '')) { + return moment.invalid({nullInput: true}); + } + + if (typeof input === 'string') { + config._i = input = getLangDefinition().preparse(input); + } + + if (moment.isMoment(input)) { + config = cloneMoment(input); + + config._d = new Date(+input._d); + } else if (format) { + if (isArray(format)) { + makeDateFromStringAndArray(config); + } else { + makeDateFromStringAndFormat(config); + } + } else { + makeDateFromInput(config); + } + + return new Moment(config); + } + + moment = function (input, format, lang, strict) { + var c; + + if (typeof(lang) === "boolean") { + strict = lang; + lang = undefined; + } + // object construction must be done this way. + // https://github.com/moment/moment/issues/1423 + c = {}; + c._isAMomentObject = true; + c._i = input; + c._f = format; + c._l = lang; + c._strict = strict; + c._isUTC = false; + c._pf = defaultParsingFlags(); + + return makeMoment(c); + }; + + moment.suppressDeprecationWarnings = false; + + moment.createFromInputFallback = deprecate( + "moment construction falls back to js Date. This is " + + "discouraged and will be removed in upcoming major " + + "release. Please refer to " + + "https://github.com/moment/moment/issues/1407 for more info.", + function (config) { + config._d = new Date(config._i); + }); + + // creating with utc + moment.utc = function (input, format, lang, strict) { + var c; + + if (typeof(lang) === "boolean") { + strict = lang; + lang = undefined; + } + // object construction must be done this way. + // https://github.com/moment/moment/issues/1423 + c = {}; + c._isAMomentObject = true; + c._useUTC = true; + c._isUTC = true; + c._l = lang; + c._i = input; + c._f = format; + c._strict = strict; + c._pf = defaultParsingFlags(); + + return makeMoment(c).utc(); + }; + + // creating with unix timestamp (in seconds) + moment.unix = function (input) { + return moment(input * 1000); + }; + + // duration + moment.duration = function (input, key) { + var duration = input, + // matching against regexp is expensive, do it on demand + match = null, + sign, + ret, + parseIso; + + if (moment.isDuration(input)) { + duration = { + ms: input._milliseconds, + d: input._days, + M: input._months + }; + } else if (typeof input === 'number') { + duration = {}; + if (key) { + duration[key] = input; + } else { + duration.milliseconds = input; + } + } else if (!!(match = aspNetTimeSpanJsonRegex.exec(input))) { + sign = (match[1] === "-") ? -1 : 1; + duration = { + y: 0, + d: toInt(match[DATE]) * sign, + h: toInt(match[HOUR]) * sign, + m: toInt(match[MINUTE]) * sign, + s: toInt(match[SECOND]) * sign, + ms: toInt(match[MILLISECOND]) * sign + }; + } else if (!!(match = isoDurationRegex.exec(input))) { + sign = (match[1] === "-") ? -1 : 1; + parseIso = function (inp) { + // We'd normally use ~~inp for this, but unfortunately it also + // converts floats to ints. + // inp may be undefined, so careful calling replace on it. + var res = inp && parseFloat(inp.replace(',', '.')); + // apply sign while we're at it + return (isNaN(res) ? 0 : res) * sign; + }; + duration = { + y: parseIso(match[2]), + M: parseIso(match[3]), + d: parseIso(match[4]), + h: parseIso(match[5]), + m: parseIso(match[6]), + s: parseIso(match[7]), + w: parseIso(match[8]) + }; + } + + ret = new Duration(duration); + + if (moment.isDuration(input) && input.hasOwnProperty('_lang')) { + ret._lang = input._lang; + } + + return ret; + }; + + // version number + moment.version = VERSION; + + // default format + moment.defaultFormat = isoFormat; + + // Plugins that add properties should also add the key here (null value), + // so we can properly clone ourselves. + moment.momentProperties = momentProperties; + + // This function will be called whenever a moment is mutated. + // It is intended to keep the offset in sync with the timezone. + moment.updateOffset = function () {}; + + // This function will load languages and then set the global language. If + // no arguments are passed in, it will simply return the current global + // language key. + moment.lang = function (key, values) { + var r; + if (!key) { + return moment.fn._lang._abbr; + } + if (values) { + loadLang(normalizeLanguage(key), values); + } else if (values === null) { + unloadLang(key); + key = 'en'; + } else if (!languages[key]) { + getLangDefinition(key); + } + r = moment.duration.fn._lang = moment.fn._lang = getLangDefinition(key); + return r._abbr; + }; + + // returns language data + moment.langData = function (key) { + if (key && key._lang && key._lang._abbr) { + key = key._lang._abbr; + } + return getLangDefinition(key); + }; + + // compare moment object + moment.isMoment = function (obj) { + return obj instanceof Moment || + (obj != null && obj.hasOwnProperty('_isAMomentObject')); + }; + + // for typechecking Duration objects + moment.isDuration = function (obj) { + return obj instanceof Duration; + }; + + for (i = lists.length - 1; i >= 0; --i) { + makeList(lists[i]); + } + + moment.normalizeUnits = function (units) { + return normalizeUnits(units); + }; + + moment.invalid = function (flags) { + var m = moment.utc(NaN); + if (flags != null) { + extend(m._pf, flags); + } + else { + m._pf.userInvalidated = true; + } + + return m; + }; + + moment.parseZone = function () { + return moment.apply(null, arguments).parseZone(); + }; + + moment.parseTwoDigitYear = function (input) { + return toInt(input) + (toInt(input) > 68 ? 1900 : 2000); + }; + + /************************************ + Moment Prototype + ************************************/ + + + extend(moment.fn = Moment.prototype, { + + clone : function () { + return moment(this); + }, + + valueOf : function () { + return +this._d + ((this._offset || 0) * 60000); + }, + + unix : function () { + return Math.floor(+this / 1000); + }, + + toString : function () { + return this.clone().lang('en').format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ"); + }, + + toDate : function () { + return this._offset ? new Date(+this) : this._d; + }, + + toISOString : function () { + var m = moment(this).utc(); + if (0 < m.year() && m.year() <= 9999) { + return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'); + } else { + return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]'); + } + }, + + toArray : function () { + var m = this; + return [ + m.year(), + m.month(), + m.date(), + m.hours(), + m.minutes(), + m.seconds(), + m.milliseconds() + ]; + }, + + isValid : function () { + return isValid(this); + }, + + isDSTShifted : function () { + + if (this._a) { + return this.isValid() && compareArrays(this._a, (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray()) > 0; + } + + return false; + }, + + parsingFlags : function () { + return extend({}, this._pf); + }, + + invalidAt: function () { + return this._pf.overflow; + }, + + utc : function () { + return this.zone(0); + }, + + local : function () { + this.zone(0); + this._isUTC = false; + return this; + }, + + format : function (inputString) { + var output = formatMoment(this, inputString || moment.defaultFormat); + return this.lang().postformat(output); + }, + + add : function (input, val) { + var dur; + // switch args to support add('s', 1) and add(1, 's') + if (typeof input === 'string') { + dur = moment.duration(+val, input); + } else { + dur = moment.duration(input, val); + } + addOrSubtractDurationFromMoment(this, dur, 1); + return this; + }, + + subtract : function (input, val) { + var dur; + // switch args to support subtract('s', 1) and subtract(1, 's') + if (typeof input === 'string') { + dur = moment.duration(+val, input); + } else { + dur = moment.duration(input, val); + } + addOrSubtractDurationFromMoment(this, dur, -1); + return this; + }, + + diff : function (input, units, asFloat) { + var that = makeAs(input, this), + zoneDiff = (this.zone() - that.zone()) * 6e4, + diff, output; + + units = normalizeUnits(units); + + if (units === 'year' || units === 'month') { + // average number of days in the months in the given dates + diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2 + // difference in months + output = ((this.year() - that.year()) * 12) + (this.month() - that.month()); + // adjust by taking difference in days, average number of days + // and dst in the given months. + output += ((this - moment(this).startOf('month')) - + (that - moment(that).startOf('month'))) / diff; + // same as above but with zones, to negate all dst + output -= ((this.zone() - moment(this).startOf('month').zone()) - + (that.zone() - moment(that).startOf('month').zone())) * 6e4 / diff; + if (units === 'year') { + output = output / 12; + } + } else { + diff = (this - that); + output = units === 'second' ? diff / 1e3 : // 1000 + units === 'minute' ? diff / 6e4 : // 1000 * 60 + units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60 + units === 'day' ? (diff - zoneDiff) / 864e5 : // 1000 * 60 * 60 * 24, negate dst + units === 'week' ? (diff - zoneDiff) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst + diff; + } + return asFloat ? output : absRound(output); + }, + + from : function (time, withoutSuffix) { + return moment.duration(this.diff(time)).lang(this.lang()._abbr).humanize(!withoutSuffix); + }, + + fromNow : function (withoutSuffix) { + return this.from(moment(), withoutSuffix); + }, + + calendar : function () { + // We want to compare the start of today, vs this. + // Getting start-of-today depends on whether we're zone'd or not. + var sod = makeAs(moment(), this).startOf('day'), + diff = this.diff(sod, 'days', true), + format = diff < -6 ? 'sameElse' : + diff < -1 ? 'lastWeek' : + diff < 0 ? 'lastDay' : + diff < 1 ? 'sameDay' : + diff < 2 ? 'nextDay' : + diff < 7 ? 'nextWeek' : 'sameElse'; + return this.format(this.lang().calendar(format, this)); + }, + + isLeapYear : function () { + return isLeapYear(this.year()); + }, + + isDST : function () { + return (this.zone() < this.clone().month(0).zone() || + this.zone() < this.clone().month(5).zone()); + }, + + day : function (input) { + var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay(); + if (input != null) { + input = parseWeekday(input, this.lang()); + return this.add({ d : input - day }); + } else { + return day; + } + }, + + month : makeAccessor('Month', true), + + startOf: function (units) { + units = normalizeUnits(units); + // the following switch intentionally omits break keywords + // to utilize falling through the cases. + switch (units) { + case 'year': + this.month(0); + /* falls through */ + case 'quarter': + case 'month': + this.date(1); + /* falls through */ + case 'week': + case 'isoWeek': + case 'day': + this.hours(0); + /* falls through */ + case 'hour': + this.minutes(0); + /* falls through */ + case 'minute': + this.seconds(0); + /* falls through */ + case 'second': + this.milliseconds(0); + /* falls through */ + } + + // weeks are a special case + if (units === 'week') { + this.weekday(0); + } else if (units === 'isoWeek') { + this.isoWeekday(1); + } + + // quarters are also special + if (units === 'quarter') { + this.month(Math.floor(this.month() / 3) * 3); + } + + return this; + }, + + endOf: function (units) { + units = normalizeUnits(units); + return this.startOf(units).add((units === 'isoWeek' ? 'week' : units), 1).subtract('ms', 1); + }, + + isAfter: function (input, units) { + units = typeof units !== 'undefined' ? units : 'millisecond'; + return +this.clone().startOf(units) > +moment(input).startOf(units); + }, + + isBefore: function (input, units) { + units = typeof units !== 'undefined' ? units : 'millisecond'; + return +this.clone().startOf(units) < +moment(input).startOf(units); + }, + + isSame: function (input, units) { + units = units || 'ms'; + return +this.clone().startOf(units) === +makeAs(input, this).startOf(units); + }, + + min: function (other) { + other = moment.apply(null, arguments); + return other < this ? this : other; + }, + + max: function (other) { + other = moment.apply(null, arguments); + return other > this ? this : other; + }, + + // keepTime = true means only change the timezone, without affecting + // the local hour. So 5:31:26 +0300 --[zone(2, true)]--> 5:31:26 +0200 + // It is possible that 5:31:26 doesn't exist int zone +0200, so we + // adjust the time as needed, to be valid. + // + // Keeping the time actually adds/subtracts (one hour) + // from the actual represented time. That is why we call updateOffset + // a second time. In case it wants us to change the offset again + // _changeInProgress == true case, then we have to adjust, because + // there is no such time in the given timezone. + zone : function (input, keepTime) { + var offset = this._offset || 0; + if (input != null) { + if (typeof input === "string") { + input = timezoneMinutesFromString(input); + } + if (Math.abs(input) < 16) { + input = input * 60; + } + this._offset = input; + this._isUTC = true; + if (offset !== input) { + if (!keepTime || this._changeInProgress) { + addOrSubtractDurationFromMoment(this, + moment.duration(offset - input, 'm'), 1, false); + } else if (!this._changeInProgress) { + this._changeInProgress = true; + moment.updateOffset(this, true); + this._changeInProgress = null; + } + } + } else { + return this._isUTC ? offset : this._d.getTimezoneOffset(); + } + return this; + }, + + zoneAbbr : function () { + return this._isUTC ? "UTC" : ""; + }, + + zoneName : function () { + return this._isUTC ? "Coordinated Universal Time" : ""; + }, + + parseZone : function () { + if (this._tzm) { + this.zone(this._tzm); + } else if (typeof this._i === 'string') { + this.zone(this._i); + } + return this; + }, + + hasAlignedHourOffset : function (input) { + if (!input) { + input = 0; + } + else { + input = moment(input).zone(); + } + + return (this.zone() - input) % 60 === 0; + }, + + daysInMonth : function () { + return daysInMonth(this.year(), this.month()); + }, + + dayOfYear : function (input) { + var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1; + return input == null ? dayOfYear : this.add("d", (input - dayOfYear)); + }, + + quarter : function (input) { + return input == null ? Math.ceil((this.month() + 1) / 3) : this.month((input - 1) * 3 + this.month() % 3); + }, + + weekYear : function (input) { + var year = weekOfYear(this, this.lang()._week.dow, this.lang()._week.doy).year; + return input == null ? year : this.add("y", (input - year)); + }, + + isoWeekYear : function (input) { + var year = weekOfYear(this, 1, 4).year; + return input == null ? year : this.add("y", (input - year)); + }, + + week : function (input) { + var week = this.lang().week(this); + return input == null ? week : this.add("d", (input - week) * 7); + }, + + isoWeek : function (input) { + var week = weekOfYear(this, 1, 4).week; + return input == null ? week : this.add("d", (input - week) * 7); + }, + + weekday : function (input) { + var weekday = (this.day() + 7 - this.lang()._week.dow) % 7; + return input == null ? weekday : this.add("d", input - weekday); + }, + + isoWeekday : function (input) { + // behaves the same as moment#day except + // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6) + // as a setter, sunday should belong to the previous week. + return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7); + }, + + isoWeeksInYear : function () { + return weeksInYear(this.year(), 1, 4); + }, + + weeksInYear : function () { + var weekInfo = this._lang._week; + return weeksInYear(this.year(), weekInfo.dow, weekInfo.doy); + }, + + get : function (units) { + units = normalizeUnits(units); + return this[units](); + }, + + set : function (units, value) { + units = normalizeUnits(units); + if (typeof this[units] === 'function') { + this[units](value); + } + return this; + }, + + // If passed a language key, it will set the language for this + // instance. Otherwise, it will return the language configuration + // variables for this instance. + lang : function (key) { + if (key === undefined) { + return this._lang; + } else { + this._lang = getLangDefinition(key); + return this; + } + } + }); + + function rawMonthSetter(mom, value) { + var dayOfMonth; + + // TODO: Move this out of here! + if (typeof value === 'string') { + value = mom.lang().monthsParse(value); + // TODO: Another silent failure? + if (typeof value !== 'number') { + return mom; + } + } + + dayOfMonth = Math.min(mom.date(), + daysInMonth(mom.year(), value)); + mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Month'](value, dayOfMonth); + return mom; + } + + function rawGetter(mom, unit) { + return mom._d['get' + (mom._isUTC ? 'UTC' : '') + unit](); + } + + function rawSetter(mom, unit, value) { + if (unit === 'Month') { + return rawMonthSetter(mom, value); + } else { + return mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value); + } + } + + function makeAccessor(unit, keepTime) { + return function (value) { + if (value != null) { + rawSetter(this, unit, value); + moment.updateOffset(this, keepTime); + return this; + } else { + return rawGetter(this, unit); + } + }; + } + + moment.fn.millisecond = moment.fn.milliseconds = makeAccessor('Milliseconds', false); + moment.fn.second = moment.fn.seconds = makeAccessor('Seconds', false); + moment.fn.minute = moment.fn.minutes = makeAccessor('Minutes', false); + // Setting the hour should keep the time, because the user explicitly + // specified which hour he wants. So trying to maintain the same hour (in + // a new timezone) makes sense. Adding/subtracting hours does not follow + // this rule. + moment.fn.hour = moment.fn.hours = makeAccessor('Hours', true); + // moment.fn.month is defined separately + moment.fn.date = makeAccessor('Date', true); + moment.fn.dates = deprecate("dates accessor is deprecated. Use date instead.", makeAccessor('Date', true)); + moment.fn.year = makeAccessor('FullYear', true); + moment.fn.years = deprecate("years accessor is deprecated. Use year instead.", makeAccessor('FullYear', true)); + + // add plural methods + moment.fn.days = moment.fn.day; + moment.fn.months = moment.fn.month; + moment.fn.weeks = moment.fn.week; + moment.fn.isoWeeks = moment.fn.isoWeek; + moment.fn.quarters = moment.fn.quarter; + + // add aliased format methods + moment.fn.toJSON = moment.fn.toISOString; + + /************************************ + Duration Prototype + ************************************/ + + + extend(moment.duration.fn = Duration.prototype, { + + _bubble : function () { + var milliseconds = this._milliseconds, + days = this._days, + months = this._months, + data = this._data, + seconds, minutes, hours, years; + + // The following code bubbles up values, see the tests for + // examples of what that means. + data.milliseconds = milliseconds % 1000; + + seconds = absRound(milliseconds / 1000); + data.seconds = seconds % 60; + + minutes = absRound(seconds / 60); + data.minutes = minutes % 60; + + hours = absRound(minutes / 60); + data.hours = hours % 24; + + days += absRound(hours / 24); + data.days = days % 30; + + months += absRound(days / 30); + data.months = months % 12; + + years = absRound(months / 12); + data.years = years; + }, + + weeks : function () { + return absRound(this.days() / 7); + }, + + valueOf : function () { + return this._milliseconds + + this._days * 864e5 + + (this._months % 12) * 2592e6 + + toInt(this._months / 12) * 31536e6; + }, + + humanize : function (withSuffix) { + var difference = +this, + output = relativeTime(difference, !withSuffix, this.lang()); + + if (withSuffix) { + output = this.lang().pastFuture(difference, output); + } + + return this.lang().postformat(output); + }, + + add : function (input, val) { + // supports only 2.0-style add(1, 's') or add(moment) + var dur = moment.duration(input, val); + + this._milliseconds += dur._milliseconds; + this._days += dur._days; + this._months += dur._months; + + this._bubble(); + + return this; + }, + + subtract : function (input, val) { + var dur = moment.duration(input, val); + + this._milliseconds -= dur._milliseconds; + this._days -= dur._days; + this._months -= dur._months; + + this._bubble(); + + return this; + }, + + get : function (units) { + units = normalizeUnits(units); + return this[units.toLowerCase() + 's'](); + }, + + as : function (units) { + units = normalizeUnits(units); + return this['as' + units.charAt(0).toUpperCase() + units.slice(1) + 's'](); + }, + + lang : moment.fn.lang, + + toIsoString : function () { + // inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js + var years = Math.abs(this.years()), + months = Math.abs(this.months()), + days = Math.abs(this.days()), + hours = Math.abs(this.hours()), + minutes = Math.abs(this.minutes()), + seconds = Math.abs(this.seconds() + this.milliseconds() / 1000); + + if (!this.asSeconds()) { + // this is the same as C#'s (Noda) and python (isodate)... + // but not other JS (goog.date) + return 'P0D'; + } + + return (this.asSeconds() < 0 ? '-' : '') + + 'P' + + (years ? years + 'Y' : '') + + (months ? months + 'M' : '') + + (days ? days + 'D' : '') + + ((hours || minutes || seconds) ? 'T' : '') + + (hours ? hours + 'H' : '') + + (minutes ? minutes + 'M' : '') + + (seconds ? seconds + 'S' : ''); + } + }); + + function makeDurationGetter(name) { + moment.duration.fn[name] = function () { + return this._data[name]; + }; + } + + function makeDurationAsGetter(name, factor) { + moment.duration.fn['as' + name] = function () { + return +this / factor; + }; + } + + for (i in unitMillisecondFactors) { + if (unitMillisecondFactors.hasOwnProperty(i)) { + makeDurationAsGetter(i, unitMillisecondFactors[i]); + makeDurationGetter(i.toLowerCase()); + } + } + + makeDurationAsGetter('Weeks', 6048e5); + moment.duration.fn.asMonths = function () { + return (+this - this.years() * 31536e6) / 2592e6 + this.years() * 12; + }; + + + /************************************ + Default Lang + ************************************/ + + + // Set default language, other languages will inherit from English. + moment.lang('en', { + ordinal : function (number) { + var b = number % 10, + output = (toInt(number % 100 / 10) === 1) ? 'th' : + (b === 1) ? 'st' : + (b === 2) ? 'nd' : + (b === 3) ? 'rd' : 'th'; + return number + output; + } + }); + + /* EMBED_LANGUAGES */ + + /************************************ + Exposing Moment + ************************************/ + + function makeGlobal(shouldDeprecate) { + /*global ender:false */ + if (typeof ender !== 'undefined') { + return; + } + oldGlobalMoment = globalScope.moment; + if (shouldDeprecate) { + globalScope.moment = deprecate( + "Accessing Moment through the global scope is " + + "deprecated, and will be removed in an upcoming " + + "release.", + moment); + } else { + globalScope.moment = moment; + } + } + + // CommonJS module is defined + if (hasModule) { + module.exports = moment; + } else if (typeof define === "function" && define.amd) { + define("moment", function (require, exports, module) { + if (module.config && module.config() && module.config().noGlobal === true) { + // release the global variable + globalScope.moment = oldGlobalMoment; + } + + return moment; + }); + makeGlobal(true); + } else { + makeGlobal(); + } +} diff --git a/qml/js/storage.js b/qml/js/storage.js new file mode 100644 index 0000000..70dd3b2 --- /dev/null +++ b/qml/js/storage.js @@ -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;"); + }); +} diff --git a/qml/js/utils.js b/qml/js/utils.js new file mode 100644 index 0000000..2ff10c3 --- /dev/null +++ b/qml/js/utils.js @@ -0,0 +1,13 @@ +// Miscellaneous functions for generic use + +// Escape strings for Text.StyledText +function escapeStyled(string) { + string = string.replace('&', '&'); + string = string.replace('<', '<'); + return string.replace('>', '>'); +} + +// Return StyledText in a specified color +function colored(color, string) { + return '' + string + ''; +} diff --git a/qml/pages/AddConnection.qml b/qml/pages/AddConnection.qml new file mode 100644 index 0000000..3bafa40 --- /dev/null +++ b/qml/pages/AddConnection.qml @@ -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" + } + } + } + } + } +} + + diff --git a/qml/pages/ConnectionList.qml b/qml/pages/ConnectionList.qml new file mode 100644 index 0000000..81ced0c --- /dev/null +++ b/qml/pages/ConnectionList.qml @@ -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(); + } + } + } +} + + diff --git a/qml/pages/DebugView.qml b/qml/pages/DebugView.qml new file mode 100644 index 0000000..0cb787a --- /dev/null +++ b/qml/pages/DebugView.qml @@ -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 } + } +} diff --git a/qml/pages/FirstPage.qml b/qml/pages/FirstPage.qml deleted file mode 100644 index 32e2d6f..0000000 --- a/qml/pages/FirstPage.qml +++ /dev/null @@ -1,73 +0,0 @@ -/* - Copyright (C) 2013 Jolla Ltd. - Contact: Thomas Perl - 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 - } - } - } -} - - diff --git a/qml/pages/SecondPage.qml b/qml/pages/SecondPage.qml deleted file mode 100644 index ead18dd..0000000 --- a/qml/pages/SecondPage.qml +++ /dev/null @@ -1,62 +0,0 @@ -/* - Copyright (C) 2013 Jolla Ltd. - Contact: Thomas Perl - 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 {} - } -} - - - - - diff --git a/qml/pages/SslVerifyDialog.qml b/qml/pages/SslVerifyDialog.qml new file mode 100644 index 0000000..2c90d13 --- /dev/null +++ b/qml/pages/SslVerifyDialog.qml @@ -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:
") + issuer + wrapMode: Text.Wrap + color: Theme.highlightColor + width: parent.width + textFormat: Text.StyledText + } + + Label { + text: U.colored(Theme.secondaryHighlightColor, "Valid from:
") + startDateText + wrapMode: Text.Wrap + color: Theme.highlightColor + width: parent.width + textFormat: Text.StyledText + } + + Label { + text: U.colored(Theme.secondaryHighlightColor, "Valid to:
") + expiryDateText + wrapMode: Text.Wrap + color: Theme.highlightColor + width: parent.width + textFormat: Text.StyledText + } + + Label { + text: U.colored(Theme.secondaryHighlightColor, "SHA-256 digest:
") + 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" + } + } + } + } +} diff --git a/qml/pages/TextListComponent.qml b/qml/pages/TextListComponent.qml new file mode 100644 index 0000000..bc98105 --- /dev/null +++ b/qml/pages/TextListComponent.qml @@ -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 + } + } +} diff --git a/rpm/harbour-weechatrelay.spec b/rpm/harbour-weechatrelay.spec index b097a16..e2a05e7 100644 --- a/rpm/harbour-weechatrelay.spec +++ b/rpm/harbour-weechatrelay.spec @@ -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} diff --git a/rpm/harbour-weechatrelay.yaml b/rpm/harbour-weechatrelay.yaml index 37ff4b4..29f241a 100644 --- a/rpm/harbour-weechatrelay.yaml +++ b/rpm/harbour-weechatrelay.yaml @@ -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: diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp new file mode 100644 index 0000000..dba2cdc --- /dev/null +++ b/src/connectionhandler.cpp @@ -0,0 +1,187 @@ +/* + * © Mikko Ahlroth 2014 + * WeeCRApp is open source software. For licensing information, please check + * the LICENCE file. + */ + +#include +#include +#include +#include + +#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); +} diff --git a/src/connectionhandler.h b/src/connectionhandler.h new file mode 100644 index 0000000..7ad94a4 --- /dev/null +++ b/src/connectionhandler.h @@ -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 +#include +#include +#include +#include +#include +#include + +#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 diff --git a/src/connectresolver.h b/src/connectresolver.h new file mode 100644 index 0000000..8dec792 --- /dev/null +++ b/src/connectresolver.h @@ -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 struct SELECT { + template + static constexpr auto OVERLOAD_OF( R (C::*pmf)(Args...) ) -> decltype(pmf) { + return pmf; + } +}; + +#endif // CONNECTRESOLVER_H diff --git a/src/harbour-weechatrelay.cpp b/src/harbour-weechatrelay.cpp index 1d58d72..91a1094 100644 --- a/src/harbour-weechatrelay.cpp +++ b/src/harbour-weechatrelay.cpp @@ -1,40 +1,22 @@ /* - Copyright (C) 2013 Jolla Ltd. - Contact: Thomas Perl - 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 #endif #include +#include +#include +#include -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("harbour.weechatrelay.connectionhandler", 1, 0, "ConnectionHandler"); + qmlRegisterType("harbour.weechatrelay.qsslcertificateinfo", 1, 0, "QSslCertificateInfo"); + return SailfishApp::main(argc, argv); } - diff --git a/src/protocolhandler.cpp b/src/protocolhandler.cpp new file mode 100644 index 0000000..182a3e4 --- /dev/null +++ b/src/protocolhandler.cpp @@ -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); +} diff --git a/src/protocolhandler.h b/src/protocolhandler.h new file mode 100644 index 0000000..795453f --- /dev/null +++ b/src/protocolhandler.h @@ -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 +#include +#include +#include + +#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 diff --git a/src/qsslcertificateinfo.cpp b/src/qsslcertificateinfo.cpp new file mode 100644 index 0000000..db659c9 --- /dev/null +++ b/src/qsslcertificateinfo.cpp @@ -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; +} diff --git a/src/qsslcertificateinfo.h b/src/qsslcertificateinfo.h new file mode 100644 index 0000000..a472c22 --- /dev/null +++ b/src/qsslcertificateinfo.h @@ -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 +#include +#include +#include + +// 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 diff --git a/src/relayconnection.cpp b/src/relayconnection.cpp index 5f97fb4..38ec8b9 100644 --- a/src/relayconnection.cpp +++ b/src/relayconnection.cpp @@ -1,36 +1,71 @@ +/* + * © Mikko Ahlroth 2014 + * WeeCRApp is open source software. For licensing information, please check + * the LICENCE file. + */ + #include "relayconnection.h" -#include -/*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(); +} + diff --git a/src/relayconnection.h b/src/relayconnection.h index a38670e..65b0cc3 100644 --- a/src/relayconnection.h +++ b/src/relayconnection.h @@ -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 #include +#include -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; }; diff --git a/src/sslrelayconnection.cpp b/src/sslrelayconnection.cpp new file mode 100644 index 0000000..96c722d --- /dev/null +++ b/src/sslrelayconnection.cpp @@ -0,0 +1,54 @@ +#include "sslrelayconnection.h" +#include "connectresolver.h" + +#include +#include +#include +#include + +SSLRelayConnection::SSLRelayConnection(QSslSocket* socket, QObject* parent) : + RelayConnection(socket, parent), sslSocket(socket) +{ + QObject::connect(socket, SELECT&>::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& 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); + } +} diff --git a/src/sslrelayconnection.h b/src/sslrelayconnection.h new file mode 100644 index 0000000..0f7af94 --- /dev/null +++ b/src/sslrelayconnection.h @@ -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 +#include +#include +#include + +#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& errors); + +private: + QSslSocket* sslSocket; + ConnectionHandler* handler; + +}; + +#endif // SSLRELAYCONNECTION_H diff --git a/src/weechatproto/array.cpp b/src/weechatproto/array.cpp new file mode 100644 index 0000000..0973e48 --- /dev/null +++ b/src/weechatproto/array.cpp @@ -0,0 +1,5 @@ +#include "array.h" + +Array::Array() +{ +} diff --git a/src/weechatproto/array.h b/src/weechatproto/array.h new file mode 100644 index 0000000..9ae60f3 --- /dev/null +++ b/src/weechatproto/array.h @@ -0,0 +1,12 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#include "protocoltype.h" + +class Array : public ProtocolType +{ +public: + Array(); +}; + +#endif // ARRAY_H diff --git a/src/weechatproto/buffer.cpp b/src/weechatproto/buffer.cpp new file mode 100644 index 0000000..d9be1ce --- /dev/null +++ b/src/weechatproto/buffer.cpp @@ -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(current)); + } +} + +const QByteArray* Buffer::getBytes() const +{ + return this->data; +} diff --git a/src/weechatproto/buffer.h b/src/weechatproto/buffer.h new file mode 100644 index 0000000..1a2b26d --- /dev/null +++ b/src/weechatproto/buffer.h @@ -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 diff --git a/src/weechatproto/char.cpp b/src/weechatproto/char.cpp new file mode 100644 index 0000000..9a68574 --- /dev/null +++ b/src/weechatproto/char.cpp @@ -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(c); +} + +char Char::getChar() const +{ + return this->data; +} + +std::size_t Char::hash() const +{ + return data * 2654435761 % 2^32; +} diff --git a/src/weechatproto/char.h b/src/weechatproto/char.h new file mode 100644 index 0000000..760ca46 --- /dev/null +++ b/src/weechatproto/char.h @@ -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 diff --git a/src/weechatproto/hashtable.cpp b/src/weechatproto/hashtable.cpp new file mode 100644 index 0000000..3374c20 --- /dev/null +++ b/src/weechatproto/hashtable.cpp @@ -0,0 +1,10 @@ +#include "hashtable.h" + +HashTable::HashTable() +{ +} + +void HashTable::readFrom(QDataStream* data) +{ + ProtocolType::markRead(); +} diff --git a/src/weechatproto/hashtable.h b/src/weechatproto/hashtable.h new file mode 100644 index 0000000..f3cd849 --- /dev/null +++ b/src/weechatproto/hashtable.h @@ -0,0 +1,17 @@ +#ifndef HASHTABLE_H +#define HASHTABLE_H + +#include "protocoltype.h" + +#include + +class HashTable : public ProtocolType +{ +public: + HashTable(); + void readFrom(QDataStream* data); + +private: +}; + +#endif // HASHTABLE_H diff --git a/src/weechatproto/hdata.cpp b/src/weechatproto/hdata.cpp new file mode 100644 index 0000000..3ab8245 --- /dev/null +++ b/src/weechatproto/hdata.cpp @@ -0,0 +1,5 @@ +#include "hdata.h" + +HData::HData() +{ +} diff --git a/src/weechatproto/hdata.h b/src/weechatproto/hdata.h new file mode 100644 index 0000000..08d0e9c --- /dev/null +++ b/src/weechatproto/hdata.h @@ -0,0 +1,12 @@ +#ifndef HDATA_H +#define HDATA_H + +#include "protocoltype.h" + +class HData : public ProtocolType +{ +public: + HData(); +}; + +#endif // HDATA_H diff --git a/src/weechatproto/info.cpp b/src/weechatproto/info.cpp new file mode 100644 index 0000000..dd6bd83 --- /dev/null +++ b/src/weechatproto/info.cpp @@ -0,0 +1,5 @@ +#include "info.h" + +Info::Info() +{ +} diff --git a/src/weechatproto/info.h b/src/weechatproto/info.h new file mode 100644 index 0000000..1598f9d --- /dev/null +++ b/src/weechatproto/info.h @@ -0,0 +1,12 @@ +#ifndef INFO_H +#define INFO_H + +#include "protocoltype.h" + +class Info : public ProtocolType +{ +public: + Info(); +}; + +#endif // INFO_H diff --git a/src/weechatproto/infolist.cpp b/src/weechatproto/infolist.cpp new file mode 100644 index 0000000..78d6ab0 --- /dev/null +++ b/src/weechatproto/infolist.cpp @@ -0,0 +1,5 @@ +#include "infolist.h" + +InfoList::InfoList() +{ +} diff --git a/src/weechatproto/infolist.h b/src/weechatproto/infolist.h new file mode 100644 index 0000000..ae5aa48 --- /dev/null +++ b/src/weechatproto/infolist.h @@ -0,0 +1,12 @@ +#ifndef INFOLIST_H +#define INFOLIST_H + +#include "protocoltype.h" + +class InfoList : public ProtocolType +{ +public: + InfoList(); +}; + +#endif // INFOLIST_H diff --git a/src/weechatproto/integer.cpp b/src/weechatproto/integer.cpp new file mode 100644 index 0000000..6b2df3e --- /dev/null +++ b/src/weechatproto/integer.cpp @@ -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; +} diff --git a/src/weechatproto/integer.h b/src/weechatproto/integer.h new file mode 100644 index 0000000..b2726be --- /dev/null +++ b/src/weechatproto/integer.h @@ -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 diff --git a/src/weechatproto/long.cpp b/src/weechatproto/long.cpp new file mode 100644 index 0000000..0b072f0 --- /dev/null +++ b/src/weechatproto/long.cpp @@ -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(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); +} diff --git a/src/weechatproto/long.h b/src/weechatproto/long.h new file mode 100644 index 0000000..6bb4d2a --- /dev/null +++ b/src/weechatproto/long.h @@ -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 diff --git a/src/weechatproto/pointer.cpp b/src/weechatproto/pointer.cpp new file mode 100644 index 0000000..a19c198 --- /dev/null +++ b/src/weechatproto/pointer.cpp @@ -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(current)); + } +} + +const QString* Pointer::getPointer() const +{ + return this->data; +} diff --git a/src/weechatproto/pointer.h b/src/weechatproto/pointer.h new file mode 100644 index 0000000..5c09d3e --- /dev/null +++ b/src/weechatproto/pointer.h @@ -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 diff --git a/src/weechatproto/protocoltype.cpp b/src/weechatproto/protocoltype.cpp new file mode 100644 index 0000000..1e66f01 --- /dev/null +++ b/src/weechatproto/protocoltype.cpp @@ -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; +} diff --git a/src/weechatproto/protocoltype.h b/src/weechatproto/protocoltype.h new file mode 100644 index 0000000..f85d5fc --- /dev/null +++ b/src/weechatproto/protocoltype.h @@ -0,0 +1,22 @@ +#ifndef PROTOCOLTYPE_H +#define PROTOCOLTYPE_H + +#include +#include + +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 diff --git a/src/weechatproto/protocoltypeoverwriteexception.cpp b/src/weechatproto/protocoltypeoverwriteexception.cpp new file mode 100644 index 0000000..c50ec19 --- /dev/null +++ b/src/weechatproto/protocoltypeoverwriteexception.cpp @@ -0,0 +1,6 @@ +#include "protocoltypeoverwriteexception.h" + +ProtocolTypeOverWriteException::ProtocolTypeOverWriteException(const std::string& reason): + std::runtime_error(reason) +{ +} diff --git a/src/weechatproto/protocoltypeoverwriteexception.h b/src/weechatproto/protocoltypeoverwriteexception.h new file mode 100644 index 0000000..afe2e6c --- /dev/null +++ b/src/weechatproto/protocoltypeoverwriteexception.h @@ -0,0 +1,13 @@ +#ifndef PROTOCOLTYPEOVERWRITEEXCEPTION_H +#define PROTOCOLTYPEOVERWRITEEXCEPTION_H + +#include +#include + +class ProtocolTypeOverWriteException : public std::runtime_error +{ +public: + explicit ProtocolTypeOverWriteException(const std::string& reason); +}; + +#endif // PROTOCOLTYPEOVERWRITEEXCEPTION_H diff --git a/src/weechatproto/string.cpp b/src/weechatproto/string.cpp new file mode 100644 index 0000000..8407dd7 --- /dev/null +++ b/src/weechatproto/string.cpp @@ -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(current)); + } +} + +const QString* String::getString() const +{ + return this->data; +} diff --git a/src/weechatproto/string.h b/src/weechatproto/string.h new file mode 100644 index 0000000..651bf93 --- /dev/null +++ b/src/weechatproto/string.h @@ -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 diff --git a/src/weechatproto/time.cpp b/src/weechatproto/time.cpp new file mode 100644 index 0000000..92c1afb --- /dev/null +++ b/src/weechatproto/time.cpp @@ -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; +} diff --git a/src/weechatproto/time.h b/src/weechatproto/time.h new file mode 100644 index 0000000..9b9c1a4 --- /dev/null +++ b/src/weechatproto/time.h @@ -0,0 +1,19 @@ +#ifndef TIME_H +#define TIME_H + +#include "protocoltype.h" + +#include + +class Time : public ProtocolType +{ +public: + Time(); + void readFrom(QDataStream* data); + const QDateTime* getTime() const; + +private: + QDateTime* data; +}; + +#endif // TIME_H diff --git a/src/weechatprotocolhandler.cpp b/src/weechatprotocolhandler.cpp new file mode 100644 index 0000000..f7a1172 --- /dev/null +++ b/src/weechatprotocolhandler.cpp @@ -0,0 +1,99 @@ +/* + * © Mikko Ahlroth 2014 + * WeeCRApp is open source software. For licensing information, please check + * the LICENCE file. + */ + +#include +#include + +#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) +{ + +} diff --git a/src/weechatprotocolhandler.h b/src/weechatprotocolhandler.h new file mode 100644 index 0000000..07c6307 --- /dev/null +++ b/src/weechatprotocolhandler.h @@ -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 +#include +#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