Module: Sanguinews

Defined in:
lib/sanguinews/config.rb,
lib/sanguinews.rb,
lib/sanguinews/version.rb,
lib/sanguinews/nntp_msg.rb,
lib/sanguinews/file_to_upload.rb

Overview

Config - class designed specifically for sanguinews Copyright © 2013-2014, Tadeus Dobrovolskij This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Defined Under Namespace

Classes: Config, FileToUpload, NntpMsg

Constant Summary collapse

VERSION =
"0.62"

Class Method Summary collapse

Class Method Details

.connect(conn_nr) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sanguinews.rb', line 87

def connect(conn_nr)
  begin
    nntp = Net::NNTP.start(
	@config.server, @config.port, @config.username, @config.password, @config.mode)
  rescue
    @s.log([$!, $@], stderr: true) if @config.debug
    if @config.verbose
      parse_error($!.to_s)
      @s.log("Connection nr. #{conn_nr} has failed. Reconnecting...\n", stderr: true)
    end
    sleep @config.reconnect_delay
    retry
  end
  return nntp
end

.form_message(data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sanguinews.rb', line 66

def form_message(data)
  message = data[:yenc]
  length = data[:length]
  pcrc32 = data[:crc32]
  file = data[:file]
  chunk = data[:chunk]
  crc32 = file.crc32
  fsize = file.size
  chunks = file.chunks
  basename = file.name
  # usenet works with ASCII
  subject="#{@config.prefix}#{file.dir_prefix}\"#{basename}\" yEnc (#{chunk}/#{chunks})"
  msg = NntpMsg.new(@config.from, @config.groups, subject)
  msg.poster = "sanguinews v#{Sanguinews::VERSION} (ruby #{RUBY_VERSION}) - https://github.com/tdobrovolskij/sanguinews"
  msg.xna = @config.xna
  msg.message = message.force_encoding('ASCII-8BIT')
  msg.yenc_body(chunk, chunks, crc32, pcrc32, length, fsize, basename)
  msg = msg.return_self
  { message: msg, filename: basename, chunk: chunk, length: length }
end

.get_msgid(responses) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/sanguinews.rb', line 103

def get_msgid(responses)
  msgid = ''
  responses.each do |response|
    msgid = response.sub(/>.*/, '').tr("<", '') if response.end_with?('Article posted')
  end
  return msgid
end

.parse_error(msg, **info) ⇒ Object



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
# File 'lib/sanguinews.rb', line 111

def parse_error(msg, **info)
  if info[:file] && info[:chunk]
    fileinfo = '(' + info[:file] + ' / Chunk: ' + info[:chunk].to_s + ')'
  else
    fileinfo = ''
  end

  case
  when /\A411/ === msg
    @s.log("Invalid newsgroup specified.\n", stderr: true)
  when /\A430/ === msg
    @s.log("No such article. Maybe server is lagging...#{fileinfo}\n", stderr: true)
  when /\A(4\d{2}\s)?437/ === msg
    @s.log("Article rejected by server. Maybe it's too big.#{fileinfo}\n", stderr: true)
  when /\A440/ === msg
    @s.log("Posting not allowed.\n", stderr: true)
  when /\A441/ === msg
    @s.log("Posting failed for some reason.#{fileinfo}\n", stderr: true)
  when /\A450/ === msg
    @s.log("Not authorized.\n", stderr: true)
  when /\A452/ === msg
    @s.log("Wrong username and/or password.\n", stderr: true)
  when /\A500/ === msg
    @s.log("Command not recognized.\n", stderr: true)
  when /\A501/ === msg
    @s.log("Command syntax error.\n", stderr: true)
  when /\A502/ === msg
    @s.log("Access denied.\n", stderr: true)
  end
end

.process_and_upload(queue, nntp_pool, info_lock, informed) ⇒ Object



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
190
191
192
193
194
195
# File 'lib/sanguinews.rb', line 142

def process_and_upload(queue, nntp_pool, info_lock, informed)
  stuff = queue.pop
  queue.synchronize do
    @cond.signal
  end
  nntp = nntp_pool.pop

  data = stuff[0]
  file = stuff[1]
  msg = data[:message]
  chunk = data[:chunk]
  basename = data[:filename]
  length = data[:length]
  full_size = msg.length
  info_lock.synchronize do
    if !informed[basename.to_sym]
      @s.log("Uploading #{basename}\n")
      @s.log(file.subject + "\n")
      @s.log("Chunks: #{file.chunks}\n", stderr: true) if @verbose
      informed[basename.to_sym] = true
    end
  end

  @s.start
  check_delay = 1
  begin
    response = nntp.post msg
    msgid = get_msgid(response)
    if @config.header_check
      sleep check_delay
      nntp.stat("<#{msgid}>")
    end
  rescue
    @s.log([$!, $@], stderr: true) if @config.debug
    if @config.verbose
      parse_error($!.to_s, file: basename, chunk: chunk)
      @s.log("Upload of chunk #{chunk} from file #{basename} unsuccessful. Retrying...\n", stderr: true)
    end
    sleep @config.reconnect_delay
    check_delay += 4
    retry
  end

  if @config.verbose
    @s.log("Uploaded chunk Nr:#{chunk}\n", stderr: true)
  end

  @s.done(length)
  @s.uploaded += full_size
  if @config.nzb
    file.write_segment_info(length, chunk, msgid)
  end
  nntp_pool.push(nntp)
end

.run!Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/sanguinews.rb', line 197

def run!
  @config = Config.new(ARGV)
  files = @config.files

  # skip hidden files
  if !@config.filemode
    Dir.foreach(@config.directory) do |item|
      next if item.start_with?('.')
      files << @config.directory+item
    end
  end

  # "max" is needed only in dirmode
  max = files.length
  current_file = 1

  unprocessed = 0
  info_lock=Mutex.new
  messages = Queue.new
  messages.extend(MonitorMixin)
  @cond = messages.new_cond
  files_to_process = []
  @s = Speedometer.new(units: "KB", progressbar: true)
  @s.uploaded = 0

  pool = Queue.new
  Thread.new {
    @config.connections.times do |conn_nr|
      nntp = connect(conn_nr)
      pool.push(nntp)
    end
  }

  thread_pool = Pool.new(@config.connections)
  informed = {}

  files.each do |file|
    next if !File.file?(file)

    informed[file.to_sym] = false
    file = FileToUpload.new(
      name: file, chunk_length: @config.article_size, prefix: @config.prefix,
	current: current_file, last: max, filemode: @config.filemode,
	from: @config.from, groups: @config.groups, nzb: @config.nzb
    )
    @s.to_upload += file.size

    info_lock.synchronize do
      unprocessed += file.chunks
    end

    files_to_process << file
    current_file += 1
  end

  # let's give a little bit higher priority for file processing thread
  @file_proc_thread = Thread.new {
    files_to_process.each do |file|
      @s.log("Calculating CRC32 value for #{file.name}\n", stderr: true) if @verbose
      file.file_crc32
      @s.log("Encoding #{file.name}\n")
      yencode(file, @config.article_size, messages)
    end
  }
  @file_proc_thread.priority += 2

  until unprocessed == 0
    thread_pool.schedule do
      process_and_upload(messages, pool, info_lock, informed)
    end
    unprocessed -= 1
  end

  thread_pool.shutdown

  until pool.empty?
    nntp = pool.pop
    nntp.finish
  end

  @s.stop
  puts

  files_to_process.each do |file|
    if files_to_process.last == file
      last = true
    else
      last = false
    end
    file.close(last)
  end
end

.yencode(file, length, queue) ⇒ Object

Method returns yenc encoded string and crc32 value



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
# File 'lib/sanguinews.rb', line 40

def yencode(file, length, queue)
   chunk = 1
   until file.eof?
      bindata = file.read(length)
      # We can't take all memory, so we wait
      queue.synchronize do
        @cond.wait_while do
          queue.length > @config.connections * 3
        end
      end
      data = {}
      final_data = []
      len = bindata.length
      yencoded = Yencoded::Data.yenc(bindata, len)
      data[:crc32] = yencoded[1].to_s(16)
      data[:yenc] = yencoded[0]
      data[:length] = len
      data[:chunk] = chunk
      data[:file] = file
      final_data[0] = form_message(data)
      final_data[1] = file
      queue.push(final_data)
      chunk += 1
   end
end