INCLUDE "hug.bac"

FUNCTION checking

IF ISFALSE(FILEEXISTS("baconforum.txt")) THEN
	OPEN "baconforum.txt" FOR WRITING AS statistic
		a$ = "0\n0\n0"
		WRITELN a$ TO statistic
	CLOSE FILE statistic
END IF

website$ = "basic-converter.proboards.com"
port$ = "80"
OPEN CONCAT$(website$, ":", port$) FOR NETWORK AS net
SEND CONCAT$("GET / HTTP/1.1\r\nHost: ", website$, "\r\n\r\n") TO net

WHILE ISTRUE(WAIT(net, 500))
	RECEIVE dat$ FROM net

	x = INSTR(dat$, "Total Topics")
	IF x != 0 THEN
		topics = VAL(MID$(dat$, x + 14, 4))
	END IF

	x = INSTR(dat$, "Total Posts")
	IF x != 0 THEN
		posts = VAL(CONCAT$(MID$ (dat$, x + 13, 1), MID$(dat$, x + 15, 3)))
	END IF

	x = INSTR(dat$, "Total Members")
	IF x != 0 THEN
		members = VAL(MID$(dat$, x + 51, 2))
		BREAK
	END IF

	SLEEP 50
WEND

CLOSE NETWORK net

OPEN "baconforum.txt" FOR READING AS statistic
READLN stat$ FROM statistic
oldtopics = VAL(stat$)
READLN stat$ FROM statistic
oldposts = VAL(stat$)
READLN stat$ FROM statistic
oldmembers = VAL(stat$)
CLOSE FILE statistic

TEXT (tottop, CONCAT$("Total topics: ", STR$(topics)))
TEXT (newtop, CONCAT$("New topics: ", STR$(topics - oldtopics)))
TEXT (totposts, CONCAT$("Total posts: ", STR$(posts)))
TEXT (newposts, CONCAT$("New posts: ", STR$(posts - oldposts)))
TEXT (totmembers, CONCAT$("Total members: ", STR$(members)))
TEXT (newmembers, CONCAT$("New members: ", STR$(members - oldmembers)))

DELETE FILE "baconforum.txt"
OPEN "baconforum.txt" FOR WRITING AS statistic
a$ = CONCAT$(STR$(topics),"\n",STR$(posts),"\n",STR$(members))
WRITELN a$ TO statistic
CLOSE FILE statistic

RETURN FALSE
END FUNCTION

win = WINDOW ("BFChecker", 200, 220)
che = BUTTON ("Check again", 100, 30)
tottop = ENTRY ("Total topics:", 180, 20)
newtop = ENTRY ("New topics:", 180, 20)
totposts = ENTRY ("Total posts:", 180, 20)
newposts = ENTRY ("New posts:", 180, 20)
totmembers = ENTRY ("Total members:", 180, 20)
newmembers = ENTRY ("New members: ", 180, 20)

ATTACH (win, newtop, 10, 10)
ATTACH (win, newposts, 10, 32)
ATTACH (win, newmembers, 10, 54)
ATTACH (win, tottop, 10, 90)
ATTACH (win, totposts, 10, 112)
ATTACH (win, totmembers, 10, 134)
ATTACH (win, che, 50, 180)

DISABLE (tottop)
DISABLE (newtop)
DISABLE (totposts)
DISABLE (newposts)
DISABLE (totmembers)
DISABLE (newmembers)

CALLBACK (che, checking)

TIMEOUT (1000, checking)

DISPLAY