!7 sbool bottomRight = false; sS templateID = bottomRight ? #1008787 : /*#1009000*/#1009081; sS heading = "Stefan's Chat"; sS roomName = "main"; // internal static int maxMessages = 100; static int longPollTick = 100; static int longPollMaxWait = 1000*60; static new L pagePostProcessors; concept Extension { S code; double priority; } concept User { S ipAddress; } sclass Msg { long time; User user; S text; L buttons; *() {} *(S ipAddress, S *text) { user = uniq_sync(User, +ipAddress); time = now(); } } concept Conversation { S cookie; // TODO: use synchro lists new LL oldDialogs; new L msgs; void add(Msg m) { if (empty(oldDialogs)) oldDialogs.add(new L); if (l(msgs) >= maxMessages) last(oldDialogs).add(popFirst(msgs)); msgs.add(m); change(); } int allCount() { ret lengthLevel2(oldDialogs) + l(msgs); } int archiveSize() { ret lengthLevel2(oldDialogs); } } p { db(); for (Extension e : sortByField('priority, list(Extension))) pcall { evalJava(e.code); } } synchronized static void sayAsync(S session, S user, S text) { Conversation conv = getConv(session); conv.add(new Msg(user, text)); print("sayAsync " + session + ", new size=" + conv.allCount()); } html { _registerThread(); registerVisitor(); //fS cookie = cookieSent(); try { Conversation conv; synchronized(mc()) { if (eq(uri, "/stats")) ret "Threads: " + ul_htmlEncode(getThreadNames(registeredThreads())); if (eq(uri, "/logs")) ret withDBLock(func -> S { L msgs = sortByFieldDesc(allMsgs(), 'time); new L l; for (Msg m : msgs) l.add(formatMsgForLog(m) + "
"); ret h3_htitle("Chat Logs") + p(join(l)); }); S message = trim(params.get("btn")); if (empty(message)) message = trim(params.get("message")); conv = getConv(roomName); print("Have " + l(conv.msgs) + " msgs in conversation " + conv.cookie); if (match("clear", message)) { print("Clearing."); conv.oldDialogs.add(conv.msgs); cset(conv, msgs := new L); conv.change(); message = null; } if (nempty(message) /*&& !lastUserMessageWas(conv, message)*/) { conv.add(new Msg(clientIP(), message)); print("Have " + l(conv.msgs) + " msgs in conversation " + conv.cookie + " after add"); } } // synchronized if (eq(uri, "/msg")) ret "OK"; if (eq(uri, "/incremental")) { int a = parseInt(params.get("a")); long start = sysNow(); L msgs; bool first = true; while (licensed() && sysNow() < start+longPollMaxWait) { msgs = subList(conv.msgs, a-conv.archiveSize()); if (empty(msgs)) { if (first) { print("Long poll starting on " + roomName + ", " + a); first = false; } sleep(longPollTick); } else { if (first) print("Long poll ended."); new StringBuilder buf; renderMessages(buf, msgs); ret "\n" + buf; } } ret ""; } synchronized(mc()) { S html = loadSnippet(templateID); // TODO: cache new StringBuilder buf; renderMessages(buf, conv.msgs); html = html.replace("#N#", str(conv.allCount())); html = html.replace("#INCREMENTALURL#", rawBotLink(programID(), "incremental?a="); html = html.replace("#MSGURL#", rawBotLink(programID(), "msg?message=")); html = html.replace("$HEADING", heading); html = html.replace("", str(buf)); html = hreplaceTitle(html, heading); html = hmobilefix(html); for (O f : pagePostProcessors) pcall { html = or((S) callF(f, html), html); } ret html; } } finally { _unregisterThread(); } } svoid renderMessages(StringBuilder buf, L msgs) { for (Msg m : msgs) { new Matches mm; S html; if (startsWith(m.text, "[IMAGE] ", mm)) { S url = trim(mm.get(0)); html = targetBlank(url, himg(url, width := 200, title := "User-submitted image", style := "display: block; margin-left: auto; margin-right: auto")); } else { dynamize_linkParams.set(new O[] { target := "_blank" }); html = dynamize(m.text); } appendMsg(buf, m.user == null ? "?" : m.user.ipAddress, formatTime(m.time), html, /*!m.fromUser*/false); } if (empty(msgs)) ret; L buttons = last(msgs).buttons; if (nempty(buttons)) appendButtons(buf, buttons); } svoid appendMsg(StringBuilder buf, S name, S time, S text, bool bot) { S imgLink = snippetImgLink(bot ? #1008323 : #1008359); buf.append([[
$NAME
message user image
$TEXT
$TIME
]].replace("$IMG", imgLink) .replace("$NAME", name) .replace("$TIME", time) .replace("$TEXT", text)); } svoid appendButtons(StringBuilder buf, L buttons) { S buttonsHtml = lines(map(buttons, func(S text) { hsubmit(text, name := "btn") })); buf.append([[
$BUTTONS
]].replace("$BUTTONS", buttonsHtml); } svoid appendDate(StringBuilder buf, S date) { buf.append([[
DATE
]].replace("DATE", date)); } sS formatTime(long time) { ret formatGMTWithOptionalDate_24(time); } sS formatMsgForLog(Msg m) { ret htmlencode(formatDateAndTime(m.time)) + " - " + htmlencode(m.user.ipAddress + ": " + m.text); } static Conversation getConv(fS cookie) { ret uniq_sync(Conversation, +cookie); } static L allMsgs() { new L l; for (Conversation c) l.addAll(c.msgs); ret l; }