aboutsummaryrefslogtreecommitdiff
path: root/cforum.c
blob: 80cc424f622081257454e529cff5a0eb98f6a878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ctl.h"	/* Controllers. */
#include "db.h"		/* Database. Defines global variable db. */
#include "err.h"	/* HTTP errors. */
#include "site.h"	/* Site settings. Defines global struct site. */
#include "query.h"	/* Query functions. Defines global struct query. */

#define MAXERR 300

int
main(int argc, char *argv[])
{
	char err[MAXERR], *p, *qs, *v;
	int 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(err, MAXERR,
		    "The database could not be opened: %s\n",
		    sqlite3_errmsg(db));
		srverr(err);
		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(err, MAXERR,
		    "The site name could not be retrieved: %s\n",
		    sqlite3_errmsg(db));
		srverr(err);
		sqlite3_close(db);
		return 1;
	}
	if(sqlite3_step(stmt) == SQLITE_ROW)
		site.name = strdup(sqlite3_column_text(stmt, 0));
	else{
		snprintf(err, MAXERR, "The site name is not set.\n");
		srverr(err);
		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. */
	postid = userid = -1;
	while(p = nextparam(GET, 128)){
		v = split(p);
		if(strcmp(p, "post") == 0) postid = atoi(v);
		else if(strcmp(p, "user") == 0) userid = atoi(v);
	}
	
	/* Handle request. */
	if(postid != -1)
		showpost(postid);
	else if(userid != -1)
		showuser(userid);
	else
		showfront();
	
end:
	sqlite3_close(db);
	return 0;
}