1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
|
/* arbitron.c -- program to report readership statistics
*
* Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sysexits.h>
#include <syslog.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include "global.h"
#include "hash.h"
#include "mailbox.h"
#include "mpool.h"
#include "mboxlist.h"
#include "seen.h"
#include "util.h"
#include "xmalloc.h"
#include "xstrlcpy.h"
/* generated headers are not necessarily in current directory */
#include "imap/imap_err.h"
#define DB (config_seenstate_db)
#define SUBDB (config_subscription_db)
extern int optind;
extern char *optarg;
/* Maintain the mailbox list */
/* xxx it'd be nice to generate a subscriber list too */
struct user_list {
const char *user;
struct user_list *next;
};
struct arb_mailbox_data {
int nreaders;
struct user_list *readers;
int nsubscribers;
struct user_list *subscribers;
};
static struct mpool *arb_pool;
static hash_table mailbox_table, mboxname_table;
static time_t report_start_time = -1, report_end_time, prune_time = 0;
static int dosubs = 1;
static int dousers = 0;
static int long_report = 0;
/* current namespace */
static struct namespace arb_namespace;
/* forward declarations */
static void usage(void) __attribute__((noreturn));
static void run_users(void);
static void make_report(const char *key, void *data, void *rock);
static void process_seen(const char *path, const char *user);
static void process_subs(const char *path, const char *user);
static int do_mailbox(struct findall_data *data, void *rock);
int main(int argc,char **argv)
{
int opt, r;
int report_days = 30;
int prune_months = 0;
char pattern[MAX_MAILBOX_BUFFER];
char *alt_config = NULL;
time_t now = time(0);
strcpy(pattern, "*");
report_end_time = now;
while ((opt = getopt(argc, argv, "C:oud:D:p:l")) != EOF) {
switch (opt) {
case 'C': /* alt config file */
alt_config = optarg;
break;
case 'd':
if (report_start_time != -1) usage();
report_days = atoi(optarg);
if (report_days <= 0) usage();
break;
case 'D': {
unsigned month = 0, day = 0, year = 0;
struct tm date;
if (strlen(optarg) < 8 ||
sscanf(optarg, "%02u%02u%04u", &month, &day, &year) < 3) {
usage();
}
memset(&date, 0, sizeof(struct tm));
date.tm_mon = month - 1;
date.tm_mday = day;
date.tm_year = year - 1900;
report_start_time = mktime(&date);
if (optarg[8] == ':' && strlen(optarg+9) == 8) {
if (sscanf(optarg+9, "%02u%02u%04u", &month, &day, &year) < 3) {
usage();
}
memset(&date, 0, sizeof(struct tm));
date.tm_mon = month - 1;
date.tm_mday = day;
date.tm_year = year - 1900;
report_end_time = mktime(&date);
}
break;
}
case 'o':
dosubs = 0;
break;
case 'u':
dousers = 1;
break;
case 'p':
prune_months = atoi(optarg);
if (prune_months <= 0) usage();
break;
case 'l':
long_report = dousers = 1;
break;
default:
usage();
}
}
/* Init Cyrus Backend Foo */
cyrus_init(alt_config, "arbitron", 0, 0);
/* Set namespace -- force standard (internal) */
if ((r = mboxname_init_namespace(&arb_namespace, 1)) != 0) {
syslog(LOG_ERR, "%s", error_message(r));
fatal(error_message(r), EX_CONFIG);
}
if (optind != argc) strlcpy(pattern, argv[optind], sizeof(pattern));
if (report_start_time == -1) {
report_start_time = now - (report_days*60*60*24);
}
if (prune_months) {
prune_time = now - (prune_months*60*60*24*31);
}
/* Allocate our shared memory pools */
arb_pool = new_mpool(0);
construct_hash_table(&mailbox_table, 2047, 1);
construct_hash_table(&mboxname_table, 2047, 1);
/* Get the mailbox list */
fprintf(stderr, "Loading Mailboxes...");
mboxlist_findall(&arb_namespace, pattern, 1, 0, 0,
do_mailbox, NULL);
fprintf(stderr, "Done\nLoading Users");
/* Now do all the users */
run_users();
fprintf(stderr, "Done\n");
/* And print the report */
hash_enumerate(&mboxname_table, make_report, NULL);
/* Free Resources */
free_hash_table(&mailbox_table, NULL);
free_hash_table(&mboxname_table, NULL);
free_mpool(arb_pool);
cyrus_done();
return 0;
}
static void usage(void)
{
fprintf(stderr,
"usage: arbitron [-o] [-u] [-l] [-C alt_config] "
"[-d days | -D mmddyyy[:mmddyyyy]]\n"
" [-p months] [mboxpattern]\n");
exit(EX_USAGE);
}
static int do_mailbox(struct findall_data *data, void *rock __attribute__((unused)))
{
if (!data) return 0;
if (!data->is_exactmatch) return 0;
int r;
struct mailbox *mailbox = NULL;
const char *name = mbname_intname(data->mbname);
r = mailbox_open_irl(name, &mailbox);
if (r) return 0;
struct arb_mailbox_data *d = mpool_malloc(arb_pool,
sizeof(struct arb_mailbox_data));
d->nreaders = 0;
d->nsubscribers = 0;
d->readers = NULL;
d->subscribers = NULL;
hash_insert(mailbox_uniqueid(mailbox), d, &mailbox_table);
hash_insert(name, d, &mboxname_table);
mailbox_close(&mailbox);
return 0;
}
static void run_users(void)
{
char prefix[MAX_MAILBOX_PATH+1],path[2*MAX_MAILBOX_PATH+1],
file[3*MAX_MAILBOX_PATH+1];
DIR *dirp, *dirq;
struct dirent *dirent1, *dirent2;
snprintf(prefix, sizeof(prefix), "%s%s", config_dir, FNAME_USERDIR);
dirp = opendir(prefix);
if(!dirp) {
fatal("can't open user directory", EX_SOFTWARE);
}
while((dirent1 = readdir(dirp)) != NULL) {
if(!strcmp(dirent1->d_name, ".") || !strcmp(dirent1->d_name,"..")) {
continue;
}
snprintf(path, sizeof(path), "%s%s", prefix, dirent1->d_name);
/* printf("trying %s\n",path); */
dirq = opendir(path);
if(dirq) {
fprintf(stderr, ".");
while(dirq && ((dirent2 = readdir(dirq)) != NULL)) {
size_t len;
if(!strcmp(dirent2->d_name, ".") ||
!strcmp(dirent2->d_name,"..")) {
continue;
}
len = strlen(dirent2->d_name);
/* 5 is magic number for strlen(".seen") and
4 is the magic number for strlen(".sub") */
if(len > 4) {
char *user = NULL;
snprintf(file, sizeof(file),
"%s/%s", path, dirent2->d_name);
/* printf("got file %s\n",file); */
if(len > 5 &&
!strcmp(dirent2->d_name + len - 5, ".seen")) {
if (dousers) {
user = mpool_strndup(arb_pool, dirent2->d_name,
len-5);
}
process_seen(file, user);
} else if (dosubs &&
!strcmp(dirent2->d_name + len - 4, ".sub")) {
if (dousers) {
user = mpool_strndup(arb_pool, dirent2->d_name,
len-4);
}
process_subs(file, user);
}
}
}
closedir(dirq);
}
}
closedir(dirp);
}
static int process_user_cb(void *rockp,
const char *key, size_t keylen,
const char *tmpdata __attribute__((unused)),
size_t tmpdatalen __attribute__((unused)))
{
/* Only called to do deletes */
/* printf("pruning entry\n"); */
cyrusdb_delete((struct db *)rockp, key, keylen, NULL, 0);
return 0;
}
/* We can cheat and do all we need to in this function */
static int process_user_p(void *rockp,
const char *key,
size_t keylen,
const char *data,
size_t datalen __attribute__((unused)))
{
int ret = 0;
long version, lastread;
char *p;
char buf[64];
struct arb_mailbox_data *mbox;
const char *user = (const char *) rockp;
/* remember that 'data' may not be null terminated ! */
version = strtol(data, &p, 10); data = p;
if (version < 0) abort();
/* xxx not checking version */
lastread = strtol(data, &p, 10); data = p;
memcpy(buf, key, keylen);
buf[keylen] = '\0';
mbox = hash_lookup(buf, &mailbox_table);
if(mbox && lastread >= report_start_time &&
lastread <= report_end_time) {
/* printf("got %s\n", mbox->name); */
mbox->nreaders++;
if (user) {
struct user_list *u = mpool_malloc(arb_pool,
sizeof(struct user_list));
u->user = user;
u->next = mbox->readers;
mbox->readers = u;
}
}
/* Check for pruning even if mailbox isn't valid */
if(lastread < prune_time) {
ret = 1;
}
/* Only return true if we need to prune this guy */
return ret;
}
static void process_seen(const char *path, const char *user)
{
int r;
struct db *tmp = NULL;
r = cyrusdb_open(DB, path, 0, &tmp);
if(r) goto done;
cyrusdb_foreach(tmp, "", 0, process_user_p, process_user_cb,
(void *) user, NULL);
done:
if(tmp) cyrusdb_close(tmp);
}
static int process_subs_cb(void *rockp __attribute__((unused)),
const char *key __attribute__((unused)),
size_t keylen __attribute__((unused)),
const char *tmpdata __attribute__((unused)),
size_t tmpdatalen __attribute__((unused)))
{
return 0;
}
static int process_subs_p(void *rockp,
const char *key, size_t keylen,
const char *tmpdata __attribute__((unused)),
size_t tmpdatalen __attribute__((unused)))
{
struct arb_mailbox_data *mbox;
char buf[MAX_MAILBOX_BUFFER];
const char *user = (const char *) rockp;
memcpy(buf, key, keylen);
buf[keylen] = '\0';
/* printf("lookup %s\n", buf); */
mbox = hash_lookup(buf, &mboxname_table);
if(mbox) {
/* printf("got sub %s\n", buf); */
mbox->nsubscribers++;
if (user) {
struct user_list *u = mpool_malloc(arb_pool,
sizeof(struct user_list));
u->user = user;
u->next = mbox->subscribers;
mbox->subscribers = u;
}
}
return 0; /* never do callback */
}
static void process_subs(const char *path, const char *user)
{
int r;
struct db *tmp = NULL;
r = cyrusdb_open(SUBDB, path, 0, &tmp);
if(r) goto done;
cyrusdb_foreach(tmp, "", 0, process_subs_p, process_subs_cb,
(void *) user, NULL);
done:
if(tmp) cyrusdb_close(tmp);
}
static void report_users(struct user_list *u)
{
char sep = ':';
while (u) {
printf("%c%s", sep, u->user);
sep = ',';
u = u->next;
}
}
static void long_report_users(struct user_list *u, const char *mbox, char type)
{
char buf[100];
struct tm *tm;
while (u) {
printf("%s,%s,%c,", mbox, u->user, type);
tm = localtime(&report_start_time);
strftime(buf, sizeof(buf), "%m-%d-%Y %H:%M:%S", tm);
printf("%s,", buf);
tm = localtime(&report_end_time);
strftime(buf, sizeof(buf), "%m-%d-%Y %H:%M:%S", tm);
printf("%s\n", buf);
u = u->next;
}
}
static void make_report(const char *key, void *data, void *rock __attribute__((unused)))
{
struct arb_mailbox_data *mbox = (struct arb_mailbox_data *)data;
/* Skip underread user mailboxes */
if(!strncasecmp(key, "user.", 5) && mbox->nreaders <= 1)
return;
char *extname = mboxname_to_external(key, &arb_namespace, NULL);
if (long_report) {
long_report_users(mbox->readers, extname, 'r');
long_report_users(mbox->subscribers, extname, 's');
}
else {
printf("%s %d", extname, mbox->nreaders);
if (dousers) report_users(mbox->readers);
if (dosubs) {
printf(" %d", mbox->nsubscribers);
if (dousers) report_users(mbox->subscribers);
}
printf("\n");
}
free(extname);
}
|