-
Notifications
You must be signed in to change notification settings - Fork 0
/
Esprout.ino
250 lines (228 loc) · 6.86 KB
/
Esprout.ino
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <NTPClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "img.h"
#include "mdp.h"
ESP8266WebServer server(80);
/*
IPAddress local_IP(192, 168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);
*/
//#define ADMIN_URL "url"
//#define THEWIFISSID "ssid"
//#define THEWIFIPWD "mdp"
String header = "<head>\
<title>Club des BG</title>\
<meta charset=\"UTF-8\">\
<style>\
img {width : auto; max-height: 200px;}\
#ecriture {text-align: center;}\
.hop, .leon, .poivron, .fleon{ width : 16px; height : 16px; display : inline-block;}\
body { background-color : Lavender; font-size: 150%;}\
.poivron { background-image : url("POIVRON");}\
.hop { background-image : url("HOP");}\
.leon { background-image : url("LEON");}\
.fleon { background-image : url("FLEON");}\
.fribouton {text-decoration : none; color : black; background-color : WhiteSmoke;}\
</style>\
<script>\
function ajoutAuMsg(txt) {\
var txtBoite = document.getElementById(\"message\");\
txtBoite.value = txtBoite.value + txt;\
}\
</script>\
</head>";
String texteBase = "<div id=\"ecriture\">\
<h1>🐗 Le club des BG 🐗 </h1>\
<form action=\"/\">\
<input type=\"submit\" style=\"height:80px;width:200px\" value=\"Rafraichir\" />\
</form>\
<a href=\"/merde\">Merde</a>\
<p>ERREUR : Accès Interdit, vous n'etes pas un bg</p>\
<form action=\"/msg\" method=\"post\">\
URL de l'image : <br>\
<input type=\"url\" maxlength=\"200\" name=\"image\"> <br>\
Message : <br>\
<a class=\"fribouton\" href=\"#\" onclick=\"ajoutAuMsg(":hop:")\">:hop: <div class=\"hop\"></div> </a>\
<a class=\"fribouton\" href=\"#\" onclick=\"ajoutAuMsg(":leon:")\">:leon: <div class=\"leon\"></div> </a>\
<a class=\"fribouton\" href=\"#\" onclick=\"ajoutAuMsg(":fleon:")\">:fleon: <div class=\"fleon\"></div> </a>\
<a class=\"fribouton\" href=\"#\" onclick=\"ajoutAuMsg(":poivron:")\">:poivron: <div class=\"poivron\"> </div></a>\
<br>\
<textarea id=\"message\" name=\"message\" maxlength=\"500\" cols=\"60\" rows=\"4\"></textarea><br>\
<input type=\"submit\" value=\"Envoyer\">\
</form></div><hr>";
String adminHTML = "<h1>Controle de BG</h1>\
<a href=\"/\" >Chat</a> <br>\
<a href=\"/merde\"> Merde</a>\
<form action=\"/"ADMIN_URL"\" method=\"post\">\
Merde: <br>\
<textarea name=\"merde\" cols=\"80\" rows=\"40\"></textarea><br>\
<input type=\"submit\" value=\"Envoyer\">\
</form>\
<form action=\"/"ADMIN_URL"\" method=\"post\">\
<button name=\"menage\" value=\"oui\">Nettoyer le chat</button>\
</form>\
<form>Message Admin : <br>\
<textarea name=\"msgAdmin\" cols=\"60\" rows=\"4\"></textarea><br>\
<input type=\"submit\" value=\"Envoyer\">\
</form>\
<form>Epingle : <br>\
<textarea name=\"epingle\" cols=\"60\" rows=\"4\"></textarea><br>\
<input type=\"submit\" value=\"Envoyer\">\
</form>";
String chat;
String merde = "Rien ici... Pour l'instant...";
String epingle = "<p>Salut et Tulas !</p>";
int nbMsgAff = 0;
int nbMsgReel = 0;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void handleRoot() {
server.send(200, "text/html", "<!DOCTYPE html>" + header + "<body>" +
texteBase + epingle + "<hr>" + chat + "</body></html>");
}
void handleMessage () {
if (server.args() > 0)
{
verifTailleMsgs();
String msg;
String imgURL;
String imgHTML;
for (int i=0; i < server.args(); ++i)
{
if (server.argName(i) == "message")
{
if (server.argName(i).length() > 550)
{
tropGros();
return;
}
msg = server.arg(i);
}
if (server.argName(i) == "image")
{
if (server.argName(i).length() > 220){
tropGros();
return;
}
imgURL = server.arg(i);
}
}
msg = formatMsg (msg);
imgURL.replace("<", "<");
imgURL.replace(">", ">");
imgURL.replace("\"", """);
if ((imgURL.length() == 0) && (msg.length() == 0))
{
server.send(200, "text/html", "<b>Message trop court...</b>");
return;
}
imgHTML = (imgURL.length() == 0) ? "" : "<img src=\"" +
imgURL +
"\" height=\"200\" width=\"200\"> ";
//TEMPS
timeClient.update();
chat = chat + "<b>N*" + nbMsgReel + " " + timeClient.getFormattedTime() +
"</b><br>" + imgHTML+ "<pre>" + msg + "</pre> <hr>";
++nbMsgReel;
++nbMsgAff;
}
server.sendHeader("Location","/");
server.send(303);
}
String formatMsg (String msg) {
msg.replace("<", "<");
msg.replace(">", ">");
msg.replace(":hop:","<div class=\"hop\"></div>");
msg.replace(":leon:","<div class=\"leon\"></div>");
msg.replace(":fleon:","<div class=\"fleon\"></div>");
msg.replace(":poivron:","<div class=\"poivron\"></div>");
return msg;
}
void verifTailleMsgs () {
if (nbMsgAff >= 20)
{
nbMsgAff = 0;
chat = "";
}
}
void handleMerde () {
server.send(200, "text/html", merde);
}
void tropGros(){
server.send(413,"text/html", "Trop gros mon ami");
}
void handleAdmin () {
if (server.args() > 0)
{
if (server.argName(0) == "menage")
{
nbMsgAff = 0;
chat = "<b style=\"color:red;\" >MESSAGES ADMINISTRES</b> <hr>";
}
if (server.argName(0) == "merde")
{
merde = server.arg(0);
}
if (server.argName(0) == "msgAdmin")
{
verifTailleMsgs();
String msg = formatMsg(server.arg(0));
chat = chat + "<b>N*" + nbMsgReel + " " + timeClient.getFormattedTime() +
" ADMIN</b><br> <pre style=\"color:red;\">" + msg + "</pre> <hr>";
++nbMsgAff;
++nbMsgReel;
}
if (server.argName(0) == "epingle")
{
epingle = server.arg(0);
}
}
server.send(200, "text/html","<!DOCTYPE html><html>" + header + "<body>" + adminHTML + "</body></html>");
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup() {
// put your setup code here, to run once:
/*
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP("ClubDesBG");
*/
Serial.begin(9600);
WiFi.mode(WIFI_STA);
// CHANGEME
WiFi.begin(THEWIFISSID, THEWIFIPWD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//TEMPS
timeClient.begin();
timeClient.setTimeOffset(60*60*2);
Serial.println(WiFi.localIP());
server.on("/"ADMIN_URL, handleAdmin);
server.on("/merde", handleMerde);
server.on("/", handleRoot);
server.on("/msg",HTTP_POST, handleMessage);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// put your main code here, to run repeatedly:
server.handleClient();
}