Restrict usernames to the same character set as tags
[blerg.git] / database / tags.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 <string.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/file.h>
12 #include <sys/mman.h>
13 #include "tags.h"
14 #include "util.h"
15 #include "database.h"
16 #include "config.h"
17
18
19 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
20         char *taglist[MAX_TAGS_PER_RECORD];
21         char last_char = ' ';
22         int n_tags = 0;
23         int i, j;
24
25         for (i = 0; i < len; i++) {
26 tag_scan_start:
27                 if (WHITESPACE(last_char) && (data[i] == '#' || data[i] == '@')) {
28                         if (n_tags == MAX_TAGS_PER_RECORD) {
29                                 fprintf(stderr, "Too many tags in message\n");
30                                 break;
31                         }
32                         int begin = i;
33                         int start = ++i;
34                         while (i < len && VALID_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
35                                 i++;
36                         }
37                         if (start - i == 0) continue;
38                         char *tag = calloc(1, MAX_TAG_LENGTH + 2);
39                         memcpy(tag, &data[begin], i - begin);
40                         for (j = 0; j < n_tags; j++) {
41                                 if (!strncmp(tag, taglist[j], MAX_TAG_LENGTH)) {
42                                         // We already have this tag.  Start over.
43                                         free(tag);
44                                         last_char = data[i-1];
45                                         goto tag_scan_start;
46                                 }
47                         }
48                         taglist[n_tags] = tag;
49                         n_tags++;
50                         last_char = data[i-1];
51                         // We goto here so i doesn't get incremented
52                         goto tag_scan_start;
53                 }
54                 last_char = data[i];
55         }
56
57         for (i = 0; i < n_tags; i++) {
58                 tag_add(author, taglist[i], record);
59                 free(taglist[i]);
60         }
61 }
62
63 int tag_add(const char *author, const char *tag, uint64_t record) {
64         char filename[512];
65         struct tag t;
66
67         memset(t.author, 0, 32);
68         strncpy(t.author, author, 32);
69         t.record = record;
70
71         switch(tag[0]) {
72         case '#':
73                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
74                 break;
75         case '@':
76                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
77                 break;
78         default:
79                 fprintf(stderr, "Invalid tag type: %s\n", tag);
80                 return 0;
81         }
82
83         int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
84         flock(tag_fd, LOCK_EX);
85         if (tag_fd == -1) {
86                 perror("Could not open tag file");
87                 return 0;
88         }
89         int n = write(tag_fd, &t, sizeof(struct tag));
90         if (n < sizeof(struct tag)) {
91                 perror("Could not write new tag");
92                 return 0;
93         }
94         flock(tag_fd, LOCK_UN);
95         close(tag_fd);
96
97         return 1;
98 }
99
100 struct tag * tag_list(const char *tag, uint64_t offset, int *count, int direction) {
101         char filename[512];
102         struct stat st;
103         struct tag *taglist;
104         struct tag *retlist;
105         uint64_t n_tag_records;
106
107         if (!valid_name(tag + 1))
108                 return NULL;
109         
110         switch(tag[0]) {
111         case '#':
112                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
113                 break;
114         case '@':
115                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
116                 break;
117         default:
118                 fprintf(stderr, "Invalid tag type: %s\n", tag);
119                 return NULL;
120         }
121
122         int tag_fd = open(filename, O_RDONLY, 0600);
123         if (tag_fd == -1) {
124                 perror("Could not open tag file");
125                 *count = 0;
126                 return NULL;
127         }
128
129         fstat(tag_fd, &st);
130         if (st.st_size == 0) {
131                 close(tag_fd);
132                 *count = 0;
133                 return NULL;
134         }
135         n_tag_records = st.st_size / sizeof(struct tag);
136         if (*count > n_tag_records)
137                 *count = n_tag_records;
138         if (offset > n_tag_records) {
139                 fprintf(stderr, "Cannot access tag record beyond end\n");
140                 return NULL;
141         }
142
143         taglist = (struct tag *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0);
144         if (taglist == MAP_FAILED) {
145                 perror("Could not mmap tag file");
146                 goto tag_list_map_failed;
147         }
148         retlist = (struct tag *) malloc(sizeof(struct tag) * *count);
149         if (retlist == NULL) {
150                 perror("Could not allocate memory for tag list");
151                 goto tag_list_malloc_failed;
152         }
153         switch(direction) {
154         case 1:
155                 memcpy(retlist, taglist + offset, sizeof(struct tag) * *count);
156                 break;
157         case -1:
158                 memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct tag) * *count);
159                 break;
160         }
161
162         munmap(taglist, st.st_size);
163         close(tag_fd);
164         return retlist;
165
166 tag_list_malloc_failed:
167         munmap(taglist, st.st_size);
168 tag_list_map_failed:
169         close(tag_fd);
170 tag_list_open_failed:
171         return NULL;
172 }
173
174 int tag_exists(const char *tag) {
175         char filename[512];
176
177         if (!valid_name(tag + 1))
178                 return 0;
179
180         if (!(tag[0] == '@' || tag[0] == '#')) {
181                 fprintf(stderr, "Invalid tag: %s\n", tag);
182                 return 0;
183         }
184
185         switch(tag[0]) {
186         case '#':
187                 snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1);
188                 break;
189         case '@':
190                 snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1);
191                 break;
192         default:
193                 fprintf(stderr, "Invalid tag type: %s\n", tag);
194                 return 0;
195         }
196         if (access(filename, F_OK) == -1)
197                 return 0;
198         else
199                 return 1;
200 }