source: SMSSender/gateways/Post/business/account.cpp @ 376:0d8179eea17f

separation-frontend-backend
Last change on this file since 376:0d8179eea17f was 376:0d8179eea17f, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Separates backend and frontend code in the Post gateway.
File size: 4.9 KB
Line 
1/*
2  Swiss Post gateway plugin - The smssender plugin for the Swiss Post platform.
3  Copyright (C) 2011-2014, gorrión. See http://smssender.gorrion.ch
4
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "account.h"
19
20#include <algorithm>
21#include <math.h>
22
23#include <QDebug>
24#include <QThread>
25
26#include "common/domain/snumber.h"
27#include "common/exceptions/loginaccountexceptions.h"
28#include "common/exceptions/stdexceptions.h"
29#include "common/network/snetworkreply.h"
30#include "common/validation/defaultaccountvalidator.h"
31#include "gateways/common/business/defaultlogininittask.h"
32#include "gateways/common/persistence/defaultaccountstoragehelper.h"
33#include "gateways/Post/business/account_tasks.h"
34#include "gateways/Post/business/gateway.h"
35
36namespace Post {
37
38Account::Account(IGateway *gateway)
39    : BaseAccount(gateway)
40    , m_httpHelper(new SNetworkHelper)
41    , m_longSMSLength(435) // Default value from post.ch
42{
43    m_httpHelper->addTrustedCA(":/certs/Verisign_class3_root.crt");
44    m_httpHelper->addTrustedCA(":/certs/VerisignCA_G3.crt");
45    m_httpHelper->addTrustedCA(":/certs/VerisignCA_G5.crt");
46
47    connect(this, SIGNAL(initStateChanged(IAccount*)),
48            this, SLOT(onInitStateChanged()));
49}
50
51Persistence::IStorageHelper* Account::createStorageHelper(
52        Persistence::IMainStorage *storage) {
53    return new Persistence::DefaultAccountStorageHelper(
54                this, storage, "s2uh98dWe9D_wf98h23:*EOnqei8D");
55}
56IValidator* Account::createValidator() const {
57    return new DefaultAccountValidator(this);
58}
59
60IAccountInitTask* Account::doCreateInitTask() {
61    return new DefaultLoginInitTask(this);
62}
63IAccountLoginTask* Account::doCreateLoginTask() {
64    return new AccountLoginTask(this);
65}
66ISMSSendTask* Account::doCreateSMSSendTask(
67        const QString &message, const QSet<const SContact *> &recipients)
68{
69    return new SMSSendTask(this, message, recipients);
70}
71
72
73SNetworkHelper *Account::httpHelper() const {
74    return m_httpHelper.data();
75}
76
77void Account::cancelSMSSending() {
78    // TODO: Implement this
79}
80
81bool Account::checkStillLoggedIn() {
82    //haltIfCancelled();
83    qDebug() << "Starting still-logged-in-check";
84
85    bool result;
86    try {
87        QString html = httpHelper()->syncGet(URL_SMSFORM)->readAll();
88        // TODO: Check if we are at https://sso.post.ch/NO_SERVICE
89        result = html.contains("Error", Qt::CaseSensitive);
90    } catch (EException &e) {
91        qWarning() << "Error while checking if we are still logged in: "
92                   << e.what()
93                   << "But ignoring it, since we just do a new login attempt.";
94        result = false;
95    }
96
97    qDebug() << "Ended still-logged-in-check";
98    return result;
99}
100
101void Account::reloadOnlineParams() {
102    QString html = httpHelper()->syncGet(URL_SMSFORM)->readAll();
103    reloadOnlineParams(html);
104}
105void Account::reloadOnlineParams(QString html) {
106    reloadLongSMSLength(html);
107    reloadFreeSMSCount(html);
108}
109void Account::reloadLongSMSLength(QString html) {
110    /*<textarea name="sms_body" style="width: 100%; height: 117px" cols=2 rows=2
111     *    maxlength="435" onKeyUp="left_char(this)"></textarea>*/
112
113    QRegExp rx("<textarea name=\"sms_body\"[^>]*maxlength=\"(\\d+)\"");
114    rx.setMinimal(true);
115    if (rx.indexIn(html) == -1) {
116        EException(tr("Could not get the longSMSLength."))
117                .raise();
118    }
119    setLongSMSLength(rx.cap(1).toInt());
120    qDebug() << "  Long SMS length: " + rx.cap(1);
121}
122
123void Account::reloadFreeSMSCount(QString html) {
124    /*<td class="label">50 von 50 TextSMS im Monat xxx</td> */
125
126    QRegExp rx("TextSMS-Kontingent von \\d+ SMS verbraucht!");
127    rx.setCaseSensitivity(Qt::CaseInsensitive);
128    if (rx.indexIn(html) != -1) {
129        setFreeSMSCount(0);
130    } else {
131        rx.setPattern("<td class=\"label\">(\\d+) von \\d+ TextSMS im Monat [a-zA-Zäöü]+</td>");
132        if (rx.indexIn(html) == -1) {
133            EException(tr("Could not get the free sms count."))
134                    .addDebugInfo("html", html)
135                    .raise(); // Will be made userfriendly in parsePage
136        }
137        setFreeSMSCount(rx.cap(1).toInt());
138    }
139    qDebug() << "  Free SMS count: " + rx.cap(1);
140}
141
142
143void Account::onInitStateChanged() {
144    if (!isInitialized()) {
145        // Reset the current state
146        httpHelper()->clearCookies();
147    }
148}
149
150} // namespace Post
Note: See TracBrowser for help on using the repository browser.