1. -- 
  2. --  Copyright (c) 2008-2009, 
  3. --  Reto Buerki, Adrian-Ken Rueegsegger 
  4. -- 
  5. --  This file is part of Alog. 
  6. -- 
  7. --  Alog is free software; you can redistribute it and/or modify 
  8. --  it under the terms of the GNU Lesser General Public License as published 
  9. --  by the Free Software Foundation; either version 2.1 of the License, or 
  10. --  (at your option) any later version. 
  11. -- 
  12. --  Alog is distributed in the hope that it will be useful, 
  13. --  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14. --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  15. --  GNU Lesser General Public License for more details. 
  16. -- 
  17. --  You should have received a copy of the GNU Lesser General Public License 
  18. --  along with Alog; if not, write to the Free Software 
  19. --  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 
  20. --  MA  02110-1301  USA 
  21.  
  22. with Ada.Characters.Latin_1; 
  23.  
  24. with AWS; 
  25.  
  26. with GNAT.Sockets; 
  27.  
  28. --  SMTP-Logging facility. Used to send log-messages to a configurable 
  29. --  mailserver. AWS must be installed for this facility to work. 
  30. package Alog.Facilities.SMTP is 
  31.  
  32.    type Instance is new Alog.Facilities.Instance with private; 
  33.    --  SMTP based logging facility. 
  34.  
  35.    type Handle is access all Instance; 
  36.  
  37.    procedure Set_Recipient 
  38.      (Facility : in out Instance; 
  39.       Name     :        String; 
  40.       EMail    :        String); 
  41.    --  Set recipient for log-messages. This procedure MUST be called before 
  42.    --  subsequent calls to Write_Message(). 
  43.  
  44.    procedure Set_Server 
  45.      (Facility : in out Instance; 
  46.       Name     :        String); 
  47.    --  Set server for log-messages. This procedure MUST be called before 
  48.    --  subsequent calls to Write_Message(). 
  49.  
  50.    procedure Set_Header 
  51.      (Facility : in out Instance; 
  52.       Header   :        String); 
  53.    --  Set Message-Header of log-messages. 
  54.  
  55.    function Get_Header (Facility : Instance) return String; 
  56.    --  Get actual Message-Header of log-messages. 
  57.  
  58.    --  Exceptions. 
  59.  
  60.    No_Recipient    : exception; 
  61.    --  No recipient specified. Cannot send mail. 
  62.  
  63.    No_Server       : exception; 
  64.    --  No server specified. Cannot send mail. 
  65.  
  66.    Delivery_Failed : exception; 
  67.    --  Mail could not be delivered. 
  68.  
  69. private 
  70.  
  71.    overriding 
  72.    procedure Write 
  73.      (Facility : Instance; 
  74.       Level    : Log_Level := Info; 
  75.       Msg      : String); 
  76.    --  Implementation of the Write procedure for SMTP. 
  77.  
  78.    function Format_Message 
  79.      (Facility : Instance; 
  80.       Level    : Log_Level; 
  81.       Msg      : String) 
  82.       return String; 
  83.    --  Compose a message from Msg, Header, Loglevel, Timestamp, PID. 
  84.  
  85.    EOL : constant Character := Ada.Characters.Latin_1.LF; 
  86.    --  EOL used in mail-messages. 
  87.  
  88.    type Mail_Address is tagged record 
  89.       Name  : Unbounded_String; 
  90.       EMail : Unbounded_String; 
  91.    end record; 
  92.    --  Holds Sender / Recipient information. 
  93.  
  94.    type Instance is new Alog.Facilities.Instance with record 
  95.       Server       : Unbounded_String; 
  96.       --  Server to connect when sending log-mails. 
  97.  
  98.       Is_Server    : Boolean := False; 
  99.       --  Indicates whether a server is set. 
  100.  
  101.       Recipient    : Mail_Address; 
  102.       --  Recipient for log-mails. Must be specified before calling 
  103.       --  Write_Message(), else No_Recipient exception is thrown. 
  104.  
  105.       Is_Recipient : Boolean := False; 
  106.       --  Indicates whether a recipient is set. 
  107.  
  108.       Sender       : Mail_Address := 
  109.         (Name  => To_Unbounded_String ("alog"), 
  110.          EMail => To_Unbounded_String ("alog@" & 
  111.            GNAT.Sockets.Host_Name)); 
  112.       --  Notification sender address/name. 
  113.  
  114.       Subject      : Unbounded_String := To_Unbounded_String 
  115.         ("Log-Message"); 
  116.       --  Subject of messages from Alog-System (default: Alog: Log-Message). 
  117.  
  118.       Header       : Unbounded_String := To_Unbounded_String 
  119.         ("This is a message from the Alog-logsystem running on host " 
  120.          & GNAT.Sockets.Host_Name & ":" & EOL & EOL); 
  121.       --  Message-Header. Can be set by calling Set_Header(). 
  122.    end record; 
  123.  
  124. end Alog.Facilities.SMTP;