X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=database%2Ftags.c;h=ba7c376031d246a65473e7b9729c5b882a40f472;hb=57bd5973e1bace9c7cf8889294c710b65462dee9;hp=5ec12fa1ed9a35e65aaee29924e48b42f65935e8;hpb=dead5bdc521b34c85256737b616f4072a78dc2fb;p=blerg.git diff --git a/database/tags.c b/database/tags.c index 5ec12fa..ba7c376 100644 --- a/database/tags.c +++ b/database/tags.c @@ -14,6 +14,7 @@ #include "util.h" #include "database.h" #include "config.h" +#include "configuration.h" int tag_scan(const char *author, const char *data, int len, uint64_t record) { @@ -58,11 +59,13 @@ tag_scan_start: tag_add(author, taglist[i], record); free(taglist[i]); } + return 1; } int tag_add(const char *author, const char *tag, uint64_t record) { - char filename[512]; - struct tag t; + char filename[FILENAME_MAX]; + struct blergref t; + const char *tagval = tag + 1; memset(t.author, 0, 32); strncpy(t.author, author, 32); @@ -70,10 +73,10 @@ int tag_add(const char *author, const char *tag, uint64_t record) { switch(tag[0]) { case '#': - snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tagval); break; case '@': - snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tagval); break; default: fprintf(stderr, "Invalid tag type: %s\n", tag); @@ -86,33 +89,47 @@ int tag_add(const char *author, const char *tag, uint64_t record) { perror("Could not open tag file"); return 0; } - int n = write(tag_fd, &t, sizeof(struct tag)); - if (n < sizeof(struct tag)) { + int n = write(tag_fd, &t, sizeof(struct blergref)); + if (n < sizeof(struct blergref)) { perror("Could not write new tag"); return 0; } flock(tag_fd, LOCK_UN); close(tag_fd); + /* Set a flag for mentioned users */ + if (tag[0] == '@' && blerg_exists(tagval)) { + struct blerg *b = blerg_open(tagval); + if (b != NULL) { + blerg_set_status(b, BLERGSTATUS_MENTIONED, 1); + blerg_close(b); + } + } + return 1; } -struct tag * tag_list(const char *tag, uint64_t offset, int *count, int direction) { - char filename[512]; +struct blergref * tag_list(const char *tag, uint64_t offset, int *count, int direction) { + char filename[FILENAME_MAX]; struct stat st; - struct tag *taglist; - struct tag *retlist; + struct blergref *taglist; + struct blergref *retlist; uint64_t n_tag_records; if (!valid_tag_name(tag + 1)) return NULL; + + if (!(direction == 1 || direction == -1)) { + fprintf(stderr, "Invalid direction: %d\n", direction); + return NULL; + } switch(tag[0]) { case '#': - snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1); break; case '@': - snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1); break; default: fprintf(stderr, "Invalid tag type: %s\n", tag); @@ -129,7 +146,7 @@ struct tag * tag_list(const char *tag, uint64_t offset, int *count, int directio if (st.st_size == 0) { goto tag_list_map_failed; } - n_tag_records = st.st_size / sizeof(struct tag); + n_tag_records = st.st_size / sizeof(struct blergref); if (*count > n_tag_records - offset) *count = n_tag_records - offset; if (offset > n_tag_records) { @@ -137,22 +154,22 @@ struct tag * tag_list(const char *tag, uint64_t offset, int *count, int directio goto tag_list_map_failed; } - taglist = (struct tag *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0); + taglist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, tag_fd, 0); if (taglist == MAP_FAILED) { perror("Could not mmap tag file"); goto tag_list_map_failed; } - retlist = (struct tag *) malloc(sizeof(struct tag) * *count); + retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count); if (retlist == NULL) { perror("Could not allocate memory for tag list"); goto tag_list_malloc_failed; } switch(direction) { case 1: - memcpy(retlist, taglist + offset, sizeof(struct tag) * *count); + memcpy(retlist, taglist + offset, sizeof(struct blergref) * *count); break; case -1: - memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct tag) * *count); + memcpy(retlist, taglist + (n_tag_records - *count - offset), sizeof(struct blergref) * *count); break; } @@ -170,7 +187,7 @@ tag_list_open_failed: } int tag_exists(const char *tag) { - char filename[512]; + char filename[FILENAME_MAX]; if (!valid_tag_name(tag + 1)) return 0; @@ -182,10 +199,10 @@ int tag_exists(const char *tag) { switch(tag[0]) { case '#': - snprintf(filename, 512, "%s/%s", HASH_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.hash_tags_path, tag + 1); break; case '@': - snprintf(filename, 512, "%s/%s", REF_TAGS_PATH, tag + 1); + snprintf(filename, FILENAME_MAX, "%s/%s", blergconf.ref_tags_path, tag + 1); break; default: fprintf(stderr, "Invalid tag type: %s\n", tag);