Skip to content
Commits on Source (4)
......@@ -150,6 +150,7 @@ static int js_push_username(int32_t id)
static int js_push_ipcmsg_event(ipc_message_t *msg)
{
const char *str;
duk_idx_t idx;
json_t *json;
json_t *val;
......@@ -178,16 +179,18 @@ static int js_push_ipcmsg_event(ipc_message_t *msg)
js_push_username(msg->head.src);
duk_put_prop_string(ctx, idx, "from_name");
if (msg->head.type == IPC_SAYTOUSER) {
js_push_username(msg->head.dst);
duk_push_string(ctx, json_getstring(json, "target"));
duk_put_prop_string(ctx, idx, "to_name");
}
duk_push_string(ctx, json_getstring(json, "type"));
duk_put_prop_string(ctx, idx, "type");
duk_push_string(ctx, json_getstring(json, "text"));
duk_put_prop_string(ctx, idx, "text");
duk_push_string(ctx, json_getstring(json, "exclude"));
duk_put_prop_string(ctx, idx, "excluded_name");
str = json_getstring(json, "exclude");
if (str != NULL) {
duk_push_string(ctx, str);
duk_put_prop_string(ctx, idx, "excluded_name");
}
val = json_object_get(json, "plural");
if (val != NULL && json_is_integer(val)) {
unsigned p = json_integer_value(val);
......@@ -869,6 +872,7 @@ int stop_js(void)
return 0;
}
/* x = mw.store.foo; */
static duk_ret_t js_store_get(duk_context *cx)
{
const char *key = duk_get_string(cx, 1);
......@@ -880,6 +884,7 @@ static duk_ret_t js_store_get(duk_context *cx)
return 1;
}
/* mw.store.foo = "bar"; */
static duk_ret_t js_store_set(duk_context *cx)
{
const char *key = duk_get_string(cx, 1);
......@@ -888,9 +893,32 @@ static duk_ret_t js_store_set(duk_context *cx)
return 0;
}
/* delete mw.store.foo; */
static duk_ret_t js_store_delete(duk_context *cx)
{
const char *key = duk_get_string(cx, 1);
userdb_set(USERDB_PUBLIC, user->record.name, key, NULL);
return 0;
}
/* "foo" in mw.store */
static duk_ret_t js_store_has(duk_context *cx)
{
const char *key = duk_get_string(cx, 1);
int exists;
exists = userdb_key_exists(USERDB_PUBLIC, user->record.name, key);
duk_push_boolean(cx, exists);
return 1;
}
static const duk_function_list_entry store_handlers[] = {
{ "get", js_store_get, 3 },
{ "set", js_store_set, 4 },
{ "has", js_store_has, 2 },
{ "deleteProperty", js_store_delete, 2 },
{ NULL, NULL, 0 }
};
......
......@@ -157,6 +157,24 @@ out_dbfree:
return out;
}
int userdb_key_exists(const char *table, const char *user, const char *key)
{
struct db_result *result;
char *query;
int nrows;
query = sqlite3_mprintf("SELECT * from %Q WHERE user=%Q AND opt=%Q", table, user, key);
result = db_query(USERSQL, query, 1);
sqlite3_free(query);
if (result == NULL)
return 0;
nrows = db_numrows(result);
db_free(result);
if (nrows < 1)
return 0;
return 1;
}
void userdb_set(const char *table, const char *user, const char *opt, const char *arg)
{
struct db_result *old;
......
......@@ -4,6 +4,7 @@
extern struct js_db_result* js_db_query(char *dbname, const char *query);
extern void js_db_free(struct js_db_result *result);
extern char * userdb_get(const char *table, const char *user, const char *opt);
extern int userdb_key_exists(const char *table, const char *user, const char *key);
extern void userdb_set(const char *table, const char *user, const char *opt, const char *arg);
#endif /* CLIENT_SQLITE_H */