Module: ODDB::State::Notify

Included in:
Drugs::Notify, Migel::Notify
Defined in:
src/state/notify.rb

Defined Under Namespace

Classes: Notification

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) passed_turing_test (readonly)

Returns the value of attribute passed_turing_test



22
23
24
# File 'src/state/notify.rb', line 22

def passed_turing_test
  @passed_turing_test
end

Instance Method Details

- (Object) breakline(txt, length)



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'src/state/notify.rb', line 29

def breakline(txt, length)
  name = ''
  line = ''
  txt.to_s.split(/(:?[\s-])/u).each { |part|
    if((line.length + part.length) > length)
      name << line << "\n"
      line = part
    else
      line << part
    end
  }
  name << line
end

- (Object) init



23
24
25
26
27
28
# File 'src/state/notify.rb', line 23

def init
  @model = Notification.new
  if(pointer = @session.user_input(:pointer))
    @model.item = pointer.resolve(@session)
  end
end

- (Object) notify_send



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'src/state/notify.rb', line 42

def notify_send 
  mandatory = [:name, :notify_sender, :notify_recipient]
  unless @passed_turing_test
    mandatory.push :captcha
  end
  keys = mandatory + [:notify_message]
  input = user_input(keys, mandatory)
  if(@passed_turing_test)
    # do nothing
  elsif((candidates = input[:captcha]) && candidates.any? { |key, word|
    @session.lookandfeel.captcha.valid_answer? key, word })
    @passed_turing_test = true
  else
    @errors.store(:captcha, create_error('e_failed_turing_test',
      :captcha, nil))
  end
  if msg = input[:message]
    input[:message] = msg[0,500]
  end
  @model.name = input[:name]
  @model.notify_sender = input[:notify_sender]
  @model.notify_recipient = input[:notify_recipient]
  @model.notify_message = input[:notify_message]
  if(error?)
    return self
  end
  recipients = model.notify_recipient
  if(model.name && model.notify_sender && recipients.is_a?(Array) \
      && !recipients.empty?)
    config = ODDB.config
    mail = RMail::Message.new
    header = mail.header
    header.add('Content-Type', 'multipart/alternative')
    header.add('Date', Time.now.rfc822)
    from = header.from = config.mail_from
    to = header.to = recipients
    header.subject = "#{@session.lookandfeel.lookup(:notify_subject)} #{@model.name}"
    header.add('Reply-To', @model.notify_sender)
    header.add('Mime-Version', '1.0')

    text = RMail::Message.new
    header = text.header
    header.add('Content-Type', 'text/plain', nil, 'charset' => 'UTF-8')
    text.body = [
      breakline(@model.notify_message, 75),
      "\n",
      @session.lookandfeel._event_url(:show, 
        {:pointer => CGI.escape(model.item.pointer.to_s)}),
    ].join("\n")
    mail.add_part text

    htmlpart = RMail::Message.new
    header = htmlpart.header
    header.add('Content-Type', 'text/html', nil, 'charset' => 'UTF-8')
    header.add('Content-Transfer-Encoding', 'quoted-printable')
    html = View::NotifyMail.new(@model, @session).to_html(@session.cgi)
    htmlpart.body = [html].pack('M')
    mail.add_part htmlpart

    Net::SMTP.start(config.smtp_server, config.smtp_port, config.smtp_domain,
                    config.smtp_user, config.smtp_pass,
                    config.smtp_authtype) { |smtp|
      smtp.sendmail(mail.to_s, config.smtp_user, recipients)
    }
    logger = @session.notification_logger
    key = [
      self.class.const_get(:ITEM_TYPE),
      @model.item.send(self.class.const_get(:CODE_KEY)),
    ]
    logger.log(key, 
      @model.notify_sender, @model.notify_recipient, Time.now)
    logger.odba_store
    klass = self.class.const_get(:CONFIRM_STATE)
    klass.new(@session, @model)
  end
end