/* Auxiliary functions for interfacing with SpamAssassin Copyright (C) 2008-2020 Sergey Poznyakoff This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ module 'sa'. const SA_SYMBOLS 0 const SA_REPORT 1 const SA_LEARN_SPAM 2 const SA_LEARN_HAM 3 const SA_FORGET 4 #pragma regex push +extended static func __sa_format_score0(string code, number prec) returns string do number len length(code) if len > prec return substring(code, 0, len - prec-1) . '.' . substring(code, len - prec, -1) else return "0." . replstr("0", prec - len) . code fi done /* Format CODE as a floating-point number with PREC decimal digits, i.e.: printf("%0.*f", prec, code/pow(10,prec)) in C equivalent. */ func sa_format_score(number code, number prec) returns string do if code < 0 return "-" . __sa_format_score0(- code, prec) else return __sa_format_score0(code, prec) fi done /* Reformat SpamAssassin report text in order to include it in a RFC 822 header: 1. Remove the preamble and epilogue parts. 2. Prefix each description line with a "* " */ func sa_format_report_header(string text) returns string do number start index(text, "pts rule name") if start == -1 return text fi set start index(text, "\n", start + 1) if start == -1 return text fi set start index(text, "\n", start + 1) if start == -1 return text fi string ret "" number len length(text) loop for number i start + 1, while i < len do if substr(text, i) matches '^ *-?[0-9]\.[0-9]' set pfx "* " else set pfx " " fi number n index(text, "\n", i) if n >= 0 set ret ret . pfx . substring(text, i, n) set i n + 1 else set ret ret . pfx . substr(text, i) break fi done return ret done func sa(string urlstr, number prec; number flag) returns number do number command if defined(flag) set command flag else set command SA_SYMBOLS fi return spamc(current_message(), urlstr, prec, command) done #pragma regex pop