1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-27 23:20:49 +01:00
CMoe-Counter/cmoe.c

248 lines
7.3 KiB
C
Raw Normal View History

2021-06-10 06:03:29 +02:00
#include <simple_protobuf.h>
2021-06-07 15:18:19 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-06-10 06:03:29 +02:00
#include <sys/file.h>
2022-03-02 10:08:10 +01:00
#include <sys/uio.h>
2021-06-07 15:18:19 +02:00
#include <unistd.h>
#include "cmoe.h"
static uint32_t* items_len;
2022-11-23 07:45:39 +01:00
static counter_t counter;
2021-06-07 15:18:19 +02:00
2022-11-23 08:22:53 +01:00
static char* datfile = "dat.sp";
static char* token = "fumiama";
2022-05-20 09:13:49 +02:00
2022-03-05 07:14:28 +01:00
#define ADD_HEADER_PARAM(buf, offset, h, p) sprintf(buf + offset, (h), (p))
#define HEADER(content_type) HTTP200 SERVER_STRING CACHE_CTRL CONTENT_TYPE(content_type)
#define headers(content_len, content_type) (_headers(content_len, HEADER(content_type), sizeof(HEADER(content_type))-1))
static void _headers(uint32_t content_len, const char* h, size_t hlen) {
char buf[64];
size_t offset = ADD_HEADER_PARAM(buf, 0, CONTENT_LEN "\r\n", content_len);
if(offset <= 0) {
write(1, "\0\0\0\0", 4);
exit(EXIT_FAILURE);
}
content_len += offset+hlen;
struct iovec iov[3] = {{&content_len, sizeof(uint32_t)}, {h, hlen}, {buf, offset}};
writev(1, &iov, 3);
2021-06-07 15:18:19 +02:00
}
2022-11-23 07:45:39 +01:00
static void http_error(errcode_enum_t code, char* msg) {
2021-09-11 18:05:52 +02:00
uint32_t len = strlen(msg) + typel[code];
char* str = malloc(len);
sprintf(str, types[code], msg);
2022-03-02 10:08:10 +01:00
struct iovec iov[2] = {{&len, sizeof(uint32_t)}, {str, len}};
writev(1, &iov, 2);
2021-06-08 08:33:50 +02:00
free(str);
2021-06-07 15:18:19 +02:00
}
2021-06-08 07:11:58 +02:00
static char* get_arg(const char* query) {
2021-06-07 18:17:42 +02:00
int len = 0;
2021-06-07 15:18:19 +02:00
while(query[len] && query[len] != '&') len++;
2022-03-02 10:08:10 +01:00
if(len <= 0) return NULL;
char* name = malloc(len+1);
memcpy(name, query, len);
name[len] = 0;
return name;
2021-06-07 15:18:19 +02:00
}
static int del_user(FILE* fp, SIMPLE_PB* spb) {
2022-11-23 07:45:39 +01:00
counter_t *d = (counter_t *)spb->target;
2021-06-07 15:18:19 +02:00
uint32_t next = ftell(fp);
uint32_t this = next - spb->real_len;
fseek(fp, 0, SEEK_END);
uint32_t end = ftell(fp);
2022-03-02 10:08:10 +01:00
if(next == end) return ftruncate(fileno(fp), end - spb->real_len);
uint32_t cap = end - next;
char *data = malloc(cap);
if(data) {
fseek(fp, next, SEEK_SET);
if(fread(data, cap, 1, fp) == 1) {
if(!ftruncate(fileno(fp), end - spb->real_len)){
fseek(fp, this, SEEK_SET);
if(fwrite(data, cap, 1, fp) == 1) {
free(data);
fflush(fp);
return 0;
2021-06-07 15:18:19 +02:00
}
}
}
2022-03-02 10:08:10 +01:00
free(data);
2021-06-07 15:18:19 +02:00
}
2021-06-10 06:03:29 +02:00
return 2;
2021-06-07 15:18:19 +02:00
}
static int add_user(char* name, uint32_t count, FILE* fp) {
counter.count = count;
strncpy(counter.name, name, COUNTER_NAME_LEN-1);
fseek(fp, 0, SEEK_END);
2022-11-23 07:45:39 +01:00
return !set_pb(fp, items_len, sizeof(counter_t), &counter);
2021-06-07 15:18:19 +02:00
}
static uint32_t get_content_len(int isbig, uint16_t* len_type, char* cntstr) {
2022-03-02 12:22:14 +01:00
uint32_t len = sizeof(svg_small) // small & big has the same len
2021-06-07 15:18:19 +02:00
+ sizeof(svg_tail) - 1;
2022-03-02 10:36:24 +01:00
for(int i = 0; cntstr[i]; i++) {
2022-03-02 10:58:19 +01:00
len += len_type[cntstr[i] - '0'] + (sizeof(img_slot_front) + sizeof(img_slot_rear) - 2);
2022-03-02 12:22:14 +01:00
if(i > 0) len++;
if(i > 2-isbig) len++;
2021-06-07 15:18:19 +02:00
}
return len;
}
#define has_next(fp, ch) ((ch=getc(fp)),(feof(fp)?0:(ungetc(ch,fp),1)))
#define set_type(name, t, l) if(!strcmp(theme, name)) {\
theme_type = t;\
len_type = l;\
}
static void return_count(char* name, char* theme) {
2022-11-23 08:22:53 +01:00
FILE* fp = fopen(datfile, "rb+");
if(!fp) fp = fopen(datfile, "wb+");
2022-11-23 07:39:37 +01:00
if(!fp) {
http_error(HTTP500, "Open File Error.");
return;
}
flock(fileno(fp), LOCK_EX);
int ch, exist = 0, user_exist = 0;
2022-11-23 07:45:39 +01:00
char buf[sizeof(SIMPLE_PB)+sizeof(counter_t)];
2022-11-23 07:39:37 +01:00
while(has_next(fp, ch)) {
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
2022-11-23 07:45:39 +01:00
counter_t *d = (counter_t *)spb->target;
2022-11-23 07:39:37 +01:00
if (strcmp(name, d->name)) continue;
if(del_user(fp, spb)) {
http_error(HTTP500, "Unable to Delete Old Data.");
return;
}
if (add_user(d->name, d->count + 1, fp)) {
http_error(HTTP500, "Add User Error.");
return;
2021-06-07 15:18:19 +02:00
}
2021-09-11 18:19:08 +02:00
fclose(fp);
2022-11-23 07:39:37 +01:00
char cntstrbuf[11];
sprintf(cntstrbuf, "%010u", d->count);
char* cntstr = cntstrbuf;
for(int i = 0; i < 10; i++) if(cntstrbuf[i] != '0') {
if(i > 2) cntstr = cntstrbuf+i-2;
break;
}
int isbig = 0;
char** theme_type = mb;
uint16_t* len_type = mbl;
if(theme) {
set_type("mbh", mbh, mbhl) else
set_type("r34", r34, r34l) else
set_type("gb", gb, gbl) else
set_type("gbh", gbh, gbhl)
isbig = (theme_type == gb || theme_type == gbh);
}
int w, h;
char *head;
if(isbig) {
w = W_BIG;
h = H_BIG;
head = svg_big;
}
else {
w = W_SMALL;
h = H_SMALL;
head = svg_small;
}
headers(get_content_len(isbig, len_type, cntstr), image/svg+xml);
printf(head, w*(10+cntstrbuf-cntstr));
for(int i = 0; cntstr[i]; i++) {
printf(img_slot_front, w * i, w, h);
int n = cntstr[i] - '0';
fwrite(theme_type[n], len_type[n], 1, stdout);
printf(img_slot_rear);
}
printf(svg_tail);
return;
}
fclose(fp);
http_error(HTTP404, "No Such User.");
2021-06-07 15:18:19 +02:00
}
static int name_exist(char* name) {
2022-11-23 08:22:53 +01:00
FILE* fp = fopen(datfile, "rb+");
if(!fp) fp = fopen(datfile, "wb+");
2021-06-07 15:18:19 +02:00
if(fp) {
2022-11-23 07:39:37 +01:00
http_error(HTTP500, "Open File Error.");
exit(EXIT_FAILURE);
}
int ch, exist = 0;
2022-11-23 07:45:39 +01:00
char buf[sizeof(SIMPLE_PB)+sizeof(counter_t)];
2022-11-23 07:39:37 +01:00
flock(fileno(fp), LOCK_EX);
while(has_next(fp, ch)) {
SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
2022-11-23 07:45:39 +01:00
counter_t *d = (counter_t *)spb->target;
2022-11-23 07:39:37 +01:00
if (!strcmp(name, d->name)) {
fclose(fp);
return 1;
2021-06-07 15:18:19 +02:00
}
2022-11-23 07:39:37 +01:00
}
fclose(fp);
return 0;
2021-06-07 15:18:19 +02:00
}
#define QS (argv[2])
2022-03-02 10:18:50 +01:00
// Usage: cmoe method query_string
2021-06-07 15:18:19 +02:00
int main(int argc, char **argv) {
2022-11-23 07:39:37 +01:00
if(argc != 3) {
http_error(HTTP500, "Argument Count Error.");
return 1;
}
char* str = getenv("DATFILE");
2022-11-23 08:22:53 +01:00
if(str != NULL) datfile = str;
2022-11-23 07:39:37 +01:00
str = getenv("TOKEN");
2022-11-23 08:22:53 +01:00
if(str != NULL) token = str;
2022-11-23 07:39:37 +01:00
char* name = strstr(QS, "name=");
2022-11-23 07:45:39 +01:00
items_len = align_struct(sizeof(counter_t), 2, &counter.name, &counter.count);
2022-11-23 07:39:37 +01:00
if(!items_len) {
http_error(HTTP500, "Align Struct Error.");
return 2;
}
if(!name) {
http_error(HTTP400, "Name Argument Notfound.");
return 3;
}
name = get_arg(name + 5);
if(!name) {
http_error(HTTP400, "Null Name Argument.");
return 4;
}
char* theme = strstr(QS, "theme=");
if(theme) {
theme = get_arg(theme + 6);
}
char* reg = strstr(QS, "reg=");
if (!reg) {
return_count(name, theme);
return 0;
}
reg = get_arg(reg + 4);
if (!reg) {
http_error(HTTP400, "Null Register Token.");
return 5;
}
2022-11-23 08:22:53 +01:00
if(strcmp(reg, token)) {
2022-11-23 07:39:37 +01:00
http_error(HTTP400, "Token Error.");
return 6;
}
if(name_exist(name)) {
http_error(HTTP400, "Name Exist.");
return 7;
}
2022-11-23 08:22:53 +01:00
FILE* fp = fopen(datfile, "ab+");
2022-11-23 07:39:37 +01:00
if (!fp) {
http_error(HTTP500, "Open File Error.");
return 8;
}
flock(fileno(fp), LOCK_EX);
add_user(name, 0, fp);
fclose(fp);
char* msg = "<P>Success.\r\n";
headers(strlen(msg), text/html);
return write(1, msg, strlen(msg)) <= 0;
2021-06-07 15:18:19 +02:00
}