File: internet.rb

package info (click to toggle)
ruby-mime 0.4.4-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 284 kB
  • sloc: ruby: 1,187; xml: 17; makefile: 2
file content (189 lines) | stat: -rw-r--r-- 4,955 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
module MIME
  module Headers

    #
    # The RFC 2822 Internet message header fields.
    #
    # Mailbox fields #to, #from, #cc, #bcc, and #reply_to may be a single email
    # address, an array of email addresses, or a hash of _email_ => _name_
    # pairs. When using a hash, set _name_ to +nil+ to omit email display name.
    # The #sender field is a special case and can only contain a single mailbox.
    #
    module Internet

      # Internet message character specifications (RFC 5322)
      ATOM     = /[[:alnum:]!#\$%&'*+\/=?^_`{|}~-]/
      DOT_ATOM = /^#{ATOM}+(#{ATOM}|\.)*$/
      SPECIALS = /[()<>\[\]:;@\,."]/

      attr_reader(
        # Required Headers
        :to,
        :from,
        :date, 

        # Optional Headers
        :cc,
        :bcc,
        :sender,
        :reply_to,
        :message_id,
        :in_reply_to,
        :references,
        :comments,
        :keywords,
        :subject
      )

      #
      # Origination date at which the creator of the message indicated that the
      # message was complete and ready to enter the mail delivery system.
      #
      def date= date
        @date = date
        headers.set('Date', date.rfc2822)
      end

      #
      # Person(s) or system(s) responsible for writing the message.
      #
      def from= mailbox
        @from = mailbox
        headers.set('From', stringify_mailbox(mailbox))
      end

      #
      # Mailbox of the agent responsible for actual transmission of the message.
      # Sender field is required if the From field contains multiple mailboxes.
      #
      # === Example scenario
      # If a secretary were to send a message for another person, the mailbox of
      # the secretary would appear in the Sender field and the mailbox of the
      # actual author would appear in the From field. 
      #
      def sender= mailbox
        if (mailbox.is_a?(Hash) || mailbox.is_a?(Array)) && mailbox.size != 1
          raise ArgumentError, '"Sender" must be a single mailbox specification'
        end
        @sender = mailbox
        headers.set('Sender', stringify_mailbox(mailbox))
      end

      #
      # Mailbox(es) of the primary recipient(s).
      #
      def to= mailbox
        @to = mailbox
        headers.set('To', stringify_mailbox(mailbox))
      end

      #
      # Mailbox(es) of others who are to receive the message, though the content
      # of the message may not be directed at them; "Carbon Copy."
      #
      def cc= mailbox
        @cc = mailbox
        headers.set('Cc', stringify_mailbox(mailbox))
      end

      #
      # Mailbox(es) of recipients of the message whose addresses are not to be
      # revealed to other recipients of the message; "Blind Carbon Copy."
      #
      def bcc= mailbox
        @bcc = mailbox
        headers.set('Bcc', stringify_mailbox(mailbox))
      end

      #
      # Mailbox(es) to which the author suggests that replies be sent.
      #
      def reply_to= mailbox
        @reply_to = mailbox
        headers.set('Reply-To', stringify_mailbox(mailbox))
      end

      #
      # Globally unique identifier of the message.
      #
      # The message +id+ must contain an embedded "@" symbol. An example +id+
      # might be <em>some-unique-id@domain.com</em>.
      #
      def message_id= id
        @message_id = id
        headers.set('Message-ID', "<#{id}>")
      end

      #
      # The +id+ of the message to which this message is a reply.
      #--
      # TODO fully implement and test
      #
      def in_reply_to= id
        @in_reply_to = id
        headers.set('In-Reply-To', "<#{id}>")
      end

      #
      # The +id+ used to identify a "thread" of conversation.
      #--
      # TODO fully implement and test
      #
      def references= id
        @references = id
        headers.set('References', "<#{id}>")
      end

      #
      # Additional comments about the message content.
      #
      def comments= comments
        @comments = comments
        headers.set('Comments', comments)
      end

      #
      # Comma-separated list of important words and phrases that might be useful
      # for the recipient.
      #
      def keywords= keywords
        @keywords = keywords
        headers.set('Keywords', keywords)
      end

      #
      # The message topic.
      #
      def subject= subject
        @subject = subject
        headers.set('Subject', subject)
      end


      private

      def stringify_mailbox mailbox
        case mailbox
        when Hash
          mailbox.map do |email, name|
            if name
              if name =~ SPECIALS
                name.gsub!('"', '\"')
                %["#{name}" <#{email}>]
              else
                %[#{name} <#{email}>]
              end
            else
              email
            end
          end.join(', ')
        when Array
          mailbox.join(', ')
        else
          return mailbox
        end
      end

    end
  end
end