2015-11-07 07:53:21 +08:00
|
|
|
NylasStore = require 'nylas-store'
|
|
|
|
fs = require 'fs'
|
|
|
|
path = require 'path'
|
|
|
|
{Utils, MessageBodyProcessor} = require 'nylas-exports'
|
|
|
|
AutoloadImagesActions = require './autoload-images-actions'
|
|
|
|
|
|
|
|
# https://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support
|
|
|
|
ImagesRegexp = /\/\/[^'"]*\.(jpg|jpeg|jpe|jif|jfif|jfi|png|xbm|bmp|dib|ico|pict|tiff|gif|pic|webp)+/gi
|
|
|
|
|
|
|
|
class AutoloadImagesStore extends NylasStore
|
|
|
|
constructor: ->
|
|
|
|
@_whitelistEmails = {}
|
|
|
|
@_whitelistMessageIds = {}
|
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
@_whitelistEmailsPath = path.join(NylasEnv.getConfigDirPath(), 'autoload-images-whitelist.txt')
|
2015-11-07 07:53:21 +08:00
|
|
|
|
|
|
|
@_loadWhitelist()
|
|
|
|
|
|
|
|
@listenTo AutoloadImagesActions.temporarilyEnableImages, @_onTemporarilyEnableImages
|
|
|
|
@listenTo AutoloadImagesActions.permanentlyEnableImages, @_onPermanentlyEnableImages
|
|
|
|
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.config.observe 'core.reading.autoloadImages', =>
|
2015-11-07 07:53:21 +08:00
|
|
|
MessageBodyProcessor.resetCache()
|
|
|
|
|
|
|
|
shouldBlockImagesIn: (message) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
return false if NylasEnv.config.get('core.reading.autoloadImages') is true
|
2015-11-07 07:53:21 +08:00
|
|
|
return false if @_whitelistEmails[Utils.toEquivalentEmailForm(message.fromContact().email)]
|
|
|
|
return false if @_whitelistMessageIds[message.id]
|
|
|
|
return false unless ImagesRegexp.test(message.body)
|
|
|
|
true
|
|
|
|
|
|
|
|
_loadWhitelist: =>
|
2015-11-10 04:13:46 +08:00
|
|
|
fs.exists @_whitelistEmailsPath, (exists) =>
|
|
|
|
return unless exists
|
|
|
|
fs.readFile @_whitelistEmailsPath, (err, body) =>
|
|
|
|
return console.log(err) if err or not body
|
|
|
|
@_whitelistEmails = {}
|
|
|
|
for email in body.toString().split(/[\n\r]+/)
|
|
|
|
@_whitelistEmails[Utils.toEquivalentEmailForm(email)] = true
|
2015-11-07 07:53:21 +08:00
|
|
|
|
|
|
|
_saveWhitelist: =>
|
|
|
|
data = Object.keys(@_whitelistEmails).join('\n')
|
|
|
|
fs.writeFile @_whitelistEmailsPath, data, (err) =>
|
|
|
|
console.error("AutoloadImagesStore could not save whitelist: #{err.toString()}") if err
|
|
|
|
|
|
|
|
_onTemporarilyEnableImages: (message) ->
|
|
|
|
@_whitelistMessageIds[message.id] = true
|
|
|
|
MessageBodyProcessor.resetCache()
|
|
|
|
|
|
|
|
_onPermanentlyEnableImages: (message) ->
|
|
|
|
@_whitelistEmails[Utils.toEquivalentEmailForm(message.fromContact().email)] = true
|
|
|
|
MessageBodyProcessor.resetCache()
|
|
|
|
setTimeout(@_saveWhitelist, 1)
|
|
|
|
|
|
|
|
module.exports = new AutoloadImagesStore
|
|
|
|
module.exports.ImagesRegexp = ImagesRegexp
|