ADD licences sur les fichier .erl
[euphorik.git] / modules / erl / euphorik_bd.erl
index 6fc651d..b938f75 100755 (executable)
@@ -1,8 +1,26 @@
-% coding: utf-8
+% coding: utf-8\r
+% Copyright 2008 Grégory Burri\r
+%\r
+% This file is part of Euphorik.\r
+%\r
+% Euphorik is free software: you can redistribute it and/or modify\r
+% it under the terms of the GNU General Public License as published by\r
+% the Free Software Foundation, either version 3 of the License, or\r
+% (at your option) any later version.\r
+%\r
+% Euphorik is distributed in the hope that it will be useful,\r
+% but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+% GNU General Public License for more details.\r
+%\r
+% You should have received a copy of the GNU General Public License\r
+% along with Euphorik.  If not, see <http://www.gnu.org/licenses/>.\r
+% 
 % Ce module permet de gérer les données persistantes lié au site d'euphorik.ch.
 % Il permet d'ajouter des message, de demande les messages sur une page donnée, etc..
 % Ce module utilise une base mnesia.
-% @author G.Burri
+% @author G.Burri\r
+
 
 -module(euphorik_bd).\r
 -export([
@@ -54,7 +72,6 @@
    % trolls :
    trolls/0,
    trolls/1,
-   trolls_attente/1,
    put_troll/2,
    mod_troll/2,
    del_troll/1,
@@ -388,7 +405,7 @@ nouveau_message(Mess, Auteur_id, Repond_A) ->
             % est-ce que l'auteur à trop floodé ?
             if Auteur#user.indice_flood =/= ?INDICE_SPAM_MAX, Auteur_maj#user.indice_flood =:= ?INDICE_SPAM_MAX, Delta =< ?DUREE_BLOCAGE_SPAM ->
                mnesia:write(Auteur#user{indice_flood = Auteur_maj#user.indice_flood}),
-               nouveau_message_sys(Auteur#user.pseudo ++ if Auteur#user.login =/= [] -> " (" ++ Auteur#user.login ++ ")"; true -> "" end ++ " est bloqué pour " ++ integer_to_list(trunc(?DUREE_BLOCAGE_SPAM / 1000)) ++ " secondes pour cause de flood.");
+               nouveau_message_sys("''" ++ Auteur#user.pseudo ++ if Auteur#user.login =/= [] -> " (" ++ Auteur#user.login ++ ")"; true -> "" end ++ "'' est bloqué pour " ++ integer_to_list(trunc(?DUREE_BLOCAGE_SPAM / 1000)) ++ " secondes pour cause de flood.");
             Auteur#user.indice_flood =:= ?INDICE_SPAM_MAX, Delta =< ?DUREE_BLOCAGE_SPAM ->
                {erreur, "Bloqué pour cause de flood"};
             true ->     
@@ -555,7 +572,7 @@ list_ban() ->
                e(q([{U#user.pseudo, U#user.login} || U <- mnesia:table(user), U#user.last_ip =:= IP#ip_table.ip]))
             } ||
             IP <- qlc:keysort(2, mnesia:table(ip_table)),
-            date_plus_minutes(IP#ip_table.ban, IP#ip_table.ban_duration) > Now
+            if IP#ip_table.ban =:= undefined -> false; true -> date_plus_minutes(IP#ip_table.ban, IP#ip_table.ban_duration) > Now end
          ]))
       end
    )).
@@ -655,58 +672,11 @@ trolls() ->
 % Renvoie les trolls manquants posté après Last_id.
 trolls(Last_id) ->
    resultat_transaction(mnesia:transaction(
-      e(q([T || T <- qlc:keysort(2, mnesia:table(troll)), T#troll.id > Last_id, T#troll.date_post =:= undefined]))
+      fun() ->
+         e(q([T || T <- qlc:keysort(2, mnesia:table(troll)), T#troll.id > Last_id, T#troll.date_post =:= undefined]))
+      end
    )).
    
-   
-% Renvoie les trolls manquants posté après Last_id.
-% Si pas de trolls alors attend un événement tel qu'un ajout, une modification ou une suppression.
-% renvoie :
-%  {mod, Troll}
-% ou {add, [Trolls]}
-% ou {del, Troll_id}
-% ou timeout
-trolls_attente(Last_id) ->
-   case mnesia:subscribe({table, troll, detailed}) of
-      {error, E} = E ->
-         E;
-      _ ->
-         R = case resultat_transaction(mnesia:transaction(
-               fun() ->
-                  e(q([T || T <- qlc:keysort(2, mnesia:table(troll)), T#troll.id > Last_id, T#troll.date_post =:= undefined]))
-               end
-            )) of
-               [] -> % pas de trolls
-                  attend_evenement_troll();
-               Trolls ->
-                  {add, Trolls}
-         end,
-         mnesia:unsubscribe({table, troll, detailed}),
-         R
-   end.
-   
-attend_evenement_troll() ->
-   % s'il n'y a pas de trolls que l'utilisateur n'a pas connaissance alors on attend un événement
-   receive
-      % cas où un troll est choisit comme courant
-      {mnesia_table_event, {write, troll, Troll, [Old_troll | _], _}} when Old_troll#troll.date_post =:= undefined, Troll#troll.date_post =/= undefined ->
-         {del, Troll#troll.id};
-      {mnesia_table_event, {write, troll, Troll, [_Old_troll | _], _}} ->
-         {mod, Troll};
-      {mnesia_table_event, {write, troll, Troll, [], _}} ->
-         {add, [Troll]};
-      {mnesia_table_event, {delete, troll, {troll, Id}, _, _}} ->
-         {del, Id};
-      {tcp_closed, _} ->
-         exit(normal);
-      _ ->
-         attend_evenement_troll()
-   % 60 minutes de timeout (on ne sais jamais)
-   % Après 60 minutes de connexion, le client doit donc reétablir une connexion
-   after 1000 * 60 * 60 -> 
-      timeout
-   end.
-   
  
  % Renvoie l'id du nouveau troll
  % ou max_troll_reached_per_user si le nombre de troll posté par l'utilisateur max a été atteind