C   85
base c
Guest on 10th May 2022 05:16:16 PM


  1. #include <libpq-fe.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/uio.h>
  10. #include <fcntl.h>
  11.  
  12. #include "serv.h"
  13.  
  14.  
  15. /*
  16.  à compiler avec :
  17.  gcc -I`pg_config --includedir` -L`pg_config --libdir` -lpq base.c server.c
  18. *//
  19. int numero = 0;
  20.  
  21. char *
  22. fichier (char * id, char * numero)
  23. {
  24.   char * res;
  25.   asprintf (&res, "%s/%s.%s", SPOOLPATH, id, numero);
  26.   return res;
  27. }
  28.  
  29. PGconn *
  30. connecter ()
  31. {
  32.   return PQconnectdb (PQCONN);
  33. }
  34.  
  35. void deconnecter (PGconn* c)
  36. {
  37.   PQfinish (c);
  38.   free (c);
  39. }
  40.  
  41. PGresult *
  42. executer (char* requete)
  43. {
  44.   PGconn* c = connecter ();
  45.   PGresult * res = PQexec (c, requete);
  46.  
  47.   deconnecter (c);
  48.   return res;
  49. }
  50.  
  51. char *
  52. escape (char* ch)
  53. {
  54.   int n = strlen (ch);
  55.   char *res = malloc (sizeof (char[2*n + 1]));
  56.   PQescapeString (res, ch, n);
  57.   return res;
  58. }
  59.  
  60. char *
  61. lire_fichier (char * fichier, int * taille)
  62. {
  63.   char *buff = malloc (1);
  64.   FILE *fich = fopen (fichier, "r");
  65.   int pos = 0;
  66.   int lus = 0;
  67.  
  68.   while (!(feof (fich)))
  69.   {
  70.     realloc (buff, pos + 1024);
  71.     lus = fread (&buff[pos], 1, 1023, fich);
  72.     pos += lus;
  73.   }
  74.  
  75.   *taille = pos;
  76.   buff[pos] = 0;
  77.   fclose (fich);
  78.   return buff;  
  79. }
  80.  
  81. char *
  82. lire_message_par_id (char* id, int * taille)
  83. {
  84.   char *requete;
  85.   asprintf (&requete, \
  86.             "select numero_fichier from messages where id = '%s';", \
  87.             id);
  88.   printf ("%s\n", requete);
  89.  
  90.   PGresult * res = executer (requete);
  91.   free (requete);
  92.  
  93.   if (0 == PQntuples (res))
  94.     return NULL;
  95.  
  96.   char * fich = fichier (id, PQgetvalue(res, 0, 0));
  97.   char * resultat = lire_fichier (fich, taille);
  98.   free (fich);
  99.  
  100.   return resultat;
  101. }
  102.  
  103. char *
  104. message_id (char * conti, char * numero)
  105. {
  106.   char *requete;
  107.   char *resultat;
  108.   asprintf (&requete, \
  109.             "select message from messages_contis where conti = '%s' and numero = %s;", \
  110.             conti, numero);
  111.   printf ("%s\n", requete);
  112.   PGresult * res = executer (requete);
  113.   free (requete);
  114.   if (0 != PQntuples (res))
  115.     resultat = PQgetvalue (res, 0, 0);
  116.   return resultat;
  117. }
  118.  
  119. char *
  120. lire_conti (char * conti)
  121. {
  122.   char *requete;
  123.   asprintf (&requete, \
  124.             "select numero,message from messages_contis where conti = '%s' order by numero;", \
  125.             conti);
  126.   printf ("%s\n", requete);
  127.   PGresult * res = executer (requete);
  128.   free (requete);
  129.   int n = PQntuples (res);
  130.   if (0 == n)
  131.     return NULL;
  132.   char * ret = malloc (1);
  133.   *ret = 0;
  134.   char * ret2;
  135.   int i;
  136.   for (i = 0; i < n; i++)
  137.     {
  138.       asprintf (&ret2, "%s%s:%s\n", \
  139.                 ret, PQgetvalue (res, i, 0), PQgetvalue (res, i, 1));
  140.       free (ret);
  141.       ret = ret2;
  142.     }
  143.   return ret;
  144. }
  145.  
  146. char *
  147. lister_contis ()
  148. {
  149.   PGresult * res = executer ("select nom from contis;");
  150.   int n = PQntuples (res);
  151.   if (0 == n)
  152.     return NULL;
  153.  
  154.   char * ret = malloc (1);
  155.   ret[0] = 0;
  156.   char * ret2;
  157.   int i;
  158.   for (i = 0; i < n; i++)
  159.     {
  160.       asprintf (&ret2, "%s%s\n", ret, PQgetvalue (res, i, 0));
  161.       free (ret);
  162.       ret = ret2;
  163.     }
  164.   return ret;
  165. }
  166.  
  167. char *
  168. creer_id (time_t date)
  169. {
  170.   char *id;
  171.   asprintf (&id, "%.8x%.8x", date, random ());
  172.   return id;
  173. }
  174.  
  175. void
  176. creer_message (struct msg * msg)
  177. {
  178.   int i;
  179.  
  180.  
  181.  /* génération du message id */ */
  182.   srandom (msg->date);
  183.   char * id = creer_id (msg->date);
  184.   printf ("Création du message %s\n", id);
  185.  
  186.   char* sender = msg->sender;
  187.   char* from = escape (msg->from);
  188.   char* subject = escape (msg->subject);
  189.  
  190.   char * fichier_corps = fichier (id, "0");
  191.  
  192.   char * date = ctime (&(msg->date));
  193.   date[24] = 0;
  194.  
  195.   char * req1 = malloc (1);
  196.   req1[0] = 0;
  197.  
  198.   char * req15;
  199.  
  200.   char * req9;
  201.  
  202.   char * body1 = malloc (13);
  203.   strcpy (body1, "REFERENCES: ");
  204.   char * body15;
  205.   PGresult * resref;
  206.   int nbref_eff = 0;
  207.  
  208.   if (msg->nrefs > 0)
  209.     {
  210.  
  211.       for (i = 0; i < msg->nrefs; i++)
  212.       {
  213.         asprintf (&req9, "select * from messages where id = '%s';", \
  214.                   msg->refs[i]);
  215.         resref = executer (req9);
  216.         free (req9);
  217.         if (0 < PQntuples(resref))
  218.           {
  219.             nbref_eff++;
  220.             asprintf (&body15, "%s%s, ", body1, msg->refs[i]);
  221.             free (body1);
  222.             body1 = body15;
  223.             asprintf (&req15, \
  224.                       "%s insert into refs(de, a) values('%s','%s') ;", \
  225.                       req1, id, msg->refs[i]);
  226.             free (req1);
  227.             req1 = req15;
  228.           }
  229.         else
  230.           {
  231.             printf("Référence à un message inexistant : '%s'\n", \
  232.                    msg->refs[i]);
  233.           }
  234.       }
  235.  
  236.       if (nbref_eff != 0)
  237.         body1[strlen (body1) - 2] = 0;
  238.      
  239.       char * pere = msg->refs[msg->nrefs - 1];
  240.  
  241.       asprintf (&req15, \
  242.                 "%s %s values ('%s', '%s', '%s', '%s', '%s', '%s'); %s (%s '%s') || '%s, ' %s '%s' ; %s;", \
  243.                 req1, \
  244.                 "insert into messages(id, date, sender, \"from\", subject, pere)", \
  245.                 id, date, sender, from, subject, pere, \
  246.                 "update messages set fils = ", \
  247.                 "select fils from messages where id = ", pere, \
  248.                 id, \
  249.                 "where id = ", pere, \
  250.                 "lock contis in row exclusive mode");
  251.       free (req1);
  252.       free (pere);
  253.       req1 = req15;
  254.  
  255.  
  256.     } else {
  257.  
  258.       asprintf (&req1, \
  259.                 "%s values ('%s', '%s', '%s', '%s', '%s'); %s;", \
  260.                 "insert into messages(id, date, sender, \"from\", subject) ", \
  261.                 id, date, sender, from, subject, \
  262.                 "lock contis in row exclusive mode");
  263.  
  264.     };
  265.  
  266.   PGresult * resconti;
  267.   int nb_effectif = 0;
  268.   char * body2 = malloc (13);
  269.   strcpy (body2, "NEWSGROUPS: ");
  270.   char * body25;
  271.  
  272.   for (i = 0; i < msg->nngrp ; i++)
  273.     {
  274.       char * conti = msg->newsgrps[i];
  275.  
  276.       asprintf (&req9, \
  277.                 "select lecture_seule from contis where nom = '%s';", \
  278.                 conti);
  279.       resconti = executer (req9);
  280.       free (req9);
  281.       if (0 < PQntuples (resconti) && \
  282.           0 != strcmp ("t", PQgetvalue (resconti,0,0)))
  283.         {
  284.           nb_effectif++;
  285.  
  286.           asprintf (&body25, " , body2, conti);
  287.          free (body2);
  288.          body2 = body25;
  289.  
  290.          asprintf (&req15, \
  291.                   "      "%s %s ('%s', '%s'%s'%s '%s')); %s %s '%s';" , \
  292.                     req1, \
  293.                     "insert into messages_contis (message, conti, numero) values", \
  294.                     id, conti, \
  295.                     "select prochain_numero from contis where nom = ", conti, \
  296.                     "update contis set prochain_numero = 1 + prochain_numero", \
  297.                     "where nom = ", conti);
  298.           free (req1);
  299.           req1 = req15;
  300.           printf ("%s\n", req1);
  301.         }
  302.       else
  303.         {
  304.           printf ("Le conti '%s' n'existe pas ou est en lecture seule.\n", \
  305.                   conti);
  306.         }
  307.     }
  308.  
  309.   if (nb_effectif != 0)
  310.     {/* effacer la dernière virgule */rgule */
  311.       body2[strlen (body2) - 2] = 0;
  312.   /* génération du corps complet */complet */
  313.   char * body;
  314.   asprintf (&body, "%s\nFROM: %s\nSENDER: %s\nSUBJECT: %s\n%s\n\n%s", \
  315.             body2, msg->from, msg->sender, msg->subject, body1, msg->body);
  316.   free (body1);
  317.   free (body2);
  318.   free (from);
  319.   free (sender);
  320.   free (subject);
  321.    
  322.   if (nb_effectif != 0)
  323.   /* écriture du fichier */ fichier */
  324.       FILE * fich = fopen (fichier_corps, "w");
  325.       fprintf (fich, "%s", body);
  326.       free (body);
  327.       fclose (fich);
  328.      
  329.       printf ("Le fichier '%s' a été écrit.\n", fichier_corps);
  330.       free (fichier_corps);
  331.  
  332.       printf ("La requête suivante va être exécutée :\n%s\n", req1);
  333.       executer (req1);
  334.  
  335.     } else {
  336.       printf ("Création annulée, pas de newsgroup.\n");
  337.     }
  338.   free (req1);
  339.  
  340. }
  341.  
  342.  
  343. char **
  344. nouveau_t ()
  345. {
  346.   char ** t = malloc (LMXSIZ * sizeof (char*));
  347.   int i;
  348.   for (i = 0; i < LMXSIZ; i++)
  349.     {
  350.       t[i] = malloc (LMXSIZ);
  351.     };
  352.   return t;
  353. }
  354.  
  355. void
  356. maj_refs (PGconn * conn, char * id, char * anc_id1, char * nouv_id1)
  357. {
  358.  
  359.   int numfich = 0;
  360.   char * req;
  361.  
  362.   asprintf (&req, "select numero_fichier from messages where id = '%s'", id);
  363.   PGresult * res = PQexec (conn, req);
  364.   free (req);
  365.  
  366.   sscanf (PQgetvalue(res, 0, 0), "%d", &numfich);
  367.   numfich++;
  368.  
  369.   char * snumfich;
  370.   asprintf (&snumfich, "%d", numfich);
  371.   char * fichier_interm = fichier (id, snumfich);
  372.   free (snumfich);
  373.  
  374.   char * snumfichp;
  375.   asprintf (&snumfichp, "%d", numfich - 1);
  376.   char * fichier_avant = fichier (id, snumfichp);
  377.   free (snumfichp);
  378.  
  379.   FILE * f1 = fopen (fichier_avant, "r");
  380.   FILE * f2 = fopen (fichier_interm, "w");
  381.   free (fichier_avant);
  382.   free (fichier_interm);
  383.  
  384.   char * ligne = malloc (LMXSIZ);
  385.  
  386.   int ligne_blanche = 0;
  387.  
  388.   char ** t;
  389.   int i, n;
  390.   char * refer = malloc (13);
  391.   strcpy (refer, "REFERENCES: ");
  392.   char * refer2;
  393.   char * tampon;
  394.  
  395.   while (!(feof (f1)))
  396.     {
  397.       if (tampon != NULL)
  398.         fputs (tampon, f2);
  399.       fgets(ligne, LMXSIZ, f1 );
  400.       if (ligne_blanche == 0 && \
  401.           (strcmp (ligne, "\n") == 0 || strcmp (ligne, "") == 0))
  402.         ligne_blanche = 1;
  403.       if (ligne_blanche == 0 && \
  404.           strcasecmp (ligne, "REFERENCES: ") > 0 && \
  405.           strcmp (ligne, "REFERENCES:!") < 0)
  406.         {
  407.           t = nouveau_t ();
  408.           n = parse (&ligne[12], t, 0, 0);
  409.           for (i = 0; i < n; i++)
  410.             {
  411.               if (strcmp (anc_id1, t[i]) == 0)
  412.                 {
  413.                   asprintf (&refer2, "%s%s, ", refer, nouv_id1);
  414.                   refer = refer2;
  415.                 } else {
  416.                   asprintf (&refer2, "%s%s, ", refer, t[i]);
  417.                 };
  418.               free (refer);
  419.               refer = refer2;
  420.               free (t[i]);
  421.             }
  422.           free (tampon);
  423.           refer[strlen(refer) - 2] = 0;
  424.           asprintf (&tampon, "%s\n", refer);
  425.           free (refer);
  426.           free (t);
  427.         }
  428.       else
  429.         {
  430.           free (tampon);
  431.           asprintf (&tampon, "%s", ligne);
  432.         }
  433.     };
  434.  
  435.   free (tampon);
  436.   fclose (f1);
  437.   fclose (f2);  
  438.  
  439.   asprintf (&req, \
  440.             "%s where id = '%s'", \
  441.             "update messages set numero_fichier = 1 + numero_fichier", \
  442.             id);
  443.   PQexec (conn, req);
  444.   free (req);
  445.  
  446. }
  447.  
  448. char *
  449. deplacer (PGconn * conn, char * id, time_t date, char * cdate, \
  450.           char * source, char * cible)
  451. {
  452.  
  453.   if (0 == strcmp (source, cible))
  454.     return id;
  455.  
  456.   printf ("Début du déplacement de %s\n", id);
  457.  
  458.   char * req;
  459.   PGresult * res;
  460.  
  461.   asprintf (&req, \
  462.             "delete from messages_contis where message = '%s' and conti = '%s';", \
  463.             id, source);
  464.   PQexec (conn, req);
  465.   free (req);
  466.  
  467.   int numfich = 0;
  468.   asprintf (&req, "select numero_fichier from messages where id = '%s'", id);
  469.   res = PQexec (conn, req);
  470.   free (req);
  471.   if (0 == PQntuples (res))
  472.     return NULL;
  473.  
  474.   char * snumfichp = PQgetvalue (res, 0, 0);
  475.   sscanf (snumfichp, "%d", &numfich);
  476.   numfich++;
  477.  
  478.   char * snumfichs;
  479.   asprintf (&snumfichs, "%d", numfich);
  480.   printf ("%s\n", snumfichs);
  481.  
  482.   char * fichier_avant = fichier (id, snumfichp);
  483.   char * fichier_interm = fichier (id, snumfichs);
  484.   free (snumfichs);
  485.  
  486.   FILE * f1 = fopen (fichier_avant, "r");
  487.   FILE * f2 = fopen (fichier_interm, "w");
  488.   free (fichier_avant);
  489.  
  490.   char * ligne = malloc (LMXSIZ);
  491.  
  492.   int ligne_blanche = 0;
  493.  
  494.   char ** t;
  495.  
  496.   char * newsgrp = malloc (13);
  497.   strcpy (newsgrp, "NEWSGROUPS: ");
  498.   char * newsgrp2;
  499.  
  500.   int n = 0;
  501.   int i;
  502.  
  503.  asprintf (&req, \
  504.            "%s = '%s'; %s = '%s' and conti = '%s';", \
  505.            "update messages set numero_fichier = 1+ numero_fichier where id", \
  506.            id, \
  507.            "select message from messages_contis where message", \
  508.            id, cible);
  509.   res = PQexec (conn, req);
  510.   free (req);
  511.  
  512.   int dans_cible = (0 != PQntuples (res));
  513.  
  514.   char * tampon = NULL;
  515.  
  516.   while (!(feof (f1)))
  517.     {
  518.       if (tampon != NULL)
  519.         fputs (tampon, f2);
  520.       fgets (ligne, LMXSIZ, f1);
  521.       if (ligne_blanche == 0 && \
  522.           (strcmp (ligne, "\n") == 0 || strcmp (ligne, "") == 0))
  523.         ligne_blanche = 1;
  524.       if (ligne_blanche == 0 && \
  525.           strcasecmp (ligne, "NEWSGROUPS: ") > 0 && \
  526.           strcasecmp (ligne, "NEWSGROUPS:!") < 0)
  527.         {
  528.           t = nouveau_t ();
  529.           n = parse (&ligne[12], t, 0, 0);
  530.           for (i = 0; i < n - 1; i++)
  531.             {
  532.               if (strcmp (source, t[i]) == 0)
  533.                 {
  534.                   if (!dans_cible)
  535.                     {
  536.                       asprintf (&newsgrp2, "%s%s, ", newsgrp, cible);
  537.                       free (newsgrp);
  538.                       newsgrp = newsgrp2;
  539.                     }
  540.                 } else {
  541.                   asprintf (&newsgrp2, "%s%s, ", newsgrp, t[i]);
  542.                   free (newsgrp);
  543.                   newsgrp = newsgrp2;
  544.                 };
  545.               free (t[i]);
  546.             }
  547.           free (t);
  548.           free (tampon);
  549.           newsgrp[strlen (newsgrp - 2)] = 0;
  550.           asprintf (&tampon, "%s\n", newsgrp);
  551.         }
  552.       else
  553.         {
  554.           free (tampon);
  555.           asprintf (&tampon, "%s", ligne);
  556.         }
  557.     };
  558.  
  559.   free (tampon);
  560.   fclose (f1);
  561.   fclose (f2);
  562.  
  563.   if (dans_cible) {
  564.     free (fichier_interm);
  565.     return id;
  566.   };
  567.  
  568.   char * nouv_id = creer_id (date);
  569.   char * nouv_fichier = fichier (nouv_id, "0");
  570.  
  571.   rename (fichier_interm, nouv_fichier);
  572.   free (fichier_interm);
  573.   free (nouv_fichier);
  574.  
  575.   asprintf (&req, " select de from refs where a = '%s' ;", id);
  576.   res = PQexec (conn, req);
  577.   free (req);
  578.   n = PQntuples (res);
  579.  
  580.   for (i = 0; i < n ; i++)
  581.     {
  582.       maj_refs (conn, PQgetvalue (res, i, 0), id,/* màj du champ fils du père du message à déplacer */du message à déplacer */
  583.  
  584.   asprintf (&req, "select pere from messages where id = '%s'; ", id);
  585.   res = PQexec (conn, req);
  586.   free (req);
  587.   char * pere = PQgetvalue (res, 0, 0);
  588.   char * req2;
  589.  
  590.   if (0 == strcmp (pere, ""))
  591.     {
  592.       printf ("Le message '%s' n'a pas de père.\n",id);
  593.       req2 = malloc (1);
  594.       req2[0] = 0;
  595.     }
  596.   else
  597.     {
  598.  
  599.       asprintf (&req, "select fils from messages where id = '%s'; ", pere);
  600.       res = PQexec (conn, req);
  601.       free (req);
  602.  
  603.       t = nouveau_t ();
  604.       n = parse (PQgetvalue (res, 0, 0) , t, 0, 0);
  605.  
  606.       char * fils = malloc (1);
  607.       fils[0] = 0;
  608.       char * fils2;
  609.  
  610.       for (i = 0; i < n - 1 ; i++)
  611.         {
  612.           if (strcmp (t[i], id) == 0)
  613.             asprintf (&fils2, "%s%s, ", fils, nouv_id);
  614.           else
  615.             asprintf (&fils2, "%s%s, ", fils, t[i]);
  616.          
  617.           free (fils);
  618.           fils = fils2;
  619.         };
  620.  
  621.       asprintf (&req2, "update messages set fils = '%s' where id = '%s';", \
  622.                 fils, pere);
  623.  
  624.     };
  625.  
  626.   asprintf (&req, "%s %s '%s' %s '%s'; %s '%s' %s '%s'; %s '%s' %s '%s'; %s '%s', %s ', (%s ''%s'; %s ('%s', '%s', (%s '%s')); %s '%s';", \
  627.             req2, \
  628.             ", nouv_id, "ges set pere =", nouv_id, "where pere =", id, \
  629.             "update refs set a =", nouv_id, "where a =", id, \
  630.             "update refs set de =", nouv_id, "where de =", id, \
  631.             "update messages set id =", nouv_id, \
  632.             ", cdate, \
  633.            " =", cdate, \
  634.             "where id =", id, \
  635.             "insert into messages_contis(message,conti,numero) values", \
  636.             nouv_id, cible, \
  637.             "select prochain_numero from contis where nom =", cible, \
  638.             "update contis set prochain_numero = 1 + prochain_numero where nom =", \
  639.             cible);
  640.   free (req2);
  641.  
  642.  
  643.  
  644.   printf ("\nLe message '%s' du conti ' du conti ' le message '%s' du conti '%s'\n%s\n", \
  645.           id, source, nouv_id, cible, req);
  646.  
  647.   PQexec (conn, req);
  648.   free (req);
  649.  
  650.   return nouv_id;
  651.  
  652. }
  653.  
  654.  
  655. void
  656. deplacer_message (char *utilisateur, char * id, char * source, char * cible)
  657. {
  658.  
  659.   time_t date = time (NULL);
  660.   char * cdate = ctime (&date);
  661.   cdate[24] = 0;
  662.  
  663.   PGconn * conn = connecter ();
  664.  
  665.   char * req;
  666.  
  667.   asprintf (&req, "select * from droits where conti = '%s' and moderateur = '%s'", \
  668.             source, utilisateur);
  669.  
  670.   PGresult * res = PQexec (conn, req);
  671.   free (req);
  672.   if (PQntuples(res) == 0) {
  673.     printf ("L'utilisateur '%s' n'a pas le droit de déplacer des messages depuis le conti '%s'.\n", \
  674.             utilisateur, source);
  675.     return;
  676.   };
  677.  
  678.   asprintf (&req, "%s '%s' and (contis.lecture_seule = 'f' or (%s '%s'))", \
  679.             "select * from droits,contis where contis.nom =", cible, \
  680.             "droits.conti = contis.nom and droits.ecrire = 't' and droits.moderateur =", \
  681.             utilisateur);
  682.   res = PQexec (conn, req);
  683.   free (req);
  684.   if (PQntuples(res) == 0)
  685.     {
  686.       printf("L'utilisateur '%s' n'a pas le droit de déplacer des messages vers le conti '%s'.\n", \
  687.              utilisateur, cible);
  688.       return;
  689.     };
  690.  
  691.   PQexec (conn, \
  692.           "begin; lock refs,messages,messages_contis in exclusive mode; lock contis in row exclusive mode;");
  693.   srandom (date);
  694.   free (deplacer (conn, id, date, cdate, source, cible));
  695.   PQexec (conn, "commit;");
  696.   deconnecter (conn);
  697. }
  698.  
  699.  
  700. void
  701. deplacer_fils_rec (PGconn * conn, char * rootid, time_t date, char * cdate, \
  702.                    char * source, char * cible)
  703. {
  704.   char * nouv_rootid = deplacer (conn, rootid, date, cdate, source, cible);
  705.  
  706.   if (NULL == nouv_rootid)
  707.     return;
  708.  
  709.   char * req;
  710.  
  711.   asprintf (&req, \
  712.             "select messages.id from messages,messages_contis where messages.id = messages_contis.message and messages_contis.conti = '%s' and messages.pere = '%s';", \
  713.             source, nouv_rootid);
  714.  
  715.   PGresult * res = PQexec (conn, req);
  716.   free (req);
  717.  
  718.   int n = PQntuples (res);
  719.   int i;
  720.  
  721.   for (i = 0; i < n; i++)
  722.     {
  723.       deplacer_fils_rec (conn, PQgetvalue (res, i, 0), date, cdate, \
  724.                          source, cible);
  725.     }
  726.   free (nouv_rootid);
  727.  
  728. }  
  729.  
  730. void
  731. deplacer_thread (char * utilisateur,
  732.                  char * rootid, char * source, char * cible)
  733. {
  734.  
  735.  
  736.   time_t date = time (NULL);
  737.   char * cdate = ctime (&date);
  738.   cdate[24] = 0;
  739.  
  740.   PGconn * conn = connecter ();
  741.  
  742.  
  743.   char * req;
  744.  
  745.   asprintf(&req, \
  746.            "select * from droits where conti = '%s' and moderateur = '%s'", \
  747.            source, utilisateur);
  748.  
  749.   PGresult * res = PQexec (conn, req);
  750.   free (req);
  751.   if (PQntuples(res) == 0)
  752.     {
  753.       printf("L'utilisateur '%s' n'a pas le droit de déplacer des messages depuis le conti '%s'.\n", \
  754.              utilisateur, source);
  755.       return;
  756.     };
  757.  
  758.   asprintf (&req, "select * from droits,contis where contis.nom = '%s'f'nd (contis.lecture_seule = 'f' or (droits.conti = contis.nom and droits.moderateur = '%s' and droits.ecrire = 't') )", \
  759.             cible, utilisateur);
  760.   res = PQexec(conn, req);
  761.   free(req);
  762.   if( PQntuples(res) == 0)
  763.     {
  764.       printf ("L'utilisateur '%s' n'a pas le droit de déplacer des messages vers le conti '%s'.\n", \
  765.               utilisateur, cible);
  766.       return;
  767.     };
  768.  
  769.   PQexec (conn, \
  770.           "begin; lock refs,messages,messages_contis in exclusive mode; lock contis in row exclusive mode;");
  771.   srandom (date);
  772.  
  773.   deplacer_fils_rec (conn, rootid, date, cdate, source, cible);
  774.  
  775.   PQexec (conn, "commi

Raw Paste

Login or Register to edit or fork this paste. It's free.