diff options
author | kcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2010-11-01 01:10:34 +0800 |
---|---|---|
committer | kcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2010-11-01 01:10:34 +0800 |
commit | 2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b (patch) | |
tree | 17fb5a43f1fc7aa61c9e0c78a1c3b83452e4f627 | |
parent | cff9012e1298d96575b2a5a3e28ae8466baf31ed (diff) | |
download | pttbbs-2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b.tar pttbbs-2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b.tar.gz pttbbs-2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b.tar.bz2 pttbbs-2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b.tar.lz pttbbs-2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b.tar.xz pttbbs-2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b.tar.zst pttbbs-2b0974ef9cf4e6f0ce46a4ccd6c8e17a1225011b.zip |
- extract captcha functions to seperated file
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@5194 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | pttbbs/mbbsd/Makefile | 3 | ||||
-rw-r--r-- | pttbbs/mbbsd/captcha.c | 154 | ||||
-rw-r--r-- | pttbbs/mbbsd/register.c | 152 |
3 files changed, 159 insertions, 150 deletions
diff --git a/pttbbs/mbbsd/Makefile b/pttbbs/mbbsd/Makefile index 190bc114..a4a7f4ac 100644 --- a/pttbbs/mbbsd/Makefile +++ b/pttbbs/mbbsd/Makefile @@ -9,6 +9,7 @@ SRCROOT= .. PROG= mbbsd COREOBJS = bbs.o announce.o read.o board.o brc.o mail.o record.o fav.o +ABUSEOBJS = captcha.o ACCOBJS = user.o acl.o register.o passwd.o emaildb.o NETOBJS = mbbsd.o io.o term.o telnet.o TALKOBJS = friend.o talk.o ccw.o @@ -19,7 +20,7 @@ PLUGOBJS = calendar.o ordersong.o gamble.o angel.o timecap.o CHESSOBJS= chess.o chc.o chc_tab.o ch_go.o ch_gomo.o ch_dark.o ch_reversi.o GAMEOBJS = card.o chicken.o OBJS:= admin.o assess.o edit.o xyz.o var.o vote.o voteboard.o \ - $(COREOBJS) $(ACCOBJS) $(NETOBJS) $(TALKOBJS) $(UTILOBJS) \ + $(COREOBJS) $(ABUSEOBJS) $(ACCOBJS) $(NETOBJS) $(TALKOBJS) $(UTILOBJS) \ $(UIOBJS) $(PAGEROBJS) $(PLUGOBJS) \ $(CHESSOBJS) $(GAMEOBJS) diff --git a/pttbbs/mbbsd/captcha.c b/pttbbs/mbbsd/captcha.c new file mode 100644 index 00000000..6fa446a6 --- /dev/null +++ b/pttbbs/mbbsd/captcha.c @@ -0,0 +1,154 @@ +/* $Id$ */ +#include "bbs.h" + +//////////////////////////////////////////////////////////////////////////// +// Figlet Captcha System +//////////////////////////////////////////////////////////////////////////// +#ifdef USE_FIGLET_CAPTCHA + +#define FN_JOBSPOOL_DIR "jobspool/" + +static int +gen_captcha(char *buf, int szbuf, char *fpath) +{ + // do not use: GQV + const char *alphabet = "ABCDEFHIJKLMNOPRSTUWXYZ"; + int calphas = strlen(alphabet); + char cmd[PATHLEN], opts[PATHLEN]; + int i, coptSpace, coptFont; + static const char *optSpace[] = { + "-S", "-s", "-k", "-W", + // "-o", + NULL + }; + static const char *optFont[] = { + "banner", "big", "slant", "small", + "smslant", "standard", + // block family + "block", "lean", + // shadow family + // "shadow", "smshadow", + // mini (better with large spacing) + // "mini", + NULL + }; + + // fill captcha code + for (i = 0; i < szbuf-1; i++) + buf[i] = alphabet[random() % calphas]; + buf[i] = 0; // szbuf-1 + + // decide options + coptSpace = coptFont = 0; + while (optSpace[coptSpace]) coptSpace++; + while (optFont[coptFont]) coptFont++; + snprintf(opts, sizeof(opts), + "%s -f %s", + optSpace[random() % coptSpace], + optFont [random() % coptFont ]); + + // create file + snprintf(fpath, PATHLEN, FN_JOBSPOOL_DIR ".captcha.%s", buf); + snprintf(cmd, sizeof(cmd), FIGLET_PATH " %s %s > %s", + opts, buf, fpath); + + // vmsg(cmd); + if (system(cmd) != 0) + return 0; + + return 1; +} + + +static int +_vgetcb_data_upper(int key, VGET_RUNTIME *prt, void *instance) +{ + if (key >= 'a' && key <= 'z') + key = toupper(key); + if (key < 'A' || key > 'Z') + { + bell(); + return VGETCB_NEXT; + } + return VGETCB_NONE; +} + +static int +_vgetcb_data_change(int key, VGET_RUNTIME *prt, void *instance) +{ + char *s = prt->buf; + while (*s) + { + if (isascii(*s) && islower(*s)) + *s = toupper(*s); + s++; + } + return VGETCB_NONE; +} + +int verify_captcha(const char *reason) +{ + char captcha[7] = "", code[STRLEN]; + char fpath[PATHLEN]; + VGET_CALLBACKS vge = { NULL, _vgetcb_data_upper, _vgetcb_data_change }; + int tries = 0, i; + + do { + // create new captcha + if (tries % 2 == 0 || !captcha[0]) + { + // if generation failed, skip captcha. + if (!gen_captcha(captcha, sizeof(captcha), fpath) || + !dashf(fpath)) + return 1; + + // prompt user about captcha + vs_hdr("CAPTCHA 驗證程序"); + outs(reason); + outs("請輸入下面圖樣顯示的文字。\n" + "圖樣只會由大寫的 A-Z 英文字母組成。\n\n"); + show_file(fpath, 4, b_lines-5, SHOWFILE_ALLOW_ALL); + unlink(fpath); + } + + // each run waits 10 seconds. + for (i = 10; i > 0; i--) + { + move(b_lines-1, 0); clrtobot(); + prints("請仔細檢查上面的圖形, %d 秒後即可輸入...", i); + // flush out current input + doupdate(); + sleep(1); + vkey_purge(); + } + + // input captcha + move(b_lines-1, 0); clrtobot(); + prints("請輸入圖樣顯示的 %d 個英文字母: ", (int)strlen(captcha)); + vgetstring(code, strlen(captcha)+1, 0, "", &vge, NULL); + + if (code[0] && strcasecmp(code, captcha) == 0) + break; + + // error case. + if (++tries >= 10) + return 0; + + // error + vmsg("輸入錯誤,請重試。注意組成文字全是大寫英文字母。"); + + } while (1); + + clear(); + return 1; +} +#else // !USE_FIGLET_CAPTCHA + +int +verify_captcha(const char *reason) +{ + return 1; +} + +#endif // !USE_FIGLET_CAPTCHA + diff --git a/pttbbs/mbbsd/register.c b/pttbbs/mbbsd/register.c index bf666d39..c952dd35 100644 --- a/pttbbs/mbbsd/register.c +++ b/pttbbs/mbbsd/register.c @@ -339,154 +339,6 @@ delregcodefile(void) //////////////////////////////////////////////////////////////////////////// -// Figlet Captcha System -//////////////////////////////////////////////////////////////////////////// -#ifdef USE_FIGLET_CAPTCHA - -static int -gen_captcha(char *buf, int szbuf, char *fpath) -{ - // do not use: GQV - const char *alphabet = "ABCDEFHIJKLMNOPRSTUWXYZ"; - int calphas = strlen(alphabet); - char cmd[PATHLEN], opts[PATHLEN]; - int i, coptSpace, coptFont; - static const char *optSpace[] = { - "-S", "-s", "-k", "-W", - // "-o", - NULL - }; - static const char *optFont[] = { - "banner", "big", "slant", "small", - "smslant", "standard", - // block family - "block", "lean", - // shadow family - // "shadow", "smshadow", - // mini (better with large spacing) - // "mini", - NULL - }; - - // fill captcha code - for (i = 0; i < szbuf-1; i++) - buf[i] = alphabet[random() % calphas]; - buf[i] = 0; // szbuf-1 - - // decide options - coptSpace = coptFont = 0; - while (optSpace[coptSpace]) coptSpace++; - while (optFont[coptFont]) coptFont++; - snprintf(opts, sizeof(opts), - "%s -f %s", - optSpace[random() % coptSpace], - optFont [random() % coptFont ]); - - // create file - snprintf(fpath, PATHLEN, FN_JOBSPOOL_DIR ".captcha.%s", buf); - snprintf(cmd, sizeof(cmd), FIGLET_PATH " %s %s > %s", - opts, buf, fpath); - - // vmsg(cmd); - if (system(cmd) != 0) - return 0; - - return 1; -} - - -static int -_vgetcb_data_upper(int key, VGET_RUNTIME *prt, void *instance) -{ - if (key >= 'a' && key <= 'z') - key = toupper(key); - if (key < 'A' || key > 'Z') - { - bell(); - return VGETCB_NEXT; - } - return VGETCB_NONE; -} - -static int -_vgetcb_data_change(int key, VGET_RUNTIME *prt, void *instance) -{ - char *s = prt->buf; - while (*s) - { - if (isascii(*s) && islower(*s)) - *s = toupper(*s); - s++; - } - return VGETCB_NONE; -} - -static int verify_captcha() -{ - char captcha[7] = "", code[STRLEN]; - char fpath[PATHLEN]; - VGET_CALLBACKS vge = { NULL, _vgetcb_data_upper, _vgetcb_data_change }; - int tries = 0, i; - - do { - // create new captcha - if (tries % 2 == 0 || !captcha[0]) - { - // if generation failed, skip captcha. - if (!gen_captcha(captcha, sizeof(captcha), fpath) || - !dashf(fpath)) - return 1; - - // prompt user about captcha - vs_hdr("CAPTCHA 認證程序"); - outs("為了確認您的註冊程序,請輸入下面圖樣顯示的文字。\n" - "圖樣只會由大寫的 A-Z 英文字母組成。\n\n"); - show_file(fpath, 4, b_lines-5, SHOWFILE_ALLOW_ALL); - unlink(fpath); - } - - // each run waits 10 seconds. - for (i = 10; i > 0; i--) - { - move(b_lines-1, 0); clrtobot(); - prints("請仔細檢查上面的圖形, %d 秒後即可輸入...", i); - // flush out current input - doupdate(); - sleep(1); - vkey_purge(); - } - - // input captcha - move(b_lines-1, 0); clrtobot(); - prints("請輸入圖樣顯示的 %d 個英文字母: ", (int)strlen(captcha)); - vgetstring(code, strlen(captcha)+1, 0, "", &vge, NULL); - - if (code[0] && strcasecmp(code, captcha) == 0) - break; - - // error case. - if (++tries >= 10) - return 0; - - // error - vmsg("輸入錯誤,請重試。注意組成文字全是大寫英文字母。"); - - } while (1); - - clear(); - return 1; -} -#else // !USE_FIGLET_CAPTCHA - -static int -verify_captcha() -{ - return 1; -} - -#endif // !USE_FIGLET_CAPTCHA - -//////////////////////////////////////////////////////////////////////////// // Justify Utilities //////////////////////////////////////////////////////////////////////////// @@ -871,11 +723,13 @@ new_register(void) newuser.day = (unsigned char)d; } - if (!verify_captcha()) +#ifdef REGISTER_VERIFY_CAPTCHA + if (!verify_captcha("為了繼續您的註冊程序\n")) { vmsg(MSG_ERR_MAXTRIES); exit(1); } +#endif setupnewuser(&newuser); |