diff options
author | victor <victor@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-09-20 20:57:23 +0800 |
---|---|---|
committer | victor <victor@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-09-20 20:57:23 +0800 |
commit | 81c98e60178549e9933961219d370252e91198cb (patch) | |
tree | 1b8d399ee196b1d1cdbd152ab261c0e598d20fc0 /mbbsd | |
parent | c909e6f54001e88ab3017d9e5ac49d60da6b00cd (diff) | |
download | pttbbs-81c98e60178549e9933961219d370252e91198cb.tar pttbbs-81c98e60178549e9933961219d370252e91198cb.tar.gz pttbbs-81c98e60178549e9933961219d370252e91198cb.tar.bz2 pttbbs-81c98e60178549e9933961219d370252e91198cb.tar.lz pttbbs-81c98e60178549e9933961219d370252e91198cb.tar.xz pttbbs-81c98e60178549e9933961219d370252e91198cb.tar.zst pttbbs-81c98e60178549e9933961219d370252e91198cb.zip |
fix bug: when deleting an user, the aloha record won't be deleted.
add file: file.c, move some useful subroutine out of friend.c
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@2205 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd')
-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 |
5 files changed, 127 insertions, 51 deletions
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; |