Browse Source

ajout des outils fcn-dolibarr, fcn-sympa

Guillaume 7 years ago
parent
commit
456cfde353
5 changed files with 179 additions and 0 deletions
  1. 18 0
      README.md
  2. 10 0
      config.ini.example
  3. 7 0
      cron.d.example
  4. 49 0
      fcn-dolibarr
  5. 95 0
      fcn-sympa

+ 18 - 0
README.md

@@ -2,3 +2,21 @@ fcn-toolbox
 ===========
 ===========
 
 
 Un dépôt pour les outils codés sur un coin de table mais qu'on ne veut pas perdre.
 Un dépôt pour les outils codés sur un coin de table mais qu'on ne veut pas perdre.
+
+## Outils
+
+### fcn-dolibarr
+
+Intéragit avec Dolibarr. 
+
+Commande disponibles: get-subscribers
+
+Dépendances: python3, psycopg2
+
+### fcn-sympa
+
+Intéragit avec Sympa.
+
+Commande disponibles: import
+
+Dépendances: perl, sympa

+ 10 - 0
config.ini.example

@@ -0,0 +1,10 @@
+[database]
+database = dolibarr
+user = dolibarr_user
+password = dolibarr_password
+
+[sympa]
+database = sympa
+user = sympa_user
+password = sympa_password
+

+ 7 - 0
cron.d.example

@@ -0,0 +1,7 @@
+# For more information see the manual pages of crontab(5) and cron(8)
+# m h  dom mon dow  user  command
+
+PATH=/usr/local/bin:/usr/bin:/bin:/path/to/fcn-toolbox
+
+# Synchronize sympa with Dolibarr, by exporting subscribers (daily)
+2  3  *  *  *  root  fcn-dolibarr get-subscribers --product "PRODUCTCODE" | sudo -u sympa fcn-sympa --import="subscribers@example.net"

+ 49 - 0
fcn-dolibarr

@@ -0,0 +1,49 @@
+#!/usr/bin/python3
+
+import argparse
+import configparser
+import psycopg2
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument("selection", type=str,
+                    choices=["get-subscribers"])
+parser.add_argument("-c", "--config", type=str,
+                    default="/etc/fcn-toolbox/config.ini",
+                    help="specify a configuration file")
+parser.add_argument("--product", type=str,
+                    default="%",
+                    help="product code (can contain %)")
+args = parser.parse_args()
+
+config = configparser.RawConfigParser()
+config.sections()
+config.read(args.config)
+configdb = config['database']
+
+conn = psycopg2.connect(database=configdb['database'],
+  user=configdb['user'], password=configdb['password'])
+
+cur = conn.cursor()
+
+def getMemberCount():
+  cur.execute("SELECT count(*) FROM llx_adherent WHERE statut = '1'")
+  return cur.fetchone()[0]
+
+def getSubscriberInfo(product, fields):
+  fieldsel = ','.join(fields)
+  cur.execute("""SELECT %s
+    FROM llx_societe, llx_contrat, llx_contratdet, llx_product
+    WHERE llx_societe.rowid=llx_contrat.fk_soc
+        AND llx_contrat.rowid=llx_contratdet.fk_contrat
+        AND llx_product.rowid=llx_contratdet.fk_product
+        AND llx_product.ref LIKE %s
+        AND llx_contrat.statut=1
+        AND llx_contratdet.statut=4
+    ORDER BY llx_product.ref, llx_contrat.rowid ASC;""" % (fieldsel, '%s'), (product, ))
+  return cur.fetchall()
+
+
+if args.selection == "get-subscribers":
+  subscribers = getSubscriberInfo(args.product, ['llx_societe.email'])
+  print("\n".join(map(lambda x: x[0], subscribers)))

+ 95 - 0
fcn-sympa

@@ -0,0 +1,95 @@
+#!/usr/bin/perl
+
+use strict;
+use Getopt::Long;
+
+use lib '/usr/share/sympa/lib';
+use Conf;
+use List;
+use Log;
+
+use Data::Dumper;
+
+my %options;
+unless (&GetOptions(\%main::options, 'import=s')) {
+    &fatal_err("Unknown options.");
+}
+
+my $config_file = $main::options{'config'} || Sympa::Constants::CONFIG;
+
+## Load configuration file. Ignoring database config for now
+unless (Conf::load($config_file,1)) {
+   &fatal_err("Configuration file $config_file has errors.");
+}
+
+## Probe Db if defined
+if ($Conf{'db_name'} and $Conf{'db_type'}) {
+    unless (&Upgrade::probe_db()) {
+        &fatal_err('Database %s defined in sympa.conf has not the right structure', $Conf{'db_name'});
+    }
+}
+
+## Now trying to load full config (including database)
+unless (Conf::load($config_file)) {
+   &fatal_err("Configuration file $config_file has errors.");
+}
+
+do_openlog($Conf::Conf{'syslog'}, $Conf::Conf{'log_socket_type'}, 'bulk');
+
+if ($main::options{'import'}) {
+    my ($list);
+
+    ## The parameter should be a list address
+    unless ($main::options{'import'} =~ /\@/) {
+        &do_log('err','Incorrect list address %s', $main::options{'import'});
+        exit;
+    }
+
+    unless ($list = new List ($main::options{'import'})) {
+        fatal_err('Unknown list name %s', $main::options{'import'});
+    }
+
+    my %subscribers = ();
+
+    ## Read imported data from STDIN
+    while (<STDIN>) {
+        next if /^\s*$/;
+        next if /^\s*\#/;
+
+        unless (/^\s*((\S+|\".*\")@\S+)(\s*(\S.*))?\s*$/) {
+            printf STDERR "Not an email address: %s\n", $_;
+        }
+
+        my $email = lc($1);
+        my $gecos = "";
+        my $u;
+        my $defaults = $list->get_default_user_options();
+        %{$u} = %{$defaults};
+        $u->{'email'} = $email;
+        $u->{'gecos'} = $gecos;
+
+        if (!$list->is_user($email)) {
+            unless($list->add_user($u)) {
+            	printf STDERR "\nCould not add %s\n", $email;
+            	next;
+            }
+        }
+
+        $subscribers{$email} = 1;
+    }
+
+    ## Remove existing subscribers not found in STDIN
+    for (my $user = $list->get_first_user(); $user; $user = $list->get_next_user() ) {
+
+        my $email = lc($user->{'email'});
+        if (!exists($subscribers{$email})) {
+            unless ( $list->delete_user('users' => [$email]) ) {
+                printf STDERR "\nCould not delete %s\n", $email;
+                next;
+            }
+        }
+
+    }
+    
+    exit 0;
+}