2021-06-13 17:22:58 +00:00
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
#include <fcall.h>
|
|
|
|
#include <thread.h>
|
|
|
|
#include <9p.h>
|
|
|
|
#include <stdio.h>
|
2021-06-24 07:17:34 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <libString.h>
|
2021-07-13 07:34:34 +00:00
|
|
|
#include <string.h>
|
2021-07-05 05:44:58 +00:00
|
|
|
#include "err.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "aux.h"
|
|
|
|
#include "cart.h"
|
|
|
|
#include "universe.h"
|
|
|
|
#include "realm.h"
|
|
|
|
#include "user.h"
|
2021-06-24 07:17:34 +00:00
|
|
|
|
2021-07-13 07:34:34 +00:00
|
|
|
#define CARTSPATH "./carts"
|
|
|
|
#define REALMSPATH "./realms"
|
|
|
|
|
2021-06-28 21:37:59 +00:00
|
|
|
int chatty9p = 1;
|
2021-06-13 17:22:58 +00:00
|
|
|
|
2021-07-02 01:59:56 +00:00
|
|
|
static Tree* tree;
|
|
|
|
|
2021-07-01 07:45:48 +00:00
|
|
|
static UserInfo users_table[64];
|
|
|
|
|
|
|
|
void xrxs_attach(Req* r) {
|
2021-07-02 01:59:56 +00:00
|
|
|
/* As it is, once the user detaches, they will stay in the table
|
|
|
|
* until the server restarts. We have to figure out some way
|
|
|
|
* to detect a detach to remove them... You can't delay respond()
|
2021-07-08 05:37:18 +00:00
|
|
|
* or the attach will never complete. We have logout() for now...
|
2021-07-02 01:59:56 +00:00
|
|
|
*/
|
2021-07-01 07:45:48 +00:00
|
|
|
int i = 0;
|
2021-07-05 05:44:58 +00:00
|
|
|
int vacancy = 0;
|
2021-07-01 07:45:48 +00:00
|
|
|
char* usr;
|
|
|
|
char* username = r->ifcall.uname;
|
2021-07-21 06:27:40 +00:00
|
|
|
|
2021-07-01 07:45:48 +00:00
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
usr = users_table[i].name;
|
2021-07-02 21:59:40 +00:00
|
|
|
if (scmp(usr, username)) {
|
2021-07-05 05:44:58 +00:00
|
|
|
respond(r, EUNAME);
|
2021-07-01 07:45:48 +00:00
|
|
|
return;
|
2021-07-02 21:59:40 +00:00
|
|
|
}
|
2021-07-01 07:45:48 +00:00
|
|
|
if (*usr == 0) {
|
2021-07-21 06:27:40 +00:00
|
|
|
scpy(username, usr, 32);
|
2021-07-05 05:44:58 +00:00
|
|
|
vacancy = 1;
|
2021-07-01 07:45:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 05:44:58 +00:00
|
|
|
if (vacancy) {
|
|
|
|
respond(r, nil);
|
|
|
|
} else {
|
|
|
|
respond(r, EUFULL);
|
|
|
|
}
|
2021-07-01 07:45:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 06:52:57 +00:00
|
|
|
void write_ctl(Req* r) {
|
2021-07-02 21:59:40 +00:00
|
|
|
char cmd[16] = {0};
|
2021-06-28 21:37:59 +00:00
|
|
|
char* c = r->ifcall.data;
|
2021-06-29 18:56:21 +00:00
|
|
|
int i;
|
2021-06-28 21:37:59 +00:00
|
|
|
|
2021-07-02 21:59:40 +00:00
|
|
|
for (i = 0; i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) {
|
2021-06-29 18:56:21 +00:00
|
|
|
ccat(cmd, *c++);
|
2021-06-28 21:37:59 +00:00
|
|
|
}
|
2021-07-13 07:34:34 +00:00
|
|
|
if (*c == ' ')
|
|
|
|
c++;
|
2021-07-19 04:58:08 +00:00
|
|
|
scsw(c, '\n', 0);
|
2021-07-13 07:34:34 +00:00
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
fprintf(stderr, "%s(%s)\n", cmd, c);
|
2021-07-08 05:37:18 +00:00
|
|
|
uvlong const cmd_hashv = hash(cmd, 0);
|
2021-06-28 21:37:59 +00:00
|
|
|
switch (cmd_hashv) {
|
2021-07-02 21:59:40 +00:00
|
|
|
case LOGIN:
|
2021-07-21 06:27:40 +00:00
|
|
|
login(users_table, r->fid->uid, c);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
case LOAD:
|
2021-07-13 07:34:34 +00:00
|
|
|
load_cart(users_table, r->fid->uid, c);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
2021-07-20 21:59:33 +00:00
|
|
|
case CHUNK:
|
2021-07-21 06:27:40 +00:00
|
|
|
get_chunk(users_table, r->fid->uid, c);
|
2021-07-20 21:59:33 +00:00
|
|
|
break;
|
2021-07-02 21:59:40 +00:00
|
|
|
case CREATE:
|
2021-07-19 04:58:08 +00:00
|
|
|
create_realm(users_table, r->fid->uid, c);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
case PROTECT:
|
2021-07-21 06:27:40 +00:00
|
|
|
protect_realm(users_table, r->fid->uid, c);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
2021-07-20 21:59:33 +00:00
|
|
|
case TRANSFER:
|
2021-07-21 06:27:40 +00:00
|
|
|
transfer_realm(users_table, r->fid->uid, c);
|
|
|
|
break;
|
2021-07-02 21:59:40 +00:00
|
|
|
case ENTER:
|
2021-07-13 07:34:34 +00:00
|
|
|
enter_realm(users_table, r->fid->uid, c);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
case LEAVE:
|
2021-07-13 07:34:34 +00:00
|
|
|
leave_realm(users_table, r->fid->uid);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
case LOGOUT:
|
2021-07-21 06:27:40 +00:00
|
|
|
logout(users_table, r->fid->uid);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
case SAVE:
|
2021-07-03 04:32:10 +00:00
|
|
|
// save(r->fid->uid);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
case RESET:
|
2021-07-03 04:32:10 +00:00
|
|
|
// reset(r->fid->uid);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
case UNLOAD:
|
2021-07-13 07:34:34 +00:00
|
|
|
unload_cart(users_table, r->fid->uid);
|
2021-07-02 21:59:40 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-29 18:56:21 +00:00
|
|
|
r->ofcall.count = r->ifcall.count;
|
|
|
|
r->fid->file->dir.length = r->ifcall.count;
|
2021-06-30 06:52:57 +00:00
|
|
|
respond(r, nil);
|
2021-06-28 21:37:59 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:34:34 +00:00
|
|
|
void write_universe(Req* r) {
|
|
|
|
char key[16] = {0};
|
|
|
|
char value[64] = {0};
|
|
|
|
char* c = r->ifcall.data;
|
|
|
|
UserInfo* u = find_user(users_table, r->fid->uid);
|
|
|
|
Atom* a;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 15 && i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) {
|
|
|
|
ccat(key, *c++);
|
|
|
|
}
|
|
|
|
c++;
|
2021-07-23 06:27:07 +00:00
|
|
|
c++;
|
2021-07-13 07:34:34 +00:00
|
|
|
for (i = 0; i < 63 && i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) {
|
|
|
|
ccat(value, *c++);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u != nil && u->realm != nil && u->realm->universe != nil) {
|
|
|
|
a = get_atom(u->realm->universe, key);
|
|
|
|
if (a != nil) {
|
|
|
|
scpy(value, a->value, 64);
|
|
|
|
} else {
|
|
|
|
a = malloc(sizeof(Atom));
|
|
|
|
scpy(key, a->name, 16);
|
|
|
|
scpy(value, a->value, 64);
|
|
|
|
a->next = nil;
|
|
|
|
set_atom(u->realm->universe, a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r->ofcall.count = r->ifcall.count;
|
|
|
|
r->fid->file->dir.length = r->ifcall.count;
|
|
|
|
respond(r, nil);
|
|
|
|
}
|
|
|
|
|
2021-07-23 06:27:07 +00:00
|
|
|
void write_scope(Req* r) {
|
|
|
|
String* scope = s_new();
|
|
|
|
char* c = r->ifcall.data;
|
|
|
|
UserInfo* u = find_user(users_table, r->fid->uid);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < r->ifcall.count; i++) {
|
|
|
|
s_putc(scope, *c++);
|
|
|
|
}
|
|
|
|
s_terminate(scope);
|
|
|
|
|
|
|
|
if (u != nil && u->realm != nil && u->realm->universe != nil) {
|
|
|
|
u->scope = malloc(r->ifcall.count);
|
|
|
|
memcpy(u->scope, scope->base, r->ifcall.count);
|
|
|
|
}
|
|
|
|
s_free(scope);
|
|
|
|
r->ofcall.count = r->ifcall.count;
|
|
|
|
r->fid->file->dir.length = r->ifcall.count;
|
|
|
|
respond(r, nil);
|
|
|
|
}
|
|
|
|
|
2021-06-30 06:52:57 +00:00
|
|
|
void xrxs_write(Req* r) {
|
2021-06-29 18:56:21 +00:00
|
|
|
Aux* a = r->fid->file->aux;
|
|
|
|
switch (a->type) {
|
|
|
|
case CTL:
|
2021-06-30 06:52:57 +00:00
|
|
|
write_ctl(r);
|
|
|
|
break;
|
|
|
|
case UNIVERSE:
|
2021-07-13 07:34:34 +00:00
|
|
|
write_universe(r);
|
2021-06-28 21:37:59 +00:00
|
|
|
break;
|
2021-07-19 21:28:37 +00:00
|
|
|
case SCOPE:
|
2021-07-23 06:27:07 +00:00
|
|
|
write_scope(r);
|
2021-06-28 21:37:59 +00:00
|
|
|
default:
|
2021-06-30 06:52:57 +00:00
|
|
|
respond(r, nil);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 07:45:48 +00:00
|
|
|
void read_users(Req* r) {
|
2021-07-02 21:59:40 +00:00
|
|
|
char buf[2113] = {0};
|
2021-07-01 07:45:48 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 64; i++) {
|
2021-07-02 21:59:40 +00:00
|
|
|
if (scmp(users_table[i].name, r->fid->uid)) {
|
|
|
|
scat(buf, users_table[i].name);
|
|
|
|
ccat(buf, '\n');
|
2021-07-01 07:45:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-07-02 21:59:40 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if (
|
|
|
|
scmp(users_table[i].name, "\0") ||
|
|
|
|
scmp(users_table[i].name, r->fid->uid)) {
|
|
|
|
continue;
|
2021-07-01 07:45:48 +00:00
|
|
|
}
|
2021-07-02 21:59:40 +00:00
|
|
|
scat(buf, users_table[i].name);
|
|
|
|
ccat(buf, '\n');
|
2021-07-01 07:45:48 +00:00
|
|
|
}
|
2021-07-02 21:59:40 +00:00
|
|
|
ccat(buf, 0);
|
2021-07-01 07:45:48 +00:00
|
|
|
readstr(r, buf);
|
|
|
|
respond(r, nil);
|
|
|
|
}
|
|
|
|
|
2021-07-13 07:34:34 +00:00
|
|
|
String** list_dir(char* path) {
|
2021-07-20 21:59:33 +00:00
|
|
|
int size = 128;
|
|
|
|
String** self = malloc(size * sizeof(String*));
|
2021-07-13 07:34:34 +00:00
|
|
|
DIR* dir;
|
|
|
|
struct dirent* ent;
|
|
|
|
int i = 0;
|
|
|
|
char* c;
|
|
|
|
if ((dir = opendir(path)) != NULL) {
|
|
|
|
while ((ent = readdir(dir)) != NULL) {
|
2021-07-20 21:59:33 +00:00
|
|
|
if (i = size) {
|
|
|
|
size *= 2;
|
2021-07-21 06:27:40 +00:00
|
|
|
self = realloc(self, size * sizeof(String*));
|
2021-07-20 21:59:33 +00:00
|
|
|
}
|
2021-07-13 07:34:34 +00:00
|
|
|
c = ent->d_name;
|
|
|
|
if (scmp(c, ".") || scmp(c, "..")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
self[i] = s_new();
|
|
|
|
|
|
|
|
while (*c) {
|
|
|
|
s_putc(self[i], *c++);
|
|
|
|
}
|
|
|
|
s_terminate(self[i++]);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
self[i] = nil;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-07-20 21:59:33 +00:00
|
|
|
void s_freemany(String** ss) {
|
|
|
|
int i;
|
|
|
|
String** s = ss;
|
|
|
|
for (i = 0; i < 128; i++) {
|
|
|
|
if (*s != nil) {
|
|
|
|
s_free(*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(ss);
|
|
|
|
}
|
|
|
|
|
2021-07-13 07:34:34 +00:00
|
|
|
void read_carts(Req* r) {
|
|
|
|
String** carts = list_dir(CARTSPATH);
|
|
|
|
String** c = carts;
|
2021-07-21 06:27:40 +00:00
|
|
|
String* data = s_new();
|
2021-07-13 07:34:34 +00:00
|
|
|
while (*c != nil) {
|
2021-07-21 06:27:40 +00:00
|
|
|
s_append(data, (*c)->base);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_putc(data, '\n');
|
2021-07-13 07:34:34 +00:00
|
|
|
c++;
|
|
|
|
}
|
2021-07-20 21:59:33 +00:00
|
|
|
s_terminate(data);
|
|
|
|
readstr(r, data->base);
|
2021-07-13 07:34:34 +00:00
|
|
|
respond(r, nil);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_free(data);
|
|
|
|
s_freemany(carts);
|
2021-07-13 07:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void read_slot(Req* r) {
|
|
|
|
UserInfo* u = find_user(users_table, r->fid->uid);
|
|
|
|
if (u == nil)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (u->cart == nil)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
readstr(r, u->cart->rom);
|
|
|
|
end:
|
|
|
|
respond(r, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_data(Req* r, FileType t) {
|
|
|
|
UserInfo* u = find_user(users_table, r->fid->uid);
|
|
|
|
|
|
|
|
if (u->cart == nil)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case SPRITE_DATA:
|
|
|
|
readstr(r, u->cart->sprite_data);
|
|
|
|
break;
|
|
|
|
case AUDIO_DATA:
|
|
|
|
readstr(r, u->cart->audio_data);
|
|
|
|
break;
|
|
|
|
case TEXT_DATA:
|
|
|
|
readstr(r, u->cart->txt_data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
respond(r, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_realms(Req* r) {
|
2021-07-19 04:58:08 +00:00
|
|
|
UserInfo* user = find_user(users_table, r->fid->uid);
|
|
|
|
char realm_path[128] = {0};
|
|
|
|
String** realms;
|
|
|
|
String** rr;
|
2021-07-13 07:34:34 +00:00
|
|
|
Realm* realm;
|
2021-07-20 21:59:33 +00:00
|
|
|
String* data = s_new();
|
2021-07-13 07:34:34 +00:00
|
|
|
int i, u, m, p;
|
|
|
|
char ubuf[8] = {0};
|
|
|
|
char mbuf[8] = {0};
|
|
|
|
char pbuf[2] = {0};
|
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
if (user->cart == nil)
|
|
|
|
respond(r, nil);
|
|
|
|
|
|
|
|
scat(realm_path, "carts/");
|
|
|
|
scat(realm_path, user->cart->name);
|
|
|
|
scat(realm_path, "/realms");
|
|
|
|
|
|
|
|
realms = list_dir(realm_path);
|
|
|
|
rr = realms;
|
|
|
|
|
2021-07-13 07:34:34 +00:00
|
|
|
while (*rr != nil) {
|
2021-07-21 06:27:40 +00:00
|
|
|
s_append(data, (*rr)->base);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_putc(data, ' ');
|
2021-07-13 07:34:34 +00:00
|
|
|
|
2021-07-19 04:58:08 +00:00
|
|
|
realm = parse_realm(user->cart->name, (*rr)->base);
|
2021-07-13 07:34:34 +00:00
|
|
|
m = realm->max;
|
|
|
|
p = realm->password ? 1 : 0;
|
|
|
|
|
|
|
|
for (i = u = 0; i < 64; i++) {
|
|
|
|
if (
|
|
|
|
users_table[i].realm != nil &&
|
|
|
|
scmp(users_table[i].realm->name, realm->name))
|
|
|
|
u++;
|
|
|
|
}
|
|
|
|
itoa(u, ubuf, 10);
|
2021-07-21 06:27:40 +00:00
|
|
|
s_append(data, ubuf);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_putc(data, ' ');
|
2021-07-13 07:34:34 +00:00
|
|
|
itoa(m, mbuf, 10);
|
2021-07-21 06:27:40 +00:00
|
|
|
s_append(data, mbuf);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_putc(data, ' ');
|
2021-07-13 07:34:34 +00:00
|
|
|
itoa(p, pbuf, 10);
|
2021-07-21 06:27:40 +00:00
|
|
|
s_append(data, pbuf);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_putc(data, '\n');
|
2021-07-13 07:34:34 +00:00
|
|
|
rr++;
|
|
|
|
}
|
2021-07-20 21:59:33 +00:00
|
|
|
s_terminate(data);
|
|
|
|
readstr(r, data->base);
|
|
|
|
respond(r, nil);
|
|
|
|
s_free(data);
|
|
|
|
s_freemany(realms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_universe(Req* r) {
|
|
|
|
char* uname = r->fid->uid;
|
|
|
|
UserInfo* u = find_user(users_table, uname);
|
|
|
|
String* data = s_new();
|
|
|
|
Universe* universe;
|
|
|
|
Atom* a;
|
|
|
|
int i;
|
2021-07-21 06:27:40 +00:00
|
|
|
|
2021-07-20 21:59:33 +00:00
|
|
|
if (u == nil || u->realm == nil) {
|
|
|
|
respond(r, nil);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 06:27:40 +00:00
|
|
|
|
2021-07-20 21:59:33 +00:00
|
|
|
universe = u->realm->universe;
|
2021-07-23 06:27:07 +00:00
|
|
|
if (universe == nil) {
|
|
|
|
respond(r, ENOUNI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-20 21:59:33 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
2021-07-21 06:27:40 +00:00
|
|
|
a = universe->atoms[i];
|
2021-07-20 21:59:33 +00:00
|
|
|
while (a != nil) {
|
2021-07-21 06:27:40 +00:00
|
|
|
s_append(data, a->name);
|
|
|
|
s_append(data, " = ");
|
|
|
|
s_append(data, a->value);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_putc(data, '\n');
|
|
|
|
a = a->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s_terminate(data);
|
|
|
|
readstr(r, data->base);
|
2021-07-13 07:34:34 +00:00
|
|
|
respond(r, nil);
|
2021-07-20 21:59:33 +00:00
|
|
|
s_free(data);
|
2021-07-13 07:34:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 06:27:07 +00:00
|
|
|
void read_scope(Req* r) {
|
|
|
|
char* uname = r->fid->uid;
|
|
|
|
UserInfo* u = find_user(users_table, uname);
|
|
|
|
String* data = s_new();
|
|
|
|
Universe* universe;
|
|
|
|
Atom* a;
|
|
|
|
char key[16] = {0};
|
|
|
|
char* c = u->scope;
|
|
|
|
|
|
|
|
if (u == nil || u->scope == nil || u->realm == nil) {
|
|
|
|
respond(r, nil);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
universe = u->realm->universe;
|
|
|
|
if (universe == nil) {
|
|
|
|
respond(r, ENOUNI);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*c) {
|
|
|
|
while (*c != '\n') {
|
|
|
|
ccat(key, *c++);
|
|
|
|
}
|
|
|
|
a = get_atom(universe, key);
|
|
|
|
if (a != nil) {
|
|
|
|
s_append(data, a->value);
|
|
|
|
s_putc(data, '\n');
|
|
|
|
}
|
|
|
|
*key = '\0';
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_terminate(data);
|
|
|
|
readstr(r, data->base);
|
|
|
|
respond(r, nil);
|
|
|
|
s_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_random(Req* r) {
|
|
|
|
char buf[8] = {0};
|
|
|
|
srand(rand());
|
|
|
|
sprintf(buf, "%d\n", rand() % 100);
|
|
|
|
readstr(r, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_grandom(Req* r) {
|
|
|
|
char buf[8] = {0};
|
|
|
|
int i;
|
|
|
|
int reset = 0;
|
|
|
|
int random;
|
|
|
|
UserInfo* u = find_user(users_table, r->fid->uid);
|
|
|
|
UserInfo** usrs = malloc(64 * sizeof(UserInfo*));
|
|
|
|
UserInfo* p = users_table;
|
|
|
|
UserInfo** uu = usrs;
|
|
|
|
|
|
|
|
if (u->realm == nil)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if (scmp(p->realm->name, u->realm->name)) {
|
|
|
|
*uu++ = p;
|
|
|
|
if (i < 64)
|
|
|
|
*uu = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uu = usrs;
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if ((*uu) != nil && (*uu)->random >= 0) {
|
|
|
|
reset = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (reset) {
|
|
|
|
srand(rand());
|
|
|
|
random = rand() % 100;
|
|
|
|
uu = usrs;
|
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
if ((*uu) != nil) {
|
|
|
|
(*uu)->random = random;
|
|
|
|
}
|
|
|
|
uu++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sprintf(buf, "%d\n", u->random);
|
|
|
|
u->random = -1;
|
|
|
|
readstr(r, buf);
|
|
|
|
}
|
|
|
|
|
2021-06-30 06:52:57 +00:00
|
|
|
void xrxs_read(Req* r) {
|
|
|
|
Aux* a = r->fid->file->aux;
|
|
|
|
switch (a->type) {
|
|
|
|
case USERS:
|
2021-07-01 07:45:48 +00:00
|
|
|
read_users(r);
|
2021-06-30 06:52:57 +00:00
|
|
|
break;
|
|
|
|
case CARTS:
|
2021-07-13 07:34:34 +00:00
|
|
|
read_carts(r);
|
2021-06-30 06:52:57 +00:00
|
|
|
break;
|
|
|
|
case SLOT:
|
2021-07-13 07:34:34 +00:00
|
|
|
read_slot(r);
|
2021-06-30 06:52:57 +00:00
|
|
|
break;
|
2021-07-13 07:34:34 +00:00
|
|
|
case SPRITE_DATA:
|
|
|
|
case AUDIO_DATA:
|
|
|
|
case TEXT_DATA:
|
|
|
|
read_data(r, a->type);
|
2021-06-30 06:52:57 +00:00
|
|
|
break;
|
|
|
|
case REALMS:
|
2021-07-13 07:34:34 +00:00
|
|
|
read_realms(r);
|
2021-06-30 06:52:57 +00:00
|
|
|
break;
|
|
|
|
case UNIVERSE:
|
2021-07-20 21:59:33 +00:00
|
|
|
read_universe(r);
|
2021-06-30 06:52:57 +00:00
|
|
|
break;
|
2021-07-19 21:28:37 +00:00
|
|
|
case SCOPE:
|
2021-07-23 06:27:07 +00:00
|
|
|
read_scope(r);
|
2021-07-19 21:28:37 +00:00
|
|
|
break;
|
|
|
|
case RANDOM:
|
2021-07-23 06:27:07 +00:00
|
|
|
read_random(r);
|
2021-07-19 21:28:37 +00:00
|
|
|
break;
|
|
|
|
case GRANDOM:
|
2021-07-23 06:27:07 +00:00
|
|
|
read_grandom(r);
|
2021-07-19 21:28:37 +00:00
|
|
|
break;
|
2021-06-30 06:52:57 +00:00
|
|
|
default:
|
|
|
|
respond(r, nil);
|
2021-06-28 21:37:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 06:52:57 +00:00
|
|
|
void fs_destroy_file(File* f) {
|
2021-06-29 18:56:21 +00:00
|
|
|
Aux* a = f->aux;
|
|
|
|
if (a && a->data) {
|
|
|
|
free(a->data);
|
|
|
|
free(a);
|
|
|
|
} else if (a) {
|
|
|
|
free(a);
|
2021-06-16 05:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 21:59:40 +00:00
|
|
|
Srv fs = {.attach = xrxs_attach, .read = xrxs_read, .write = xrxs_write};
|
2021-06-13 17:22:58 +00:00
|
|
|
|
2021-06-23 06:48:23 +00:00
|
|
|
int threadmaybackground(void) { return 1; }
|
2021-06-16 05:46:47 +00:00
|
|
|
|
2021-06-14 20:23:06 +00:00
|
|
|
void threadmain(int argc, char* argv[]) {
|
|
|
|
char* mtpt = nil;
|
|
|
|
char* usocket = nil;
|
|
|
|
int i;
|
|
|
|
|
2021-07-02 21:59:40 +00:00
|
|
|
/* if -h CMD is given, print the hash value of CMD */
|
|
|
|
if (argc == 3 && scmp(argv[1], "-h")) {
|
2021-07-08 05:37:18 +00:00
|
|
|
printf("%llu\n", hash(argv[2], 0));
|
2021-07-02 21:59:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:23:06 +00:00
|
|
|
/* if -m PATH is supplied, mount on PATH */
|
|
|
|
/* if -s NAME is supplied, create a socket for the namespace */
|
|
|
|
/* otherwise, just use srv() (for wrapping with socat or inetd) */
|
2021-06-13 17:22:58 +00:00
|
|
|
|
2021-06-14 20:23:06 +00:00
|
|
|
if (argc >= 3) {
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (scmp(argv[i], "-m")) {
|
|
|
|
mtpt = argv[++i];
|
|
|
|
printf("serving on %s", mtpt);
|
|
|
|
} else if (scmp(argv[i], "-s")) {
|
|
|
|
usocket = argv[++i];
|
|
|
|
printf("serving socket namespace %s", usocket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:56:21 +00:00
|
|
|
fs.foreground = 1;
|
2021-06-30 06:52:57 +00:00
|
|
|
fs.tree = alloctree(nil, nil, DMDIR | 0777, fs_destroy_file);
|
2021-06-23 06:48:23 +00:00
|
|
|
tree = fs.tree;
|
2021-07-02 21:59:40 +00:00
|
|
|
|
2021-07-01 07:45:48 +00:00
|
|
|
closefile(
|
|
|
|
createfile(tree->root, "ctl", nil, DMAPPEND | 0300, create_aux(CTL)));
|
2021-07-02 21:59:40 +00:00
|
|
|
closefile(createfile(tree->root, "carts", nil, 0400, create_aux(CARTS)));
|
|
|
|
closefile(createfile(tree->root, "users", nil, 0400, create_aux(USERS)));
|
|
|
|
closefile(createfile(tree->root, "slot", nil, 0400, create_aux(SLOT)));
|
|
|
|
closefile(createfile(tree->root, "data", nil, DMDIR | 0500, nil));
|
2021-07-13 07:34:34 +00:00
|
|
|
closefile(createfile(
|
|
|
|
walkfile(tree->root, "data"),
|
|
|
|
"sprite",
|
|
|
|
nil,
|
|
|
|
0400,
|
|
|
|
create_aux(SPRITE_DATA)));
|
|
|
|
closefile(createfile(
|
|
|
|
walkfile(tree->root, "data"),
|
|
|
|
"audio",
|
|
|
|
nil,
|
|
|
|
0400,
|
|
|
|
create_aux(AUDIO_DATA)));
|
|
|
|
closefile(createfile(
|
|
|
|
walkfile(tree->root, "data"),
|
|
|
|
"text",
|
|
|
|
nil,
|
|
|
|
0400,
|
|
|
|
create_aux(TEXT_DATA)));
|
2021-07-02 21:59:40 +00:00
|
|
|
closefile(createfile(tree->root, "realms", nil, 0400, create_aux(REALMS)));
|
2021-07-13 07:34:34 +00:00
|
|
|
closefile(createfile(
|
|
|
|
tree->root,
|
|
|
|
"universe",
|
|
|
|
nil,
|
|
|
|
DMAPPEND | 0600,
|
|
|
|
create_aux(UNIVERSE)));
|
2021-07-19 21:28:37 +00:00
|
|
|
closefile(
|
|
|
|
createfile(tree->root, "scope", nil, DMAPPEND | 0600, create_aux(SCOPE)));
|
|
|
|
closefile(createfile(tree->root, "random", nil, 0400, create_aux(RANDOM)));
|
|
|
|
closefile(createfile(tree->root, "grandom", nil, 0400, create_aux(GRANDOM)));
|
2021-06-30 06:52:57 +00:00
|
|
|
|
2021-07-01 07:45:48 +00:00
|
|
|
if (argc >= 3) {
|
|
|
|
if (mtpt != nil && access(mtpt, AEXIST) < 0 && access(mtpt, AEXIST) < 0)
|
|
|
|
sysfatal("mountpoint %s does not exist", mtpt);
|
2021-06-13 17:22:58 +00:00
|
|
|
|
2021-07-01 07:45:48 +00:00
|
|
|
threadpostmountsrv(&fs, usocket, mtpt, MREPL | MCREATE);
|
|
|
|
threadexits(0);
|
|
|
|
} else {
|
|
|
|
srv(&fs);
|
|
|
|
}
|
2021-06-14 20:23:06 +00:00
|
|
|
}
|