Class: Sanguinews::NntpMsg

Inherits:
Object
  • Object
show all
Defined in:
lib/sanguinews/nntp_msg.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, groups, subject, message = '', **opts) ⇒ NntpMsg

Returns a new instance of NntpMsg.



25
26
27
28
29
30
31
32
33
# File 'lib/sanguinews/nntp_msg.rb', line 25

def initialize(from, groups, subject, message='', **opts)
  @from = from
  @groups = groups
  @subject = subject
  @message = message
  @date = opts[:date] if opts[:date]
  @date ||= DateTime.now().strftime('%a, %d %b %Y %T %z')
  @poster = "sanguinews v#{Sanguinews::VERSION} (ruby #{RUBY_VERSION}) - https://github.com/tdobrovolskij/sanguinews"
end

Instance Attribute Details

#crc32Object

Returns the value of attribute crc32.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def crc32
  @crc32
end

#dateObject

Returns the value of attribute date.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def date
  @date
end

#fromObject

Returns the value of attribute from.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def from
  @from
end

#groupsObject

Returns the value of attribute groups.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def groups
  @groups
end

#lengthObject

Returns the value of attribute length.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def length
  @length
end

#messageObject

Returns the value of attribute message.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def message
  @message
end

#part_crc32Object

Returns the value of attribute part_crc32.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def part_crc32
  @part_crc32
end

#posterObject

Returns the value of attribute poster.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def poster
  @poster
end

#subjectObject

Returns the value of attribute subject.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def subject
  @subject
end

#xnaObject

Returns the value of attribute xna.



23
24
25
# File 'lib/sanguinews/nntp_msg.rb', line 23

def xna
  @xna
end

Instance Method Details

#create_headerObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sanguinews/nntp_msg.rb', line 35

def create_header
  sio = StringIO.new("", "w:ASCII-8BIT")
  sio.puts "From: #{@from}"
  sio.puts "Newsgroups: #{@groups}"
  sio.puts "Subject: #{@subject}"
  sio.puts "X-Newsposter: #{@poster}" if @poster
  sio.puts "X-No-Archive: yes" if @xna
  sio.puts "Date: #{@date}"
  sio.puts
  header = sio.string
  sio.close
  return header
end

#return_selfObject



69
70
71
72
# File 'lib/sanguinews/nntp_msg.rb', line 69

def return_self
  header = self.create_header
  header << @message
end

#sizeObject



74
75
76
# File 'lib/sanguinews/nntp_msg.rb', line 74

def size
  return @message.length
end

#unsetObject



78
79
80
81
82
83
84
85
# File 'lib/sanguinews/nntp_msg.rb', line 78

def unset
  @from = nil
  @groups = nil
  @subject = nil
  @poster = nil
  @date = nil
  @message = nil
end

#yenc_body(current_part, parts, file_size, filename) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sanguinews/nntp_msg.rb', line 49

def yenc_body(current_part, parts, file_size, filename)
  chunk_start = ((current_part - 1) * @length) + 1
  chunk_end = current_part * @length
  if (parts==1)
    headerline = "=ybegin line=128 size=#{file_size} name=#{filename}"
    trailer = "=yend size=#{file_size} crc32=#{@crc32}"
  else
    headerline = "=ybegin part=#{current_part} total=#{parts} line=128 size=#{file_size} name=#{filename}\n=ypart begin=#{chunk_start} end=#{chunk_end}"
    # last part
    if (current_part == parts)
      trailer = "=yend size=#{@length} part=#{current_part} pcrc32=#{@part_crc32} crc32=#{@crc32}"
    else
      trailer = "=yend size=#{@length} part=#{current_part} pcrc32=#{@part_crc32}"
    end
  end
  headerline << "\n#{@message}\n"
  headerline << trailer
  @message = headerline
end