Browse Source

Done with useless spaces

Brendan Abolivier 7 years ago
parent
commit
21e86f5fe8
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
7 changed files with 117 additions and 117 deletions
  1. 22
    22
      front/form.js
  2. 11
    11
      front/index.html
  3. 18
    18
      locales/en.json
  4. 18
    18
      locales/fr.json
  5. 16
    16
      server.js
  6. 17
    17
      settings.example.json
  7. 15
    15
      template.example.pug

+ 22
- 22
front/form.js View File

88
 				return url.match(/^(https?:\/\/[^\/]+)/)[1];
88
 				return url.match(/^(https?:\/\/[^\/]+)/)[1];
89
 			}
89
 			}
90
 		}
90
 		}
91
-	}	
91
+	}
92
 }
92
 }
93
 
93
 
94
 
94
 
100
 	getLangSync();
100
 	getLangSync();
101
 	// Get custom fields if defined in the configuration
101
 	// Get custom fields if defined in the configuration
102
 	getCustomFieldsSync();
102
 	getCustomFieldsSync();
103
-	
103
+
104
 	var el = document.getElementById(id);
104
 	var el = document.getElementById(id);
105
-	
105
+
106
 	// Set the form's behaviour
106
 	// Set the form's behaviour
107
 	el.setAttribute('onsubmit', 'sendForm(); return false;');
107
 	el.setAttribute('onsubmit', 'sendForm(); return false;');
108
-	
108
+
109
 	// Add an empty paragraph for status
109
 	// Add an empty paragraph for status
110
 	var status = document.createElement('p');
110
 	var status = document.createElement('p');
111
 	status.setAttribute('id', 'form_status');
111
 	status.setAttribute('id', 'form_status');
138
 			required: true
138
 			required: true
139
 		})
139
 		})
140
 	};
140
 	};
141
-	
141
+
142
 	// Adding custom fields
142
 	// Adding custom fields
143
 	for(let fieldName in customFields) {
143
 	for(let fieldName in customFields) {
144
 		let field = customFields[fieldName];
144
 		let field = customFields[fieldName];
145
 		DOMFields[fieldName] = getField(field);
145
 		DOMFields[fieldName] = getField(field);
146
 	}
146
 	}
147
-	
147
+
148
 	// Adding all nodes to document
148
 	// Adding all nodes to document
149
 	for(let field in DOMFields) {
149
 	for(let field in DOMFields) {
150
 		el.appendChild(DOMFields[field]);
150
 		el.appendChild(DOMFields[field]);
152
 
152
 
153
 	// Adding submit button
153
 	// Adding submit button
154
 	el.appendChild(getSubmitButton('form_subm', lang.form_subm_label));
154
 	el.appendChild(getSubmitButton('form_subm', lang.form_subm_label));
155
-	
155
+
156
 	// Retrieve the token from the server
156
 	// Retrieve the token from the server
157
 	getToken();
157
 	getToken();
158
 }
158
 }
184
 	if(labels) {
184
 	if(labels) {
185
 		block.appendChild(getLabel(fieldInfos.label, field.id));
185
 		block.appendChild(getLabel(fieldInfos.label, field.id));
186
 	}
186
 	}
187
-	
187
+
188
 	// Assemble the block and return it
188
 	// Assemble the block and return it
189
 	block.appendChild(field);
189
 	block.appendChild(field);
190
 	return block;
190
 	return block;
197
 // return: a label node the field's description
197
 // return: a label node the field's description
198
 function getLabel(content, id) {
198
 function getLabel(content, id) {
199
 	var label = document.createElement('label');
199
 	var label = document.createElement('label');
200
-	
200
+
201
 	label.setAttribute('for', id);
201
 	label.setAttribute('for', id);
202
 	label.innerHTML = content;
202
 	label.innerHTML = content;
203
-	
203
+
204
 	return label;
204
 	return label;
205
 }
205
 }
206
 
206
 
219
 	field.setAttribute('id', prefix + '_' + fieldInfos.name + '_select');
219
 	field.setAttribute('id', prefix + '_' + fieldInfos.name + '_select');
220
 
220
 
221
 	let index = 0;
221
 	let index = 0;
222
-	
222
+
223
 	// Add header option, useful if the field is required
223
 	// Add header option, useful if the field is required
224
 	let header = document.createElement('option');
224
 	let header = document.createElement('option');
225
 	// The value must be an empty string so the browser can block the submit
225
 	// The value must be an empty string so the browser can block the submit
227
 	header.setAttribute('value', '');
227
 	header.setAttribute('value', '');
228
 	header.innerHTML = lang.form_select_header_option;
228
 	header.innerHTML = lang.form_select_header_option;
229
 	field.appendChild(header);
229
 	field.appendChild(header);
230
-	
230
+
231
 	// Add all options to select
231
 	// Add all options to select
232
 	for(let choice of fieldInfos.options) {
232
 	for(let choice of fieldInfos.options) {
233
 		let option = document.createElement('option');
233
 		let option = document.createElement('option');
273
 // return: a HTML element of the given tag with basic info given as input
273
 // return: a HTML element of the given tag with basic info given as input
274
 function getBaseField(fieldInfos, required, tag) {
274
 function getBaseField(fieldInfos, required, tag) {
275
 	let field = document.createElement(tag);
275
 	let field = document.createElement(tag);
276
-	
276
+
277
 	if(fieldInfos.required) {
277
 	if(fieldInfos.required) {
278
 		field.setAttribute('required', 'required');
278
 		field.setAttribute('required', 'required');
279
 	}
279
 	}
280
 	field.setAttribute('placeholder', fieldInfos.label);
280
 	field.setAttribute('placeholder', fieldInfos.label);
281
 	field.setAttribute('id', prefix + '_' + fieldInfos.name + '_' + tag);
281
 	field.setAttribute('id', prefix + '_' + fieldInfos.name + '_' + tag);
282
-	
282
+
283
 	return field;
283
 	return field;
284
 }
284
 }
285
 
285
 
290
 // return: a div node containing the button
290
 // return: a div node containing the button
291
 function getSubmitButton(id, text) {
291
 function getSubmitButton(id, text) {
292
 	var submit = document.createElement('div');
292
 	var submit = document.createElement('div');
293
-	
293
+
294
 	submit.setAttribute('id', id);
294
 	submit.setAttribute('id', id);
295
-	
295
+
296
 	var button = document.createElement('button');
296
 	var button = document.createElement('button');
297
-	
297
+
298
 	button.setAttribute('type', 'submit');
298
 	button.setAttribute('type', 'submit');
299
 	button.setAttribute('id', id + '_btn');
299
 	button.setAttribute('id', id + '_btn');
300
-	
300
+
301
 	button.innerHTML = text;
301
 	button.innerHTML = text;
302
-	
302
+
303
 	submit.appendChild(button);
303
 	submit.appendChild(button);
304
-	
304
+
305
 	return submit;
305
 	return submit;
306
 }
306
 }
307
 
307
 
313
 	let status = document.getElementById('form_status');
313
 	let status = document.getElementById('form_status');
314
 	status.setAttribute('class', 'sending');
314
 	status.setAttribute('class', 'sending');
315
 	status.innerHTML = lang.send_status_progress;
315
 	status.innerHTML = lang.send_status_progress;
316
-	
316
+
317
 	xhr.send.open('POST', server + '/send');
317
 	xhr.send.open('POST', server + '/send');
318
 	xhr.send.setRequestHeader('Content-Type', 'application/json');
318
 	xhr.send.setRequestHeader('Content-Type', 'application/json');
319
 	xhr.send.send(JSON.stringify(getFormData()));
319
 	xhr.send.send(JSON.stringify(getFormData()));
320
-	
320
+
321
 	// Get a new token
321
 	// Get a new token
322
 	getToken();
322
 	getToken();
323
 }
323
 }

+ 11
- 11
front/index.html View File

1
 <!DOCTYPE html>
1
 <!DOCTYPE html>
2
 <html>
2
 <html>
3
-    <head>
4
-        <meta charset="utf-8">
5
-        <title>Send Me A Mail</title>
6
-        <script src="http://localhost:1970/form.js" charset="utf-8"></script>
7
-    </head>
8
-    <body>
9
-        <form id="smam"></form>
10
-        <script type="text/javascript">
11
-            generateForm('smam');
12
-        </script>
13
-    </body>
3
+	<head>
4
+		<meta charset="utf-8">
5
+		<title>Send Me A Mail</title>
6
+		<script src="http://localhost:1970/form.js" charset="utf-8"></script>
7
+	</head>
8
+	<body>
9
+		<form id="smam"></form>
10
+		<script type="text/javascript">
11
+			generateForm('smam');
12
+		</script>
13
+	</body>
14
 </html>
14
 </html>

+ 18
- 18
locales/en.json View File

1
 {
1
 {
2
-    "client": {
3
-        "form_name_label": "Your name",
4
-        "form_addr_label": "Your e-mail address",
5
-        "form_subj_label": "Your message's subject",
6
-        "form_mesg_label": "Your message",
7
-        "form_subm_label": "Send the mail",
2
+	"client": {
3
+		"form_name_label": "Your name",
4
+		"form_addr_label": "Your e-mail address",
5
+		"form_subj_label": "Your message's subject",
6
+		"form_mesg_label": "Your message",
7
+		"form_subm_label": "Send the mail",
8
 		"form_select_header_option": "Select an answer",
8
 		"form_select_header_option": "Select an answer",
9
-        "send_status_success": "Your message has been sent.",
10
-        "send_status_failure": "An error happened while sending your message, please retry later.",
11
-        "send_status_progress": "Sending the e-mail"
12
-    },
13
-    "server": {
14
-        "log_server_start": "Server started on",
15
-        "log_sending": "Sending message from",
16
-        "log_send_success": "Message sent to",
17
-        "log_send_failure": "Message failed to send to",
18
-        "log_invalid_token": "just tried to send a message with an invalid token",
19
-        "log_cleared_token": "Cleared expired tokens"
20
-    }
9
+		"send_status_success": "Your message has been sent.",
10
+		"send_status_failure": "An error happened while sending your message, please retry later.",
11
+		"send_status_progress": "Sending the e-mail"
12
+	},
13
+	"server": {
14
+		"log_server_start": "Server started on",
15
+		"log_sending": "Sending message from",
16
+		"log_send_success": "Message sent to",
17
+		"log_send_failure": "Message failed to send to",
18
+		"log_invalid_token": "just tried to send a message with an invalid token",
19
+		"log_cleared_token": "Cleared expired tokens"
20
+	}
21
 }
21
 }

+ 18
- 18
locales/fr.json View File

1
 {
1
 {
2
-    "client": {
3
-        "form_name_label": "Votre nom",
4
-        "form_addr_label": "Votre adresse e-mail",
5
-        "form_subj_label": "Sujet de votre message",
6
-        "form_mesg_label": "Votre message",
7
-        "form_subm_label": "Envoyer",
2
+	"client": {
3
+		"form_name_label": "Votre nom",
4
+		"form_addr_label": "Votre adresse e-mail",
5
+		"form_subj_label": "Sujet de votre message",
6
+		"form_mesg_label": "Votre message",
7
+		"form_subm_label": "Envoyer",
8
 		"form_select_header_option": "Sélectionnez une réponse",
8
 		"form_select_header_option": "Sélectionnez une réponse",
9
-        "send_status_success": "Votre message a été envoyé.",
10
-        "send_status_failure": "Une erreur est survenue lors de l'envoi du message, merci de réessayer plus tard.",
11
-        "send_status_progress": "Envoi du message en cours"
12
-    },
13
-    "server": {
14
-        "log_server_start": "Serveur démarré pour",
15
-        "log_sending": "Envoi d'un message de",
16
-        "log_send_success": "Message envoyé à",
17
-        "log_send_failure": "Erreur lors de l'envoi du message à",
18
-        "log_invalid_token": "vient d'essayer d'envoyer un message avec un jeton invalide",
19
-        "log_cleared_token": "Les jetons expirés ont été effacés"
20
-    }
9
+		"send_status_success": "Votre message a été envoyé.",
10
+		"send_status_failure": "Une erreur est survenue lors de l'envoi du message, merci de réessayer plus tard.",
11
+		"send_status_progress": "Envoi du message en cours"
12
+	},
13
+	"server": {
14
+		"log_server_start": "Serveur démarré pour",
15
+		"log_sending": "Envoi d'un message de",
16
+		"log_send_success": "Message envoyé à",
17
+		"log_send_failure": "Erreur lors de l'envoi du message à",
18
+		"log_invalid_token": "vient d'essayer d'envoyer un message avec un jeton invalide",
19
+		"log_cleared_token": "Les jetons expirés ont été effacés"
20
+	}
21
 }
21
 }

+ 16
- 16
server.js View File

70
 app.post('/send', function(req, res, next) {
70
 app.post('/send', function(req, res, next) {
71
 	// Response will be JSON
71
 	// Response will be JSON
72
 	res.header('Access-Control-Allow-Headers', 'Content-Type');
72
 	res.header('Access-Control-Allow-Headers', 'Content-Type');
73
-	
73
+
74
 	if(!checkBody(req.body)) {
74
 	if(!checkBody(req.body)) {
75
 		return res.status(400).send();
75
 		return res.status(400).send();
76
 	}
76
 	}
77
-	
77
+
78
 	let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
78
 	let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
79
-	
79
+
80
 	// Token verification
80
 	// Token verification
81
 	if(!checkToken(ip, req.body.token)) {
81
 	if(!checkToken(ip, req.body.token)) {
82
 		return res.status(403).send();
82
 		return res.status(403).send();
83
 	}
83
 	}
84
-	
84
+
85
 	// Count the failures
85
 	// Count the failures
86
 	let status = {
86
 	let status = {
87
 		failed: 0,
87
 		failed: 0,
88
 		total: settings.recipients.length
88
 		total: settings.recipients.length
89
 	};
89
 	};
90
-	
90
+
91
 	// params will be used as:
91
 	// params will be used as:
92
 	// - values for html generation from the pug template
92
 	// - values for html generation from the pug template
93
 	// - parameters for sending the mail(s)
93
 	// - parameters for sending the mail(s)
100
 
100
 
101
 	// Process custom fields to get data we can use in the HTML generation
101
 	// Process custom fields to get data we can use in the HTML generation
102
 	params.custom = processCustom(req.body.custom);
102
 	params.custom = processCustom(req.body.custom);
103
-	
103
+
104
 	// Replacing the mail's content with HTML from the pug template
104
 	// Replacing the mail's content with HTML from the pug template
105
 	// Commenting the line below will bypass the generation and only user the
105
 	// Commenting the line below will bypass the generation and only user the
106
 	// text entered by the user
106
 	// text entered by the user
107
 	params.html = pug.renderFile('template.pug', params);
107
 	params.html = pug.renderFile('template.pug', params);
108
-	
108
+
109
 	log.info(lang.log_sending, params.replyTo);
109
 	log.info(lang.log_sending, params.replyTo);
110
-	
110
+
111
 	// Send the email to all users
111
 	// Send the email to all users
112
 	sendMails(params, function(err, infos) {
112
 	sendMails(params, function(err, infos) {
113
 		if(err) {
113
 		if(err) {
136
 	if(settings.labels !== undefined) {
136
 	if(settings.labels !== undefined) {
137
 		labels = settings.labels;
137
 		labels = settings.labels;
138
 	}
138
 	}
139
-	
139
+
140
 	// Send the infos
140
 	// Send the infos
141
 	res.status(200).send({
141
 	res.status(200).send({
142
 		'labels': labels,
142
 		'labels': labels,
152
 
152
 
153
 	// Send an object anyway, its size will determine if we need to display any
153
 	// Send an object anyway, its size will determine if we need to display any
154
 	let customFields = settings.customFields || {};
154
 	let customFields = settings.customFields || {};
155
-	
155
+
156
 	// Send custom fields data
156
 	// Send custom fields data
157
 	res.status(200).send(customFields);
157
 	res.status(200).send(customFields);
158
 });
158
 });
223
 // return: true if the user was registered, false else
223
 // return: true if the user was registered, false else
224
 function checkToken(ip, token) {
224
 function checkToken(ip, token) {
225
 	let verified = false;
225
 	let verified = false;
226
-	
226
+
227
 	// Check if there's at least one token for this IP
227
 	// Check if there's at least one token for this IP
228
 	if(tokens[ip] !== undefined) {
228
 	if(tokens[ip] !== undefined) {
229
 		if(tokens[ip].length !== 0) {
229
 		if(tokens[ip].length !== 0) {
239
 			}
239
 			}
240
 		} 
240
 		} 
241
 	}
241
 	}
242
-	
242
+
243
 	if(!verified) {
243
 	if(!verified) {
244
 		log.warn(ip, lang.log_invalid_token);
244
 		log.warn(ip, lang.log_invalid_token);
245
 	}
245
 	}
246
-	
246
+
247
 	return verified;
247
 	return verified;
248
 }
248
 }
249
 
249
 
285
 function cleanTokens() {
285
 function cleanTokens() {
286
 	// Get current time for comparison
286
 	// Get current time for comparison
287
 	let now = new Date().getTime();
287
 	let now = new Date().getTime();
288
-	
288
+
289
 	for(let ip in tokens) { // Check for each IP in the object
289
 	for(let ip in tokens) { // Check for each IP in the object
290
 		for(let token of tokens[ip]) { // Check for each token of an IP
290
 		for(let token of tokens[ip]) { // Check for each token of an IP
291
 			if(token.expire < now) { // Token has expired
291
 			if(token.expire < now) { // Token has expired
296
 			delete tokens[ip];
296
 			delete tokens[ip];
297
 		}
297
 		}
298
 	}
298
 	}
299
-	
299
+
300
 	log.info(lang.log_cleared_token);
300
 	log.info(lang.log_cleared_token);
301
 }
301
 }
302
 
302
 
332
 			}
332
 			}
333
 		}
333
 		}
334
 	}
334
 	}
335
-	
335
+
336
 	return fields;
336
 	return fields;
337
 }
337
 }

+ 17
- 17
settings.example.json View File

1
 {
1
 {
2
-    "mailserver": {
3
-        "pool": true,
4
-        "host": "mail.example.tld",
5
-        "port": 465,
6
-        "secure": true,
7
-        "auth": {
8
-            "user": "noreply@example.tld",
9
-            "pass": "hackme"
10
-        }
11
-    },
12
-    "recipients": [
13
-        "you@example.tld",
14
-        "someone.else@example.com"
15
-    ],
16
-    "formOrigin": "https://example.tld",
17
-    "language": "en",
18
-    "labels": true
2
+	"mailserver": {
3
+		"pool": true,
4
+		"host": "mail.example.tld",
5
+		"port": 465,
6
+		"secure": true,
7
+		"auth": {
8
+			"user": "noreply@example.tld",
9
+			"pass": "hackme"
10
+		}
11
+	},
12
+	"recipients": [
13
+		"you@example.tld",
14
+		"someone.else@example.com"
15
+	],
16
+	"formOrigin": "https://example.tld",
17
+	"language": "en",
18
+	"labels": true
19
 }
19
 }

+ 15
- 15
template.example.pug View File

1
 html
1
 html
2
-    body
3
-        p.subj
4
-            span(style="font-weight:bold") Subject:&nbsp;
5
-            span= subject
6
-        p.from
7
-            span(style="font-weight:bold") Sent from:&nbsp;
8
-            span= replyTo
9
-        each field in custom
10
-            p.custom
11
-                span(style="font-weight:bold")= field.label + ': '
12
-                span= field.value
13
-        p.message
14
-            span(style="font-weight:bold") Message:&nbsp;
15
-        
16
-        p= html
2
+	body
3
+		p.subj
4
+			span(style="font-weight:bold") Subject:&nbsp;
5
+			span= subject
6
+		p.from
7
+			span(style="font-weight:bold") Sent from:&nbsp;
8
+			span= replyTo
9
+		each field in custom
10
+			p.custom
11
+				span(style="font-weight:bold")= field.label + ': '
12
+				span= field.value
13
+		p.message
14
+			span(style="font-weight:bold") Message:&nbsp;
15
+		
16
+		p= html