-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStewitter.cpp
124 lines (115 loc) · 3.08 KB
/
Stewitter.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
/*
Stewitter.cpp - Arduino library to Post messages to Twitter with OAuth.
Copyright (c) arms22 2010 - 2012. All right reserved.
*/
/*
Twitter.cpp - Arduino library to Post messages to Twitter.
Copyright (c) NeoCat 2009. All right reserved.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "Stewitter.h"
#define STEWGATE_POST_API "/sg1/post/"
#define STEWGATE_LAST_MENTION_API "/sg1/last_mention/"
#define STEWGATE_HOST "stewgate.appspot.com"
Stewitter::Stewitter(const char *token) : client(), token(token)
{
httpBody.reserve(100);
}
bool Stewitter::post(const char *msg)
{
statusCode = 0;
parseStatus = 0;
httpBody = "";
if (client.connect(STEWGATE_HOST, 80)) {
int length;
if (msg != NULL) {
client.println(F("POST " STEWGATE_POST_API " HTTP/1.0"));
} else {
client.println(F("POST " STEWGATE_LAST_MENTION_API " HTTP/1.0"));
}
client.println(F("Host: " STEWGATE_HOST));
client.print(F("Content-Length: "));
if (msg != NULL) {
length = strlen(token) + strlen(msg) + 8;
}else{
length = strlen(token) + 3;
}
client.println(length);
client.println();
client.print(F("_t="));
client.print(token);
if (msg != NULL) {
client.print(F("&msg="));
client.print(msg);
}
client.println();
} else {
return false;
}
return true;
}
bool Stewitter::lastMention(void)
{
httpBody.reserve(200);
return post(NULL);
}
bool Stewitter::checkStatus(Print *debug)
{
bool ret = true;
if (client.available()) {
char c = client.read();
if (debug)
debug->print(c);
switch(parseStatus) {
case 0: // skip "HTTP/1.1 "
if (c == ' ') {
parseStatus++;
}
break;
case 1: // parse Status Code
if (c >= '0' && c <= '9') {
statusCode *= 10;
statusCode += c - '0';
} else {
parseStatus++;
}
break;
case 2: // skip HTTP Headers
if (c == '\x0a') {
parseStatus++;
}
break;
case 3:
if (c == '\x0d') {
parseStatus++;
}else{
parseStatus = 2;
}
break;
case 4:
if (c == '\x0a') {
parseStatus++;
}else{
parseStatus = 2;
}
break;
case 5: // recv HTTP Body
httpBody += c;
break;
}
}else{
if (!client.connected()) {
client.flush();
client.stop();
ret = false;
}
}
return ret;
}
int Stewitter::wait(Print *debug)
{
while (checkStatus(debug));
return statusCode;
}