Refactor post fetching in rss.cgi
[blerg.git] / tools / blergtool.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "database.h"
7 #include "tags.h"
8
9 void help() {
10         printf(
11           "Usage: blergtool <command> [args]\n"
12           "\n"
13           "Where command is one of:\n"
14           "\n"
15           "  blergtool store <storename>\n"
16           "  blergtool fetch <storename> [record]\n"
17           "  blergtool mute <storename>\n"
18           "  blergtool unmute <storename>\n"
19           "  blergtool subscribe <storename> <target>\n"
20           "  blergtool unsubscribe <storename> <target>\n"
21         );
22 }
23
24 int main(int argc, char *argv[]) {
25         if (argc < 2) {
26                 help();
27                 exit(1);
28         }
29
30         if (strncmp(argv[1], "store", 5) == 0) {
31                 char *store = argv[2];
32                 struct blerg *f = blerg_open(store);
33                 if (!f) {
34                         printf("Blerg open failed\n");
35                         exit(1);
36                 }
37
38                 size_t bytes_read = 0;
39                 char *data = malloc(65536);
40                 do {
41                         bytes_read += fread(data + bytes_read, 1, 65536 - bytes_read, stdin);
42                 } while (bytes_read < 65536 && !feof(stdin));
43                 int record = blerg_store(f, data, bytes_read);
44                 blerg_close(f);
45                 free(data);
46
47                 if (record < 0) {
48                         fprintf(stderr, "Could not store\n");
49                         exit(1);
50                 } else {
51                         fprintf(stderr, "Stored record %d\n", record);
52                 }
53         } else if (strncmp(argv[1], "fetch", 5) == 0) {
54                 char *store = argv[2];
55                 int record = atoi(argv[3]);
56                 struct blerg *f = blerg_open(store);
57                 if (!f) {
58                         printf("Blerg open failed\n");
59                         exit(1);
60                 }
61
62                 char *data;
63                 int size;
64                 if (blerg_fetch(f, record, &data, &size)) {
65                         fwrite(data, 1, size, stdout);
66                         free(data);
67                 }
68                 blerg_close(f);
69         } else if (strncmp(argv[1], "list", 4) == 0) {
70                 char *tag = argv[2];
71                 int count = 50;
72                 struct blergref *list = tag_list(tag, 0, &count, -1);
73                 if (list == NULL) {
74                         printf("No entries");
75                 } else {
76                         int i;
77                         for (i = 0; i < count; i++) {
78                                 printf("%s %d\n", list[i].author, list[i].record);
79                         }
80                         free(list);
81                 }
82         } else if (strncmp(argv[1], "mute", 4) == 0) {
83                 char *store = argv[2];
84                 struct blerg *f = blerg_open(store);
85                 if (!f) {
86                         printf("Blerg open failed\n");
87                         exit(1);
88                 }
89
90                 blerg_set_mute(f, 1);
91                 blerg_close(f);
92         } else if (strncmp(argv[1], "unmute", 6) == 0) {
93                 char *store = argv[2];
94                 struct blerg *f = blerg_open(store);
95                 if (!f) {
96                         printf("Blerg open failed\n");
97                         exit(1);
98                 }
99
100                 blerg_set_mute(f, 0);
101                 blerg_close(f);
102         } else if (strncmp(argv[1], "subscribe", 9) == 0) {
103                 if (argc < 4) {
104                         printf("Not enough arguments for subscribe\n");
105                         help();
106                         exit(1);
107                 }
108                 if (!subscription_add(argv[2], argv[3])) {
109                         printf("Could not subscribe %s to %s\n", argv[2], argv[3]);
110                         exit(1);
111                 }
112         } else if (strncmp(argv[1], "unsubscribe", 11) == 0) {
113                 if (argc < 4) {
114                         printf("Not enough arguments for unsubscribe\n");
115                         help();
116                         exit(1);
117                 }
118                 if (!subscription_remove(argv[2], argv[3])) {
119                         printf("Could not subscribe %s to %s\n", argv[2], argv[3]);
120                         exit(1);
121                 }
122         } else {
123                 help();
124         }
125
126         return 0;
127 }