237 lines
5.1 KiB
C
237 lines
5.1 KiB
C
/* api.c - Database API */
|
|
|
|
#define stmt_prepare(stmt) \
|
|
sqlite3_prepare_v2(connection, stmt ## _template, -1, &stmt, NULL)
|
|
|
|
VARDECL char const * db = DBFILE;
|
|
|
|
VARDECL sqlite3 * connection = NULL;
|
|
|
|
DECL void DBERR(const int l)
|
|
{
|
|
if(l != SQLITE_OK && l != SQLITE_ROW && l != SQLITE_DONE)
|
|
{
|
|
fprintf(stderr,
|
|
"sqlite (%d): %s\n",
|
|
sqlite3_errcode(connection), sqlite3_errmsg(connection));
|
|
exit(DB_ERROR);
|
|
}
|
|
}
|
|
|
|
DECL int
|
|
api_init(void)
|
|
{
|
|
DBERR(sqlite3_open_v2(db, &connection, SQLITE_OPEN_READWRITE, NULL));
|
|
/* dont you fucking dare to remove this spacing */
|
|
DBERR(stmt_prepare(remind_stmt));
|
|
DBERR(stmt_prepare(set_repo_stmt));
|
|
DBERR(stmt_prepare(get_nth_id_stmt));
|
|
DBERR(stmt_prepare(new_assignment_stmt));
|
|
DBERR(stmt_prepare(purge_assignments_stmt));
|
|
DBERR(stmt_prepare(is_no_assignment_stmt));
|
|
return 0;
|
|
}
|
|
|
|
DECL void
|
|
api_rope(void)
|
|
{
|
|
DBERR(sqlite3_finalize(remind_stmt));
|
|
DBERR(sqlite3_finalize(set_repo_stmt));
|
|
DBERR(sqlite3_finalize(get_nth_id_stmt));
|
|
DBERR(sqlite3_finalize(new_assignment_stmt));
|
|
DBERR(sqlite3_finalize(purge_assignments_stmt));
|
|
DBERR(sqlite3_finalize(is_no_assignment_stmt));
|
|
sqlite3_close(connection);
|
|
}
|
|
|
|
#define DBERR_NOMEM(v) if (v) { r = (IRC_RED "No memory!" IRC_STOP); goto fail; }
|
|
|
|
DECL char *
|
|
remind(char * who)
|
|
{
|
|
char * r;
|
|
char * title;
|
|
char * desc = NULL;
|
|
/* char * repo; */
|
|
DBERR(sqlite3_reset(remind_stmt));
|
|
DBERR(sqlite3_bind_text(remind_stmt, 1, who, -1, SQLITE_STATIC));
|
|
const int i = sqlite3_step(remind_stmt);
|
|
DBERR(i);
|
|
if (i == SQLITE_ROW)
|
|
{
|
|
title = (char *) sqlite3_column_text(remind_stmt, 0);
|
|
title = strdup(title);
|
|
if (!title) { goto fail; }
|
|
desc = (char *) sqlite3_column_text(remind_stmt, 1);
|
|
if (desc && (desc = strdup(desc)) == NULL)
|
|
{ goto fail; }
|
|
if (-1 == asprintf(&r,
|
|
IRC_RED "%s " IRC_YELLOW "%s" IRC_STOP,
|
|
title, desc ? desc : ""))
|
|
{ r = IRC_RED "No memory!" IRC_STOP; }
|
|
fail:
|
|
free(title);
|
|
free(desc);
|
|
}
|
|
else
|
|
{ r = IRC_RED "No current assignment." IRC_STOP; }
|
|
return r;
|
|
}
|
|
|
|
DECL void
|
|
set_repo(char const * const who,
|
|
char const * const link)
|
|
{
|
|
DBERR(sqlite3_reset(set_repo_stmt));
|
|
DBERR(sqlite3_bind_text(set_repo_stmt, 1, link, -1, SQLITE_STATIC));
|
|
DBERR(sqlite3_bind_text(set_repo_stmt, 2, who, -1, SQLITE_STATIC));
|
|
DBERR(sqlite3_step(set_repo_stmt));
|
|
}
|
|
|
|
#ifndef NO_VULN_COMMANDS
|
|
|
|
/* Note that this function is may not be vuln,
|
|
but is included as it's only used by vulns */
|
|
DECL int
|
|
rtos(void * data,
|
|
int argc,
|
|
char** argv,
|
|
char** colname)
|
|
{
|
|
(void) colname;
|
|
|
|
char ** r = (char**)data;
|
|
|
|
size_t data_len = 0;
|
|
for(int i = 0; i < argc; i++)
|
|
{
|
|
if(argv[i])
|
|
{
|
|
data_len += strlen(argv[i]);
|
|
}
|
|
else
|
|
{
|
|
/* strlen("NULL") == 4 */
|
|
data_len += 4;
|
|
}
|
|
/* strlen("|") * 2 == 2 */
|
|
data_len += 2;
|
|
}
|
|
++data_len;
|
|
|
|
*r = (char *)calloc(data_len, sizeof(char));
|
|
|
|
for(int i = 0; i < argc; i++){
|
|
strcat(*r, "|");
|
|
if(argv[i]){
|
|
strcat(*r, argv[i]);
|
|
}
|
|
else
|
|
{
|
|
strcat(*r, "NULL");
|
|
}
|
|
}
|
|
strcat(*r, "|\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
DECL char *
|
|
dump()
|
|
{
|
|
char* errmsg;
|
|
char* r = NULL;
|
|
|
|
DBERR(sqlite3_exec(connection, dump_stmt, rtos, &r, &errmsg));
|
|
|
|
return r;
|
|
}
|
|
|
|
DECL char *
|
|
raw(char const * const sql)
|
|
{
|
|
char* errmsg;
|
|
char *r = NULL;
|
|
|
|
sqlite3_exec(connection, sql, rtos, &r, &errmsg);
|
|
|
|
if (errmsg){
|
|
free(r);
|
|
r = errmsg;
|
|
} else { strcat(r, "\00"); }
|
|
return r;
|
|
}
|
|
|
|
#endif /* !NO_VULN_COMMANDS */
|
|
|
|
DECL int
|
|
get_project_count_callback(void* data, int argc, char** argv, char** colname)
|
|
{
|
|
(void)argc;
|
|
(void)colname;
|
|
int* count = (int*)data;
|
|
*count = atoi(argv[0]);
|
|
return 0;
|
|
}
|
|
|
|
DECL int
|
|
get_project_count(void)
|
|
{
|
|
int r = 0;
|
|
char const * sql = "SELECT COUNT(*) FROM project;";
|
|
DBERR(sqlite3_exec(connection, sql, get_project_count_callback, &r, NULL));
|
|
return r;
|
|
}
|
|
|
|
DECL int
|
|
get_nth_id(const int i)
|
|
{
|
|
int r;
|
|
DBERR(sqlite3_reset(get_nth_id_stmt));
|
|
DBERR(sqlite3_bind_int(get_nth_id_stmt, 1, i));
|
|
DBERR(sqlite3_step(get_nth_id_stmt));
|
|
r = sqlite3_column_int(get_nth_id_stmt, 0);
|
|
return r;
|
|
}
|
|
|
|
DECL void
|
|
new_assignment(char const * const who,
|
|
int const project)
|
|
{
|
|
DBERR(sqlite3_reset(new_assignment_stmt));
|
|
DBERR(sqlite3_bind_text(new_assignment_stmt, 1, who, -1, SQLITE_STATIC));
|
|
DBERR(sqlite3_bind_int(new_assignment_stmt, 2, project));
|
|
DBERR(sqlite3_step(new_assignment_stmt));
|
|
}
|
|
|
|
DECL void
|
|
random_assign(char const * const who)
|
|
{
|
|
int i = rand() % get_project_count();
|
|
i = get_nth_id(i);
|
|
new_assignment(who, i);
|
|
}
|
|
|
|
DECL void
|
|
purge_assignments(char const * const who)
|
|
{
|
|
DBERR(sqlite3_reset(purge_assignments_stmt));
|
|
DBERR(sqlite3_bind_text(purge_assignments_stmt, 1, who, -1, SQLITE_STATIC));
|
|
DBERR(sqlite3_step(purge_assignments_stmt));
|
|
}
|
|
|
|
#ifdef INITIAL_ASSIGNMENT_MESSAGE
|
|
|
|
DECL int
|
|
is_no_assignment(char const * const who)
|
|
{
|
|
const int e;
|
|
DBERR(sqlite3_reset(is_no_assignment_stmt));
|
|
DBERR(sqlite3_bind_text(is_no_assignment_stmt, 1, who, -1, SQLITE_STATIC));
|
|
e = sqlite3_step(is_no_assignment_stmt);
|
|
DBERR(e);
|
|
return (e == SQLITE_DONE);
|
|
}
|
|
|
|
#endif /* INITIAL_ASSIGNMENT_MESSAGE */
|