<commit_before>-----------------------------------------------------------------------
--  awa-mail -- Mail module
--  Copyright (C) 2011 <NAME>
--  Written by <NAME> (<EMAIL>)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

with Ada.Strings.Unbounded;
with Ada.Exceptions;

with AWA.Applications;
with ASF.Requests.Mockup;
with ASF.Responses.Mockup;
with Util.Beans.Basic;
with Util.Beans.Objects;
with Util.Log.Loggers;

with AWS.SMTP.Client;
package body AWA.Mail.Module is

   use Util.Log;

   Log : constant Loggers.Logger := Loggers.Create ("AWA.Mail.Module");

   --  Get the SMTP server to send an email
   function Get_Smtp_Server (Plugin : in Mail_Module) return AWS.SMTP.Receiver is
      Server : constant String := Plugin.Get_Config (Name => "smtp.host", Default => "localhost");
      Port   : constant String := Plugin.Get_Config (Name => "smtp.port", Default => "25");
   begin
      return AWS.SMTP.Client.Initialize (Server_Name => Server,
                                         Port => Positive'Value (Port));
   end Get_Smtp_Server;

   --  ------------------------------
   --  Get the mail template that must be used for the given event name.
   --  The mail template is configured by the property: <i>module</i>.template.<i>event</i>.
   --  ------------------------------
   function Get_Template (Plugin : in Mail_Module;
                          Name   : in String) return String is
      Prop_Name : constant String := Plugin.Get_Name & ".template." & Name;
   begin
      return Plugin.Get_Config (Prop_Name);
   end Get_Template;

   --  Receive an event sent by another module with <b>Send_Event</b> method.
   procedure Receive_Event (Plugin  : in out Mail_Module;
                            Content : in ASF.Events.Modules.Module_Event'Class) is
      Name : constant String := Content.Get_Parameter ("name");
   begin
      Log.Info ("Receive event {0}", Name);

      declare
         File  : constant String := Plugin.Get_Template (Name);
         Req   : ASF.Requests.Mockup.Request;
         Reply : ASF.Responses.Mockup.Response;
         Ptr   : constant Util.Beans.Basic.Readonly_Bean_Access := Content'Unrestricted_Access;
         Bean  : constant Util.Beans.Objects.Object
           := Util.Beans.Objects.To_Object (Ptr, Util.Beans.Objects.STATIC);
      begin
         Log.Info ("With template '{0}'", File);

         Req.Set_Path_Info (File);
         Req.Set_Method ("GET");
         Req.Set_Attribute (Name => "event", Value => Bean);

         Plugin.Get_Application.Dispatch (Page     => File,
                                          Request  => Req,
                                          Response => Reply);

         declare
            Email   : constant String := Content.Get_Parameter ("email");
            Server  : constant AWS.SMTP.Receiver := Get_Smtp_Server (Plugin);
            Content : Ada.Strings.Unbounded.Unbounded_String;
            Result  : AWS.SMTP.Status;
         begin
            Reply.Read_Content (Content);
            Log.Info ("Mail to be sent: {0}", Content);

            AWS.SMTP.Client.Send (Server  => Server,
                                  From    => AWS.SMTP.E_Mail (Name    => "awa",
                                                              Address => "<EMAIL>"),
                                  To      => AWS.SMTP.E_Mail (Name    => Email,
                                                              Address => "<EMAIL>"),
                                  Subject => "Welcome to awa",
                                  Message => Ada.Strings.Unbounded.To_String (Content),
                                  Status  => Result);

         exception
            when E : others =>
               Log.Error ("Error when sending email with template {0}: {1}: {2}", File,
                          Ada.Exceptions.Exception_Name (E),
                          Ada.Exceptions.Exception_Message (E));
         end;
      end;

   end Receive_Event;

end AWA.Mail.Module;
<commit_msg>If there is no email template associated with an event, do nothing<commit_after>-----------------------------------------------------------------------
--  awa-mail -- Mail module
--  Copyright (C) 2011 <NAME>
--  Written by <NAME> (<EMAIL>)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

with Ada.Strings.Unbounded;
with Ada.Exceptions;

with AWA.Applications;
with ASF.Requests.Mockup;
with ASF.Responses.Mockup;
with Util.Beans.Basic;
with Util.Beans.Objects;
with Util.Log.Loggers;

with AWS.SMTP.Client;
package body AWA.Mail.Module is

   use Util.Log;

   Log : constant Loggers.Logger := Loggers.Create ("AWA.Mail.Module");

   --  Get the SMTP server to send an email
   function Get_Smtp_Server (Plugin : in Mail_Module) return AWS.SMTP.Receiver is
      Server : constant String := Plugin.Get_Config (Name => "smtp.host", Default => "localhost");
      Port   : constant String := Plugin.Get_Config (Name => "smtp.port", Default => "25");
   begin
      return AWS.SMTP.Client.Initialize (Server_Name => Server,
                                         Port => Positive'Value (Port));
   end Get_Smtp_Server;

   --  ------------------------------
   --  Get the mail template that must be used for the given event name.
   --  The mail template is configured by the property: <i>module</i>.template.<i>event</i>.
   --  ------------------------------
   function Get_Template (Plugin : in Mail_Module;
                          Name   : in String) return String is
      Prop_Name : constant String := Plugin.Get_Name & ".template." & Name;
   begin
      return Plugin.Get_Config (Prop_Name);
   end Get_Template;

   --  Receive an event sent by another module with <b>Send_Event</b> method.
   procedure Receive_Event (Plugin  : in out Mail_Module;
                            Content : in ASF.Events.Modules.Module_Event'Class) is
      Name : constant String := Content.Get_Parameter ("name");
   begin
      Log.Info ("Receive event {0}", Name);

      declare
         File  : constant String := Plugin.Get_Template (Name);
         Req   : ASF.Requests.Mockup.Request;
         Reply : ASF.Responses.Mockup.Response;
         Ptr   : constant Util.Beans.Basic.Readonly_Bean_Access := Content'Unrestricted_Access;
         Bean  : constant Util.Beans.Objects.Object
           := Util.Beans.Objects.To_Object (Ptr, Util.Beans.Objects.STATIC);
      begin
         if File = "" then
            Log.Debug ("No email template associated with event {0}", Name);
            return;
         end if;
         Log.Info ("With template '{0}'", File);

         Req.Set_Path_Info (File);
         Req.Set_Method ("GET");
         Req.Set_Attribute (Name => "event", Value => Bean);

         Plugin.Get_Application.Dispatch (Page     => File,
                                          Request  => Req,
                                          Response => Reply);

         declare
            Email   : constant String := Content.Get_Parameter ("email");
            Server  : constant AWS.SMTP.Receiver := Get_Smtp_Server (Plugin);
            Content : Ada.Strings.Unbounded.Unbounded_String;
            Result  : AWS.SMTP.Status;
         begin
            Reply.Read_Content (Content);
            Log.Info ("Mail to be sent: {0}", Content);

            AWS.SMTP.Client.Send (Server  => Server,
                                  From    => AWS.SMTP.E_Mail (Name    => "awa",
                                                              Address => "<EMAIL>"),
                                  To      => AWS.SMTP.E_Mail (Name    => Email,
                                                              Address => "<EMAIL>"),
                                  Subject => "Welcome to awa",
                                  Message => Ada.Strings.Unbounded.To_String (Content),
                                  Status  => Result);

         exception
            when E : others =>
               Log.Error ("Error when sending email with template {0}: {1}: {2}", File,
                          Ada.Exceptions.Exception_Name (E),
                          Ada.Exceptions.Exception_Message (E));
         end;
      end;

   end Receive_Event;

end AWA.Mail.Module;

{'fraction_non_alphanumeric': 0.1439102564102564, 'fraction_numerical': 0.0035256410256410257, 'mean_word_length': 1.6206606942889137, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '939222', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2359, 'n_words': 732}