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,379 @@
1
+ =begin rdoc
2
+
3
+ = Port class
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
+ require 'tmail/stringio'
33
+
34
+
35
+ module TMail
36
+
37
+ class Port
38
+ def reproducible?
39
+ false
40
+ end
41
+ end
42
+
43
+
44
+ ###
45
+ ### FilePort
46
+ ###
47
+
48
+ class FilePort < Port
49
+
50
+ def initialize( fname )
51
+ @filename = File.expand_path(fname)
52
+ super()
53
+ end
54
+
55
+ attr_reader :filename
56
+
57
+ alias ident filename
58
+
59
+ def ==( other )
60
+ other.respond_to?(:filename) and @filename == other.filename
61
+ end
62
+
63
+ alias eql? ==
64
+
65
+ def hash
66
+ @filename.hash
67
+ end
68
+
69
+ def inspect
70
+ "#<#{self.class}:#{@filename}>"
71
+ end
72
+
73
+ def reproducible?
74
+ true
75
+ end
76
+
77
+ def size
78
+ File.size @filename
79
+ end
80
+
81
+
82
+ def ropen( &block )
83
+ File.open(@filename, &block)
84
+ end
85
+
86
+ def wopen( &block )
87
+ File.open(@filename, 'w', &block)
88
+ end
89
+
90
+ def aopen( &block )
91
+ File.open(@filename, 'a', &block)
92
+ end
93
+
94
+
95
+ def read_all
96
+ ropen {|f|
97
+ return f.read
98
+ }
99
+ end
100
+
101
+
102
+ def remove
103
+ File.unlink @filename
104
+ end
105
+
106
+ def move_to( port )
107
+ begin
108
+ File.link @filename, port.filename
109
+ rescue Errno::EXDEV
110
+ copy_to port
111
+ end
112
+ File.unlink @filename
113
+ end
114
+
115
+ alias mv move_to
116
+
117
+ def copy_to( port )
118
+ if FilePort === port
119
+ copy_file @filename, port.filename
120
+ else
121
+ File.open(@filename) {|r|
122
+ port.wopen {|w|
123
+ while s = r.sysread(4096)
124
+ w.write << s
125
+ end
126
+ } }
127
+ end
128
+ end
129
+
130
+ alias cp copy_to
131
+
132
+ private
133
+
134
+ # from fileutils.rb
135
+ def copy_file( src, dest )
136
+ st = r = w = nil
137
+
138
+ File.open(src, 'rb') {|r|
139
+ File.open(dest, 'wb') {|w|
140
+ st = r.stat
141
+ begin
142
+ while true
143
+ w.write r.sysread(st.blksize)
144
+ end
145
+ rescue EOFError
146
+ end
147
+ } }
148
+ end
149
+
150
+ end
151
+
152
+
153
+ module MailFlags
154
+
155
+ def seen=( b )
156
+ set_status 'S', b
157
+ end
158
+
159
+ def seen?
160
+ get_status 'S'
161
+ end
162
+
163
+ def replied=( b )
164
+ set_status 'R', b
165
+ end
166
+
167
+ def replied?
168
+ get_status 'R'
169
+ end
170
+
171
+ def flagged=( b )
172
+ set_status 'F', b
173
+ end
174
+
175
+ def flagged?
176
+ get_status 'F'
177
+ end
178
+
179
+ private
180
+
181
+ def procinfostr( str, tag, true_p )
182
+ a = str.upcase.split(//)
183
+ a.push true_p ? tag : nil
184
+ a.delete tag unless true_p
185
+ a.compact.sort.join('').squeeze
186
+ end
187
+
188
+ end
189
+
190
+
191
+ class MhPort < FilePort
192
+
193
+ include MailFlags
194
+
195
+ private
196
+
197
+ def set_status( tag, flag )
198
+ begin
199
+ tmpfile = @filename + '.tmailtmp.' + $$.to_s
200
+ File.open(tmpfile, 'w') {|f|
201
+ write_status f, tag, flag
202
+ }
203
+ File.unlink @filename
204
+ File.link tmpfile, @filename
205
+ ensure
206
+ File.unlink tmpfile
207
+ end
208
+ end
209
+
210
+ def write_status( f, tag, flag )
211
+ stat = ''
212
+ File.open(@filename) {|r|
213
+ while line = r.gets
214
+ if line.strip.empty?
215
+ break
216
+ elsif m = /\AX-TMail-Status:/i.match(line)
217
+ stat = m.post_match.strip
218
+ else
219
+ f.print line
220
+ end
221
+ end
222
+
223
+ s = procinfostr(stat, tag, flag)
224
+ f.puts 'X-TMail-Status: ' + s unless s.empty?
225
+ f.puts
226
+
227
+ while s = r.read(2048)
228
+ f.write s
229
+ end
230
+ }
231
+ end
232
+
233
+ def get_status( tag )
234
+ File.foreach(@filename) {|line|
235
+ return false if line.strip.empty?
236
+ if m = /\AX-TMail-Status:/i.match(line)
237
+ return m.post_match.strip.include?(tag[0])
238
+ end
239
+ }
240
+ false
241
+ end
242
+
243
+ end
244
+
245
+
246
+ class MaildirPort < FilePort
247
+
248
+ def move_to_new
249
+ new = replace_dir(@filename, 'new')
250
+ File.rename @filename, new
251
+ @filename = new
252
+ end
253
+
254
+ def move_to_cur
255
+ new = replace_dir(@filename, 'cur')
256
+ File.rename @filename, new
257
+ @filename = new
258
+ end
259
+
260
+ def replace_dir( path, dir )
261
+ "#{File.dirname File.dirname(path)}/#{dir}/#{File.basename path}"
262
+ end
263
+ private :replace_dir
264
+
265
+
266
+ include MailFlags
267
+
268
+ private
269
+
270
+ MAIL_FILE = /\A(\d+\.[\d_]+\.[^:]+)(?:\:(\d),(\w+)?)?\z/
271
+
272
+ def set_status( tag, flag )
273
+ if m = MAIL_FILE.match(File.basename(@filename))
274
+ s, uniq, type, info, = m.to_a
275
+ return if type and type != '2' # do not change anything
276
+ newname = File.dirname(@filename) + '/' +
277
+ uniq + ':2,' + procinfostr(info.to_s, tag, flag)
278
+ else
279
+ newname = @filename + ':2,' + tag
280
+ end
281
+
282
+ File.link @filename, newname
283
+ File.unlink @filename
284
+ @filename = newname
285
+ end
286
+
287
+ def get_status( tag )
288
+ m = MAIL_FILE.match(File.basename(@filename)) or return false
289
+ m[2] == '2' and m[3].to_s.include?(tag[0])
290
+ end
291
+
292
+ end
293
+
294
+
295
+ ###
296
+ ### StringPort
297
+ ###
298
+
299
+ class StringPort < Port
300
+
301
+ def initialize( str = '' )
302
+ @buffer = str
303
+ super()
304
+ end
305
+
306
+ def string
307
+ @buffer
308
+ end
309
+
310
+ def to_s
311
+ @buffer.dup
312
+ end
313
+
314
+ alias read_all to_s
315
+
316
+ def size
317
+ @buffer.size
318
+ end
319
+
320
+ def ==( other )
321
+ StringPort === other and @buffer.equal? other.string
322
+ end
323
+
324
+ alias eql? ==
325
+
326
+ def hash
327
+ @buffer.object_id.hash
328
+ end
329
+
330
+ def inspect
331
+ "#<#{self.class}:id=#{sprintf '0x%x', @buffer.object_id}>"
332
+ end
333
+
334
+ def reproducible?
335
+ true
336
+ end
337
+
338
+ def ropen( &block )
339
+ @buffer or raise Errno::ENOENT, "#{inspect} is already removed"
340
+ StringInput.open(@buffer, &block)
341
+ end
342
+
343
+ def wopen( &block )
344
+ @buffer = ''
345
+ StringOutput.new(@buffer, &block)
346
+ end
347
+
348
+ def aopen( &block )
349
+ @buffer ||= ''
350
+ StringOutput.new(@buffer, &block)
351
+ end
352
+
353
+ def remove
354
+ @buffer = nil
355
+ end
356
+
357
+ alias rm remove
358
+
359
+ def copy_to( port )
360
+ port.wopen {|f|
361
+ f.write @buffer
362
+ }
363
+ end
364
+
365
+ alias cp copy_to
366
+
367
+ def move_to( port )
368
+ if StringPort === port
369
+ str = @buffer
370
+ port.instance_eval { @buffer = str }
371
+ else
372
+ copy_to port
373
+ end
374
+ remove
375
+ end
376
+
377
+ end
378
+
379
+ end # module TMail
@@ -0,0 +1,118 @@
1
+ =begin rdoc
2
+
3
+ = Quoting methods
4
+
5
+ =end
6
+ module TMail
7
+ class Mail
8
+ def subject(to_charset = 'utf-8')
9
+ Unquoter.unquote_and_convert_to(quoted_subject, to_charset)
10
+ end
11
+
12
+ def unquoted_body(to_charset = 'utf-8')
13
+ from_charset = sub_header("content-type", "charset")
14
+ case (content_transfer_encoding || "7bit").downcase
15
+ when "quoted-printable"
16
+ # the default charset is set to iso-8859-1 instead of 'us-ascii'.
17
+ # This is needed as many mailer do not set the charset but send in ISO. This is only used if no charset is set.
18
+ if !from_charset.blank? && from_charset.downcase == 'us-ascii'
19
+ from_charset = 'iso-8859-1'
20
+ end
21
+
22
+ Unquoter.unquote_quoted_printable_and_convert_to(quoted_body,
23
+ to_charset, from_charset, true)
24
+ when "base64"
25
+ Unquoter.unquote_base64_and_convert_to(quoted_body, to_charset,
26
+ from_charset)
27
+ when "7bit", "8bit"
28
+ Unquoter.convert_to(quoted_body, to_charset, from_charset)
29
+ when "binary"
30
+ quoted_body
31
+ else
32
+ quoted_body
33
+ end
34
+ end
35
+
36
+ def body(to_charset = 'utf-8', &block)
37
+ attachment_presenter = block || Proc.new { |file_name| "Attachment: #{file_name}\n" }
38
+
39
+ if multipart?
40
+ parts.collect { |part|
41
+ header = part["content-type"]
42
+
43
+ if part.multipart?
44
+ part.body(to_charset, &attachment_presenter)
45
+ elsif header.nil?
46
+ ""
47
+ elsif !attachment?(part)
48
+ part.unquoted_body(to_charset)
49
+ else
50
+ attachment_presenter.call(header["name"] || "(unnamed)")
51
+ end
52
+ }.join
53
+ else
54
+ unquoted_body(to_charset)
55
+ end
56
+ end
57
+ end
58
+
59
+ class Unquoter
60
+ class << self
61
+ def unquote_and_convert_to(text, to_charset, from_charset = "iso-8859-1", preserve_underscores=false)
62
+ return "" if text.nil?
63
+ text.gsub(/(.*?)(?:(?:=\?(.*?)\?(.)\?(.*?)\?=)|$)/) do
64
+ before = $1
65
+ from_charset = $2
66
+ quoting_method = $3
67
+ text = $4
68
+
69
+ before = convert_to(before, to_charset, from_charset) if before.length > 0
70
+ before + case quoting_method
71
+ when "q", "Q" then
72
+ unquote_quoted_printable_and_convert_to(text, to_charset, from_charset, preserve_underscores)
73
+ when "b", "B" then
74
+ unquote_base64_and_convert_to(text, to_charset, from_charset)
75
+ when nil then
76
+ # will be nil at the end of the string, due to the nature of
77
+ # the regex used.
78
+ ""
79
+ else
80
+ raise "unknown quoting method #{quoting_method.inspect}"
81
+ end
82
+ end
83
+ end
84
+
85
+ def unquote_quoted_printable_and_convert_to(text, to, from, preserve_underscores=false)
86
+ text = text.gsub(/_/, " ") unless preserve_underscores
87
+ text = text.gsub(/\r\n|\r/, "\n") # normalize newlines
88
+ convert_to(text.unpack("M*").first, to, from)
89
+ end
90
+
91
+ def unquote_base64_and_convert_to(text, to, from)
92
+ convert_to(Base64.decode(text), to, from)
93
+ end
94
+
95
+ begin
96
+ require 'iconv'
97
+ def convert_to(text, to, from)
98
+ return text unless to && from
99
+ text ? Iconv.iconv(to, from, text).first : ""
100
+ rescue Iconv::IllegalSequence, Iconv::InvalidEncoding, Errno::EINVAL
101
+ # the 'from' parameter specifies a charset other than what the text
102
+ # actually is...not much we can do in this case but just return the
103
+ # unconverted text.
104
+ #
105
+ # Ditto if either parameter represents an unknown charset, like
106
+ # X-UNKNOWN.
107
+ text
108
+ end
109
+ rescue LoadError
110
+ # Not providing quoting support
111
+ def convert_to(text, to, from)
112
+ warn "Action Mailer: iconv not loaded; ignoring conversion from #{from} to #{to} (#{__FILE__}:#{__LINE__})"
113
+ text
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end