#encoding: UTF-8 =begin /*************************************************************************** * 2023-2024, Michael Uplawski * * This program is free software; you can redistribute it and/or modify * * it under the terms of the WTFPL 2.0 or later, see * * http://www.wtfpl.net/about/ * * * * This program 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. * * * ***************************************************************************/ =end # An object of this class concentrates the specificities of chosen # newsgroups, as defined in the configuration. require_relative 'configuration' require_relative 'basic_logging' class Newsgroups include BasicLogging def initialize(groups) @config = Configuration.instance @groups = groups.split(',') debug('set signature, intro, no_archive') # set details for this post if @groups.size == 1 set_signature set_intro set_no_archive end end def no_archive debug('returning ' <<( @no_archive ? @no_archive : ' nil ') ) return @no_archive ? @no_archive : nil end attr_reader :signature, :intro, :groups private # defines the intro-line as per group. def set_intro @intro = nil # only one group. group = @groups[0] # all configured intro-lines. gintros = @config.GROUP_INTROS if gintros && gintros.respond_to?(:to_hash) # find the intro for the group. # either by name if gintros.keys.include?(group) @intro = gintros[group] else # or by a regular expression. gintros.each do |gr, intro| unless @intro @intro = intro if group.match(gr) debug "matched group against " << gr if @intro end end end debug('group_intro is ' << @intro.to_s) else msg = 'Cannot set the introduction line from the configuration!' msg << "\nPlease verify that GROUP_INTROS is set" warn(msg) end end # replace all \n by \r\n def correct_linebreaks(text) warned = false # find all lonely \n while text.match(/([^\r])\n/) do warn("ATTN! Line-breaks should be \\r\\n! Verify signatures!" ) if !warned warned ||= true # ... and silently marry them to \r text.gsub!($~[0],$~[1] + "\r\n") text.gsub!("\n ", "\n") # Luxury you can afford. end # \n text end # define the signature def set_signature @signature = nil # 1 group group = @groups[0] gsigs = @config.GROUP_SIGS if gsigs && gsigs.respond_to?(:to_hash) # find the signature for the group # either by name if gsigs.keys.include?(group) @signature = gsigs[group] debug('signature is ' << @signature ) if @signature # .., or by applying a regexp. else gsigs.each do |g, s| unless @signature rg = Regexp.new(g) sm = group.match(rg) debug('signature for group(s) ' << g << ': ' << s) if sm if sm @signature = s end # if sm end # if no signature end # gsigs.each end # gsigs for group? if(@signature && @signature.strip.start_with?('/') ) debug('picking signature from file ' << @signature) @signature = pick_sig(@signature) end @signature = correct_linebreaks(@signature) if @signature else # gsigs and is hash? msg = "Cannot read the signatures from the configuration." msg << "\nPlease verify that GROUP_SIGS is set." warn(msg) end end # pick a signature from a file def pick_sig(file) if file && File.exist?(file) && File.readable?(file) allSigs = File::read(file).force_encoding('utf-8').split("\n\n") numSigs = allSigs.length srand(Time.now.nsec) return allSigs[rand(numSigs)] else error 'cannot read signature from file ' << file end end # define the no_archive header. def set_no_archive @no_archive = nil xgs = @config.NO_ARCHIVE_GROUPS if xgs && !xgs.empty? && xgs.detect {|g| @groups[0].match(g) } debug("setting no_archive") @no_archive = 'yes' end end end # EOF