| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2008 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file choose.c |
| 21 | * @brief Random track chooser |
| 22 | * |
| 23 | * Picks a track at random and writes it to standard output. If for |
| 24 | * any reason no track can be picked - even a trivial reason like a |
| 25 | * deadlock - it just exits and expects the server to try again. |
| 26 | */ |
| 27 | |
| 28 | #include <config.h> |
| 29 | #include "types.h" |
| 30 | |
| 31 | #include <getopt.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <db.h> |
| 35 | #include <locale.h> |
| 36 | #include <errno.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
| 39 | #include <pcre.h> |
| 40 | #include <string.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <syslog.h> |
| 43 | #include <time.h> |
| 44 | |
| 45 | #include "configuration.h" |
| 46 | #include "log.h" |
| 47 | #include "defs.h" |
| 48 | #include "mem.h" |
| 49 | #include "kvp.h" |
| 50 | #include "syscalls.h" |
| 51 | #include "printf.h" |
| 52 | #include "trackdb.h" |
| 53 | #include "trackdb-int.h" |
| 54 | #include "version.h" |
| 55 | #include "trackname.h" |
| 56 | #include "queue.h" |
| 57 | #include "server-queue.h" |
| 58 | |
| 59 | static DB_TXN *global_tid; |
| 60 | |
| 61 | static const struct option options[] = { |
| 62 | { "help", no_argument, 0, 'h' }, |
| 63 | { "version", no_argument, 0, 'V' }, |
| 64 | { "config", required_argument, 0, 'c' }, |
| 65 | { "debug", no_argument, 0, 'd' }, |
| 66 | { "no-debug", no_argument, 0, 'D' }, |
| 67 | { "syslog", no_argument, 0, 's' }, |
| 68 | { "no-syslog", no_argument, 0, 'S' }, |
| 69 | { 0, 0, 0, 0 } |
| 70 | }; |
| 71 | |
| 72 | /* display usage message and terminate */ |
| 73 | static void help(void) { |
| 74 | xprintf("Usage:\n" |
| 75 | " disorder-choose [OPTIONS]\n" |
| 76 | "Options:\n" |
| 77 | " --help, -h Display usage message\n" |
| 78 | " --version, -V Display version number\n" |
| 79 | " --config PATH, -c PATH Set configuration file\n" |
| 80 | " --debug, -d Turn on debugging\n" |
| 81 | " --[no-]syslog Enable/disable logging to syslog\n" |
| 82 | "\n" |
| 83 | "Track choose for DisOrder. Not intended to be run\n" |
| 84 | "directly.\n"); |
| 85 | xfclose(stdout); |
| 86 | exit(0); |
| 87 | } |
| 88 | |
| 89 | /** @brief Weighted track record */ |
| 90 | struct weighted_track { |
| 91 | /** @brief Next track in the list */ |
| 92 | struct weighted_track *next; |
| 93 | /** @brief Track name */ |
| 94 | const char *track; |
| 95 | /** @brief Weight for this track (always positive) */ |
| 96 | unsigned long weight; |
| 97 | }; |
| 98 | |
| 99 | /** @brief List of tracks with nonzero weight */ |
| 100 | static struct weighted_track *tracks; |
| 101 | |
| 102 | /** @brief Sum of all weights */ |
| 103 | static unsigned long long total_weight; |
| 104 | |
| 105 | /** @brief Count of tracks */ |
| 106 | static long ntracks; |
| 107 | |
| 108 | static char **required_tags; |
| 109 | static char **prohibited_tags; |
| 110 | |
| 111 | static int queue_contains(const struct queue_entry *head, |
| 112 | const char *track) { |
| 113 | const struct queue_entry *q; |
| 114 | |
| 115 | for(q = head->next; q != head; q = q->next) |
| 116 | if(!strcmp(q->track, track)) |
| 117 | return 1; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /** @brief Compute the weight of a track |
| 122 | * @param track Track name (UTF-8) |
| 123 | * @param data Track data |
| 124 | * @param prefs Track preferences |
| 125 | * @return Track weight (non-negative) |
| 126 | * |
| 127 | * Tracks to be excluded entirely are given a weight of 0. |
| 128 | */ |
| 129 | static unsigned long compute_weight(const char *track, |
| 130 | struct kvp *data, |
| 131 | struct kvp *prefs) { |
| 132 | const char *s; |
| 133 | char **track_tags; |
| 134 | time_t last, now; |
| 135 | |
| 136 | /* Reject tracks not in any collection (race between edit config and |
| 137 | * rescan) */ |
| 138 | if(!find_track_root(track)) { |
| 139 | info("found track not in any collection: %s", track); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* Reject aliases to avoid giving aliased tracks extra weight */ |
| 144 | if(kvp_get(data, "_alias_for")) |
| 145 | return 0; |
| 146 | |
| 147 | /* Reject tracks with random play disabled */ |
| 148 | if((s = kvp_get(prefs, "pick_at_random")) |
| 149 | && !strcmp(s, "0")) |
| 150 | return 0; |
| 151 | |
| 152 | /* Reject tracks played within the last 8 hours */ |
| 153 | if((s = kvp_get(prefs, "played_time"))) { |
| 154 | last = atoll(s); |
| 155 | now = time(0); |
| 156 | if(now < last + 8 * 3600) /* TODO configurable */ |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* Reject tracks currently in the queue or in the recent list */ |
| 161 | if(queue_contains(&qhead, track) |
| 162 | || queue_contains(&phead, track)) |
| 163 | return 0; |
| 164 | |
| 165 | /* We'll need tags for a number of things */ |
| 166 | track_tags = parsetags(kvp_get(prefs, "tags")); |
| 167 | |
| 168 | /* Reject tracks with prohibited tags */ |
| 169 | if(prohibited_tags && tag_intersection(track_tags, prohibited_tags)) |
| 170 | return 0; |
| 171 | |
| 172 | /* Reject tracks that lack required tags */ |
| 173 | if(*required_tags && !tag_intersection(track_tags, required_tags)) |
| 174 | return 0; |
| 175 | |
| 176 | return 90000; |
| 177 | } |
| 178 | |
| 179 | /** @brief Called for each track */ |
| 180 | static int collect_tracks_callback(const char *track, |
| 181 | struct kvp *data, |
| 182 | struct kvp *prefs, |
| 183 | void attribute((unused)) *u, |
| 184 | DB_TXN attribute((unused)) *tid) { |
| 185 | const unsigned long weight = compute_weight(track, data, prefs); |
| 186 | |
| 187 | if(weight) { |
| 188 | struct weighted_track *const t = xmalloc(sizeof *t); |
| 189 | |
| 190 | t->next = tracks; |
| 191 | t->track = track; |
| 192 | t->weight = weight; |
| 193 | tracks = t; |
| 194 | total_weight += weight; |
| 195 | ++ntracks; |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /** @brief Pick a random integer uniformly from [0, limit) */ |
| 201 | static unsigned long long pick_weight(unsigned long long limit) { |
| 202 | unsigned long long n; |
| 203 | static int fd = -1; |
| 204 | int r; |
| 205 | |
| 206 | if(fd < 0) { |
| 207 | if((fd = open("/dev/urandom", O_RDONLY)) < 0) |
| 208 | fatal(errno, "opening /dev/urandom"); |
| 209 | } |
| 210 | if((r = read(fd, &n, sizeof n)) < 0) |
| 211 | fatal(errno, "reading /dev/urandom"); |
| 212 | if((size_t)r < sizeof n) |
| 213 | fatal(0, "short read from /dev/urandom"); |
| 214 | return n % limit; |
| 215 | } |
| 216 | |
| 217 | /** @brief Pick a track at random and write it to stdout */ |
| 218 | static void pick_track(void) { |
| 219 | long long w; |
| 220 | struct weighted_track *t; |
| 221 | |
| 222 | w = pick_weight(total_weight); |
| 223 | t = tracks; |
| 224 | while(t && w >= t->weight) { |
| 225 | w -= t->weight; |
| 226 | t = t->next; |
| 227 | } |
| 228 | if(!t) |
| 229 | fatal(0, "ran out of tracks but %lld weighting left", w); |
| 230 | xprintf("%s", t->track); |
| 231 | } |
| 232 | |
| 233 | int main(int argc, char **argv) { |
| 234 | int n, logsyslog = !isatty(2), err; |
| 235 | const char *tags; |
| 236 | |
| 237 | set_progname(argv); |
| 238 | mem_init(); |
| 239 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
| 240 | while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) { |
| 241 | switch(n) { |
| 242 | case 'h': help(); |
| 243 | case 'V': version("disorder-choose"); |
| 244 | case 'c': configfile = optarg; break; |
| 245 | case 'd': debugging = 1; break; |
| 246 | case 'D': debugging = 0; break; |
| 247 | case 'S': logsyslog = 0; break; |
| 248 | case 's': logsyslog = 1; break; |
| 249 | default: fatal(0, "invalid option"); |
| 250 | } |
| 251 | } |
| 252 | if(logsyslog) { |
| 253 | openlog(progname, LOG_PID, LOG_DAEMON); |
| 254 | log_default = &log_syslog; |
| 255 | } |
| 256 | if(config_read(0)) fatal(0, "cannot read configuration"); |
| 257 | /* Find out current queue/recent list */ |
| 258 | queue_read(); |
| 259 | recent_read(); |
| 260 | /* Generate the candidate track list */ |
| 261 | trackdb_init(TRACKDB_NO_RECOVER); |
| 262 | trackdb_open(TRACKDB_NO_UPGRADE|TRACKDB_READ_ONLY); |
| 263 | global_tid = trackdb_begin_transaction(); |
| 264 | if((err = trackdb_get_global_tid("required-tags", global_tid, &tags))) |
| 265 | fatal(0, "error getting required-tags: %s", db_strerror(err)); |
| 266 | required_tags = parsetags(tags); |
| 267 | if((err = trackdb_get_global_tid("prohibited-tags", global_tid, &tags))) |
| 268 | fatal(0, "error getting prohibited-tags: %s", db_strerror(err)); |
| 269 | prohibited_tags = parsetags(tags); |
| 270 | if(trackdb_scan(0, collect_tracks_callback, 0, global_tid)) |
| 271 | exit(1); |
| 272 | trackdb_commit_transaction(global_tid); |
| 273 | trackdb_close(); |
| 274 | trackdb_deinit(); |
| 275 | //info("ntracks=%ld total_weight=%lld", ntracks, total_weight); |
| 276 | if(!total_weight) |
| 277 | fatal(0, "no tracks match random choice criteria"); |
| 278 | /* Pick a track */ |
| 279 | pick_track(); |
| 280 | xfclose(stdout); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | Local Variables: |
| 286 | c-basic-offset:2 |
| 287 | comment-column:40 |
| 288 | fill-column:79 |
| 289 | indent-tabs-mode:nil |
| 290 | End: |
| 291 | */ |