diff options
Diffstat (limited to 'mbbsd')
-rw-r--r-- | mbbsd/angel.c | 204 | ||||
-rw-r--r-- | mbbsd/chat.c | 4 |
2 files changed, 150 insertions, 58 deletions
diff --git a/mbbsd/angel.c b/mbbsd/angel.c index 2e918219..ef0a4fac 100644 --- a/mbbsd/angel.c +++ b/mbbsd/angel.c @@ -5,6 +5,8 @@ #ifdef PLAY_ANGEL +#define FN_ANGELMSG "angelmsg" + void angel_toggle_pause() { @@ -17,10 +19,83 @@ angel_toggle_pause() cuser.uflag2 &= ~UF2_ANGEL_OLDMASK; } +// cache my angel's nickname +static char _myangel[IDLEN+1] = "", + _myangel_nick[IDLEN+1] = ""; +static time4_t _myangel_touched = 0; +static char _valid_angelmsg = 0; + void -angel_load_data() +angel_reload_nick() +{ + char reload = 0; + char fn[PATHLEN]; + time4_t ts = 0; + FILE *fp = NULL; + + fn[0] = 0; + // see if we have angel id change (reload whole) + if (strcmp(_myangel, cuser.myangel) != 0) + { + strlcpy(_myangel, cuser.myangel, sizeof(_myangel)); + reload = 1; + } + // see if we need to check file touch date + if (!reload && _myangel[0] && _myangel[0] != '-') + { + sethomefile(fn, _myangel, FN_ANGELMSG); + ts = dasht(fn); + if (ts != -1 && ts > _myangel_touched) + reload = 1; + } + // if no need to reload, reuse current data. + if (!reload) + { + // vmsg("angel_data: no need to reload."); + return; + } + + // reset cache + _myangel_touched = ts; + _myangel_nick[0] = 0; + _valid_angelmsg = 0; + + // quick check + if (_myangel[0] == '-' || !_myangel[0]) + return; + + // do reload data. + if (!fn[0]) + { + sethomefile(fn, _myangel, FN_ANGELMSG); + ts = dasht(fn); + _myangel_touched = ts; + } + + assert(*fn); + // complex load + fp = fopen(fn, "rt"); + if (fp) + { + _valid_angelmsg = 1; + if (fgets(fn, sizeof(fn), fp)) + { + // verify first line + if (fn[0] == '%' && fn[1] == '%' && fn[2] == '[') + { + chomp(fn+3); + strlcpy(_myangel_nick, fn+3, sizeof(_myangel_nick)); + } + } + fclose(fp); + } +} + +const char * +angel_get_nickprefix() { - // TODO cache angelmsg here. + angel_reload_nick(); + return _myangel_nick; } int @@ -103,6 +178,8 @@ t_angelmsg(){ unlink(buf); else { FILE* fp = fopen(buf, "w"); + if (!fp) + return 0; if(nick[0]) fprintf(fp, "%%%%[%s\n", nick); for (i = 0; i < 3 && msg[i][0]; ++i) { @@ -185,7 +262,7 @@ GotoNewHand(){ // usually crashed as 'assert(currbid == brc_currbid)' if (currboard[0]) { strlcpy(old_board, currboard, IDLEN + 1); - currboard = "";// force enter_board + currboard = ""; // force enter_board } if (enter_board(BN_NEWBIE) == 0) @@ -202,6 +279,8 @@ GotoNewHand(){ static inline void NoAngelFound(const char* msg){ move(b_lines, 0); + if (!msg) + msg = "�A���p�Ѩϲ{�b���b�u�W"; outs(msg); if (currutmp == NULL || currutmp->mode != EDITING) outs("�A�Х��b�s��O�W�M�䵪�שΫ� Ctrl-P �o��"); @@ -214,81 +293,89 @@ NoAngelFound(const char* msg){ static inline void AngelNotOnline(){ - char buf[PATHLEN] = ""; - const static char* const not_online_message = "�z���p�Ѩϲ{�b���b�u�W"; - - // TODO cache angel's nick name! + char buf[PATHLEN]; + FILE *fp; + + // use cached angel data (assume already called before.) + // angel_reload_nick(); + if (!_valid_angelmsg) + { + NoAngelFound(NULL); + return; + } - if (cuser.myangel[0] != '-') - sethomefile(buf, cuser.myangel, "angelmsg"); - if (cuser.myangel[0] == '-' || !dashf(buf)) - NoAngelFound(not_online_message); - else { - time4_t mod = dasht(buf); - FILE* fp = fopen(buf, "r"); - clear(); - showtitle("�p�Ѩϯd��", BBSNAME); - move(4, 0); - buf[0] = 0; - fgets(buf, sizeof(buf), fp); - if (strncmp(buf, "%%[", 3) == 0) { - chomp(buf); - prints("�z��%s�p�Ѩϲ{�b���b�u�W", buf + 3); - fgets(buf, sizeof(buf), fp); - } else - outs(not_online_message); - - outs("\n͢�d�����A�G\n"); - outs(ANSI_COLOR(1;31;44) "��s�w�w�w�w�w�w�w�w�w�w�w�w�w�w�t" ANSI_COLOR(37) "" - "�p�Ѩϯd��" ANSI_COLOR(31) "�u�w�w�w�w�w�w�w�w�w�w�w�w�w�w�s��" ANSI_RESET "\n"); - outs(ANSI_COLOR(1;31) "�~�t" ANSI_COLOR(32) " �p�Ѩ� " - " " ANSI_COLOR(31) "�u��" ANSI_RESET "\n"); - do { - chomp(buf); - prints(ANSI_COLOR(1;31) "�x" ANSI_RESET "%-74.74s" ANSI_COLOR(1;31) "�x" ANSI_RESET "\n", buf); - } while (fgets(buf, sizeof(buf), fp)); - - outs(ANSI_COLOR(1;31) "���s�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w" - "�w�w�w�w�w�w�w�w�w�w�w�w�w�s��" ANSI_RESET "\n"); - outs(ANSI_COLOR(1;31;44) "��r�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w" - "�w�w�w�w�w�w�w�w�w�w�w�w�w�w�r��" ANSI_RESET "\n"); - prints("%55s%s", "�d�����: ", Cdatelite(&mod)); - - - move(b_lines - 4, 0); - outs("�p�D�H�ϥΤW���D�䤣��p�ѨϽШ�s�⪩(" BN_NEWBIE ")\n" - " �Q�d�����p�ѨϽШ�\\�@��(AngelPray)\n" - " �Q��ݪO�b�����ܥi��(AskBoard)\n" - "�Х��b�U�O�W�M�䵪�שΫ� Ctrl-P �o��"); - pressanykey(); - - GotoNewHand(); + // valid angelmsg is ready for being loaded. + sethomefile(buf, cuser.myangel, FN_ANGELMSG); + fp = fopen(buf, "rt"); + if (!fp) + { + // safer + NoAngelFound(NULL); + return; + } + clear(); + showtitle("�p�Ѩϯd��", BBSNAME); + move(4, 0); + buf[0] = 0; + prints("�z��%s�p�Ѩϲ{�b���b�u�W", _myangel_nick); + + outs("\n͢�d�����A�G\n"); + outs(ANSI_COLOR(1;31;44) "��s�w�w�w�w�w�w�w�w�w�w�w�w�w�w�t" ANSI_COLOR(37) "" + "�p�Ѩϯd��" ANSI_COLOR(31) "�u�w�w�w�w�w�w�w�w�w�w�w�w�w�w�s��" ANSI_RESET "\n"); + outs(ANSI_COLOR(1;31) "�~�t" ANSI_COLOR(32) " �p�Ѩ� " + " " ANSI_COLOR(31) "�u��" ANSI_RESET "\n"); + fgets(buf, sizeof(buf), fp); // skip first line: entry for nick + while (fgets(buf, sizeof(buf), fp)) + { + chomp(buf); + prints(ANSI_COLOR(1;31) "�x" ANSI_RESET "%-74.74s" ANSI_COLOR(1;31) "�x" ANSI_RESET "\n", buf); } + fclose(fp); + outs(ANSI_COLOR(1;31) "���s�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w" + "�w�w�w�w�w�w�w�w�w�w�w�w�w�s��" ANSI_RESET "\n"); + outs(ANSI_COLOR(1;31;44) "��r�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w" + "�w�w�w�w�w�w�w�w�w�w�w�w�w�w�r��" ANSI_RESET "\n"); + prints("%55s%s", "�d�����: ", Cdatelite(&_myangel_touched)); + + + move(b_lines - 4, 0); + outs("�p�D�H�ϥΤW���D�䤣��p�ѨϽШ�s�⪩(" BN_NEWBIE ")\n" + " �Q�d�����p�ѨϽШ�\\�@��(AngelPray)\n" + " �Q��ݪO�b�����ܥi��(AskBoard)\n" + "�Х��b�U�O�W�M�䵪�שΫ� Ctrl-P �o��"); + pressanykey(); + + // too many problems - prevent doing so here. + // GotoNewHand(); } static void TalkToAngel(){ - static int AngelPermChecked = 0; + static char AngelPermChecked = 0; userinfo_t* uent; - userec_t xuser; if (strcmp(cuser.myangel, "-") == 0){ - AngelNotOnline(); + NoAngelFound(NULL); return; } if (cuser.myangel[0] && !AngelPermChecked) { + userec_t xuser; + memset(&xuser, 0, sizeof(xuser)); getuser(cuser.myangel, &xuser); // XXX if user doesn't exist if (!(xuser.userlevel & PERM_ANGEL)) cuser.myangel[0] = 0; } AngelPermChecked = 1; - if (cuser.myangel[0] == 0 && ! FindAngel()){ + if (cuser.myangel[0] == 0 && !FindAngel()){ NoAngelFound("�{�b�S���p�ѨϦb�u�W"); return; } + // now try to load angel data + angel_reload_nick(); + uent = search_ulist_userid(cuser.myangel); if (uent == 0 || uent->angelpause || angel_reject_me(uent)){ AngelNotOnline(); @@ -303,7 +390,12 @@ TalkToAngel(){ "�A�i�H��ܤ��V���z�S�ۤv�����ӫO�@�ۤv "); */ - my_write(uent->pid, "�ݤp�ѨϡG ", "�p�Ѩ�", WATERBALL_ANGEL, uent); + { + char xnick[IDLEN+1], prompt[IDLEN*2]; + snprintf(xnick, sizeof(xnick), "%s�p�Ѩ�", _myangel_nick); + snprintf(prompt, sizeof(prompt), "��%s�p�Ѩ�: ", _myangel_nick); + my_write(uent->pid, prompt, xnick, WATERBALL_ANGEL, uent); + } return; } diff --git a/mbbsd/chat.c b/mbbsd/chat.c index 6623fbc2..1bc12472 100644 --- a/mbbsd/chat.c +++ b/mbbsd/chat.c @@ -93,7 +93,7 @@ chat_recv(struct ChatBuf *cb, int fd, char *chatroom, char *chatid, size_t chati clrtoeol(); break; case 'r': - strlcpy(chatroom, bptr + 2, IDLEN); + strlcpy(chatroom, bptr + 2, sizeof(chatroom)); break; case 't': move(0, 0); @@ -267,7 +267,7 @@ static time4_t lastEnter = 0; int t_chat(void) { - char chatroom[IDLEN];/* Chat-Room Name */ + char chatroom[IDLEN+1];/* Chat-Room Name */ char inbuf[80], chatid[20], lastcmd[MAXLASTCMD][80], *ptr = ""; struct sockaddr_in sin; int cfd, cmdpos, ch; |