Class: PasteHub::SendMail

Inherits:
PluginBase show all
Defined in:
lib/pastehub/plugin/sendmail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSendMail

Returns a new instance of SendMail.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pastehub/plugin/sendmail.rb', line 50

def initialize()
  @mail_command = "mail"
  # load env variables
  @mail_settings = 10.times.collect {|n|
    mail_setting = MailSetting.new
    val = ENV[ sprintf("PASTEHUB_MAIL%d", n) ]
    if val
      if val.match(/^[a-zA-Z]+[,][a-zA-Z0-9_.-]+[@].+$/)
        arr = val.split(/,/)
        mail_setting.tag  = arr[0]
        mail_setting.mail = arr[1]
        mail_setting
      else
        nil
      end
    else
      nil
    end
  }.select( ) {|x| x}
end

Instance Attribute Details

#mail_commandObject

Returns the value of attribute mail_command.



48
49
50
# File 'lib/pastehub/plugin/sendmail.rb', line 48

def mail_command
  @mail_command
end

#mail_settingsObject (readonly)

Returns the value of attribute mail_settings.



47
48
49
# File 'lib/pastehub/plugin/sendmail.rb', line 47

def mail_settings
  @mail_settings
end

Instance Method Details

#display_configObject



71
72
73
74
75
76
77
78
# File 'lib/pastehub/plugin/sendmail.rb', line 71

def display_config
  # display settings
  @mail_settings.each { |setting|
    STDERR.printf( "Info: SendMail-plugin   tag:[%s] mail[%s]\n",
                   setting.tag,
                   setting.mail )
  }
end

#encode_subject(subject) ⇒ Object



97
98
99
100
# File 'lib/pastehub/plugin/sendmail.rb', line 97

def encode_subject(subject)
  enc_str = Base64.strict_encode64(subject)
  "=?utf-8?B?" + enc_str + "?="
end

#newly_arrived(message) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pastehub/plugin/sendmail.rb', line 102

def newly_arrived(message)
  mailaddress = sendmail?(message)
  lines = message.split( /\n/ )
  if mailaddress
    command = sprintf( "|%s %s -i -s '%s'", @mail_command, mailaddress, encode_subject(lines[0]))
    if @mail_command
      p @mail_command
      open( command, "w" ) {|f|
        f.print message
      }
      STDERR.printf( "Info: send mail [%s]\n", command )
      nil
    else
      command
    end
  else
    nil
  end
end

#sendmail?(message) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pastehub/plugin/sendmail.rb', line 80

def sendmail?(message)
  arr = mail_settings.collect { |setting|
    tag_exp = sprintf("(^|[ ])[#]%s([ ]|$)", setting.tag)
    regexp = Regexp.new(tag_exp)
    if regexp.match(message)
      setting.mail
    else
      nil
    end
  }.select( ) {|x| x}
  if 0 < arr.size
    arr[0]
  else
    nil
  end
end