Example #1
0
static enum v7_err jsBotSay(struct v7 *v7, v7_val_t *res) {
	long argc = v7_argc(v7);
	if (argc == 0) {
		return V7_OK;
	}
	v7_val_t v = v7_arg(v7, 0);
	if (!v7_is_string(v)) {
		return V7_OK;
	}
	TString str = (const char*)v7_to_cstring(v7, &v);
	TString font;
	if (argc >= 2) {
		v7_val_t fontt = v7_arg(v7, 1);
		if (!v7_is_null(fontt) && !v7_is_undefined(fontt)) font = (const char*)v7_to_cstring(v7, &fontt);
	}
	int size = 0;
	if (argc >= 3) {
		v7_val_t sizee = v7_arg(v7, 2);
		if (v7_is_number(sizee)) size = floor(v7_to_number(sizee));
		if (size < 10) size = 10;
	}
	TString col;
	if (argc >= 4) {
		v7_val_t coll = v7_arg(v7, 3);
		if (v7_is_string(coll)) col = (const char*)v7_to_cstring(v7, &coll);
		if (col.GetAt(0) != L'#' || col.GetLength() != 7) col = L"";
	}
	_activeBot->say(str, font, size, col);
	return V7_OK;
}
Example #2
0
/*
 * Serve static files.
 *
 * Takes an object containing mongoose http server options.
 * Commonly used properties:
 * - `document_root`: Path to the web root directory
 * - `enable_directory_listing`: Set to "no" to disable directory listing.
 *   Enabled by default.
 * - `extra_headers`: Extra HTTP headers to add to each server response.
 *
 * For the full option object definition see:
 * https://docs.cesanta.com/mongoose/dev/index.html#/c-api/http.h/struct_mg_serve_http_opts/
 */
SJ_PRIVATE enum v7_err Http_response_serve(struct v7 *v7, v7_val_t *res) {
  struct mg_serve_http_opts opts;
  struct http_message hm;
  enum v7_err rcode = V7_OK;
  DECLARE_CONN();

  size_t i, n;
  v7_val_t request = v7_get(v7, v7_get_this(v7), "_r", ~0);
  v7_val_t url_v = v7_get(v7, request, "url", ~0);
  const char *url = v7_get_string_data(v7, &url_v, &n);
  const char *quest = strchr(url, '?');

  memset(&opts, 0, sizeof(opts));
  memset(&hm, 0, sizeof(hm));

  /* Set up "fake" parsed HTTP message */
  hm.uri.p = url;
  hm.uri.len = quest == NULL ? n : n - (quest - url);

  if (v7_argc(v7) > 0) {
    populate_opts_from_js_argument(v7, v7_arg(v7, 0), &opts);
  }
  mg_serve_http(c, &hm, opts);
  for (i = 0; i < ARRAY_SIZE(s_map); i++) {
    free(*(char **) ((char *) &opts + s_map[i].offset));
  }

  *res = v7_get_this(v7);

clean:
  return rcode;
}
Example #3
0
/*
 * Prints message to current debug output
 */
v7_val_t Debug_print(struct v7 *v7) {
  int i, num_args = v7_argc(v7);

  for (i = 0; i < num_args; i++) {
    v7_fprint(stderr, v7, v7_arg(v7, i));
    fprintf(stderr, " ");
  }
  fprintf(stderr, "\n");

  return v7_create_undefined();
}
Example #4
0
/*
 * Prints message to current debug output
 */
enum v7_err Debug_print(struct v7 *v7, v7_val_t *res) {
  int i, num_args = v7_argc(v7);
  (void) res;

  for (i = 0; i < num_args; i++) {
    v7_fprint(stderr, v7, v7_arg(v7, i));
    fprintf(stderr, " ");
  }
  fprintf(stderr, "\n");

  return V7_OK;
}
Example #5
0
MG_PRIVATE enum v7_err Console_log(struct v7 *v7, v7_val_t *res) {
  int argc = v7_argc(v7);
  /* Put everything into one message */
  for (int i = 0; i < argc; i++) {
    v7_val_t arg = v7_arg(v7, i);
    if (v7_is_string(arg)) {
      size_t len;
      const char *str = v7_get_string(v7, &arg, &len);
      miot_console_puts_n(str, len);
    } else {
      char buf[100], *p;
      p = v7_stringify(v7, arg, buf, sizeof(buf), V7_STRINGIFY_DEBUG);
      miot_console_puts_n(p, strlen(p));
      if (p != buf) free(p);
    }
    if (i != argc - 1) {
      miot_console_putc(' ');
    }
  }
  miot_console_putc('\n');

  *res = V7_UNDEFINED; /* like JS print */
  return V7_OK;
}