--- /dev/null
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include "tags.h"
+#include "config.h"
+
+#define MAX_TAG_LENGTH 64
+#define MAX_TAGS 1024
+
+#define TAG_CHAR(c) ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_')
+
+int tag_scan(const char *author, const char *data, int len, uint64_t record) {
+ char *taglist[MAX_TAGS];
+ int n_tags = 0;
+ int i, j;
+
+ for (i = 0; i < len; i++) {
+tag_scan_start:
+ if (data[i] == '#') {
+ if (n_tags == MAX_TAGS) {
+ fprintf(stderr, "Too many tags in message");
+ continue;
+ }
+ int start = ++i;
+ while (i < len && TAG_CHAR(data[i]) && (i - start < MAX_TAG_LENGTH)) {
+ i++;
+ }
+ if (start - i == 0) continue;
+ char *tag = calloc(1, MAX_TAG_LENGTH + 1);
+ memcpy(tag, &data[start], i - start);
+ for (j = 0; j < n_tags; j++) {
+ if (!strncmp(tag, taglist[j], MAX_TAG_LENGTH)) {
+ // We already have this tag. Start over.
+ free(tag);
+ goto tag_scan_start;
+ }
+ }
+ taglist[n_tags] = tag;
+ n_tags++;
+ // We goto here so i doesn't get incremented
+ goto tag_scan_start;
+ }
+ }
+
+ for (i = 0; i < n_tags; i++) {
+ tag_add(author, taglist[i], record);
+ free(taglist[i]);
+ }
+}
+
+int tag_add(const char *author, const char *tag, uint64_t record) {
+ char filename[512];
+ struct tag t;
+
+ memset(t.author, 0, 32);
+ strncpy(t.author, author, 32);
+ t.record = record;
+
+ snprintf(filename, 512, "%s/%s", TAGS_PATH, tag);
+ int tag_fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
+ flock(tag_fd, LOCK_EX);
+ if (tag_fd == -1) {
+ perror("Could not open tag file");
+ return 0;
+ }
+ int n = write(tag_fd, &t, sizeof(struct tag));
+ if (n < sizeof(struct tag)) {
+ perror("Could not write new tag");
+ return 0;
+ }
+ flock(tag_fd, LOCK_UN);
+ close(tag_fd);
+
+ return 1;
+}
+
+int tag_list(const char *tag, int start, int count) {
+}