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