12 #define MAX_TAG_LENGTH 64
15 #define TAG_CHAR(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_')
17 int tag_scan(const char *author, const char *data, int len, uint64_t record) {
18 char *taglist[MAX_TAGS];
22 for (i = 0; i < len; i++) {
25 if (n_tags == MAX_TAGS) {
26 fprintf(stderr, "Too many tags in message");
30 while (i < len && TAG_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
33 if (start - i == 0) continue;
34 char *tag = calloc(1, MAX_TAG_LENGTH + 1);
35 memcpy(tag, &data[start], i - start);
36 for (j = 0; j < n_tags; j++) {
37 if (!strncmp(tag, taglist[j], MAX_TAG_LENGTH)) {
38 // We already have this tag. Start over.
43 taglist[n_tags] = tag;
45 // We goto here so i doesn't get incremented
50 for (i = 0; i < n_tags; i++) {
51 tag_add(author, taglist[i], record);
56 int tag_add(const char *author, const char *tag, uint64_t record) {
60 memset(t.author, 0, 32);
61 strncpy(t.author, author, 32);
64 snprintf(filename, 512, "%s/%s", TAGS_PATH, tag);
65 int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
66 flock(tag_fd, LOCK_EX);
68 perror("Could not open tag file");
71 int n = write(tag_fd, &t, sizeof(struct tag));
72 if (n < sizeof(struct tag)) {
73 perror("Could not write new tag");
76 flock(tag_fd, LOCK_UN);
82 int tag_list(const char *tag, int start, int count) {