Subversion Repositories IRC-Archiver

Rev

Rev 13 | Rev 18 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13 Rev 14
Line 1... Line 1...
1
/* $Id: html.c 13 2024-08-30 07:33:43Z nishi $ */
1
/* $Id: html.c 14 2024-08-30 07:49:29Z nishi $ */
2
 
2
 
3
#include "web_html.h"
3
#include "web_html.h"
4
 
4
 
5
#include "web_db.h"
5
#include "web_db.h"
6
 
6
 
Line 9... Line 9...
9
#include <time.h>
9
#include <time.h>
10
#include <stdio.h>
10
#include <stdio.h>
11
#include <stdlib.h>
11
#include <stdlib.h>
12
#include <string.h>
12
#include <string.h>
13
#include <stdbool.h>
13
#include <stdbool.h>
-
 
14
#include <sys/stat.h>
-
 
15
#include <dirent.h>
14
 
16
 
15
extern char* webroot;
17
extern char* webroot;
16
extern const char* ircarc_version;
18
extern const char* ircarc_version;
17
 
19
 
18
char* web_html_escape(const char* html) {
20
char* web_html_escape(const char* html) {
Line 71... Line 73...
71
		char* tmp = fmtmsg; \
73
		char* tmp = fmtmsg; \
72
		fmtmsg = ia_strcat(tmp, "</" tagname ">"); \
74
		fmtmsg = ia_strcat(tmp, "</" tagname ">"); \
73
		free(tmp); \
75
		free(tmp); \
74
	}
76
	}
75
 
77
 
-
 
78
int mtimesort(const struct dirent** d1_, const struct dirent** d2_){
-
 
79
	struct dirent* d1 = (struct dirent*)d1_;
-
 
80
	struct dirent* d2 = (struct dirent*)d2_;
-
 
81
	char* d1_path = ia_strcat3(webroot, "/", d1->d_name);
-
 
82
	char* d2_path = ia_strcat3(webroot, "/", d2->d_name);
-
 
83
	struct stat s1;
-
 
84
	struct stat s2;
-
 
85
	stat(d1_path, &s1);
-
 
86
	stat(d2_path, &s2);
-
 
87
	free(d1_path);
-
 
88
	free(d2_path);
-
 
89
	return s1.st_mtime - s2.st_mtime;
-
 
90
}
-
 
91
 
76
int web_html_generate(const char* name, web_range_t range) {
92
int web_html_generate(const char* name, web_range_t range) {
77
	time_t t = time(NULL);
93
	time_t t = time(NULL);
78
	char* path = ia_strcat4(webroot, "/", name, ".html");
94
	char* path = ia_strcat4(webroot, "/", name, ".html");
79
	FILE* f = fopen(path, "w");
95
	FILE* f = fopen(path, "w");
80
	if(f != NULL) {
96
	if(f != NULL) {
Line 241... Line 257...
241
		fprintf(f, "</html>\n");
257
		fprintf(f, "</html>\n");
242
		fclose(f);
258
		fclose(f);
243
		free(title);
259
		free(title);
244
	}
260
	}
245
	free(path);
261
	free(path);
-
 
262
	path = ia_strcat(webroot, "/index.html");
-
 
263
	f = fopen(path, "w");
-
 
264
	if(f != NULL){
-
 
265
		char* title = ia_strdup("Index");
-
 
266
		struct dirent **namelist;
-
 
267
		int n = scandir(webroot, &namelist, NULL, mtimesort);
-
 
268
		fprintf(f, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
-
 
269
		fprintf(f, "<html>\n");
-
 
270
		fprintf(f, "	<head>\n");
-
 
271
		fprintf(f, "		<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">\n");
-
 
272
		fprintf(f, "		<title>%s</title>\n", title);
-
 
273
		fprintf(f, "		<style type=\"text/css\">\n");
-
 
274
		fprintf(f, "		</style>\n");
-
 
275
		fprintf(f, "	</head>\n");
-
 
276
		fprintf(f, "	<body style=\"padding: 15px; margin: 0 auto; width: 900px;\">\n");
-
 
277
		fprintf(f, "		<div style=\"padding: 1px 0; margin: 0; text-align: center; background-color: #8080ff;\">\n");
-
 
278
		fprintf(f, "			<h1>%s</h1>\n", title);
-
 
279
		fprintf(f, "		</div>\n");
-
 
280
		fprintf(f, "		<hr>\n");
-
 
281
		fprintf(f, "		<table border=\"1\" style=\"width: 100%%;\">\n");
-
 
282
		fprintf(f, "			<tr>\n");
-
 
283
		fprintf(f, "				<th>Name</th><th style=\"width: 250px;\">Archived at</th><th style=\"width: 100px;\">Link</th>");
-
 
284
		fprintf(f, "			</tr>\n");
-
 
285
		int i;
-
 
286
		for(i = 0; i < n; i++){
-
 
287
			if(strcmp(namelist[i]->d_name, "..") != 0 && strcmp(namelist[i]->d_name, ".") != 0 && strcmp(namelist[i]->d_name, "index.html") != 0){
-
 
288
				struct stat s;
-
 
289
				char* np = ia_strcat3(webroot, "/", namelist[i]->d_name);
-
 
290
				stat(np, &s);
-
 
291
				struct tm* tm = gmtime(&s.st_mtime);
-
 
292
				char date[512];
-
 
293
				strftime(date, 512, "%Y/%m/%d %H:%M:%S UTC", tm);
-
 
294
				char* name = ia_strdup(namelist[i]->d_name);
-
 
295
				int j;
-
 
296
				for(j = strlen(name) - 1; j >= 0; j--){
-
 
297
					if(name[j] == '.'){
-
 
298
						name[j] = 0;
-
 
299
						break;
-
 
300
					}
-
 
301
				}
-
 
302
				char* esc = web_html_escape(name);
-
 
303
				free(name);
-
 
304
				fprintf(f, "			<tr>\n");
-
 
305
				fprintf(f, "				<td>%s</td>", esc);
-
 
306
				fprintf(f, "				<td>%s</td>", date);
-
 
307
				fprintf(f, "				<td><a href=\"%s\">Link</a></td>", namelist[i]->d_name);
-
 
308
				fprintf(f, "			</tr>\n");
-
 
309
				free(esc);
-
 
310
			}
-
 
311
			free(namelist[i]);
-
 
312
		}
-
 
313
		free(namelist);
-
 
314
		fprintf(f, "		</table>\n");
-
 
315
		fprintf(f, "		<hr>\n");
-
 
316
		fprintf(f, "		<i>Generated by <a href=\"http://nishi.boats/ircarc\">IRC-Archiver</a> %s</i>\n", ircarc_version);
-
 
317
		fprintf(f, "	</body>\n");
-
 
318
		fprintf(f, "</html>\n");
-
 
319
		fclose(f);
-
 
320
		free(title);
-
 
321
	}
-
 
322
	free(path);
246
}
323
}