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

FileToUpload - File class’ extension specifically for sanguinews Copyright © 2013, 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.71"

Class Method Summary collapse

Class Method Details

.connect(conn_nr) ⇒ Object



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

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

.create_upload_list(info_lock) ⇒ Object



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

def create_upload_list(info_lock)
  files = @config.files

  # skip hidden files
  if !@config.filemode && Dir.exists?(@config.directory)
    @config.recursive ? glob = '**/*' : glob = '*'
    Dir.glob(@config.directory + glob).sort.each do |item|
      next unless File.file?(item)
      files << item
    end
  end

  files_to_process = []
  informed = {}
  unprocessed = 0
  current_file = 1
  # "max" is needed only in dirmode
  max = files.length

  files.each do |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,
	xna: @config.xna
    )
    @s.to_upload += file.size

    info_lock.synchronize do
      unprocessed += file.chunks
    end

    files_to_process << file
    current_file += 1
  end

  if files_to_process.empty?
    puts "Upload list is empty! Make sure that you spelled file/directory name(s) correctly!"
    exit 1
  end
  return files_to_process, informed, unprocessed
end

.get_msgid(responses) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/sanguinews.rb', line 85

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



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

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



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

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



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

def run!
  @config = Config.new(ARGV)
  info_lock=Mutex.new
  messages = Queue.new
  messages.extend(MonitorMixin)
  @cond = messages.new_cond
  @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)

  files_to_process, informed, unprocessed = create_upload_list(info_lock)

  # 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("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
65
66
67
# 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)
      msg = file.messages[chunk-1]
      msg.message = yencoded[0].force_encoding('ASCII-8BIT')
      msg.part_crc32 = yencoded[1].to_s(16)
      msg.length = len
      msg.crc32 = file.file_crc32 if chunk == file.chunks
      msg.yenc_body(chunk, file.chunks, file.size, file.name)
      final_data[0] = { message: msg.return_self, filename: file.name, chunk: chunk, length: len }
      final_data[1] = file
      queue.push(final_data)
      # we do care about user's memory
      msg.unset
      chunk += 1
   end
end