X-Git-Url: https://nos-oignons.net/gitweb/gestion-adh.git/blobdiff_plain/13767cc69697b7eaf4be6e47788efe636b56b6fd..25a6402a90eedd41ae895d68e4a3a0823fbda33a:/lib/nos_oignons/mailman.rb?ds=sidebyside diff --git a/lib/nos_oignons/mailman.rb b/lib/nos_oignons/mailman.rb index 9c0d590..007fd33 100644 --- a/lib/nos_oignons/mailman.rb +++ b/lib/nos_oignons/mailman.rb @@ -1,36 +1,77 @@ #-*- coding: utf-8 -*- +# +# Système de gestion des adhésions de Nos oignons +# Copyright © 2013-2014 Nos oignons +# +# This program 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. +# +# 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. 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 this program. If not, see . -require 'shellwords' +require 'json' +require 'rest-client' module NosOignons module Mailman + class MailmanMember < Struct.new(:member_id, :email, keyword_init: true) + def unsubscribe! + Mailman::request(:delete, "/3.0/members/#{URI::encode_www_form_component(member_id)}") + end + end + class << self def list_members(list) - `sudo -u list list_members #{Shellwords.escape(list)}`.strip.split + s = request(:get, "/3.0/lists/#{URI::encode_www_form_component(list)}/roster/member") + r = JSON.parse(s) + r["entries"].collect { |entry| + MailmanMember.new(member_id: entry["member_id"], email: entry["email"]) + } end - def add_member(list, email) - add_members(list, [email]) + def susbcribe_email(list, email) + list_id = list.gsub(/@/, '.') + request(:post, "/3.0/members", payload: { + list_id: list_id, + subscriber: email, + pre_verified: true, + pre_confirmed: true, + pre_approved: true, + }.to_json, + headers: { content_type: :json }, + ) end - def add_members(list, emails) - IO.popen(['sudo', '-u', 'list', 'add_members', '-r', '-', list], 'w') do |io| - emails.each do |email| - io.puts email - end - end + def request(method, path, **args) + RestClient::Request.execute( + **args, + method: method, + url: "#{rest_url}#{path}" + ) end - def remove_member(list, email) - remove_members(list, [email]) - end + private + + def rest_url + @rest_url if defined?(@rest_url) - def remove_members(list, emails) - IO.popen(['sudo', '-u', 'list', 'remove_members', '-f', '-', list], 'w') do |io| - emails.each do |email| - io.puts email + @rest_url = ENV["NOS_OIGNONS_MAILMAN_REST_API_URL"] + if @rest_url.nil? + cred_path = File.join("#{ENV["CREDENTIALS_DIRECTORY"]}", "mailman-rest-url") + begin + @rest_url = File.open(cred_path).read + rescue Errno::ENOENT + raise ArgumentError.new("Environment variable `NOS_OIGNONS_MAILMAN_REST_API_URL` not defined, and unable to read the file `$CREDENTIALS_DIRECTORY/mailman-rest-url` either") end end + @rest_url end end end