apphunkd 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. data/.document +5 -0
  2. data/.gitignore +22 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +18 -0
  5. data/Rakefile +78 -0
  6. data/TODO +1 -0
  7. data/VERSION +1 -0
  8. data/apphunkd.gemspec +258 -0
  9. data/bin/apphunkd +4 -0
  10. data/config/arguments.rb +12 -0
  11. data/config/boot.rb +68 -0
  12. data/config/environment.rb +23 -0
  13. data/config/environments/development.rb +2 -0
  14. data/config/environments/production.rb +2 -0
  15. data/config/environments/test.rb +2 -0
  16. data/config/post-daemonize/readme +5 -0
  17. data/config/pre-daemonize/readme +12 -0
  18. data/config/pre-daemonize/requires.rb +2 -0
  19. data/lib/apphunkd.rb +25 -0
  20. data/lib/apphunkd/api.rb +5 -0
  21. data/lib/apphunkd/api/service.rb +27 -0
  22. data/lib/apphunkd/queue.rb +96 -0
  23. data/lib/apphunkd/remote.rb +29 -0
  24. data/lib/apphunkd/remote/result.rb +15 -0
  25. data/libexec/apphunkd-daemon.rb +13 -0
  26. data/script/console +3 -0
  27. data/script/destroy +14 -0
  28. data/script/generate +14 -0
  29. data/spec/lib/apphunkd/api/service_spec.rb +47 -0
  30. data/spec/lib/apphunkd/queue_spec.rb +129 -0
  31. data/spec/lib/apphunkd/remote_spec.rb +61 -0
  32. data/spec/lib/apphunkd_spec.rb +50 -0
  33. data/spec/spec.opts +1 -0
  34. data/spec/spec_helper.rb +13 -0
  35. data/support/apphunkd.initd +47 -0
  36. data/support/apphunkd.monitrc +3 -0
  37. data/tasks/rspec.rake +21 -0
  38. data/vendor/daemon-kit/Configuration.txt +102 -0
  39. data/vendor/daemon-kit/Deployment.txt +113 -0
  40. data/vendor/daemon-kit/History.txt +97 -0
  41. data/vendor/daemon-kit/Logging.txt +92 -0
  42. data/vendor/daemon-kit/Manifest.txt +166 -0
  43. data/vendor/daemon-kit/PostInstall.txt +6 -0
  44. data/vendor/daemon-kit/README.rdoc +130 -0
  45. data/vendor/daemon-kit/Rakefile +37 -0
  46. data/vendor/daemon-kit/RuoteParticipants.txt +113 -0
  47. data/vendor/daemon-kit/TODO.txt +37 -0
  48. data/vendor/daemon-kit/app_generators/daemon_kit/USAGE +7 -0
  49. data/vendor/daemon-kit/app_generators/daemon_kit/daemon_kit_generator.rb +161 -0
  50. data/vendor/daemon-kit/app_generators/daemon_kit/templates/README +48 -0
  51. data/vendor/daemon-kit/app_generators/daemon_kit/templates/Rakefile +6 -0
  52. data/vendor/daemon-kit/app_generators/daemon_kit/templates/bin/daemon.erb +7 -0
  53. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/arguments.rb +12 -0
  54. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/boot.rb +68 -0
  55. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environment.rb +23 -0
  56. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/development.rb +2 -0
  57. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/production.rb +2 -0
  58. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/environments/test.rb +2 -0
  59. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/post-daemonize/readme +5 -0
  60. data/vendor/daemon-kit/app_generators/daemon_kit/templates/config/pre-daemonize/readme +12 -0
  61. data/vendor/daemon-kit/app_generators/daemon_kit/templates/lib/daemon.rb +2 -0
  62. data/vendor/daemon-kit/app_generators/daemon_kit/templates/libexec/daemon.erb +18 -0
  63. data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/console +3 -0
  64. data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/destroy +14 -0
  65. data/vendor/daemon-kit/app_generators/daemon_kit/templates/script/generate +14 -0
  66. data/vendor/daemon-kit/bin/daemon_kit +18 -0
  67. data/vendor/daemon-kit/daemon_generators/amqp/USAGE +5 -0
  68. data/vendor/daemon-kit/daemon_generators/amqp/amqp_generator.rb +65 -0
  69. data/vendor/daemon-kit/daemon_generators/amqp/templates/config/amqp.yml +28 -0
  70. data/vendor/daemon-kit/daemon_generators/amqp/templates/config/initializers/amqp.rb +7 -0
  71. data/vendor/daemon-kit/daemon_generators/amqp/templates/libexec/daemon.rb +37 -0
  72. data/vendor/daemon-kit/daemon_generators/cron/USAGE +5 -0
  73. data/vendor/daemon-kit/daemon_generators/cron/cron_generator.rb +64 -0
  74. data/vendor/daemon-kit/daemon_generators/cron/templates/config/initializers/cron.rb +11 -0
  75. data/vendor/daemon-kit/daemon_generators/cron/templates/libexec/daemon.rb +43 -0
  76. data/vendor/daemon-kit/daemon_generators/cucumber/USAGE +11 -0
  77. data/vendor/daemon-kit/daemon_generators/cucumber/cucumber_generator.rb +38 -0
  78. data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber +8 -0
  79. data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber.rake +13 -0
  80. data/vendor/daemon-kit/daemon_generators/cucumber/templates/cucumber_environment.rb +2 -0
  81. data/vendor/daemon-kit/daemon_generators/cucumber/templates/env.rb +7 -0
  82. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/deploy_capistrano_generator.rb +35 -0
  83. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/Capfile +10 -0
  84. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/USAGE +10 -0
  85. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy.rb +53 -0
  86. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/production.rb +6 -0
  87. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/deploy/staging.rb +6 -0
  88. data/vendor/daemon-kit/daemon_generators/deploy_capistrano/templates/config/environments/staging.rb +0 -0
  89. data/vendor/daemon-kit/daemon_generators/jabber/USAGE +5 -0
  90. data/vendor/daemon-kit/daemon_generators/jabber/jabber_generator.rb +65 -0
  91. data/vendor/daemon-kit/daemon_generators/jabber/templates/config/initializers/jabber.rb +7 -0
  92. data/vendor/daemon-kit/daemon_generators/jabber/templates/config/jabber.yml +26 -0
  93. data/vendor/daemon-kit/daemon_generators/jabber/templates/libexec/daemon.rb +27 -0
  94. data/vendor/daemon-kit/daemon_generators/nanite_agent/USAGE +5 -0
  95. data/vendor/daemon-kit/daemon_generators/nanite_agent/nanite_agent_generator.rb +68 -0
  96. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/config/initializers/nanite_agent.rb +6 -0
  97. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/config/nanite.yml +35 -0
  98. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/lib/actors/sample.rb +11 -0
  99. data/vendor/daemon-kit/daemon_generators/nanite_agent/templates/libexec/daemon.rb +31 -0
  100. data/vendor/daemon-kit/daemon_generators/rspec/USAGE +5 -0
  101. data/vendor/daemon-kit/daemon_generators/rspec/rspec_generator.rb +55 -0
  102. data/vendor/daemon-kit/daemon_generators/rspec/templates/spec.rb +11 -0
  103. data/vendor/daemon-kit/daemon_generators/rspec/templates/spec/spec.opts +1 -0
  104. data/vendor/daemon-kit/daemon_generators/rspec/templates/spec/spec_helper.rb +21 -0
  105. data/vendor/daemon-kit/daemon_generators/rspec/templates/tasks/rspec.rake +21 -0
  106. data/vendor/daemon-kit/daemon_generators/ruote/USAGE +5 -0
  107. data/vendor/daemon-kit/daemon_generators/ruote/ruote_generator.rb +67 -0
  108. data/vendor/daemon-kit/daemon_generators/ruote/templates/config/amqp.yml +30 -0
  109. data/vendor/daemon-kit/daemon_generators/ruote/templates/config/initializers/ruote.rb +13 -0
  110. data/vendor/daemon-kit/daemon_generators/ruote/templates/config/ruote.yml +23 -0
  111. data/vendor/daemon-kit/daemon_generators/ruote/templates/lib/daemon.rb +4 -0
  112. data/vendor/daemon-kit/daemon_generators/ruote/templates/lib/sample.rb +26 -0
  113. data/vendor/daemon-kit/daemon_generators/ruote/templates/libexec/daemon.rb +33 -0
  114. data/vendor/daemon-kit/lib/daemon_kit.rb +54 -0
  115. data/vendor/daemon-kit/lib/daemon_kit/abstract_logger.rb +235 -0
  116. data/vendor/daemon-kit/lib/daemon_kit/amqp.rb +38 -0
  117. data/vendor/daemon-kit/lib/daemon_kit/application.rb +187 -0
  118. data/vendor/daemon-kit/lib/daemon_kit/arguments.rb +165 -0
  119. data/vendor/daemon-kit/lib/daemon_kit/commands/console.rb +38 -0
  120. data/vendor/daemon-kit/lib/daemon_kit/config.rb +108 -0
  121. data/vendor/daemon-kit/lib/daemon_kit/console_daemon.rb +2 -0
  122. data/vendor/daemon-kit/lib/daemon_kit/core_ext.rb +1 -0
  123. data/vendor/daemon-kit/lib/daemon_kit/core_ext/configurable.rb +96 -0
  124. data/vendor/daemon-kit/lib/daemon_kit/core_ext/string.rb +22 -0
  125. data/vendor/daemon-kit/lib/daemon_kit/cron.rb +48 -0
  126. data/vendor/daemon-kit/lib/daemon_kit/cucumber/world.rb +38 -0
  127. data/vendor/daemon-kit/lib/daemon_kit/deployment/capistrano.rb +482 -0
  128. data/vendor/daemon-kit/lib/daemon_kit/em.rb +43 -0
  129. data/vendor/daemon-kit/lib/daemon_kit/error_handlers/base.rb +32 -0
  130. data/vendor/daemon-kit/lib/daemon_kit/error_handlers/hoptoad.rb +61 -0
  131. data/vendor/daemon-kit/lib/daemon_kit/error_handlers/mail.rb +85 -0
  132. data/vendor/daemon-kit/lib/daemon_kit/exceptions.rb +8 -0
  133. data/vendor/daemon-kit/lib/daemon_kit/initializer.rb +438 -0
  134. data/vendor/daemon-kit/lib/daemon_kit/jabber.rb +170 -0
  135. data/vendor/daemon-kit/lib/daemon_kit/nanite.rb +7 -0
  136. data/vendor/daemon-kit/lib/daemon_kit/nanite/agent.rb +56 -0
  137. data/vendor/daemon-kit/lib/daemon_kit/pid_file.rb +61 -0
  138. data/vendor/daemon-kit/lib/daemon_kit/ruote_participants.rb +119 -0
  139. data/vendor/daemon-kit/lib/daemon_kit/ruote_pseudo_participant.rb +68 -0
  140. data/vendor/daemon-kit/lib/daemon_kit/ruote_workitem.rb +169 -0
  141. data/vendor/daemon-kit/lib/daemon_kit/safety.rb +85 -0
  142. data/vendor/daemon-kit/lib/daemon_kit/tasks.rb +2 -0
  143. data/vendor/daemon-kit/lib/daemon_kit/tasks/environment.rake +10 -0
  144. data/vendor/daemon-kit/lib/daemon_kit/tasks/framework.rake +120 -0
  145. data/vendor/daemon-kit/lib/daemon_kit/tasks/god.rake +62 -0
  146. data/vendor/daemon-kit/lib/daemon_kit/tasks/log.rake +8 -0
  147. data/vendor/daemon-kit/lib/daemon_kit/tasks/monit.rake +29 -0
  148. data/vendor/daemon-kit/script/console +10 -0
  149. data/vendor/daemon-kit/script/destroy +14 -0
  150. data/vendor/daemon-kit/script/generate +14 -0
  151. data/vendor/daemon-kit/script/txt2html +71 -0
  152. data/vendor/daemon-kit/spec/abstract_logger_spec.rb +126 -0
  153. data/vendor/daemon-kit/spec/argument_spec.rb +70 -0
  154. data/vendor/daemon-kit/spec/config_spec.rb +79 -0
  155. data/vendor/daemon-kit/spec/configurable_spec.rb +56 -0
  156. data/vendor/daemon-kit/spec/daemon_kit_spec.rb +7 -0
  157. data/vendor/daemon-kit/spec/error_handlers_spec.rb +23 -0
  158. data/vendor/daemon-kit/spec/fixtures/env.yml +15 -0
  159. data/vendor/daemon-kit/spec/fixtures/noenv.yml +4 -0
  160. data/vendor/daemon-kit/spec/initializer_spec.rb +26 -0
  161. data/vendor/daemon-kit/spec/spec.opts +1 -0
  162. data/vendor/daemon-kit/spec/spec_helper.rb +27 -0
  163. data/vendor/daemon-kit/tasks/rspec.rake +21 -0
  164. data/vendor/daemon-kit/templates/god/god.erb +69 -0
  165. data/vendor/daemon-kit/templates/monit/monit.erb +14 -0
  166. data/vendor/daemon-kit/test/test_amqp_generator.rb +48 -0
  167. data/vendor/daemon-kit/test/test_cron_generator.rb +45 -0
  168. data/vendor/daemon-kit/test/test_daemon-kit_generator.rb +84 -0
  169. data/vendor/daemon-kit/test/test_daemon_kit_config.rb +28 -0
  170. data/vendor/daemon-kit/test/test_deploy_capistrano_generator.rb +48 -0
  171. data/vendor/daemon-kit/test/test_generator_helper.rb +29 -0
  172. data/vendor/daemon-kit/test/test_helper.rb +7 -0
  173. data/vendor/daemon-kit/test/test_jabber_generator.rb +49 -0
  174. data/vendor/daemon-kit/test/test_nanite_agent_generator.rb +49 -0
  175. data/vendor/daemon-kit/test/test_ruote_generator.rb +45 -0
  176. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail.rb +5 -0
  177. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/address.rb +426 -0
  178. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/attachments.rb +46 -0
  179. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/base64.rb +46 -0
  180. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/compat.rb +41 -0
  181. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/config.rb +67 -0
  182. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/core_extensions.rb +63 -0
  183. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/encode.rb +581 -0
  184. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/header.rb +960 -0
  185. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/index.rb +9 -0
  186. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/interface.rb +1130 -0
  187. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/loader.rb +3 -0
  188. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mail.rb +578 -0
  189. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mailbox.rb +495 -0
  190. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/main.rb +6 -0
  191. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/mbox.rb +3 -0
  192. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/net.rb +248 -0
  193. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/obsolete.rb +132 -0
  194. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/parser.rb +1476 -0
  195. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/port.rb +379 -0
  196. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/quoting.rb +118 -0
  197. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/require_arch.rb +58 -0
  198. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/scanner.rb +49 -0
  199. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/scanner_r.rb +261 -0
  200. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/stringio.rb +280 -0
  201. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/utils.rb +337 -0
  202. data/vendor/daemon-kit/vendor/tmail-1.2.3/tmail/version.rb +39 -0
  203. data/vendor/daemon-kit/vendor/tmail.rb +13 -0
  204. metadata +281 -0
@@ -0,0 +1,9 @@
1
+ #:stopdoc:
2
+ # This is here for Rolls.
3
+ # Rolls uses this instead of lib/tmail.rb.
4
+
5
+ require 'tmail/version'
6
+ require 'tmail/mail'
7
+ require 'tmail/mailbox'
8
+ require 'tmail/core_extensions'
9
+ #:startdoc:
@@ -0,0 +1,1130 @@
1
+ =begin rdoc
2
+
3
+ = interface.rb Provides an interface to the TMail object
4
+
5
+ =end
6
+ #--
7
+ # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
29
+ # with permission of Minero Aoki.
30
+ #++
31
+
32
+ # TMail::Mail objects get accessed primarily through the methods in this file.
33
+ #
34
+ #
35
+
36
+ require 'tmail/utils'
37
+
38
+ module TMail
39
+
40
+ class Mail
41
+
42
+ # Allows you to query the mail object with a string to get the contents
43
+ # of the field you want.
44
+ #
45
+ # Returns a string of the exact contents of the field
46
+ #
47
+ # mail.from = "mikel <mikel@lindsaar.net>"
48
+ # mail.header_string("From") #=> "mikel <mikel@lindsaar.net>"
49
+ def header_string( name, default = nil )
50
+ h = @header[name.downcase] or return default
51
+ h.to_s
52
+ end
53
+
54
+ #:stopdoc:
55
+ #--
56
+ #== Attributes
57
+
58
+ include TextUtils
59
+
60
+ def set_string_array_attr( key, strs )
61
+ strs.flatten!
62
+ if strs.empty?
63
+ @header.delete key.downcase
64
+ else
65
+ store key, strs.join(', ')
66
+ end
67
+ strs
68
+ end
69
+ private :set_string_array_attr
70
+
71
+ def set_string_attr( key, str )
72
+ if str
73
+ store key, str
74
+ else
75
+ @header.delete key.downcase
76
+ end
77
+ str
78
+ end
79
+ private :set_string_attr
80
+
81
+ def set_addrfield( name, arg )
82
+ if arg
83
+ h = HeaderField.internal_new(name, @config)
84
+ h.addrs.replace [arg].flatten
85
+ @header[name] = h
86
+ else
87
+ @header.delete name
88
+ end
89
+ arg
90
+ end
91
+ private :set_addrfield
92
+
93
+ def addrs2specs( addrs )
94
+ return nil unless addrs
95
+ list = addrs.map {|addr|
96
+ if addr.address_group?
97
+ then addr.map {|a| a.spec }
98
+ else addr.spec
99
+ end
100
+ }.flatten
101
+ return nil if list.empty?
102
+ list
103
+ end
104
+ private :addrs2specs
105
+
106
+ #:startdoc:
107
+
108
+ #== Date and Time methods
109
+
110
+ # Returns the date of the email message as per the "date" header value or returns
111
+ # nil by default (if no date field exists).
112
+ #
113
+ # You can also pass whatever default you want into this method and it will return
114
+ # that instead of nil if there is no date already set.
115
+ def date( default = nil )
116
+ if h = @header['date']
117
+ h.date
118
+ else
119
+ default
120
+ end
121
+ end
122
+
123
+ # Destructively sets the date of the mail object with the passed Time instance,
124
+ # returns a Time instance set to the date/time of the mail
125
+ #
126
+ # Example:
127
+ #
128
+ # now = Time.now
129
+ # mail.date = now
130
+ # mail.date #=> Sat Nov 03 18:47:50 +1100 2007
131
+ # mail.date.class #=> Time
132
+ def date=( time )
133
+ if time
134
+ store 'Date', time2str(time)
135
+ else
136
+ @header.delete 'date'
137
+ end
138
+ time
139
+ end
140
+
141
+ # Returns the time of the mail message formatted to your taste using a
142
+ # strftime format string. If no date set returns nil by default or whatever value
143
+ # you pass as the second optional parameter.
144
+ #
145
+ # time = Time.now # (on Nov 16 2007)
146
+ # mail.date = time
147
+ # mail.strftime("%D") #=> "11/16/07"
148
+ def strftime( fmt, default = nil )
149
+ if t = date
150
+ t.strftime(fmt)
151
+ else
152
+ default
153
+ end
154
+ end
155
+
156
+ #== Destination methods
157
+
158
+ # Return a TMail::Addresses instance for each entry in the "To:" field of the mail object header.
159
+ #
160
+ # If the "To:" field does not exist, will return nil by default or the value you
161
+ # pass as the optional parameter.
162
+ #
163
+ # Example:
164
+ #
165
+ # mail = TMail::Mail.new
166
+ # mail.to_addrs #=> nil
167
+ # mail.to_addrs([]) #=> []
168
+ # mail.to = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
169
+ # mail.to_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
170
+ def to_addrs( default = nil )
171
+ if h = @header['to']
172
+ h.addrs
173
+ else
174
+ default
175
+ end
176
+ end
177
+
178
+ # Return a TMail::Addresses instance for each entry in the "Cc:" field of the mail object header.
179
+ #
180
+ # If the "Cc:" field does not exist, will return nil by default or the value you
181
+ # pass as the optional parameter.
182
+ #
183
+ # Example:
184
+ #
185
+ # mail = TMail::Mail.new
186
+ # mail.cc_addrs #=> nil
187
+ # mail.cc_addrs([]) #=> []
188
+ # mail.cc = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
189
+ # mail.cc_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
190
+ def cc_addrs( default = nil )
191
+ if h = @header['cc']
192
+ h.addrs
193
+ else
194
+ default
195
+ end
196
+ end
197
+
198
+ # Return a TMail::Addresses instance for each entry in the "Bcc:" field of the mail object header.
199
+ #
200
+ # If the "Bcc:" field does not exist, will return nil by default or the value you
201
+ # pass as the optional parameter.
202
+ #
203
+ # Example:
204
+ #
205
+ # mail = TMail::Mail.new
206
+ # mail.bcc_addrs #=> nil
207
+ # mail.bcc_addrs([]) #=> []
208
+ # mail.bcc = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
209
+ # mail.bcc_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
210
+ def bcc_addrs( default = nil )
211
+ if h = @header['bcc']
212
+ h.addrs
213
+ else
214
+ default
215
+ end
216
+ end
217
+
218
+ # Destructively set the to field of the "To:" header to equal the passed in string.
219
+ #
220
+ # TMail will parse your contents and turn each valid email address into a TMail::Address
221
+ # object before assigning it to the mail message.
222
+ #
223
+ # Example:
224
+ #
225
+ # mail = TMail::Mail.new
226
+ # mail.to = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
227
+ # mail.to_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
228
+ def to_addrs=( arg )
229
+ set_addrfield 'to', arg
230
+ end
231
+
232
+ # Destructively set the to field of the "Cc:" header to equal the passed in string.
233
+ #
234
+ # TMail will parse your contents and turn each valid email address into a TMail::Address
235
+ # object before assigning it to the mail message.
236
+ #
237
+ # Example:
238
+ #
239
+ # mail = TMail::Mail.new
240
+ # mail.cc = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
241
+ # mail.cc_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
242
+ def cc_addrs=( arg )
243
+ set_addrfield 'cc', arg
244
+ end
245
+
246
+ # Destructively set the to field of the "Bcc:" header to equal the passed in string.
247
+ #
248
+ # TMail will parse your contents and turn each valid email address into a TMail::Address
249
+ # object before assigning it to the mail message.
250
+ #
251
+ # Example:
252
+ #
253
+ # mail = TMail::Mail.new
254
+ # mail.bcc = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
255
+ # mail.bcc_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
256
+ def bcc_addrs=( arg )
257
+ set_addrfield 'bcc', arg
258
+ end
259
+
260
+ # Returns who the email is to as an Array of email addresses as opposed to an Array of
261
+ # TMail::Address objects which is what Mail#to_addrs returns
262
+ #
263
+ # Example:
264
+ #
265
+ # mail = TMail::Mail.new
266
+ # mail.to = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
267
+ # mail.to #=> ["mikel@me.org", "mikel@you.org"]
268
+ def to( default = nil )
269
+ addrs2specs(to_addrs(nil)) || default
270
+ end
271
+
272
+ # Returns who the email cc'd as an Array of email addresses as opposed to an Array of
273
+ # TMail::Address objects which is what Mail#to_addrs returns
274
+ #
275
+ # Example:
276
+ #
277
+ # mail = TMail::Mail.new
278
+ # mail.cc = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
279
+ # mail.cc #=> ["mikel@me.org", "mikel@you.org"]
280
+ def cc( default = nil )
281
+ addrs2specs(cc_addrs(nil)) || default
282
+ end
283
+
284
+ # Returns who the email bcc'd as an Array of email addresses as opposed to an Array of
285
+ # TMail::Address objects which is what Mail#to_addrs returns
286
+ #
287
+ # Example:
288
+ #
289
+ # mail = TMail::Mail.new
290
+ # mail.bcc = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
291
+ # mail.bcc #=> ["mikel@me.org", "mikel@you.org"]
292
+ def bcc( default = nil )
293
+ addrs2specs(bcc_addrs(nil)) || default
294
+ end
295
+
296
+ # Destructively sets the "To:" field to the passed array of strings (which should be valid
297
+ # email addresses)
298
+ #
299
+ # Example:
300
+ #
301
+ # mail = TMail::Mail.new
302
+ # mail.to = ["mikel@abc.com", "Mikel <mikel@xyz.com>"]
303
+ # mail.to #=> ["mikel@abc.org", "mikel@xyz.org"]
304
+ # mail['to'].to_s #=> "mikel@abc.com, Mikel <mikel@xyz.com>"
305
+ def to=( *strs )
306
+ set_string_array_attr 'To', strs
307
+ end
308
+
309
+ # Destructively sets the "Cc:" field to the passed array of strings (which should be valid
310
+ # email addresses)
311
+ #
312
+ # Example:
313
+ #
314
+ # mail = TMail::Mail.new
315
+ # mail.cc = ["mikel@abc.com", "Mikel <mikel@xyz.com>"]
316
+ # mail.cc #=> ["mikel@abc.org", "mikel@xyz.org"]
317
+ # mail['cc'].to_s #=> "mikel@abc.com, Mikel <mikel@xyz.com>"
318
+ def cc=( *strs )
319
+ set_string_array_attr 'Cc', strs
320
+ end
321
+
322
+ # Destructively sets the "Bcc:" field to the passed array of strings (which should be valid
323
+ # email addresses)
324
+ #
325
+ # Example:
326
+ #
327
+ # mail = TMail::Mail.new
328
+ # mail.bcc = ["mikel@abc.com", "Mikel <mikel@xyz.com>"]
329
+ # mail.bcc #=> ["mikel@abc.org", "mikel@xyz.org"]
330
+ # mail['bcc'].to_s #=> "mikel@abc.com, Mikel <mikel@xyz.com>"
331
+ def bcc=( *strs )
332
+ set_string_array_attr 'Bcc', strs
333
+ end
334
+
335
+ #== Originator methods
336
+
337
+ # Return a TMail::Addresses instance for each entry in the "From:" field of the mail object header.
338
+ #
339
+ # If the "From:" field does not exist, will return nil by default or the value you
340
+ # pass as the optional parameter.
341
+ #
342
+ # Example:
343
+ #
344
+ # mail = TMail::Mail.new
345
+ # mail.from_addrs #=> nil
346
+ # mail.from_addrs([]) #=> []
347
+ # mail.from = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
348
+ # mail.from_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
349
+ def from_addrs( default = nil )
350
+ if h = @header['from']
351
+ h.addrs
352
+ else
353
+ default
354
+ end
355
+ end
356
+
357
+ # Destructively set the to value of the "From:" header to equal the passed in string.
358
+ #
359
+ # TMail will parse your contents and turn each valid email address into a TMail::Address
360
+ # object before assigning it to the mail message.
361
+ #
362
+ # Example:
363
+ #
364
+ # mail = TMail::Mail.new
365
+ # mail.from_addrs = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
366
+ # mail.from_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
367
+ def from_addrs=( arg )
368
+ set_addrfield 'from', arg
369
+ end
370
+
371
+ # Returns who the email is from as an Array of email address strings instead to an Array of
372
+ # TMail::Address objects which is what Mail#from_addrs returns
373
+ #
374
+ # Example:
375
+ #
376
+ # mail = TMail::Mail.new
377
+ # mail.from = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
378
+ # mail.from #=> ["mikel@me.org", "mikel@you.org"]
379
+ def from( default = nil )
380
+ addrs2specs(from_addrs(nil)) || default
381
+ end
382
+
383
+ # Destructively sets the "From:" field to the passed array of strings (which should be valid
384
+ # email addresses)
385
+ #
386
+ # Example:
387
+ #
388
+ # mail = TMail::Mail.new
389
+ # mail.from = ["mikel@abc.com", "Mikel <mikel@xyz.com>"]
390
+ # mail.from #=> ["mikel@abc.org", "mikel@xyz.org"]
391
+ # mail['from'].to_s #=> "mikel@abc.com, Mikel <mikel@xyz.com>"
392
+ def from=( *strs )
393
+ set_string_array_attr 'From', strs
394
+ end
395
+
396
+ # Returns the "friendly" human readable part of the address
397
+ #
398
+ # Example:
399
+ #
400
+ # mail = TMail::Mail.new
401
+ # mail.from = "Mikel Lindsaar <mikel@abc.com>"
402
+ # mail.friendly_from #=> "Mikel Lindsaar"
403
+ def friendly_from( default = nil )
404
+ h = @header['from']
405
+ a, = h.addrs
406
+ return default unless a
407
+ return a.phrase if a.phrase
408
+ return h.comments.join(' ') unless h.comments.empty?
409
+ a.spec
410
+ end
411
+
412
+ # Return a TMail::Addresses instance for each entry in the "Reply-To:" field of the mail object header.
413
+ #
414
+ # If the "Reply-To:" field does not exist, will return nil by default or the value you
415
+ # pass as the optional parameter.
416
+ #
417
+ # Example:
418
+ #
419
+ # mail = TMail::Mail.new
420
+ # mail.reply_to_addrs #=> nil
421
+ # mail.reply_to_addrs([]) #=> []
422
+ # mail.reply_to = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
423
+ # mail.reply_to_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
424
+ def reply_to_addrs( default = nil )
425
+ if h = @header['reply-to']
426
+ h.addrs.blank? ? default : h.addrs
427
+ else
428
+ default
429
+ end
430
+ end
431
+
432
+ # Destructively set the to value of the "Reply-To:" header to equal the passed in argument.
433
+ #
434
+ # TMail will parse your contents and turn each valid email address into a TMail::Address
435
+ # object before assigning it to the mail message.
436
+ #
437
+ # Example:
438
+ #
439
+ # mail = TMail::Mail.new
440
+ # mail.reply_to_addrs = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
441
+ # mail.reply_to_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
442
+ def reply_to_addrs=( arg )
443
+ set_addrfield 'reply-to', arg
444
+ end
445
+
446
+ # Returns who the email is from as an Array of email address strings instead to an Array of
447
+ # TMail::Address objects which is what Mail#reply_to_addrs returns
448
+ #
449
+ # Example:
450
+ #
451
+ # mail = TMail::Mail.new
452
+ # mail.reply_to = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
453
+ # mail.reply_to #=> ["mikel@me.org", "mikel@you.org"]
454
+ def reply_to( default = nil )
455
+ addrs2specs(reply_to_addrs(nil)) || default
456
+ end
457
+
458
+ # Destructively sets the "Reply-To:" field to the passed array of strings (which should be valid
459
+ # email addresses)
460
+ #
461
+ # Example:
462
+ #
463
+ # mail = TMail::Mail.new
464
+ # mail.reply_to = ["mikel@abc.com", "Mikel <mikel@xyz.com>"]
465
+ # mail.reply_to #=> ["mikel@abc.org", "mikel@xyz.org"]
466
+ # mail['reply_to'].to_s #=> "mikel@abc.com, Mikel <mikel@xyz.com>"
467
+ def reply_to=( *strs )
468
+ set_string_array_attr 'Reply-To', strs
469
+ end
470
+
471
+ # Return a TMail::Addresses instance of the "Sender:" field of the mail object header.
472
+ #
473
+ # If the "Sender:" field does not exist, will return nil by default or the value you
474
+ # pass as the optional parameter.
475
+ #
476
+ # Example:
477
+ #
478
+ # mail = TMail::Mail.new
479
+ # mail.sender #=> nil
480
+ # mail.sender([]) #=> []
481
+ # mail.sender = "Mikel <mikel@me.org>"
482
+ # mail.reply_to_addrs #=> [#<TMail::Address mikel@me.org>]
483
+ def sender_addr( default = nil )
484
+ f = @header['sender'] or return default
485
+ f.addr or return default
486
+ end
487
+
488
+ # Destructively set the to value of the "Sender:" header to equal the passed in argument.
489
+ #
490
+ # TMail will parse your contents and turn each valid email address into a TMail::Address
491
+ # object before assigning it to the mail message.
492
+ #
493
+ # Example:
494
+ #
495
+ # mail = TMail::Mail.new
496
+ # mail.sender_addrs = "Mikel <mikel@me.org>, another Mikel <mikel@you.org>"
497
+ # mail.sender_addrs #=> [#<TMail::Address mikel@me.org>, #<TMail::Address mikel@you.org>]
498
+ def sender_addr=( addr )
499
+ if addr
500
+ h = HeaderField.internal_new('sender', @config)
501
+ h.addr = addr
502
+ @header['sender'] = h
503
+ else
504
+ @header.delete 'sender'
505
+ end
506
+ addr
507
+ end
508
+
509
+ # Returns who the sender of this mail is as string instead to an Array of
510
+ # TMail::Address objects which is what Mail#sender_addr returns
511
+ #
512
+ # Example:
513
+ #
514
+ # mail = TMail::Mail.new
515
+ # mail.sender = "Mikel <mikel@me.org>"
516
+ # mail.sender #=> "mikel@me.org"
517
+ def sender( default = nil )
518
+ f = @header['sender'] or return default
519
+ a = f.addr or return default
520
+ a.spec
521
+ end
522
+
523
+ # Destructively sets the "Sender:" field to the passed string (which should be a valid
524
+ # email address)
525
+ #
526
+ # Example:
527
+ #
528
+ # mail = TMail::Mail.new
529
+ # mail.sender = "mikel@abc.com"
530
+ # mail.sender #=> "mikel@abc.org"
531
+ # mail['sender'].to_s #=> "mikel@abc.com"
532
+ def sender=( str )
533
+ set_string_attr 'Sender', str
534
+ end
535
+
536
+ #== Subject methods
537
+
538
+ # Returns the subject of the mail instance.
539
+ #
540
+ # If the subject field does not exist, returns nil by default or you can pass in as
541
+ # the parameter for what you want the default value to be.
542
+ #
543
+ # Example:
544
+ #
545
+ # mail = TMail::Mail.new
546
+ # mail.subject #=> nil
547
+ # mail.subject("") #=> ""
548
+ # mail.subject = "Hello"
549
+ # mail.subject #=> "Hello"
550
+ def subject( default = nil )
551
+ if h = @header['subject']
552
+ h.body
553
+ else
554
+ default
555
+ end
556
+ end
557
+ alias quoted_subject subject
558
+
559
+ # Destructively sets the passed string as the subject of the mail message.
560
+ #
561
+ # Example
562
+ #
563
+ # mail = TMail::Mail.new
564
+ # mail.subject #=> "This subject"
565
+ # mail.subject = "Another subject"
566
+ # mail.subject #=> "Another subject"
567
+ def subject=( str )
568
+ set_string_attr 'Subject', str
569
+ end
570
+
571
+ #== Message Identity & Threading Methods
572
+
573
+ # Returns the message ID for this mail object instance.
574
+ #
575
+ # If the message_id field does not exist, returns nil by default or you can pass in as
576
+ # the parameter for what you want the default value to be.
577
+ #
578
+ # Example:
579
+ #
580
+ # mail = TMail::Mail.new
581
+ # mail.message_id #=> nil
582
+ # mail.message_id(TMail.new_message_id) #=> "<47404c5326d9c_2ad4fbb80161@baci.local.tmail>"
583
+ # mail.message_id = TMail.new_message_id
584
+ # mail.message_id #=> "<47404c5326d9c_2ad4fbb80161@baci.local.tmail>"
585
+ def message_id( default = nil )
586
+ if h = @header['message-id']
587
+ h.id || default
588
+ else
589
+ default
590
+ end
591
+ end
592
+
593
+ # Destructively sets the message ID of the mail object instance to the passed in string
594
+ #
595
+ # Invalid message IDs are ignored (silently, unless configured otherwise) and result in
596
+ # a nil message ID. Left and right angle brackets are required.
597
+ #
598
+ # Example:
599
+ #
600
+ # mail = TMail::Mail.new
601
+ # mail.message_id = "<348F04F142D69C21-291E56D292BC@xxxx.net>"
602
+ # mail.message_id #=> "<348F04F142D69C21-291E56D292BC@xxxx.net>"
603
+ # mail.message_id = "this_is_my_badly_formatted_message_id"
604
+ # mail.message_id #=> nil
605
+ def message_id=( str )
606
+ set_string_attr 'Message-Id', str
607
+ end
608
+
609
+ # Returns the "In-Reply-To:" field contents as an array of this mail instance if it exists
610
+ #
611
+ # If the in_reply_to field does not exist, returns nil by default or you can pass in as
612
+ # the parameter for what you want the default value to be.
613
+ #
614
+ # Example:
615
+ #
616
+ # mail = TMail::Mail.new
617
+ # mail.in_reply_to #=> nil
618
+ # mail.in_reply_to([]) #=> []
619
+ # TMail::Mail.load("../test/fixtures/raw_email_reply")
620
+ # mail.in_reply_to #=> ["<348F04F142D69C21-291E56D292BC@xxxx.net>"]
621
+ def in_reply_to( default = nil )
622
+ if h = @header['in-reply-to']
623
+ h.ids
624
+ else
625
+ default
626
+ end
627
+ end
628
+
629
+ # Destructively sets the value of the "In-Reply-To:" field of an email.
630
+ #
631
+ # Accepts an array of a single string of a message id
632
+ #
633
+ # Example:
634
+ #
635
+ # mail = TMail::Mail.new
636
+ # mail.in_reply_to = ["<348F04F142D69C21-291E56D292BC@xxxx.net>"]
637
+ # mail.in_reply_to #=> ["<348F04F142D69C21-291E56D292BC@xxxx.net>"]
638
+ def in_reply_to=( *idstrs )
639
+ set_string_array_attr 'In-Reply-To', idstrs
640
+ end
641
+
642
+ # Returns the references of this email (prior messages relating to this message)
643
+ # as an array of message ID strings. Useful when you are trying to thread an
644
+ # email.
645
+ #
646
+ # If the references field does not exist, returns nil by default or you can pass in as
647
+ # the parameter for what you want the default value to be.
648
+ #
649
+ # Example:
650
+ #
651
+ # mail = TMail::Mail.new
652
+ # mail.references #=> nil
653
+ # mail.references([]) #=> []
654
+ # mail = TMail::Mail.load("../test/fixtures/raw_email_reply")
655
+ # mail.references #=> ["<473FF3B8.9020707@xxx.org>", "<348F04F142D69C21-291E56D292BC@xxxx.net>"]
656
+ def references( default = nil )
657
+ if h = @header['references']
658
+ h.refs
659
+ else
660
+ default
661
+ end
662
+ end
663
+
664
+ # Destructively sets the value of the "References:" field of an email.
665
+ #
666
+ # Accepts an array of strings of message IDs
667
+ #
668
+ # Example:
669
+ #
670
+ # mail = TMail::Mail.new
671
+ # mail.references = ["<348F04F142D69C21-291E56D292BC@xxxx.net>"]
672
+ # mail.references #=> ["<348F04F142D69C21-291E56D292BC@xxxx.net>"]
673
+ def references=( *strs )
674
+ set_string_array_attr 'References', strs
675
+ end
676
+
677
+ #== MIME header methods
678
+
679
+ # Returns the listed MIME version of this email from the "Mime-Version:" header field
680
+ #
681
+ # If the mime_version field does not exist, returns nil by default or you can pass in as
682
+ # the parameter for what you want the default value to be.
683
+ #
684
+ # Example:
685
+ #
686
+ # mail = TMail::Mail.new
687
+ # mail.mime_version #=> nil
688
+ # mail.mime_version([]) #=> []
689
+ # mail = TMail::Mail.load("../test/fixtures/raw_email")
690
+ # mail.mime_version #=> "1.0"
691
+ def mime_version( default = nil )
692
+ if h = @header['mime-version']
693
+ h.version || default
694
+ else
695
+ default
696
+ end
697
+ end
698
+
699
+ def mime_version=( m, opt = nil )
700
+ if opt
701
+ if h = @header['mime-version']
702
+ h.major = m
703
+ h.minor = opt
704
+ else
705
+ store 'Mime-Version', "#{m}.#{opt}"
706
+ end
707
+ else
708
+ store 'Mime-Version', m
709
+ end
710
+ m
711
+ end
712
+
713
+ # Returns the current "Content-Type" of the mail instance.
714
+ #
715
+ # If the content_type field does not exist, returns nil by default or you can pass in as
716
+ # the parameter for what you want the default value to be.
717
+ #
718
+ # Example:
719
+ #
720
+ # mail = TMail::Mail.new
721
+ # mail.content_type #=> nil
722
+ # mail.content_type([]) #=> []
723
+ # mail = TMail::Mail.load("../test/fixtures/raw_email")
724
+ # mail.content_type #=> "text/plain"
725
+ def content_type( default = nil )
726
+ if h = @header['content-type']
727
+ h.content_type || default
728
+ else
729
+ default
730
+ end
731
+ end
732
+
733
+ # Returns the current main type of the "Content-Type" of the mail instance.
734
+ #
735
+ # If the content_type field does not exist, returns nil by default or you can pass in as
736
+ # the parameter for what you want the default value to be.
737
+ #
738
+ # Example:
739
+ #
740
+ # mail = TMail::Mail.new
741
+ # mail.main_type #=> nil
742
+ # mail.main_type([]) #=> []
743
+ # mail = TMail::Mail.load("../test/fixtures/raw_email")
744
+ # mail.main_type #=> "text"
745
+ def main_type( default = nil )
746
+ if h = @header['content-type']
747
+ h.main_type || default
748
+ else
749
+ default
750
+ end
751
+ end
752
+
753
+ # Returns the current sub type of the "Content-Type" of the mail instance.
754
+ #
755
+ # If the content_type field does not exist, returns nil by default or you can pass in as
756
+ # the parameter for what you want the default value to be.
757
+ #
758
+ # Example:
759
+ #
760
+ # mail = TMail::Mail.new
761
+ # mail.sub_type #=> nil
762
+ # mail.sub_type([]) #=> []
763
+ # mail = TMail::Mail.load("../test/fixtures/raw_email")
764
+ # mail.sub_type #=> "plain"
765
+ def sub_type( default = nil )
766
+ if h = @header['content-type']
767
+ h.sub_type || default
768
+ else
769
+ default
770
+ end
771
+ end
772
+
773
+ # Destructively sets the "Content-Type:" header field of this mail object
774
+ #
775
+ # Allows you to set the main type, sub type as well as parameters to the field.
776
+ # The main type and sub type need to be a string.
777
+ #
778
+ # The optional params hash can be passed with keys as symbols and values as a string,
779
+ # or strings as keys and values.
780
+ #
781
+ # Example:
782
+ #
783
+ # mail = TMail::Mail.new
784
+ # mail.set_content_type("text", "plain")
785
+ # mail.to_s #=> "Content-Type: text/plain\n\n"
786
+ #
787
+ # mail.set_content_type("text", "plain", {:charset => "EUC-KR", :format => "flowed"})
788
+ # mail.to_s #=> "Content-Type: text/plain; charset=EUC-KR; format=flowed\n\n"
789
+ #
790
+ # mail.set_content_type("text", "plain", {"charset" => "EUC-KR", "format" => "flowed"})
791
+ # mail.to_s #=> "Content-Type: text/plain; charset=EUC-KR; format=flowed\n\n"
792
+ def set_content_type( str, sub = nil, param = nil )
793
+ if sub
794
+ main, sub = str, sub
795
+ else
796
+ main, sub = str.split(%r</>, 2)
797
+ raise ArgumentError, "sub type missing: #{str.inspect}" unless sub
798
+ end
799
+ if h = @header['content-type']
800
+ h.main_type = main
801
+ h.sub_type = sub
802
+ h.params.clear
803
+ else
804
+ store 'Content-Type', "#{main}/#{sub}"
805
+ end
806
+ @header['content-type'].params.replace param if param
807
+ str
808
+ end
809
+
810
+ alias content_type= set_content_type
811
+
812
+ # Returns the named type parameter as a string, from the "Content-Type:" header.
813
+ #
814
+ # Example:
815
+ #
816
+ # mail = TMail::Mail.new
817
+ # mail.type_param("charset") #=> nil
818
+ # mail.type_param("charset", []) #=> []
819
+ # mail.set_content_type("text", "plain", {:charset => "EUC-KR", :format => "flowed"})
820
+ # mail.type_param("charset") #=> "EUC-KR"
821
+ # mail.type_param("format") #=> "flowed"
822
+ def type_param( name, default = nil )
823
+ if h = @header['content-type']
824
+ h[name] || default
825
+ else
826
+ default
827
+ end
828
+ end
829
+
830
+ # Returns the character set of the email. Returns nil if no encoding set or returns
831
+ # whatever default you pass as a parameter - note passing the parameter does NOT change
832
+ # the mail object in any way.
833
+ #
834
+ # Example:
835
+ #
836
+ # mail = TMail::Mail.load("path_to/utf8_email")
837
+ # mail.charset #=> "UTF-8"
838
+ #
839
+ # mail = TMail::Mail.new
840
+ # mail.charset #=> nil
841
+ # mail.charset("US-ASCII") #=> "US-ASCII"
842
+ def charset( default = nil )
843
+ if h = @header['content-type']
844
+ h['charset'] or default
845
+ else
846
+ default
847
+ end
848
+ end
849
+
850
+ # Destructively sets the character set used by this mail object to the passed string, you
851
+ # should note though that this does nothing to the mail body, just changes the header
852
+ # value, you will need to transliterate the body as well to match whatever you put
853
+ # in this header value if you are changing character sets.
854
+ #
855
+ # Example:
856
+ #
857
+ # mail = TMail::Mail.new
858
+ # mail.charset #=> nil
859
+ # mail.charset = "UTF-8"
860
+ # mail.charset #=> "UTF-8"
861
+ def charset=( str )
862
+ if str
863
+ if h = @header[ 'content-type' ]
864
+ h['charset'] = str
865
+ else
866
+ store 'Content-Type', "text/plain; charset=#{str}"
867
+ end
868
+ end
869
+ str
870
+ end
871
+
872
+ # Returns the transfer encoding of the email. Returns nil if no encoding set or returns
873
+ # whatever default you pass as a parameter - note passing the parameter does NOT change
874
+ # the mail object in any way.
875
+ #
876
+ # Example:
877
+ #
878
+ # mail = TMail::Mail.load("path_to/base64_encoded_email")
879
+ # mail.transfer_encoding #=> "base64"
880
+ #
881
+ # mail = TMail::Mail.new
882
+ # mail.transfer_encoding #=> nil
883
+ # mail.transfer_encoding("base64") #=> "base64"
884
+ def transfer_encoding( default = nil )
885
+ if h = @header['content-transfer-encoding']
886
+ h.encoding || default
887
+ else
888
+ default
889
+ end
890
+ end
891
+
892
+ # Destructively sets the transfer encoding of the mail object to the passed string, you
893
+ # should note though that this does nothing to the mail body, just changes the header
894
+ # value, you will need to encode or decode the body as well to match whatever you put
895
+ # in this header value.
896
+ #
897
+ # Example:
898
+ #
899
+ # mail = TMail::Mail.new
900
+ # mail.transfer_encoding #=> nil
901
+ # mail.transfer_encoding = "base64"
902
+ # mail.transfer_encoding #=> "base64"
903
+ def transfer_encoding=( str )
904
+ set_string_attr 'Content-Transfer-Encoding', str
905
+ end
906
+
907
+ alias encoding transfer_encoding
908
+ alias encoding= transfer_encoding=
909
+ alias content_transfer_encoding transfer_encoding
910
+ alias content_transfer_encoding= transfer_encoding=
911
+
912
+ # Returns the content-disposition of the mail object, returns nil or the passed
913
+ # default value if given
914
+ #
915
+ # Example:
916
+ #
917
+ # mail = TMail::Mail.load("path_to/raw_mail_with_attachment")
918
+ # mail.disposition #=> "attachment"
919
+ #
920
+ # mail = TMail::Mail.load("path_to/plain_simple_email")
921
+ # mail.disposition #=> nil
922
+ # mail.disposition(false) #=> false
923
+ def disposition( default = nil )
924
+ if h = @header['content-disposition']
925
+ h.disposition || default
926
+ else
927
+ default
928
+ end
929
+ end
930
+
931
+ alias content_disposition disposition
932
+
933
+ # Allows you to set the content-disposition of the mail object. Accepts a type
934
+ # and a hash of parameters.
935
+ #
936
+ # Example:
937
+ #
938
+ # mail.set_disposition("attachment", {:filename => "test.rb"})
939
+ # mail.disposition #=> "attachment"
940
+ # mail['content-disposition'].to_s #=> "attachment; filename=test.rb"
941
+ def set_disposition( str, params = nil )
942
+ if h = @header['content-disposition']
943
+ h.disposition = str
944
+ h.params.clear
945
+ else
946
+ store('Content-Disposition', str)
947
+ h = @header['content-disposition']
948
+ end
949
+ h.params.replace params if params
950
+ end
951
+
952
+ alias disposition= set_disposition
953
+ alias set_content_disposition set_disposition
954
+ alias content_disposition= set_disposition
955
+
956
+ # Returns the value of a parameter in an existing content-disposition header
957
+ #
958
+ # Example:
959
+ #
960
+ # mail.set_disposition("attachment", {:filename => "test.rb"})
961
+ # mail['content-disposition'].to_s #=> "attachment; filename=test.rb"
962
+ # mail.disposition_param("filename") #=> "test.rb"
963
+ # mail.disposition_param("missing_param_key") #=> nil
964
+ # mail.disposition_param("missing_param_key", false) #=> false
965
+ # mail.disposition_param("missing_param_key", "Nothing to see here") #=> "Nothing to see here"
966
+ def disposition_param( name, default = nil )
967
+ if h = @header['content-disposition']
968
+ h[name] || default
969
+ else
970
+ default
971
+ end
972
+ end
973
+
974
+ # Convert the Mail object's body into a Base64 encoded email
975
+ # returning the modified Mail object
976
+ def base64_encode!
977
+ store 'Content-Transfer-Encoding', 'Base64'
978
+ self.body = base64_encode
979
+ end
980
+
981
+ # Return the result of encoding the TMail::Mail object body
982
+ # without altering the current body
983
+ def base64_encode
984
+ Base64.folding_encode(self.body)
985
+ end
986
+
987
+ # Convert the Mail object's body into a Base64 decoded email
988
+ # returning the modified Mail object
989
+ def base64_decode!
990
+ if /base64/i === self.transfer_encoding('')
991
+ store 'Content-Transfer-Encoding', '8bit'
992
+ self.body = base64_decode
993
+ end
994
+ end
995
+
996
+ # Returns the result of decoding the TMail::Mail object body
997
+ # without altering the current body
998
+ def base64_decode
999
+ Base64.decode(self.body, @config.strict_base64decode?)
1000
+ end
1001
+
1002
+ # Returns an array of each destination in the email message including to: cc: or bcc:
1003
+ #
1004
+ # Example:
1005
+ #
1006
+ # mail.to = "Mikel <mikel@lindsaar.net>"
1007
+ # mail.cc = "Trans <t@t.com>"
1008
+ # mail.bcc = "bob <bob@me.com>"
1009
+ # mail.destinations #=> ["mikel@lindsaar.net", "t@t.com", "bob@me.com"]
1010
+ def destinations( default = nil )
1011
+ ret = []
1012
+ %w( to cc bcc ).each do |nm|
1013
+ if h = @header[nm]
1014
+ h.addrs.each {|i| ret.push i.address }
1015
+ end
1016
+ end
1017
+ ret.empty? ? default : ret
1018
+ end
1019
+
1020
+ # Yields a block of destination, yielding each as a string.
1021
+ # (from the destinations example)
1022
+ # mail.each_destination { |d| puts "#{d.class}: #{d}" }
1023
+ # String: mikel@lindsaar.net
1024
+ # String: t@t.com
1025
+ # String: bob@me.com
1026
+ def each_destination( &block )
1027
+ destinations([]).each do |i|
1028
+ if Address === i
1029
+ yield i
1030
+ else
1031
+ i.each(&block)
1032
+ end
1033
+ end
1034
+ end
1035
+
1036
+ alias each_dest each_destination
1037
+
1038
+ # Returns an array of reply to addresses that the Mail object has,
1039
+ # or if the Mail message has no reply-to, returns an array of the
1040
+ # Mail objects from addresses. Else returns the default which can
1041
+ # either be passed as a parameter or defaults to nil
1042
+ #
1043
+ # Example:
1044
+ # mail.from = "Mikel <mikel@lindsaar.net>"
1045
+ # mail.reply_to = nil
1046
+ # mail.reply_addresses #=> [""]
1047
+ #
1048
+ def reply_addresses( default = nil )
1049
+ reply_to_addrs(nil) or from_addrs(nil) or default
1050
+ end
1051
+
1052
+ # Returns the "sender" field as an array -> useful to find out who to
1053
+ # send an error email to.
1054
+ def error_reply_addresses( default = nil )
1055
+ if s = sender(nil)
1056
+ [s]
1057
+ else
1058
+ from_addrs(default)
1059
+ end
1060
+ end
1061
+
1062
+ # Returns true if the Mail object is a multipart message
1063
+ def multipart?
1064
+ main_type('').downcase == 'multipart'
1065
+ end
1066
+
1067
+ # Creates a new email in reply to self. Sets the In-Reply-To and
1068
+ # References headers for you automagically.
1069
+ #
1070
+ # Example:
1071
+ # mail = TMail::Mail.load("my_email")
1072
+ # reply_email = mail.create_reply
1073
+ # reply_email.class #=> TMail::Mail
1074
+ # reply_email.references #=> ["<d3b8cf8e49f04480850c28713a1f473e@lindsaar.net>"]
1075
+ # reply_email.in_reply_to #=> ["<d3b8cf8e49f04480850c28713a1f473e@lindsaar.net>"]
1076
+ def create_reply
1077
+ setup_reply create_empty_mail()
1078
+ end
1079
+
1080
+ # Creates a new email in reply to self. Sets the In-Reply-To and
1081
+ # References headers for you automagically.
1082
+ #
1083
+ # Example:
1084
+ # mail = TMail::Mail.load("my_email")
1085
+ # forward_email = mail.create_forward
1086
+ # forward_email.class #=> TMail::Mail
1087
+ # forward_email.content_type #=> "multipart/mixed"
1088
+ # forward_email.body #=> "Attachment: (unnamed)"
1089
+ # forward_email.encoded #=> Returns the original email as a MIME attachment
1090
+ def create_forward
1091
+ setup_forward create_empty_mail()
1092
+ end
1093
+
1094
+ #:stopdoc:
1095
+ private
1096
+
1097
+ def create_empty_mail
1098
+ self.class.new(StringPort.new(''), @config)
1099
+ end
1100
+
1101
+ def setup_reply( mail )
1102
+ if tmp = reply_addresses(nil)
1103
+ mail.to_addrs = tmp
1104
+ end
1105
+
1106
+ mid = message_id(nil)
1107
+ tmp = references(nil) || []
1108
+ tmp.push mid if mid
1109
+ mail.in_reply_to = [mid] if mid
1110
+ mail.references = tmp unless tmp.empty?
1111
+ mail.subject = 'Re: ' + subject('').sub(/\A(?:\[[^\]]+\])?(?:\s*Re:)*\s*/i, '')
1112
+ mail.mime_version = '1.0'
1113
+ mail
1114
+ end
1115
+
1116
+ def setup_forward( mail )
1117
+ m = Mail.new(StringPort.new(''))
1118
+ m.body = decoded
1119
+ m.set_content_type 'message', 'rfc822'
1120
+ m.encoding = encoding('7bit')
1121
+ mail.parts.push m
1122
+ # call encoded to reparse the message
1123
+ mail.encoded
1124
+ mail
1125
+ end
1126
+
1127
+ #:startdoc:
1128
+ end # class Mail
1129
+
1130
+ end # module TMail