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