aboutsummaryrefslogtreecommitdiff
path: root/ctl.c
blob: 64934cc223aee309bbd3f6ce80f736381c48a3bb (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "err.h"
#include "db.h"
#include "query.h"
#include "site.h"

/* Print UNIX timestamp as written date. */
void
printdate(int timestamp)
{
	char buf[20];
	struct tm *t;
	
	t = localtime((time_t *)&timestamp);
	strftime(buf, 20, "%Y-%m-%d %H:%M", t);
	
	printf(buf);
}

/*
 * The `new' functions provide a way to add a new attachment/post/user.
 * On GET, they show a form. On POST, they insert the posted information
 * into the database.
 */
void
newatt()
{}

void
newpost()
{}

void
newuser()
{}

/*
 * The `show' functions show an existing attachment/post/user
 * or some other type of page.
 */
void
showatt(id)
{
	struct att *att;
	
	if(!(att = getatt(selectbyint("atts", "oid", id)))){
		srverr("Could not retrieve attachment");
		return;
	}
	
	printf("Content-Type: %s\n\n", att->mime);
	printf("%.*s", att->bytes, att->data);
	free(att);
}

void
showfront()
{
	char *title;
	struct post *post;
	struct user *user;
	sqlite3_stmt *pstmt, *ustmt;
	
	if(sqlite3_prepare(db,
	    "SELECT oid, * from posts ORDER BY created DESC",
	    -1, &pstmt, 0) != SQLITE_OK){
		srverr("Could not retrieve posts");
		return;
	}
	
	if(sqlite3_prepare(db,
	    "SELECT oid, * from users ORDER BY created DESC",
	    -1, &ustmt, 0) != SQLITE_OK){
		srverr("Could not retrieve users");
		return;
	}
	
	title = site.name;
	printf("Content-Type: text/html\n\n");
	#include "t/front.tc"
}

void
showpost(int id)
{
	char *title;
	struct att *att;
	struct post *post;
	struct user *user;
	sqlite3_stmt *stmt;
	
	if(!(post = getpost(selectbyint("posts", "oid", id)))){
		srverr("Could not retrieve post");
		return;
	}
	
	if(!(user = getuser(selectbyint("users", "oid", post->user)))){
		srverr("Could not retrieve post author");
		return;
	}
	
	stmt = selectbyint("atts", "post", id);
	
	title = post->subject;
	printf("Content-Type: text/html\n\n");
	#include "t/post.tc"
	free(user);
	free(post);
}

void
showuser(int id)
{
	char title[120];
	sqlite3_stmt *stmt;
	struct post *post;
	struct user *user;
	
	if(!(user = getuser(selectbyint("users", "oid", id)))){
		srverr("Could not retrieve user");
		return;
	}
	
	if(sqlite3_prepare(db,
	    "SELECT oid, * from posts WHERE user = ? ORDER BY created DESC",
	    -1, &stmt, 0) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_int(stmt, 1, id) != SQLITE_OK)
		goto err;
	
	snprintf(title, 120, "User: %s", user->full? user->full: user->name);
	printf("Content-Type: text/html\n\n");
	#include "t/user.tc"
	free(user);
	return;
	
err:
	srverr("Could not retrieve posts");
	return;
}