diff options
-rw-r--r-- | include/proto.h | 7 | ||||
-rw-r--r-- | include/pttstruct.h | 2 | ||||
-rw-r--r-- | mbbsd/Makefile | 2 | ||||
-rw-r--r-- | mbbsd/file.c | 83 | ||||
-rw-r--r-- | mbbsd/friend.c | 75 | ||||
-rw-r--r-- | mbbsd/stuff.c | 16 | ||||
-rw-r--r-- | mbbsd/user.c | 2 | ||||
-rw-r--r-- | pttbbs.mk | 4 |
8 files changed, 137 insertions, 54 deletions
diff --git a/include/proto.h b/include/proto.h index 6c42c241..187f4c90 100644 --- a/include/proto.h +++ b/include/proto.h @@ -239,6 +239,12 @@ fav_t *get_fav_root(void); void updatenewfav(int mode); void subscribe_newfav(void); +/* file */ +int file_count_line(char *file); +int file_append_line(char *file, char *string); +int file_delete_line(char *file, char *string); +int file_exist_record(char *file, char *string); + /* friend */ void friend_edit(int type); void friend_load(int); @@ -246,6 +252,7 @@ int t_override(void); int t_reject(void); void friend_add(char *uident, int type, char *des); void friend_delete(char *uident, int type); +void friend_delete_all(char *uident, int type); void friend_special(void); void setfriendfile(char *fpath, int type); diff --git a/include/pttstruct.h b/include/pttstruct.h index 8f00de01..135967ee 100644 --- a/include/pttstruct.h +++ b/include/pttstruct.h @@ -80,7 +80,7 @@ typedef struct userec_t { chicken_t mychicken; time_t lastsong; unsigned int loginview; - unsigned char channel; /* 動態看板 */ + unsigned char channel; /* 動態看板 (unused?) */ unsigned short vl_count; /* ViolateLaw counter */ unsigned short five_win; unsigned short five_lose; diff --git a/mbbsd/Makefile b/mbbsd/Makefile index abfa1e0a..124ae821 100644 --- a/mbbsd/Makefile +++ b/mbbsd/Makefile @@ -14,7 +14,7 @@ OBJS= admin.o announce.o args.o assess.o bbs.o board.o cache.o cal.o card.o\ gomo.o guess.o indict.o io.o kaede.o lovepaper.o mail.o mbbsd.o menu.o\ more.o name.o osdep.o othello.o page.o read.o record.o register.o\ screen.o stuff.o talk.o term.o topsong.o user.o brc.o vice.o vote.o\ - xyz.o voteboard.o syspost.o var.o passwd.o calendar.o go.o + xyz.o voteboard.o syspost.o var.o passwd.o calendar.o go.o file.o .if defined(MERGEBBS) CFLAGS+= -DMERGEBBS diff --git a/mbbsd/file.c b/mbbsd/file.c new file mode 100644 index 00000000..aced35ff --- /dev/null +++ b/mbbsd/file.c @@ -0,0 +1,83 @@ +/* $Id: file.c 2191 2004-09-10 00:49:47Z victor $ */ + +#include "bbs.h" + +int file_count_line(char *file) +{ + FILE *fp; + int count = 0; + char buf[200]; + + if ((fp = fopen(file, "r"))) { + while (fgets(buf, sizeof(buf), fp)) + count++; + fclose(fp); + } + return count; +} + +int file_append_line(char *file, char *string) +{ + FILE *fp; + if ((fp = fopen(file, "a")) == NULL) + return -1; + flock(fileno(fp), LOCK_EX); + fputs(string, fp); + flock(fileno(fp), LOCK_UN); + fclose(fp); + return 0; +} + +int file_delete_line(char *file, char *string) +{ + FILE *fp, *nfp = NULL; + char fnew[80]; + char genbuf[STRLEN + 1]; + + sprintf(fnew, "%s.%3.3X", file, rand() & 0xFFF); + if ((fp = fopen(file, "r")) && (nfp = fopen(fnew, "w"))) { + int length = strlen(string); + + while (fgets(genbuf, sizeof(genbuf), fp)) + if ((genbuf[0] > ' ') && strncmp(genbuf, string, length)) + fputs(genbuf, nfp); + Rename(fnew, file); + } + if(fp) + fclose(fp); + if(nfp) + fclose(nfp); + return 0; +} + +int file_exist_record(char *file, char *string) +{ + FILE *fp; + char buf[STRLEN], *ptr; + + if ((fp = fopen(file, "r")) == NULL) + return 0; + + while (fgets(buf, STRLEN, fp)) { + if ((ptr = strtok(buf, str_space)) && !strcasecmp(ptr, string)) + return 1; + } + fclose(fp); + return 0; +} + +int file_foreach_entry(char *file, int (*func)(char *, int), int info) +{ + char line[80]; + FILE *fp; + + if ((fp = fopen(file, "r")) == NULL) + return -1; + + while (fgets(line, sizeof(line), fp)) { + (*func)(line, info); + } + + fclose(fp); + return 0; +} diff --git a/mbbsd/friend.c b/mbbsd/friend.c index d70a33da..64761c2a 100644 --- a/mbbsd/friend.c +++ b/mbbsd/friend.c @@ -56,19 +56,10 @@ setfriendfile(char *fpath, int type) setbfile(fpath, currboard, friend_file[type]); } -static int +inline static int friend_count(char *fname) { - FILE *fp; - int count = 0; - char buf[200]; - - if ((fp = fopen(fname, "r"))) { - while (fgets(buf, sizeof(buf), fp)) - count++; - fclose(fp); - } - return count; + return file_count_line(fname); } void @@ -81,8 +72,7 @@ friend_add(char *uident, int type, char* des) return; if ((uident[0] > ' ') && !belong(fpath, uident)) { - FILE *fp; - char buf[40] = ""; + char buf[40] = "", buf2[256]; char t_uident[IDLEN + 1]; /* Thor: avoid uident run away when get data */ @@ -95,12 +85,8 @@ friend_add(char *uident, int type, char* des) getdata_str(2, 0, friend_desc[type], buf, sizeof(buf), DOECHO, des); } - if ((fp = fopen(fpath, "a"))) { - flock(fileno(fp), LOCK_EX); - fprintf(fp, "%-13s%s\n", t_uident, buf); - flock(fileno(fp), LOCK_UN); - fclose(fp); - } + sprintf(buf2, "%-13s%s\n", t_uident, buf); + file_append_line(fpath, buf2); } } @@ -184,7 +170,7 @@ friend_append(int type, int count) char the_id[15]; sscanf(buf, "%s", the_id); // XXX check buffer size - if (!belong(fpath, the_id)) { + if (!file_exist_record(fpath, the_id)) { if ((fp1 = fopen(fpath, "a"))) { flock(fileno(fp1), LOCK_EX); fputs(buf, fp1); @@ -200,25 +186,44 @@ friend_append(int type, int count) void friend_delete(char *uident, int type) { - FILE *fp, *nfp = NULL; - char fn[80], fnnew[80]; - char genbuf[200]; - + char fn[80]; setfriendfile(fn, type); + file_delete_line(fn, uident); +} - sprintf(fnnew, "%s-", fn); - if ((fp = fopen(fn, "r")) && (nfp = fopen(fnnew, "w"))) { - int length = strlen(uident); +static void +delete_user_friend(char *uident, char *friend, int type) +{ + char fn[80]; +#if 0 + if (type == FRIEND_ALOHA) { +#endif + sethomefile(fn, uident, "aloha"); + file_delete_line(fn, friend); +#if 0 + } + else { + } +#endif +} - while (fgets(genbuf, STRLEN, fp)) - if ((genbuf[0] > ' ') && strncmp(genbuf, uident, length)) - fputs(genbuf, nfp); - Rename(fnnew, fn); +void +friend_delete_all(char *uident, int type) +{ + char buf[80], line[80]; + FILE *fp; + + sethomefile(buf, uident, friend_file[type]); + + if ((fp = fopen(buf, "r")) == NULL) + return; + + while (fgets(line, sizeof(line), fp)) { + sscanf(line, "%s", buf); + delete_user_friend(buf, uident, type); } - if(fp) - fclose(fp); - if(nfp) - fclose(nfp); + + fclose(fp); } static void diff --git a/mbbsd/stuff.c b/mbbsd/stuff.c index 2f41f536..4002f39e 100644 --- a/mbbsd/stuff.c +++ b/mbbsd/stuff.c @@ -300,21 +300,7 @@ dashd(char *fname) int belong(char *filelist, char *key) { - FILE *fp; - int rc = 0; - - if ((fp = fopen(filelist, "r"))) { - char buf[STRLEN], *ptr; - - while (fgets(buf, STRLEN, fp)) { - if ((ptr = strtok(buf, str_space)) && !strcasecmp(ptr, key)) { - rc = 1; - break; - } - } - fclose(fp); - } - return rc; + return file_exist_record(filelist, key); } unsigned int diff --git a/mbbsd/user.c b/mbbsd/user.c index e2813c77..f4145381 100644 --- a/mbbsd/user.c +++ b/mbbsd/user.c @@ -202,6 +202,7 @@ violate_law(userec_t * u, int unum) char src[STRLEN], dst[STRLEN]; snprintf(src, sizeof(src), "home/%c/%s", u->userid[0], u->userid); snprintf(dst, sizeof(dst), "tmp/%s", u->userid); + friend_delete_all(u->userid, FRIEND_ALOHA); Rename(src, dst); post_violatelaw(u->userid, cuser.userid, reason, "砍除 ID"); kill_user(unum); @@ -653,6 +654,7 @@ uinfo_query(userec_t * u, int real, int unum) snprintf(src, sizeof(src), "home/%c/%s", x.userid[0], x.userid); snprintf(dst, sizeof(dst), "tmp/%s", x.userid); + friend_delete_all(x.userid, FRIEND_ALOHA); Rename(src, dst); /* do not remove user home */ kill_user(unum); return; @@ -33,8 +33,8 @@ CFLAGS_Solaris= -DSolaris -DHAVE_DES_CRYPT -I/usr/local/include LDFLAGS_Solaris= -L/usr/local/lib -L/usr/lib/ LIBS_Solaris= -lnsl -lsocket -liconv -lkstat -OS_FLAGS= -D__OS_MAJOR_VERSION__=$(OS_MAJOR_VER) \ - -D__OS_MINOR_VERSION__=$(OS_MINOR_VER) +OS_FLAGS= -D__OS_MAJOR_VERSION__="$(OS_MAJOR_VER)" \ + -D__OS_MINOR_VERSION__="$(OS_MINOR_VER)" # CFLAGS, LDFLAGS, LIBS 加入 OS 相關參數 PTT_CFLAGS+= $(CFLAGS_$(OSTYPE)) $(OS_FLAGS) |