int dm_sievescript_activate(uint64_t user_idnr, char *scriptname) { Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE; assert(scriptname); c = db_con_get(); TRY db_begin_transaction(c); s = db_stmt_prepare(c,"UPDATE %ssievescripts SET active = 0 WHERE owner_idnr = ? ", DBPFX); db_stmt_set_u64(s, 1, user_idnr); db_stmt_exec(s); db_con_clear(c); s = db_stmt_prepare(c,"UPDATE %ssievescripts SET active = 1 WHERE owner_idnr = ? AND name = ?", DBPFX); db_stmt_set_u64(s, 1, user_idnr); db_stmt_set_str(s, 2, scriptname); db_stmt_exec(s); db_commit_transaction(c); t = TRUE; CATCH(SQLException) LOG_SQLERROR; db_rollback_transaction(c); FINALLY db_con_close(c); END_TRY; return t; }
int dm_sievescript_rename(uint64_t user_idnr, char *scriptname, char *newname) { int active = 0; Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE; assert(scriptname); /* * According to the draft RFC, a script with the same * name as an existing script should *atomically* replace it. */ c = db_con_get(); TRY db_begin_transaction(c); s = db_stmt_prepare(c,"SELECT active FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX); db_stmt_set_u64(s,1, user_idnr); db_stmt_set_str(s,2, newname); r = db_stmt_query(s); if (db_result_next(r)) { active = db_result_get_int(r,0); db_con_clear(c); s = db_stmt_prepare(c, "DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX); db_stmt_set_u64(s, 1, user_idnr); db_stmt_set_str(s, 2, newname); db_stmt_exec(s); } db_con_clear(c); s = db_stmt_prepare(c, "UPDATE %ssievescripts SET name = ?, active = ? WHERE owner_idnr = ? AND name = ?", DBPFX); db_stmt_set_str(s, 1, newname); db_stmt_set_int(s, 2, active); db_stmt_set_u64(s, 3, user_idnr); db_stmt_set_str(s, 4, scriptname); db_stmt_exec(s); t = db_commit_transaction(c); CATCH(SQLException) LOG_SQLERROR; t = DM_EQUERY; db_rollback_transaction(c); FINALLY db_con_close(c); END_TRY; return t; }
int auth_change_password(uint64_t user_idnr, const char *new_pass, const char *enctype) { C c; S s; volatile int t = FALSE; const char *encoding = enctype?enctype:""; if (strlen(new_pass) > 128) { TRACE(TRACE_ERR, "new password length is insane"); return -1; } c = db_con_get(); TRY s = db_stmt_prepare(c, "UPDATE %susers SET passwd = ?, encryption_type = ? WHERE user_idnr=?", DBPFX); db_stmt_set_str(s, 1, new_pass); db_stmt_set_str(s, 2, encoding); db_stmt_set_u64(s, 3, user_idnr); db_stmt_exec(s); t = TRUE; CATCH(SQLException) LOG_SQLERROR; t = DM_EQUERY; FINALLY db_con_close(c); END_TRY; return t; }
int dm_sievescript_add(uint64_t user_idnr, char *scriptname, char *script) { Connection_T c; ResultSet_T r; PreparedStatement_T s; volatile int t = FALSE; assert(scriptname); c = db_con_get(); TRY db_begin_transaction(c); s = db_stmt_prepare(c,"SELECT COUNT(*) FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX); db_stmt_set_u64(s, 1, user_idnr); db_stmt_set_str(s, 2, scriptname); r = db_stmt_query(s); if (db_result_next(r)) { db_con_clear(c); s = db_stmt_prepare(c,"DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX); db_stmt_set_u64(s, 1, user_idnr); db_stmt_set_str(s, 2, scriptname); db_stmt_exec(s); } db_con_clear(c); s = db_stmt_prepare(c,"INSERT INTO %ssievescripts (owner_idnr, name, script, active) VALUES (?,?,?,1)", DBPFX); db_stmt_set_u64(s, 1, user_idnr); db_stmt_set_str(s, 2, scriptname); db_stmt_set_blob(s, 3, script, strlen(script)); db_stmt_exec(s); t = db_commit_transaction(c); CATCH(SQLException) LOG_SQLERROR; db_rollback_transaction(c); t = DM_EQUERY; FINALLY db_con_close(c); END_TRY; return t; }
int dm_sievescript_delete(uint64_t user_idnr, char *scriptname) { Connection_T c; PreparedStatement_T s; volatile gboolean t = FALSE; assert(scriptname); c = db_con_get(); TRY s = db_stmt_prepare(c,"DELETE FROM %ssievescripts WHERE owner_idnr = ? AND name = ?", DBPFX); db_stmt_set_u64(s, 1, user_idnr); db_stmt_set_str(s, 2, scriptname); db_stmt_exec(s); t = TRUE; CATCH(SQLException) LOG_SQLERROR; FINALLY db_con_close(c); END_TRY; return t; }
static int delete_plugin(const char *user, int argc, const char **argv, int *intv, void (*msg)(void *opaque, const char *fmt, ...), void *opaque) { db_stmt_t *s; db_conn_t *c = db_get_conn(); if(c == NULL) { msg(opaque, "Database connection problems"); return 0; } s = db_stmt_get(c, "DELETE FROM version WHERE plugin_id=? AND version=?"); if(db_stmt_exec(s, "ss", argv[0], argv[1])) { msg(opaque, "Database query problems"); return 0; } if(db_stmt_affected_rows(s)) trace(LOG_NOTICE, "User '%s' deleted %s %s", user, argv[0], argv[1]); msg(opaque, "OK, %d rows deleted", db_stmt_affected_rows(s)); return 0; }
static int show_plugin(const char *user, int argc, const char **argv, int *intv, void (*msg)(void *opaque, const char *fmt, ...), void *opaque) { db_stmt_t *s; char tstr[64]; struct tm tm; db_conn_t *c = db_get_conn(); if(c == NULL) { msg(opaque, "Database connection problems"); return 0; } s = db_stmt_get(c, SQL_GET_PLUGIN_BY_ID); if(db_stmt_exec(s, "s", argv[0])) { msg(opaque, "Database query problems"); return 0; } time_t created; int userid; char betasecret[128]; char downloadurl[1024]; int r = db_stream_row(0, s, DB_RESULT_TIME(created), DB_RESULT_INT(userid), DB_RESULT_STRING(betasecret), DB_RESULT_STRING(downloadurl)); db_stmt_reset(s); if(r < 0) { msg(opaque, "Database query problems"); return 0; } if(r) { msg(opaque, "No such plugin"); return 0; } msg(opaque, "'%s' owned by user #%d", argv[0], userid); gmtime_r(&created, &tm); strftime(tstr, sizeof(tstr), "%d-%b-%Y %T UTC", &tm); msg(opaque, " Created %s Betasecret: %s", tstr, betasecret); msg(opaque, " Download URL: %s", downloadurl); msg(opaque, ""); msg(opaque, "Versions:"); s = db_stmt_get(c, SQL_GET_PLUGIN_VERSIONS); if(db_stmt_exec(s, "s", argv[0])) { msg(opaque, "Database query problems"); return 0; } while(1) { char version[64]; char type[64]; char author[128]; char showtime_min_version[64]; int downloads; char title[256]; char category[64]; char synopsis[256]; char description[4096]; char homepage[256]; char pkg_digest[64]; char icon_digest[64]; int published; char comment[4096]; char status[8]; r = db_stream_row(0, s, DB_RESULT_TIME(created), DB_RESULT_STRING(version), DB_RESULT_STRING(type), DB_RESULT_STRING(author), DB_RESULT_INT(downloads), DB_RESULT_STRING(showtime_min_version), DB_RESULT_STRING(title), DB_RESULT_STRING(category), DB_RESULT_STRING(synopsis), DB_RESULT_STRING(description), DB_RESULT_STRING(homepage), DB_RESULT_STRING(pkg_digest), DB_RESULT_STRING(icon_digest), DB_RESULT_INT(published), DB_RESULT_STRING(comment), DB_RESULT_STRING(status) ); if(r) break; gmtime_r(&created, &tm); strftime(tstr, sizeof(tstr), "%d-%b-%Y %T UTC", &tm); msg(opaque, "%-9s %-25s %s %-9s %-9s", version, title, tstr, published ? "Published" : "", *status == 'a' ? "Approved" : *status == 'r' ? "Rejected" : *status == 'p' ? "Pending" : "Unknown"); msg(opaque, " '%s' - %s", *category ? category : "<no category>", synopsis); msg(opaque, " '%s' requierd Showtime ver. %s", type, showtime_min_version); msg(opaque, " %d downloads", downloads); msg(opaque, ""); } return 0; }