#include #include #include #include #include #include "cforum.h" #define MAXMSG 300 int main(int argc, char *argv[]) { char msg[MAXMSG], *new, *p, *v; int attid, postid, userid; sqlite3_stmt *stmt; /* * The database is opened or a server error is generated. * In the rest of the program, the database is always * assumed to be opened. */ if(sqlite3_open("db", &db) != SQLITE_OK){ snprintf(msg, MAXMSG, "The database could not be opened: %s\n", sqlite3_errmsg(db)); srverr(msg); sqlite3_close(db); return 1; } /* * The site name is retrieved from the database. This early on, * it is appropriate to die with a server error on failure. */ if(sqlite3_prepare(db, "SELECT value FROM settings WHERE key = 'name'", -1, &stmt, 0) != SQLITE_OK){ snprintf(msg, MAXMSG, "The site name could not be retrieved: %s\n", sqlite3_errmsg(db)); srverr(msg); sqlite3_close(db); return 1; } if(sqlite3_step(stmt) == SQLITE_ROW) site.name = strdup((char *)sqlite3_column_text(stmt, 0)); else{ snprintf(msg, MAXMSG, "The site name is not set.\n"); srverr(msg); sqlite3_finalize(stmt); sqlite3_close(db); return 1; } sqlite3_finalize(stmt); /* * The global struct query is set, or the program dies. * From now on, query is assumed to be set. */ setquery(); /* Handle empty request. */ if(!*query.string){ showfront(); goto end; } /* Parse query string. */ new = NULL; attid = postid = userid = 0; while(p = nextparam(GET, NULL, 128)){ v = split(p); if(!attid && strcmp(p, "att") == 0) attid = atoi(v); else if(!postid && strcmp(p, "post") == 0) postid = atoi(v); else if(!userid && strcmp(p, "user") == 0) userid = atoi(v); else if(!new && strcmp(p, "new") == 0) new = strdup(v); } /* Handle request. */ if(attid) showatt(attid); else if(postid) showpost(postid); else if(userid) showuser(userid); else if(new){ if(strcmp(v, "att") == 0) newatt(); else if(strcmp(v, "post") == 0) newpost(); else if(strcmp(v, "user") == 0) newuser(); else if(strcmp(v, "session") == 0) login(); else showfront(); /* TODO */ }else showfront(); end: sqlite3_close(db); return 0; }