%global pypi_name mailman
%global mmuser       mailman
%global mmuserid     41
%global mmgroup      mailman
%global mmgroupid    41
%{?python_enable_dependency_generator}

Name:                mailman
Version:             3.3.2
Release:             5
Epoch:               3
Summary:             The GNU mailing list manager
License:             GPLv3
URL:                 https://pypi.org/project/mailman
Source0:             https://files.pythonhosted.org/packages/source/m/mailman/mailman-%{version}.tar.gz
Source1:             mailman.cfg
Source2:             mailman3-tmpfiles.conf
Source3:             mailman3.service
Source4:             mailman3.logrotate
Source5:             mailman3-digests.service
Source6:             mailman3-digests.timer
#Refer: https://gitlab.com/mailman/mailman/-/merge_requests/860
Patch01:             mailman3-click8.patch
Patch11:             mailman-subject-prefix.patch
Patch14:             mailman-use-either-importlib_resources-or-directly-importlib.patch
#Refer: https://bugzilla.redhat.com/show_bug.cgi?id=1900668#c9
Patch15:             mailman3-do-not-assume-sapce-in-banner.patch
#Refer: https://gitlab.com/mailman/mailman/-/merge_requests/772
Patch16:             mailman3-test_as_string_python_bug_27321.patch
BuildArch:           noarch
BuildRequires:       glibc-langpack-en
BuildRequires:       python%{python3_pkgversion}-devel >= 3.5 python%{python3_pkgversion}-setuptools
%if %{undefined python_enable_dependency_generator}
Requires:            python%{python3_pkgversion} >= 3.5 python%{python3_pkgversion}-setuptools
Requires:            python%{python3_pkgversion}-aiosmtpd >= 1.1 python%{python3_pkgversion}-alembic
Requires:            python%{python3_pkgversion}-atpublic
Requires:            python%{python3_pkgversion}-authheaders >= 0.9.2
Requires:            python%{python3_pkgversion}-authres >= 1.0.1
Requires:            python%{python3_pkgversion}-click >= 7.0
Requires:            python%{python3_pkgversion}-dateutil >= 2.0
Requires:            python%{python3_pkgversion}-dns >= 1.14.0
Requires:            python%{python3_pkgversion}-falcon >= 1.0.0
Requires:            python%{python3_pkgversion}-flufl.bounce
Requires:            python%{python3_pkgversion}-flufl.i18n >= 2.0.1
Requires:            python%{python3_pkgversion}-flufl.lock >= 3.1
Requires:            python%{python3_pkgversion}-gunicorn python%{python3_pkgversion}-lazr.config
Requires:            python%{python3_pkgversion}-passlib >= 1.6.0
Requires:            python%{python3_pkgversion}-requests
Requires:            python%{python3_pkgversion}-sqlalchemy >= 1.2.3
Requires:            python%{python3_pkgversion}-zope-component
Requires:            python%{python3_pkgversion}-zope-configuration
Requires:            python%{python3_pkgversion}-zope-event
Requires:            python%{python3_pkgversion}-zope-interface >= 5.0
Requires:            python%{python3_pkgversion}-importlib-resources
%endif
BuildRequires:       python%{python3_pkgversion}-aiosmtpd >= 1.1 python%{python3_pkgversion}-alembic
BuildRequires:       python%{python3_pkgversion}-atpublic
BuildRequires:       python%{python3_pkgversion}-authheaders >= 0.9.2
BuildRequires:       python%{python3_pkgversion}-authres >= 1.0.1
BuildRequires:       python%{python3_pkgversion}-click >= 7.0
BuildRequires:       python%{python3_pkgversion}-dateutil >= 2.0
BuildRequires:       python%{python3_pkgversion}-dns >= 1.14.0
BuildRequires:       python%{python3_pkgversion}-falcon >= 1.0.0
BuildRequires:       python%{python3_pkgversion}-flufl.bounce
BuildRequires:       python%{python3_pkgversion}-flufl.i18n >= 2.0.1
BuildRequires:       python%{python3_pkgversion}-flufl.lock >= 3.1
BuildRequires:       python%{python3_pkgversion}-flufl.testing python%{python3_pkgversion}-gunicorn
BuildRequires:       python%{python3_pkgversion}-lazr.config
BuildRequires:       python%{python3_pkgversion}-mock
BuildRequires:       python%{python3_pkgversion}-nose2 python%{python3_pkgversion}-passlib >= 1.6.0
BuildRequires:       python%{python3_pkgversion}-psycopg2 python%{python3_pkgversion}-requests
BuildRequires:       python%{python3_pkgversion}-sqlalchemy >= 1.2.3
BuildRequires:       python%{python3_pkgversion}-zope-component
BuildRequires:       python%{python3_pkgversion}-zope-configuration
BuildRequires:       python%{python3_pkgversion}-zope-event
BuildRequires:       python%{python3_pkgversion}-zope-interface >= 5.0
BuildRequires:       python%{python3_pkgversion}-importlib-resources
Provides:            %{name}-selinux == %{version}-%{release}
%global selinux_variants mls targeted
Requires:            selinux-policy >= %{_selinux_policy_version}
BuildRequires:       git pkgconfig(systemd) selinux-policy selinux-policy-devel
Requires(post):      selinux-policy-base >= %{_selinux_policy_version}
Requires(post):      libselinux-utils
Requires(post):      policycoreutils
Requires(post):      policycoreutils-python-utils
BuildRequires:       checkpolicy, selinux-policy-devel util-linux
%{?systemd_requires}
BuildRequires:       systemd
Requires(pre):       shadow-utils

%description
This is GNU Mailman, a mailing list management system distributed under the
terms of the GNU General Public License (GPL) version 3 or later.  The name of
this software is spelled 'Mailman' with a leading capital 'M' but with a lower
case second `m'.  Any other spelling is incorrect.

%prep
%autosetup -n %{pypi_name}-%{version} -p1
mkdir SELinux
echo '%{_localstatedir}/lib/mailman3/data(/.*)? gen_context(system_u:object_r:etc_mail_t,s0)' \
    > SELinux/mailman3.fc
cat > SELinux/mailman3.te << EOF
policy_module(mailman3, 1.4)
EOF

%build
%py3_build
cd SELinux
for selinuxvariant in %{selinux_variants}; do
  make NAME=${selinuxvariant} -f /usr/share/selinux/devel/Makefile
  mv mailman3.pp mailman3.pp.${selinuxvariant}
  make NAME=${selinuxvariant} -f /usr/share/selinux/devel/Makefile clean
done
cd -

%install
%py3_install
mkdir -p %{buildroot}%{_libexecdir}/mailman3
mv %{buildroot}%{_bindir}/* %{buildroot}%{_libexecdir}/mailman3/
cat > %{buildroot}%{_bindir}/mailman3 << EOF
#!/bin/bash
if [ "\$(whoami)" != "mailman" ]; then
    echo "This command must be run under the mailman user."
    exit 1
fi
%{_libexecdir}/mailman3/mailman \$@
EOF
chmod +x %{buildroot}%{_bindir}/mailman3
install -D -m 0640 %{SOURCE1} %{buildroot}%{_sysconfdir}/mailman.cfg
install -D -m 0644 %{SOURCE2} %{buildroot}%{_prefix}/lib/tmpfiles.d/mailman3.conf
install -D -m 0644 %{SOURCE3} %{buildroot}%{_unitdir}/mailman3.service
mkdir -p %{buildroot}%{_sysconfdir}/logrotate.d/
sed -e 's,@LOGDIR@,%{_localstatedir}/log/mailman3,g;s,@BINDIR@,%{_bindir},g' \
    %{SOURCE4} > %{buildroot}%{_sysconfdir}/logrotate.d/mailman3
install -D -m 0644 %{SOURCE5} %{buildroot}%{_unitdir}/mailman3-digests.service
install -D -m 0644 %{SOURCE6} %{buildroot}%{_unitdir}/mailman3-digests.timer
mkdir -p %{buildroot}%{_localstatedir}/{lib,spool,log}/mailman3
mkdir -p %{buildroot}/run/mailman3 %{buildroot}/run/lock/mailman3
mkdir -p %{buildroot}%{_sysconfdir}/mailman3.d
mkdir -p %{buildroot}%{_localstatedir}/lib/mailman3/data
for selinuxvariant in %{selinux_variants}; do
  install -d %{buildroot}%{_datadir}/selinux/${selinuxvariant}
  install -p -m 644 SELinux/mailman3.pp.${selinuxvariant} \
    %{buildroot}%{_datadir}/selinux/${selinuxvariant}/mailman3.pp
done
hardlink -cv %{buildroot}%{_datadir}/selinux

%check
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
find -name '*.rst' -exec rm {} \; 
rm -rf src/mailman/core/tests/test_logging.py
%{__python3} -m venv --system-site-packages --without-pip --clear venv
venv/bin/python setup.py develop
venv/bin/python -m nose2 -v

%pre
getent group %{mmgroup} >/dev/null || \
    groupadd -g %{mmgroupid} %{mmgroup} >/dev/null
getent passwd %{mmuser} >/dev/null || \
    useradd -r -u %{mmuserid} -g %{mmgroupid} -d %{_localstatedir}/lib/mailman3 -s /sbin/nologin \
        -c "Mailman, the mailing-list manager" %{mmuser} >/dev/null
for selinuxvariant in %{selinux_variants}; do
    %selinux_relabel_pre -s ${selinuxvariant}
done

%post
%systemd_post mailman3.service mailman3-digests.timer
for selinuxvariant in %{selinux_variants}; do
    %selinux_modules_install -s ${selinuxvariant} %{_datadir}/selinux/${selinuxvariant}/mailman3.pp || :
done

%preun
%systemd_preun mailman3.service mailman3-digests.timer

%postun
%systemd_postun_with_restart mailman3.service mailman3-digests.timer
if [ $1 -eq 0 ] ; then
  for selinuxvariant in %{selinux_variants}; do
    %selinux_modules_uninstall -s ${selinuxvariant} mailman3 || :
  done
fi

%posttrans
for selinuxvariant in %{selinux_variants}; do
    %selinux_relabel_post -s ${selinuxvariant}
done

%files
%license COPYING
%{_bindir}/mailman3
%{_libexecdir}/mailman3
%{python3_sitelib}/%{pypi_name}
%{python3_sitelib}/%{pypi_name}-*-py%{python3_version}.egg-info
%{_unitdir}/*.service
%{_unitdir}/*.timer
%{_prefix}/lib/tmpfiles.d/mailman3.conf
%config(noreplace) %attr(640,mailman,mailman) %{_sysconfdir}/mailman.cfg
%dir %{_sysconfdir}/mailman3.d
%config(noreplace) %{_sysconfdir}/logrotate.d/mailman3
%dir %attr(755,mailman,mailman) %{_localstatedir}/lib/mailman3
%dir %attr(2775,mailman,mail)   %{_localstatedir}/lib/mailman3/data
%dir %attr(755,mailman,mailman) %{_localstatedir}/spool/mailman3
%dir %attr(755,mailman,mailman) %{_localstatedir}/log/mailman3
%dir %attr(755,mailman,mailman) /run/mailman3
%dir %attr(755,mailman,mailman) /run/lock/mailman3
%doc SELinux/*
%{_datadir}/selinux/*/mailman3.pp

%changelog
* Tue May 24 2022 wulei <wulei80@h-partners.com> - 3:3.3.2-5
- Require click >=8.0.0 and fix the tests it breaks

* Fri May 13 2022 caodongxia <caodongxia@h-partners.com> - 3:3.3.2-4
- Fix test_interact and test_message failure 

* Thu Aug 12 2021 wangyue <wangyue92@huawei.com> - 3.3.2-3
- fix build error

* Tue Mar 09 2021 huanghaitao <huanghaitao8@huawei.com> - 3.3.2-2
- Fix command error in %postun

* Fri Dec 18 2020 leiju <leiju4@huawei.com> - 3.3.2-1
- Update to 3.3.2

* Fri Feb 14 2020 lihao <lihao129@huawei.com> - 2.1.29-5
- Package init
