Esempio n. 1
0
int things_list(struct http_request *req) {
    int rc;
    char *zErrMsg = 0;
    char *query = "SELECT * FROM found ORDER BY last DESC";
    template_t tmpl;
	attrlist_t attributes;

    struct timespec when;

    buf = kore_buf_create(mb);

    if(!thing) thing = hashmap_new();

    // load template from assets
    template_load
        (asset_things_list_html, asset_len_things_list_html, &tmpl);

    // initialise attribute list
    attributes = attrinit();

    if( ! parse_datetime(&when, "now", NULL) )
        kore_log(LOG_ERR,"parse-datetime error");
    else {
        struct tm *tt;
        tt = localtime (&when.tv_sec);
        mktime(tt);
        strftime(line, ml, "Dowse :: %d %m %Y - %H:%M:%S", tt);
        attrcat(attributes, "title", line);
    }
    
    sqlquery(query, things_list_cb, attributes);

    template_apply(&tmpl, attributes, buf);

	http_response(req, 200, buf->data, buf->offset);

    template_free(&tmpl);
    attrfree(attributes);

    kore_buf_free(buf);


	return (KORE_RESULT_OK);
}
Esempio n. 2
0
int thing_show(struct http_request *req) {
    int rc;
    template_t tmpl;
	attrlist_t attributes;
    char *zErrMsg = 0;
    char *macaddr;

	http_populate_get(req);

    // we shouldn't free the result in macaddr
	if (http_argument_get_string(req, "macaddr", &macaddr))
		kore_log(LOG_DEBUG, "thing_show macaddr %s",macaddr);
    else
        kore_log(LOG_ERR,"thing_show get argument error");

    // prepare query
    snprintf(line,ml,"SELECT * FROM found WHERE macaddr = '%s'",macaddr);
    
    // allocate output buffer
    buf = kore_buf_create(mb);

    // load template
    template_load
        (asset_thing_show_html, asset_len_thing_show_html, &tmpl);
    attributes = attrinit();

    attrcat(attributes, "title", "Dowse information panel");

    // SQL query
    sqlquery(line, thing_show_cb, attributes);

    template_apply(&tmpl,attributes,buf);

	http_response(req, 200, buf->data, buf->offset);

    template_free(&tmpl);
    attrfree(attributes);

    kore_buf_free(buf);

	return (KORE_RESULT_OK);

}
Esempio n. 3
0
#include "escape.h"

static void template_test(void);
static void escape_test(void);

static void template_test(void) {
	struct template *t;
	attrlist_t al;
	int i;
	const char *files[] = {
		"test1.template",
		"test2.template",
		"test3.template",
	};

	al=attrinit();
	attrset(al, "SUBJECT", "[[SUBJECT]]");
	attrset(al, "ICON_URL", "[[ICON_URL]]");
	attrset(al, "DATE", "[[DATE]]");
	attrset(al, "FROM", "[[FROM]]");
	attrset(al, "MESSAGE_BODY", "[[MESSAGE_BODY]]");

	/* test with mapped files */
	for(i=0;i<sizeof files/sizeof *files;i++) {
		printf("::%s::\n", files[i]);
		t=template_loadfile(files[i]);
		if(t) {
			template_apply(t, al);
			template_free(t);
		}
		printf("\n");