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