-static void exp_image(int attribute((unused)) nargs,
- char **args,
- cgi_sink *output,
- void attribute((unused)) *u) {
- char *labelname;
- const char *imagestem;
-
- byte_xasprintf(&labelname, "images.%s", args[0]);
- if(cgi_label_exists(labelname))
- imagestem = cgi_label(labelname);
- else if(strchr(args[0], '.'))
- imagestem = args[0];
- else
- byte_xasprintf((char **)&imagestem, "%s.png", args[0]);
- if(cgi_label_exists("url.static"))
- cgi_output(output, "%s/%s", cgi_label("url.static"), imagestem);
- else
- cgi_output(output, "/disorder/%s", imagestem);
-}
-
-static const struct cgi_expansion expansions[] = {
- { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
- { "action", 0, 0, 0, exp_action },
- { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
- { "arg", 1, 1, 0, exp_arg },
- { "basename", 0, 1, 0, exp_basename },
- { "choose", 2, 2, EXP_MAGIC, exp_choose },
- { "define", 3, 3, EXP_MAGIC, exp_define },
- { "dirname", 0, 1, 0, exp_dirname },
- { "enabled", 0, 0, 0, exp_enabled },
- { "eq", 2, 2, 0, exp_eq },
- { "file", 0, 0, 0, exp_file },
- { "files", 1, 1, EXP_MAGIC, exp_files },
- { "fullname", 0, 0, 0, exp_fullname },
- { "id", 0, 0, 0, exp_id },
- { "if", 2, 3, EXP_MAGIC, exp_if },
- { "image", 1, 1, 0, exp_image },
- { "include", 1, 1, 0, exp_include },
- { "index", 0, 0, 0, exp_index },
- { "isdirectories", 0, 0, 0, exp_isdirectories },
- { "isfiles", 0, 0, 0, exp_isfiles },
- { "isfirst", 0, 0, 0, exp_isfirst },
- { "islast", 0, 0, 0, exp_islast },
- { "isnew", 0, 0, 0, exp_isnew },
- { "isplaying", 0, 0, 0, exp_isplaying },
- { "isqueue", 0, 0, 0, exp_isqueue },
- { "isrecent", 0, 0, 0, exp_isrecent },
- { "label", 1, 1, 0, exp_label },
- { "length", 0, 0, 0, exp_length },
- { "movable", 0, 0, 0, exp_movable },
- { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
- { "ne", 2, 2, 0, exp_ne },
- { "new", 1, 1, EXP_MAGIC, exp_new },
- { "nfiles", 0, 0, 0, exp_nfiles },
- { "nonce", 0, 0, 0, exp_nonce },
- { "not", 1, 1, 0, exp_not },
- { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
- { "parity", 0, 0, 0, exp_parity },
- { "part", 1, 3, 0, exp_part },
- { "paused", 0, 0, 0, exp_paused },
- { "playing", 1, 1, EXP_MAGIC, exp_playing },
- { "pref", 2, 2, 0, exp_pref },
- { "prefname", 0, 0, 0, exp_prefname },
- { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
- { "prefvalue", 0, 0, 0, exp_prefvalue },
- { "queue", 1, 1, EXP_MAGIC, exp_queue },
- { "random-enabled", 0, 0, 0, exp_random_enabled },
- { "recent", 1, 1, EXP_MAGIC, exp_recent },
- { "removable", 0, 0, 0, exp_removable },
- { "resolve", 1, 1, 0, exp_resolve },
- { "right", 1, 3, EXP_MAGIC, exp_right },
- { "scratchable", 0, 0, 0, exp_scratchable },
- { "search", 2, 3, EXP_MAGIC, exp_search },
- { "server-version", 0, 0, 0, exp_server_version },
- { "shell", 1, 1, 0, exp_shell },
- { "state", 0, 0, 0, exp_state },
- { "stats", 0, 0, 0, exp_stats },
- { "thisurl", 0, 0, 0, exp_thisurl },
- { "track", 0, 0, 0, exp_track },
- { "trackstate", 1, 1, 0, exp_trackstate },
- { "transform", 2, 3, 0, exp_transform },
- { "url", 0, 0, 0, exp_url },
- { "urlquote", 1, 1, 0, exp_urlquote },
- { "user", 0, 0, 0, exp_user },
- { "userinfo", 1, 1, 0, exp_userinfo },
- { "version", 0, 0, 0, exp_version },
- { "volume", 1, 1, 0, exp_volume },
- { "when", 0, 0, 0, exp_when },
- { "who", 0, 0, 0, exp_who }
-};
-
-static void expand(cgi_sink *output,
- const char *template,
- dcgi_state *ds) {
- cgi_expand(template,
- expansions, sizeof expansions / sizeof *expansions,
- output,
- ds);
-}
-
-static void expandstring(cgi_sink *output,
- const char *string,
- dcgi_state *ds) {
- cgi_expand_string("",
- string,
- expansions, sizeof expansions / sizeof *expansions,
- output,
- ds);
-}
-
-static void perform_action(cgi_sink *output, dcgi_state *ds,
- const char *action) {
- int n;
-
- /* We don't ever want anything to be cached */
- cgi_header(output->sink, "Cache-Control", "no-cache");
- if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
- actions[n].handler(output, ds);
- else
- expand_template(ds, output, action);
-}
-
-void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
- const char *action = cgi_get("action");
-
- if(!action) {
- /* We allow URLs which are just confirm=... in order to keep confirmation
- * URLs, which are user-facing, as short as possible. */
- if(cgi_get("c"))
- action = "confirm";
- else
- action = "playing";
- }
- perform_action(output, ds, action);
-}
-
-void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
- const char *msg) {
- cgi_set_option("error", msg);
- perform_action(output, ds, "error");
-}
-
-/** @brief Log in as the current user or guest if none */
-void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
- /* Create a new connection */
- ds->g->client = disorder_new(0);
- /* Forget everything we knew */
- ds->g->flags = 0;
- /* Reconnect */
- if(disorder_connect_cookie(ds->g->client, login_cookie)) {
- disorder_cgi_error(output, ds, "connect");
- exit(0);
- }
- /* If there was a cookie but it went bad, we forget it */
- if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))
- login_cookie = 0;
-}
-