-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.cpp
162 lines (129 loc) · 4.16 KB
/
sms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "sms.h"
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QEventLoop>
#include <QNetworkReply>
#include <QRegExp>
#include <QDebug>
SMS::SMS(QString& agent, QString& username, QString& password)
{
this->password = password ;
this->username = username ;
this->agent = DEFAULT ;
if (agent.toLower().compare("clockwork")==0) this->agent=Clockwork ;
}
bool SMS::checkCredits()
{
if (smsErrorData.left(8).toLower().compare("error 3:")==0) return false ;
else return true ;
}
QString& SMS::getErrorMessage()
{
return smsErrorData ;
}
// Returns: -1 - Not Started Sending
// -2 - Network Error
// -3 - Error during transaction
// -4 - Unable to Send / Possibly Sent
// 1 - Send OK
int SMS::send(QString number, QString from, QString message)
{
switch (agent) {
case Clockwork:
return sendClockwork(number, from, message) ;
break ;
}
return false ;
}
int SMS::getBalance()
{
switch (agent) {
case Clockwork:
return getClockworkBalance() ;
break ;
}
return -1 ;
}
//////////////////////////////////////////////////////////////
//
// Clockwork SMS API
//
int SMS::sendClockwork(QString number, QString from, QString message)
{
QString URL = "https://api.clockworksms.com/http/send.aspx" ;
QUrl url(URL) ;
QNetworkRequest request(url) ;
QNetworkAccessManager manager ;
QEventLoop eventLoop ;
QNetworkReply *reply ;
QByteArray params ;
number = number.replace(" ", "") ;
from = from.replace(" ", "") ;
message = message.replace(" ", "+") ;
params.append("key=").append(password.toLatin1()).append("&to=").append(number.toLatin1()).append("&content=").append(message.toLatin1()) ;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
qDebug() << "SMS Connecting to " << URL ;
/*
// This is not supported in QT6
QNetworkAccessManager::NetworkAccessibility na = manager.networkAccessible() ;
if (na != QNetworkAccessManager::Accessible) {
// Connection Failure (network not up)
qDebug() << "Connection Failed" ;
return -2 ;
}
*/
qDebug() << "Sending " << params ;
reply = manager.post(request, params) ;
eventLoop.exec() ;
if (reply->error()!=QNetworkReply::NoError) {
// Unable to send
qDebug() << "Post returned error " << reply->errorString() ;
return -1 ;
}
smsErrorData = reply->readAll().trimmed() ;
bool iserror = smsErrorData.left(5).toLower().compare("error")==0 ;
if (iserror) {
qDebug() << "Post response error " << smsErrorData ;
// Received error response
return -3 ;
} else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)==200) {
qDebug() << "Post returned 200 OK" ;
// Received 200 OK response
return 1 ;
} else {
// Not received 200 OK response
return -4 ;
}
}
int SMS::getClockworkBalance()
{
QString URL = "https://api.clockworksms.com/http/balance" ;
QUrl url(URL) ;
QNetworkRequest request(url) ;
QNetworkAccessManager manager ;
QEventLoop eventLoop ;
QNetworkReply *reply ;
QByteArray params ;
params.append("key=").append(password.toLatin1()) ;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
/*
// Not supported in QT6
if (manager.networkAccessible() == QNetworkAccessManager::NotAccessible) {
// Connection Error
return -2 ;
}
*/
reply = manager.post(request, params) ;
eventLoop.exec() ;
// Error response returned
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)!=200) return -4 ;
QString balance = reply->readAll().toLower().trimmed() ;
QRegExp exp("[^0-9]*([0-9]+)[\\.:]([0-9]+)[^0-9]*") ;
if (exp.indexIn(balance)>=0) {
return exp.cap(1).toInt() * 100 + exp.cap(2).toInt() ;
}
// Unable to decode
return -3 ;
}