# signature { license=0, seed=277031123, key="a28e3b0d4a3150954f93e12cefc8c7ed" }
require 'paths' 'oauth'
local parsecs = require( 'parsecs' )



local dumplog = io.open( 'dumplog.txt', 'w' )
function dump( a, ... )
	if not false then return end

	if a then
		a = tostring( a )
		dumplog:write( a )
		while #a > 0 do
			io.write( a:sub( 1, 80 ) )
			a = a:sub( 81 )
		end
		return dump( ... )
	else
		return print('\n')
	end
end




local oauth2 = require( 'oauth2' )

print( "InOAuth-2.0.2 starting..." )


parsecs.MAX_TIMEOUT = 1000000






local f = io.open( 'install.log' )
if f then

	f:close()
else

	CGI = { AUTHORIZATION = 'Basic dGVjaDoxMjM0NTY3OA==' }

	require( 'install' )


	require 'paths' 'oauth'
end





local stopped = false
function stop()
	print "stop() has been called"
	stopped = true
end

local config = require( 'config' )









_G.cfg = config()





i18nTexts = config.read( 'i18n.txt' ).i18n
function i18n( key, default) return i18nTexts[key] or default or key end

local pop3 = require( 'pop3' )
local smtp = require( 'smtp' )

local refresher = function()
	parsecs.sleep( 0 )

	while true do
		oauth2.useRefreshToken()
		parsecs.sleep( 86400 )
	end
end

parsecs.addthread( refresher )
pop3.start()
smtp.start()

print( "InOAuth-2.0.2 started" )



while not stopped do
	for _=1,10 do
		parsecs.step( 1 )
		if stopped then break end
	end
	collectgarbage()
end



print( "InOAuth-2.0.2 stopping in a moment..." )

pop3.stop()
smtp.stop()


stop()



print( "InOAuth-2.0.2 stopped." )
