Class: ODDB::Log

Inherits:
Object show all
Includes:
Persistence
Defined in:
src/util/log.rb,
test/test_util/zlog.rb

Constant Summary

MAIL_FROM =
'update@oddb.org'
MAIL_TO =
[
  'hwyss@ywesee.com',
]
ODBA_SERIALIZABLE =
['@change_flags', '@pointers', '@recipients',
'@files']

Constants included from Persistence

ODBA_PREDEFINE_SERIALIZABLE

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from PersistenceMethods

#checkout, #data_origin, #data_origins, #diff, #init, #nil_if_empty, #parent, #pointer_descr, #structural_ancestors, #undiffable?, #update_values

Constructor Details

- (Log) initialize(date)

A new instance of Log



25
26
27
28
29
30
31
32
# File 'src/util/log.rb', line 25

def initialize(date)
  @date = date
  @report = ''
  @pointers = []
  @files = {}
  @parts = []
  @recipients = []
end

Instance Attribute Details

- (Object) change_flags

Returns the value of attribute change_flags



21
22
23
# File 'src/util/log.rb', line 21

def change_flags
  @change_flags
end

- (Object) date (readonly)

Returns the value of attribute date



23
24
25
# File 'src/util/log.rb', line 23

def date
  @date
end

- (Object) date_str

Returns the value of attribute date_str



21
22
23
# File 'src/util/log.rb', line 21

def date_str
  @date_str
end

- (Object) files

Returns the value of attribute files



21
22
23
# File 'src/util/log.rb', line 21

def files
  @files
end

- (Object) mail_from

Returns the value of attribute mail_from



21
22
23
# File 'src/util/log.rb', line 21

def mail_from
  @mail_from
end

- (Object) parts

Returns the value of attribute parts



21
22
23
# File 'src/util/log.rb', line 21

def parts
  @parts
end

- (Object) pointers

Returns the value of attribute pointers



21
22
23
# File 'src/util/log.rb', line 21

def pointers
  @pointers
end

- (Object) recipients

Returns the value of attribute recipients



21
22
23
# File 'src/util/log.rb', line 21

def recipients
  @recipients
end

- (Object) report

Returns the value of attribute report



21
22
23
# File 'src/util/log.rb', line 21

def report
  @report
end

Instance Method Details

- (Object) file_part(type1, type2, filename, attachment)



132
133
134
135
136
137
138
139
# File 'src/util/log.rb', line 132

def file_part(type1, type2, filename, attachment)
  file = TMail::Mail.new
  file.set_content_type(type1, type2, 'name' => filename)
  file.disposition = 'attachment'
  file.transfer_encoding = 'base64'
  file.body = [attachment].pack('m')
  file
end

- (Object) notify(subject = nil, reply_to = nil)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'src/util/log.rb', line 33

def notify(subject = nil, reply_to = nil)
  LogFile.append('oddb/debug', " getin Log.notify (SL-Update)", Time.now) if subject =~ /SL-Update/

  subj = [
    'ch.ODDB.org Report', 
    subject, 
    (@date_str || @date.strftime('%m/%Y')),
  ].compact.join(' - ')
  
  text = text_part(@report)

  parts = @parts.nil? ? [] : @parts.dup
  LogFile.append('oddb/debug', " @files=" + @files.inspect.to_s, Time.now)
  unless(@files.nil?)
    @files.each { |path, (mime, iconv)|
      begin
        content = File.read(path)
        if iconv
          content = Iconv.new(iconv, 'UTF-8').iconv content
        end
        parts.push([mime, File.basename(path), content])
      #rescue Errno::ENOENT
      rescue Errno::ENOENT => e
        LogFile.append('oddb/debug', " " + e.inspect.to_s + "\n" + e.backtrace.inspect.to_s, Time.now)
      end
    }
  end
  LogFile.append('oddb/debug', " start outgoing process", Time.now)
  outgoing = if(parts.empty?)
    text
  else
    multipart = TMail::Mail.new
    multipart.parts << text
    parts.each { |mime, name, content|
      mtype, stype = mime.split('/')
      multipart.parts << file_part(mtype, stype, name, content)
    }
    multipart
  end
  
  outgoing.from = @mail_from || self::class::MAIL_FROM
  if reply_to
    outgoing.reply_to = reply_to
  end
  LogFile.append('oddb/debug', " @recipients=" + @recipients.inspect.to_s, Time.now)
  LogFile.append('oddb/debug', " self::class::MAIL_TO=" + self::class::MAIL_TO.to_s, Time.now)
  LogFile.append('oddb/debug', " self::class=" + self::class.to_s, Time.now)
  @recipients = (@recipients + self::class::MAIL_TO).uniq
  outgoing.subject = subj
  outgoing.date = Time.now
  outgoing['User-Agent'] = 'ODDB Updater'

  LogFile.append('oddb/debug', " before send_mail(outgoing)", Time.now)
  send_mail(outgoing)
end

- (Object) notify_attachment(attachment, headers)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'src/util/log.rb', line 88

def notify_attachment(attachment, headers)
  multipart = TMail::Mail.new
  subject = headers[:subject]
  multipart.parts << text_part(subject)
  type1, type2 = (headers[:mime_type] || 'text/plain').split('/')
  multipart.parts << file_part(type1, type2, headers[:filename], attachment)
  multipart.from = @mail_from || self::class::MAIL_FROM
  @recipients = (@recipients + self::class::MAIL_TO).uniq
  multipart.to = @recipients
  multipart.subject = subject
  multipart.date = Time.now
  
  send_mail(multipart)
end

- (Object) send_mail(multipart)



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'src/util/log.rb', line 108

def send_mail(multipart)
  LogFile.append('oddb/debug', " getin send_mail", Time.now)
  LogFile.append('oddb/debug', " @recipients=" + @recipients.inspect.to_s, Time.now)

  config = ODDB.config
  LogFile.append('oddb/debug', " config.smtp_server=" + config.smtp_server.inspect.to_s, Time.now)
  LogFile.append('oddb/debug', " config.smtp_port=" + config.smtp_port.inspect.to_s, Time.now)
  LogFile.append('oddb/debug', " config.smtp_domain=" + config.smtp_domain.inspect.to_s, Time.now)
  LogFile.append('oddb/debug', " config.smtp_user=" + config.smtp_user.inspect.to_s, Time.now)
  LogFile.append('oddb/debug', " config.smtp_pass=" + config.smtp_pass.inspect.to_s, Time.now)
  LogFile.append('oddb/debug', " config.smtp_authtype=" + config.smtp_authtype.inspect.to_s, Time.now)
  Net::SMTP.start(config.smtp_server, config.smtp_port, config.smtp_domain,
                  config.smtp_user, config.smtp_pass,
                  config.smtp_authtype) { |smtp|
  LogFile.append('oddb/debug', " getin Net::SMTP", Time.now)
    
    @recipients.each { |recipient|
      LogFile.append('oddb/debug', " recipient=" + recipient.to_s, Time.now)
      multipart.to = [recipient]
      smtp.sendmail(multipart.encoded, 
                    @mail_from || config.smtp_user, recipient)
    }
  }
end

- (Object) text_part(body)



102
103
104
105
106
107
# File 'src/util/log.rb', line 102

def text_part(body)
  text = TMail::Mail.new
  text.set_content_type('text', 'plain', 'charset'=>'UTF-8')
  text.body = body
  text
end