summaryrefslogtreecommitdiff
path: root/client.c
blob: 4bec7bbd5ba781fe3575d4eb94eecb394264b90d (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "login.h"
#include "icclient/product.h"
#include "icclient/catalog.h"
#include "icclient/ord.h"
#include "icclient/member.h"
#include "icclient/client.h"

typedef struct icclient_product icclient_product;
typedef struct icclient_catalog icclient_catalog;
typedef struct icclient_ord_item icclient_ord_item;
typedef struct icclient_ord_order icclient_ord_order;

CURL *curl = NULL;
char *server_url = NULL;

bool icclient_init(const char *url, const char *certificate)
{
	curl_global_init(CURL_GLOBAL_SSL);
	curl = curl_easy_init();
	if (curl) {
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
		curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
		if (certificate)
			curl_easy_setopt(curl, CURLOPT_CAINFO, certificate);
#ifdef DEBUG
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif
		size_t length = strlen(url);
		bool append = !(bool)(url[length - 1] == '/');
		server_url = malloc(length + (size_t)append + 1);
		strcpy(server_url, url);
		if (append)
			strcat(server_url, "/");
	}

	return (bool)curl;
}

void icclient_results(size_t (*handler)(void *contents, size_t size,
			size_t nmemb, void *userdata),
		struct icclient_catalog **catalogptr, const char *prodgroup)
{
	char nonspaced[strlen(prodgroup) + 1];
	strcpy(nonspaced, prodgroup);
	char *space = NULL;
	while ((space = strchr(nonspaced, ' ')))
		*space = '-';
	request(handler, (void *)catalogptr, NULL, "%s", nonspaced);
}

void icclient_allproducts(size_t (*handler)(void *, size_t, size_t, void *)
		, icclient_catalog **catalogptr)
{
	request(handler, (void *)catalogptr, NULL, "%s", "All-Products");
}

void icclient_flypage(size_t (*handler)(void *, size_t, size_t, void *),
		icclient_product **productptr, const char *sku)
{
	request(handler, (void *)productptr, NULL, "%s", sku);
}

static int prodcmp(const void *product1, const void *product2)
{
	return strcmp((*(icclient_product * const *)product1)->sku
			, (*(icclient_product * const *)product2)->sku);
}

static int itemcmp(const void *item1, const void *item2)
{
	return strcmp((*(icclient_ord_item * const *)item1)->product->sku
			, (*(icclient_ord_item * const *)item2)->product->sku);
}

void icclient_order(icclient_ord_order **orderptr, const char *sku
		, icclient_catalog *catalog)
{
	icclient_product **products = catalog->products;
	qsort(products, catalog->length, sizeof(icclient_product *), prodcmp);
	icclient_product *key_product = malloc(sizeof(icclient_product));
	icclient_product_init(key_product);
	key_product->sku = malloc(strlen(sku) + 1);
	strcpy(key_product->sku, sku);
	icclient_product *product = *(icclient_product **)bsearch(&key_product
			, products, catalog->length, sizeof(icclient_product *)
			, prodcmp);
	icclient_product_free(key_product);

	icclient_ord_order *order = *orderptr;
	icclient_ord_item *item = NULL;

	if (order) {
		icclient_ord_item **items = order->items;
		qsort(items, order->nitems, sizeof(icclient_ord_item *), itemcmp);
		icclient_ord_item *key_item = malloc(sizeof(icclient_ord_item));
		key_item->product = product;
		icclient_ord_item **itemptr = bsearch(&key_item, items
				, order->nitems, sizeof(icclient_ord_item *)
				, itemcmp);
		if (itemptr)
			item = *itemptr;
		free(key_item);
	} else {
		*orderptr = malloc(sizeof(icclient_ord_order));
		order = *orderptr;
		order->subtotal = .0;
		order->shipping = .0;
		order->subtotal = .0;
		order->nitems = 0;
	}

	if (item)
		item->quantity++;
	else {
		size_t i = order->nitems;
		*orderptr = realloc(order, sizeof(icclient_ord_order)
				+ (i + 1) * sizeof(icclient_ord_item));
		order = *orderptr;
		order->items[i] = malloc(sizeof(icclient_ord_item));
		order->nitems++;
		item = order->items[i];
		item->product = product;
		item->quantity = 1;
	}

	order->subtotal += item->product->price;
	order->total_cost += item->product->price;

	request(NULL, NULL, NULL, "%s%s", "order?mv_arg=", sku);
}

void icclient_newaccount(size_t (*handler)(void *contents, size_t size
				, size_t nmemb, void *userdata)
		, struct icclient_user *user
		, const char *username, const char *password
		, const char *verify, const char *successpage, const char *nextpage
		, const char *failpage)
{
	login(handler, user, username, password, verify, "NewAccount", successpage
			, nextpage, failpage);
}

void icclient_login(size_t (*handler)(void *contents, size_t size
			, size_t nmemb, void *userdata)
		, struct icclient_user *user
		, const char *username, const char *password
		, const char *successpage, const char *nextpage
		, const char *failpage)
{
	login(handler, user, username, password, NULL, "Login", successpage
			, nextpage, failpage);
}

void icclient_logout()
{
	request(NULL, NULL, NULL, "%s", "logout");
}

void icclient_page(const char *path, size_t (*handler)(void *, size_t, size_t
			, void *), void **dataptr)
{
	request(handler, (void *)dataptr, NULL, "%s", path);
}

void icclient_cleanup()
{
	if (curl) {
		free(server_url);
		curl_easy_cleanup(curl);
	}
	curl_global_cleanup();
}