 
 xOAuthCache = {}
 
+# This file handle the generation of config for node-imap and nodemailer
+# it also handles xoauthgenerators as singletons by account
+# to prevent race between imap & smtp in creating access tokens
+
 CLIENT_ID = '260645850650-2oeufakc8ddbrn8p4o58emsl7u0r0c8s' +
             '.apps.googleusercontent.com'
 
+# @TODO : investigate a better way to distribute this
 CLIENT_SECRET = '1gNUceDM59TjFAks58ftsniZ'
 
+
+# get the single instance of xoauthGenerator for this account
 getXoauth2Generator = (account) ->
     unless xOAuthCache[account.id]
         log.info "XOAUTH GENERATOR FOR #{account.label}"
 
-        timeout = 1000*account.oauthTimeout - Date.now() #timeout in ms
-        timeout = Math.floor timeout/1000 # timeout in s
-        timeout = Math.max timeout, 0 # min = 0
+        timeout = 1000 * account.oauthTimeout - Date.now() # timeout in ms
+        timeout = Math.floor(timeout / 1000) # timeout in s
+        timeout = Math.max(timeout, 0) # min = 0
 
         generator = xoauth2.createXOAuth2Generator
             user: account.login

    return options



module.exports.makeIMAPConfig = (account, callback) ->

    if account.oauthProvider is "GMAIL"

            accessToken: account.oauthAccessToken
            timeout: timeout

        # when the generator is forced to create a new token, we save it in
        # the DS for later reuse
        generator.on 'token', ({user, accessToken, timeout}) ->
            account.updateAttributes
                oauthAccessToken: accessToken
