/* $Id: boardlist.c,v 1.9 2006/06/18 16:50:03 starfish Exp $ */ #include "boardlist.h" // #include "../include/perm.h" /* Set *bl to the root board list. Return non-zero if failure. */ int db_BoardList_InitRoot(db_boardlist_t *bl, db_database_t *db) { if (db==NULL || db->SHM==NULL || !db->SHM->loaded) return 1; bl->database = db; bl->bcache = db->SHM->bcache; bl->bnumber = db->SHM->Bnumber; bl->groupid = 1; bl->attr_mask = BRD_HIDE; /* default hide the hidden boards */ if (bl->database->userinfo == NULL) bl->perm = 0; else bl->perm = bl->database->userinfo->userlevel; return 0; } /* Set *bl be the boardlist of a subgroup. It returns non-zero if failure. */ int db_BoardList_InitSublist(db_boardlist_t *bl, db_board_t *sub) { if (!db_Board_Attribute(sub, BRD_GROUPBOARD)) return 1; bl->database = sub->database; bl->bcache = bl->database->SHM->bcache; bl->bnumber = bl->database->SHM->Bnumber; bl->groupid = sub->boardid + 1; bl->attr_mask = BRD_HIDE; /* default hide the hidden boards */ if (bl->database->userinfo == NULL) bl->perm = 0; else bl->perm = bl->database->userinfo->userlevel; return 0; } void db_BoardList_Done(db_boardlist_t *bl) { bl->database = NULL; bl->bcache = NULL; bl->bnumber = 0; bl->groupid = 0; bl->attr_mask = 0; bl->perm = 0; } static int is_BM_cache(db_boardlist_t const *bl, unsigned int bid) { userinfo_t *ui = bl->database->userinfo; SHM_t *shm = bl->database->SHM; if (ui->uid == shm->BMcache[bid][0] || ui->uid == shm->BMcache[bid][1] || ui->uid == shm->BMcache[bid][2] || ui->uid == shm->BMcache[bid][3]) { // bl->perm |= PERM_BM; return 1; } return 0; } static int hbflcheck(db_boardlist_t const *bl, unsigned int bid) { SHM_t *shm = bl->database->SHM; userinfo_t *ui = bl->database->userinfo; int i; /* if (shm->hbfl[bid][0] < login_start_time - HBFLexpire) hbflreload(bid); */ for (i = 1; shm->hbfl[bid][i] != 0 && i <= MAX_FRIEND; ++i) { if (shm->hbfl[bid][i] == ui->uid) return 0; } return 1; } static int Permit(db_boardlist_t const *bl, unsigned int bid) { boardheader_t *bptr = bl->bcache + bid; int level = bptr->level; int brdattr = bptr->brdattr; if (bl->database->userinfo != NULL) { if (is_BM_cache(bl, bid)) return 1; if (brdattr & BRD_HIDE) { /* check friend list for hidden boards */ if (hbflcheck(bl, bid)) { if (brdattr & BRD_POSTMASK) return 0; else return 2; } else return 1; } } if (level && !(brdattr & BRD_POSTMASK) && !(level ? bl->perm & level : 1)) return 0; return 1; } /* This function list the "from"-th to "to"-th boards to the callback function. If "to" is zero or negative, there is no upper bound. "datum" will pass to the callback function without any modification. The data past to callback function may destory after it returned. Once the callback return non-zero, this function will stop and return. */ int db_BoardList_List(db_boardlist_t const *bl, unsigned int from, unsigned int to, int (*callback)(db_board_t const *board, void *datum), void *datum) { unsigned int i, count; db_board_t board; board.database = bl->database; /* scan the boardlist of all boards */ for (count=i=0; i<=bl->bnumber; ++i) { if (bl->bcache[i].brdname[0] != '\0' && (!bl->groupid || bl->bcache[i].gid==bl->groupid) && Permit(bl, i) ) { ++count; if (to > 0 && count > to) break; if (count >= from) { /* if match the conditions, send it to callback function */ board.boardid = i; board.header = bl->bcache + i; if (callback(&board, datum)) break; } } } return 0; }