<commit_before># Copyright 2008-2010 Universidad Politécnica de Madrid and Agora Systems S.A.
#
# This file is part of VCC (Virtual Conference Center).
#
# VCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# VCC 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with VCC.  If not, see <http://www.gnu.org/licenses/>.

class Group < ActiveRecord::Base
 
    has_many :memberships, :dependent => :destroy
    has_many :users, :through => :memberships
    belongs_to :space
    
    validates_presence_of :name
    validates_uniqueness_of(:mailing_list, :allow_nil => true, :allow_blank => true,:message => "name has already been taken")
    
    def validate
      for user in users
        unless user.stages.include?(space)
          errors.add(:users, "not belongs to the space of the group")
        end
      end
      
      if self.mailing_list == "sir"
        errors.add("The mailing list <EMAIL> is reserved")
      end
      
      if self.mailing_list && self.mailing_list.match(/^[a-zA-Z1-9][\s\w\-\.]*$/).to_s != self.mailing_list
        errors.add("The mailing list have invalid characters. Only lowercase letters and numbers are allowed")
      end
      
    end
    
    before_save { |group|
      if group.mailing_list.present?
        group.mailing_list = group.mailing_list.gsub(/ /, "-")
      end
    }
    
    after_create { |group|
        #create the new mailing_list if it has the option activated
        if group.mailing_list.present?
          group.mail_list_archive
          copy_list(group, group.mailing_list)
          request_list_update
        end
    }
    
    after_destroy { |group|
        if group.mailing_list
          #destroy the existing mailing_list
          delete_list(group, group.mailing_list)
          request_list_update
        end
    }   
    
    after_update { |group|
    
        #delete the old mailing_list      
        if group.mailing_list_changed?
          delete_list(group,group.mailing_list_was) if group.mailing_list_was.present?
        else
          delete_list(group,group.mailing_list) if group.mailing_list.present?
        end
  
        #create the new mailing_list
        if group.mailing_list.present?  
          group.mail_list_archive        
          copy_list(group,group.mailing_list)
        end
        request_list_update
    }
       
    # Do not reload mail list server if not in production mode, it could cause server overload
    #def self.reload_mail_list_server_because_of_environment
      #RAILS_ENV == "production"
    #end
    
    def self.request_list_update
      `/usr/local/bin/newautomatic.sh`
    end
    
    def self.copy_list(group,list)
      `cp #{ group.temp_file } /var/lib/mailman/data/listas_automaticas/vcc-#{list}`
    end
    
    def self.delete_list(group,list)
      `rm -f /var/lib/mailman/data/listas_automaticas/vcc-#{list}`
    end
    
    def self.disable_list(group,list)
      `mv -f /var/lib/mailman/data/listas_automaticas/vcc-#{list} /var/lib/mailman/data/listas_automaticas/.vcc-#{list}-#{group.name}`
    end

    def self.enable_list(group,list)
      `mv -f /var/lib/mailman/data/listas_automaticas/.vcc-#{list}-#{group.name} /var/lib/mailman/data/listas_automaticas/vcc-#{list}`
    end
    
    # Transforms the list of users in the group into a string for the mail list server
    def mail_list
       str =""
       self.users.each do |person|
         str << "#{Group.remove_accents(person.login)}  <#{person.email}> \n"
       end
       str
   end
   
   def mail_list_archive
     doc = "#{self.mail_list}"
     File.open(temp_file, 'w') {|f| f.write(doc) }<commit_msg>Disable forced invocation of mailman script for every group list change<commit_after># Copyright 2008-2010 Universidad Politécnica de Madrid and Agora Systems S.A.
#
# This file is part of VCC (Virtual Conference Center).
#
# VCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# VCC 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with VCC.  If not, see <http://www.gnu.org/licenses/>.

class Group < ActiveRecord::Base
 
    has_many :memberships, :dependent => :destroy
    has_many :users, :through => :memberships
    belongs_to :space
    
    validates_presence_of :name
    validates_uniqueness_of(:mailing_list, :allow_nil => true, :allow_blank => true,:message => "name has already been taken")
    
    def validate
      for user in users
        unless user.stages.include?(space)
          errors.add(:users, "do not belong to the same space as the group")
        end
      end
      
      if self.mailing_list == "sir"
        errors.add("The mailing list <EMAIL> is reserved")
      end
      
      if self.mailing_list && self.mailing_list.match(/^[a-zA-Z1-9][\s\w\-\.]*$/).to_s != self.mailing_list
        errors.add("The mailing list have invalid characters. Only lowercase letters and numbers are allowed")
      end
      
    end
    
    before_save { |group|
      if group.mailing_list.present?
        group.mailing_list = group.mailing_list.gsub(/ /, "-")
      end
    }
    
    after_create { |group|
        #create the new mailing_list if it has the option activated
        if group.mailing_list.present?
          group.mail_list_archive
          copy_list(group, group.mailing_list)
          request_list_update
        end
    }
    
    after_destroy { |group|
        if group.mailing_list
          #destroy the existing mailing_list
          delete_list(group, group.mailing_list)
          request_list_update
        end
    }   
    
    after_update { |group|
    
        #delete the old mailing_list      
        if group.mailing_list_changed?
          delete_list(group,group.mailing_list_was) if group.mailing_list_was.present?
        else
          delete_list(group,group.mailing_list) if group.mailing_list.present?
        end
  
        #create the new mailing_list
        if group.mailing_list.present?  
          group.mail_list_archive        
          copy_list(group,group.mailing_list)
        end
        request_list_update
    }
       
    # Do not reload mail list server if not in production mode, it could cause server overload
    #def self.reload_mail_list_server_because_of_environment
      #RAILS_ENV == "production"
    #end
    
    def self.request_list_update
      #`/usr/local/bin/newautomatic.sh`
    end
    
    def self.copy_list(group,list)
      `cp #{ group.temp_file } /var/lib/mailman/data/listas_automaticas/vcc-#{list}`
    end
    
    def self.delete_list(group,list)
      `rm -f /var/lib/mailman/data/listas_automaticas/vcc-#{list}`
    end
    
    def self.disable_list(group,list)
      `mv -f /var/lib/mailman/data/listas_automaticas/vcc-#{list} /var/lib/mailman/data/listas_automaticas/.vcc-#{list}-#{group.name}`
    end

    def self.enable_list(group,list)
      `mv -f /var/lib/mailman/data/listas_automaticas/.vcc-#{list}-#{group.name} /var/lib/mailman/data/listas_automaticas/vcc-#{list}`
    end
    
    # Transforms the list of users in the group into a string for the mail list server
    def mail_list
       str =""
       self.users.each do |person|
         str << "#{Group.remove_accents(person.login)}  <#{person.email}> \n"
       end
       str
   end
   
   def mail_list_archive
     doc = "#{self.mail_list}"
     File.open(temp_file, 'w') {|f| f.write(doc) }
{'fraction_non_alphanumeric': 0.09230955846676371, 'fraction_numerical': 0.002668607472100922, 'mean_word_length': 2.546236559139785, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3349804', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2562, 'n_words': 700}