1--// quiet_login.lua -- stop spamming logfiles with MOTDs
2
3if not dcpp._chat_ready then
4	math.randomseed( os.time() )
5	dcpp._chat_ready = {}
6end
7
8dcpp._chat_ready.func =
9	function(hub, user, msg, me_msg)
10		if string.find(msg, dcpp._chat_ready[hub]._cookie) then
11			dcpp._chat_ready[hub]._ready = 1
12			DC():PrintDebug("Enabled mainchat for "..hub:getHubName())
13			return 1
14		end
15	end
16
17dcpp:setListener("pm", "quiet_login", dcpp._chat_ready.func)
18dcpp:setListener("adcPm", "quiet_login", dcpp._chat_ready.func)
19
20dcpp:setListener( "userInf", "quiet_login",
21	function( hub, user, flags )
22		if hub:getOwnSid() == user:getSid() and not dcpp._chat_ready[hub]._ready then
23			DC():SendHubMessage( hub:getId(), string.format("DMSG %s %s %s PM%s\n", hub:getOwnSid(), hub:getOwnSid(), dcpp._chat_ready[hub]._cookie, hub:getOwnSid()))
24		end
25	end
26)
27
28dcpp:setListener( "userMyInfo", "quiet_login",
29	function( hub, user, message )
30		if hub:getOwnNick() == user:getNick() then
31			-- Some Ptokax hubs seem not to reflect PMs or $SR's back.
32			-- For them, detect 2nd myinfo. However: this only works
33			-- If someone is an operator, because otherwise there's
34			-- no update post-$OpList.
35			dcpp._chat_ready[hub]._myInfoCount = dcpp._chat_ready[hub]._myInfoCount + 1
36			if dcpp._chat_ready[hub]._myInfoCount == 2 then
37				DC():PrintDebug("Enabled mainchat for "..hub:getHubName())
38				dcpp._chat_ready[hub]._ready = 1
39			else
40				user:sendPrivMsgFmt(dcpp._chat_ready[hub]._cookie, 1)
41			end
42		end
43	end
44)
45
46chatHandler =
47	function( hub, user, text )
48		if not dcpp._chat_ready[hub]._ready then
49			-- Other half of Ptokax workaround: the second post-$OpList chat
50			-- message is always a non-MOTD message (the first might be, but
51			-- only if the hub has disabled the MOTD). Risks delaying a chat
52			-- message, but allows non-op users to squelch Ptokax MOTDs.
53			if hub.gotOpList and hub:gotOpList() then
54				cc = dcpp._chat_ready[hub]._postOpListChatCount
55				dcpp._chat_ready[hub]._postOpListChatCount = cc + 1
56				if cc == 1 then
57					dcpp._chat_ready[hub]._ready = 1
58					-- Don't block this message.
59					return nil
60				end
61			end
62
63			dcpp._chat_ready[hub]._text = dcpp._chat_ready[hub]._text ..
64			                              text .. "\n"
65			return 1
66		end
67	end
68
69dcpp:setListener( "chat", "quiet_login", chatHandler)
70dcpp:setListener( "adcChat", "quiet_login", chatHandler)
71
72dcpp:setListener( "unknownchat", "quiet_login",
73	function( hub, text )
74		if not dcpp._chat_ready[hub]._ready then
75			dcpp._chat_ready[hub]._text = dcpp._chat_ready[hub]._text ..
76			                              text .. "\n"
77			return 1
78		end
79	end
80)
81
82dcpp:setListener( "connected", "quiet_login",
83	function( hub )
84		rand = function() return math.random(1000000000,2100000000) end
85		cookie = string.format("%d%d%d", rand(), rand(), rand())
86		dcpp._chat_ready[hub] = { _ready = nil, _text = "", _cookie = cookie, _myInfoCount = 0, _postOpListChatCount = 0 }
87	end
88)
89
90-- However, if login failed, best know why.
91dcpp:setListener( "disconnected", "quiet_login",
92	function( hub )
93		-- If disconnected prematurely, show archived messages.
94		-- They probably include error messages.
95		if not dcpp._chat_ready[hub]._ready then
96			DC():PrintDebug(dcpp._chat_ready[hub]._text)
97		end
98
99		dcpp._chat_ready[hub] = nil
100	end
101)
102
103DC():PrintDebug( "  ** Loaded quiet_login.lua **" )
104