Mercurial > emacs
view lisp/calc/calc-funcs.el @ 91817:f0b22bbb77fb
;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
;; Copyright (C) 1995, 1996, 2001, 2002, 2003, 2004, 2005,
;; 2006, 2007, 2008 Free Software Foundation, Inc.
;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
;; Maintainer: Simon Josefsson <simon@josefsson.org>
;; w32 Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
;; ESMTP support: Simon Leinen <simon@switch.ch>
;; Hacked by Mike Taylor, 11th October 1999 to add support for
;; automatically appending a domain to RCPT TO: addresses.
;; AUTH=LOGIN support: Stephen Cranefield <scranefield@infoscience.otago.ac.nz>
;; Keywords: mail
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Send Mail to smtp host from smtpmail temp buffer.
;; Please add these lines in your .emacs(_emacs) or use customize.
;;
;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
;;(setq smtpmail-debug-info t) ; only to debug problems
;;(setq smtpmail-auth-credentials ; or use ~/.authinfo
;; '(("YOUR SMTP HOST" 25 "username" "password")))
;;(setq smtpmail-starttls-credentials
;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
;; Where the 25 equals the value of `smtpmail-smtp-service', it can be an
;; integer or a string, just as long as they match (eq).
;; To queue mail, set smtpmail-queue-mail to t and use
;; smtpmail-send-queued-mail to send.
;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
;; Rewritten by Simon Josefsson to use same credential variable as AUTH
;; support below.
;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
;; Authentication by the AUTH mechanism.
;; See http://www.ietf.org/rfc/rfc2554.txt
;; Modified by Simon Josefsson <simon@josefsson.org>, 2000-10-07, to support
;; STARTTLS. Requires external program
;; ftp://ftp.opaopa.org/pub/elisp/starttls-*.tar.gz.
;; See http://www.ietf.org/rfc/rfc2246.txt, http://www.ietf.org/rfc/rfc2487.txt
;;; Code:
(require 'sendmail)
(autoload 'starttls-open-stream "starttls")
(autoload 'starttls-negotiate "starttls")
(autoload 'mail-strip-quoted-names "mail-utils")
(autoload 'message-make-date "message")
(autoload 'message-make-message-id "message")
(autoload 'rfc2104-hash "rfc2104")
(autoload 'netrc-parse "netrc")
(autoload 'netrc-machine "netrc")
(autoload 'netrc-get "netrc")
;;;
(defgroup smtpmail nil
"SMTP protocol for sending mail."
:group 'mail)
(defcustom smtpmail-default-smtp-server nil
"*Specify default SMTP server.
This only has effect if you specify it before loading the smtpmail library."
:type '(choice (const nil) string)
:group 'smtpmail)
(defcustom smtpmail-smtp-server
(or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
"*The name of the host running SMTP server."
:type '(choice (const nil) string)
:group 'smtpmail)
(defcustom smtpmail-smtp-service 25
"*SMTP service port number.
The default value would be \"smtp\" or 25."
:type '(choice (integer :tag "Port") (string :tag "Service"))
:group 'smtpmail)
(defcustom smtpmail-local-domain nil
"*Local domain name without a host name.
If the function `system-name' returns the full internet address,
don't define this value."
:type '(choice (const nil) string)
:group 'smtpmail)
(defcustom smtpmail-sendto-domain nil
"*Local domain name without a host name.
This is appended (with an @-sign) to any specified recipients which do
not include an @-sign, so that each RCPT TO address is fully qualified.
\(Some configurations of sendmail require this.)
Don't bother to set this unless you have get an error like:
Sending failed; SMTP protocol error
when sending mail, and the *trace of SMTP session to <somewhere>*
buffer includes an exchange like:
RCPT TO: <someone>
501 <someone>: recipient address must contain a domain
"
:type '(choice (const nil) string)
:group 'smtpmail)
(defcustom smtpmail-debug-info nil
"Whether to print info in buffer *trace of SMTP session to <somewhere>*.
See also `smtpmail-debug-verb' which determines if the SMTP protocol should
be verbose as well."
:type 'boolean
:group 'smtpmail)
(defcustom smtpmail-debug-verb nil
"Whether this library sends the SMTP VERB command or not.
The commands enables verbose information from the SMTP server."
:type 'boolean
:group 'smtpmail)
(defcustom smtpmail-code-conv-from nil ;; *junet*
"*smtpmail code convert from this code to *internal*..for tiny-mime.."
:type 'boolean
:group 'smtpmail)
(defcustom smtpmail-queue-mail nil
"*If set, mail is queued; otherwise it is sent immediately.
If queued, it is stored in the directory `smtpmail-queue-dir'
and sent with `smtpmail-send-queued-mail'."
:type 'boolean
:group 'smtpmail)
(defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
"*Directory where `smtpmail.el' stores queued mail."
:type 'directory
:group 'smtpmail)
(defcustom smtpmail-auth-credentials "~/.authinfo"
"Specify username and password for servers, directly or via .netrc file.
This variable can either be a filename pointing to a file in netrc(5)
format, or list of four-element lists that contain, in order,
`servername' (a string), `port' (an integer), `user' (a string) and
`password' (a string, or nil to query the user when needed). If you
need to enter a `realm' too, add it to the user string, so that it
looks like `user@realm'."
:type '(choice file
(repeat (list (string :tag "Server")
(integer :tag "Port")
(string :tag "Username")
(choice (const :tag "Query when needed" nil)
(string :tag "Password")))))
:version "22.1"
:group 'smtpmail)
(defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
"Specify STARTTLS keys and certificates for servers.
This is a list of four-element list with `servername' (a string),
`port' (an integer), `key' (a filename) and `certificate' (a
filename).
If you do not have a certificate/key pair, leave the `key' and
`certificate' fields as `nil'. A key/certificate pair is only
needed if you want to use X.509 client authenticated
connections."
:type '(repeat (list (string :tag "Server")
(integer :tag "Port")
(file :tag "Key")
(file :tag "Certificate")))
:version "21.1"
:group 'smtpmail)
(defcustom smtpmail-warn-about-unknown-extensions nil
"*If set, print warnings about unknown SMTP extensions.
This is mainly useful for development purposes, to learn about
new SMTP extensions that might be useful to support."
:type 'boolean
:version "21.1"
:group 'smtpmail)
(defvar smtpmail-queue-index-file "index"
"File name of queued mail index.
This is relative to `smtpmail-queue-dir'.")
(defvar smtpmail-address-buffer)
(defvar smtpmail-recipient-address-list)
(defvar smtpmail-queue-counter 0)
;; Buffer-local variable.
(defvar smtpmail-read-point)
(defvar smtpmail-queue-index (concat smtpmail-queue-dir
smtpmail-queue-index-file))
(defconst smtpmail-auth-supported '(cram-md5 plain login)
"List of supported SMTP AUTH mechanisms.")
;;;
;;;
;;;
(defvar smtpmail-mail-address nil
"Value to use for envelope-from address for mail from ambient buffer.")
;;;###autoload
(defun smtpmail-send-it ()
(let ((errbuf (if mail-interactive
(generate-new-buffer " smtpmail errors")
0))
(tembuf (generate-new-buffer " smtpmail temp"))
(case-fold-search nil)
delimline
(mailbuf (current-buffer))
;; Examine this variable now, so that
;; local binding in the mail buffer will take effect.
(smtpmail-mail-address
(or (and mail-specify-envelope-from (mail-envelope-from))
user-mail-address))
(smtpmail-code-conv-from
(if enable-multibyte-characters
(let ((sendmail-coding-system smtpmail-code-conv-from))
(select-message-coding-system)))))
(unwind-protect
(save-excursion
(set-buffer tembuf)
(erase-buffer)
;; Use the same buffer-file-coding-system as in the mail
;; buffer, otherwise any write-region invocations (e.g., in
;; mail-do-fcc below) will annoy with asking for a suitable
;; encoding.
(set-buffer-file-coding-system smtpmail-code-conv-from nil t)
(insert-buffer-substring mailbuf)
(goto-char (point-max))
;; require one newline at the end.
(or (= (preceding-char) ?\n)
(insert ?\n))
;; Change header-delimiter to be what sendmail expects.
(mail-sendmail-undelimit-header)
(setq delimline (point-marker))
;; (sendmail-synch-aliases)
(if mail-aliases
(expand-mail-aliases (point-min) delimline))
(goto-char (point-min))
;; ignore any blank lines in the header
(while (and (re-search-forward "\n\n\n*" delimline t)
(< (point) delimline))
(replace-match "\n"))
(let ((case-fold-search t))
;; We used to process Resent-... headers here,
;; but it was not done properly, and the job
;; is done correctly in smtpmail-deduce-address-list.
;; Don't send out a blank subject line
(goto-char (point-min))
(if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
(replace-match "")
;; This one matches a Subject just before the header delimiter.
(if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
(= (match-end 0) delimline))
(replace-match "")))
;; Put the "From:" field in unless for some odd reason
;; they put one in themselves.
(goto-char (point-min))
(if (not (re-search-forward "^From:" delimline t))
(let* ((login smtpmail-mail-address)
(fullname (user-full-name)))
(cond ((eq mail-from-style 'angles)
(insert "From: " fullname)
(let ((fullname-start (+ (point-min) 6))
(fullname-end (point-marker)))
(goto-char fullname-start)
;; Look for a character that cannot appear unquoted
;; according to RFC 822.
(if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
fullname-end 1)
(progn
;; Quote fullname, escaping specials.
(goto-char fullname-start)
(insert "\"")
(while (re-search-forward "[\"\\]"
fullname-end 1)
(replace-match "\\\\\\&" t))
(insert "\""))))
(insert " <" login ">\n"))
((eq mail-from-style 'parens)
(insert "From: " login " (")
(let ((fullname-start (point)))
(insert fullname)
(let ((fullname-end (point-marker)))
(goto-char fullname-start)
;; RFC 822 says \ and nonmatching parentheses
;; must be escaped in comments.
;; Escape every instance of ()\ ...
(while (re-search-forward "[()\\]" fullname-end 1)
(replace-match "\\\\\\&" t))
;; ... then undo escaping of matching parentheses,
;; including matching nested parentheses.
(goto-char fullname-start)
(while (re-search-forward
"\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
fullname-end 1)
(replace-match "\\1(\\3)" t)
(goto-char fullname-start))))
(insert ")\n"))
((null mail-from-style)
(insert "From: " login "\n")))))
;; Insert a `Message-Id:' field if there isn't one yet.
(goto-char (point-min))
(unless (re-search-forward "^Message-Id:" delimline t)
(insert "Message-Id: " (message-make-message-id) "\n"))
;; Insert a `Date:' field if there isn't one yet.
(goto-char (point-min))
(unless (re-search-forward "^Date:" delimline t)
(insert "Date: " (message-make-date) "\n"))
;; Possibly add a MIME header for the current coding system
(let (charset)
(goto-char (point-min))
(and (eq mail-send-nonascii 'mime)
(not (re-search-forward "^MIME-version:" delimline t))
(progn (skip-chars-forward "\0-\177")
(/= (point) (point-max)))
smtpmail-code-conv-from
(setq charset
(coding-system-get smtpmail-code-conv-from
'mime-charset))
(goto-char delimline)
(insert "MIME-version: 1.0\n"
"Content-type: text/plain; charset="
(symbol-name charset)
"\nContent-Transfer-Encoding: 8bit\n")))
;; Insert an extra newline if we need it to work around
;; Sun's bug that swallows newlines.
(goto-char (1+ delimline))
(if (eval mail-mailer-swallows-blank-line)
(newline))
;; Find and handle any FCC fields.
(goto-char (point-min))
(if (re-search-forward "^FCC:" delimline t)
;; Force mail-do-fcc to use the encoding of the mail
;; buffer to encode outgoing messages on FCC files.
(let ((coding-system-for-write smtpmail-code-conv-from))
(mail-do-fcc delimline)))
(if mail-interactive
(with-current-buffer errbuf
(erase-buffer))))
;;
;;
;;
(setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
(setq smtpmail-recipient-address-list
(smtpmail-deduce-address-list tembuf (point-min) delimline))
(kill-buffer smtpmail-address-buffer)
(smtpmail-do-bcc delimline)
; Send or queue
(if (not smtpmail-queue-mail)
(if (not (null smtpmail-recipient-address-list))
(if (not (smtpmail-via-smtp
smtpmail-recipient-address-list tembuf))
(error "Sending failed; SMTP protocol error"))
(error "Sending failed; no recipients"))
(let* ((file-data
(expand-file-name
(format "%s_%i"
(format-time-string "%Y-%m-%d_%H:%M:%S")
(setq smtpmail-queue-counter
(1+ smtpmail-queue-counter)))
smtpmail-queue-dir))
(file-data (convert-standard-filename file-data))
(file-elisp (concat file-data ".el"))
(buffer-data (create-file-buffer file-data))
(buffer-elisp (create-file-buffer file-elisp))
(buffer-scratch "*queue-mail*"))
(unless (file-exists-p smtpmail-queue-dir)
(make-directory smtpmail-queue-dir t))
(with-current-buffer buffer-data
(erase-buffer)
(set-buffer-file-coding-system smtpmail-code-conv-from nil t)
(insert-buffer-substring tembuf)
(write-file file-data)
(set-buffer buffer-elisp)
(erase-buffer)
(insert (concat
"(setq smtpmail-recipient-address-list '"
(prin1-to-string smtpmail-recipient-address-list)
")\n"))
(write-file file-elisp)
(set-buffer (generate-new-buffer buffer-scratch))
(insert (concat file-data "\n"))
(append-to-file (point-min)
(point-max)
smtpmail-queue-index)
)
(kill-buffer buffer-scratch)
(kill-buffer buffer-data)
(kill-buffer buffer-elisp))))
(kill-buffer tembuf)
(if (bufferp errbuf)
(kill-buffer errbuf)))))
;;;###autoload
(defun smtpmail-send-queued-mail ()
"Send mail that was queued as a result of setting `smtpmail-queue-mail'."
(interactive)
(with-temp-buffer
;;; Get index, get first mail, send it, update index, get second
;;; mail, send it, etc...
(let ((file-msg ""))
(insert-file-contents smtpmail-queue-index)
(goto-char (point-min))
(while (not (eobp))
(setq file-msg (buffer-substring (point) (line-end-position)))
(load file-msg)
;; Insert the message literally: it is already encoded as per
;; the MIME headers, and code conversions might guess the
;; encoding wrongly.
(with-temp-buffer
(let ((coding-system-for-read 'no-conversion))
(insert-file-contents file-msg))
(let ((smtpmail-mail-address
(or (and mail-specify-envelope-from (mail-envelope-from))
user-mail-address)))
(if (not (null smtpmail-recipient-address-list))
(if (not (smtpmail-via-smtp smtpmail-recipient-address-list
(current-buffer)))
(error "Sending failed; SMTP protocol error"))
(error "Sending failed; no recipients"))))
(delete-file file-msg)
(delete-file (concat file-msg ".el"))
(delete-region (point-at-bol) (point-at-bol 2)))
(write-region (point-min) (point-max) smtpmail-queue-index))))
;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
(defun smtpmail-fqdn ()
(if smtpmail-local-domain
(concat (system-name) "." smtpmail-local-domain)
(system-name)))
(defsubst smtpmail-cred-server (cred)
(nth 0 cred))
(defsubst smtpmail-cred-port (cred)
(nth 1 cred))
(defsubst smtpmail-cred-key (cred)
(nth 2 cred))
(defsubst smtpmail-cred-user (cred)
(nth 2 cred))
(defsubst smtpmail-cred-cert (cred)
(nth 3 cred))
(defsubst smtpmail-cred-passwd (cred)
(nth 3 cred))
(defun smtpmail-find-credentials (cred server port)
(catch 'done
(let ((l cred) el)
(while (setq el (pop l))
(when (and (equal server (smtpmail-cred-server el))
(equal port (smtpmail-cred-port el)))
(throw 'done el))))))
(defun smtpmail-maybe-append-domain (recipient)
(if (or (not smtpmail-sendto-domain)
(string-match "@" recipient))
recipient
(concat recipient "@" smtpmail-sendto-domain)))
(defun smtpmail-intersection (list1 list2)
(let ((result nil))
(dolist (el2 list2)
(when (memq el2 list1)
(push el2 result)))
(nreverse result)))
(defvar starttls-extra-args)
(defvar starttls-extra-arguments)
(defun smtpmail-open-stream (process-buffer host port)
(let ((cred (smtpmail-find-credentials
smtpmail-starttls-credentials host port)))
(if (null (and cred (condition-case ()
(with-no-warnings
(require 'starttls)
(call-process (if starttls-use-gnutls
starttls-gnutls-program
starttls-program)))
(error nil))))
;; The normal case.
(open-network-stream "SMTP" process-buffer host port)
(let* ((cred-key (smtpmail-cred-key cred))
(cred-cert (smtpmail-cred-cert cred))
(starttls-extra-args
(append
starttls-extra-args
(when (and (stringp cred-key) (stringp cred-cert)
(file-regular-p
(setq cred-key (expand-file-name cred-key)))
(file-regular-p
(setq cred-cert (expand-file-name cred-cert))))
(list "--key-file" cred-key "--cert-file" cred-cert))))
(starttls-extra-arguments
(append
starttls-extra-arguments
(when (and (stringp cred-key) (stringp cred-cert)
(file-regular-p
(setq cred-key (expand-file-name cred-key)))
(file-regular-p
(setq cred-cert (expand-file-name cred-cert))))
(list "--x509keyfile" cred-key "--x509certfile" cred-cert)))))
(starttls-open-stream "SMTP" process-buffer host port)))))
(defun smtpmail-try-auth-methods (process supported-extensions host port)
(let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
(mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
(cred (if (stringp smtpmail-auth-credentials)
(let* ((netrc (netrc-parse smtpmail-auth-credentials))
(port-name (format "%s" (or port "smtp")))
(hostentry (netrc-machine netrc host port-name
port-name)))
(when hostentry
(list host port
(netrc-get hostentry "login")
(netrc-get hostentry "password"))))
(smtpmail-find-credentials
smtpmail-auth-credentials host port)))
(passwd (when cred
(or (smtpmail-cred-passwd cred)
(read-passwd
(format "SMTP password for %s:%s: "
(smtpmail-cred-server cred)
(smtpmail-cred-port cred))))))
ret)
(when (and cred mech)
(cond
((eq mech 'cram-md5)
(smtpmail-send-command process (upcase (format "AUTH %s" mech)))
(if (or (null (car (setq ret (smtpmail-read-response process))))
(not (integerp (car ret)))
(>= (car ret) 400))
(throw 'done nil))
(when (eq (car ret) 334)
(let* ((challenge (substring (cadr ret) 4))
(decoded (base64-decode-string challenge))
(hash (rfc2104-hash 'md5 64 16 passwd decoded))
(response (concat (smtpmail-cred-user cred) " " hash))
;; Osamu Yamane <yamane@green.ocn.ne.jp>:
;; SMTP auth fails because the SMTP server identifies
;; only the first part of the string (delimited by
;; new line characters) as a response from the
;; client, and the rest as distinct commands.
;; In my case, the response string is 80 characters
;; long. Without the no-line-break option for
;; base64-encode-sting, only the first 76 characters
;; are taken as a response to the server, and the
;; authentication fails.
(encoded (base64-encode-string response t)))
(smtpmail-send-command process (format "%s" encoded))
(if (or (null (car (setq ret (smtpmail-read-response process))))
(not (integerp (car ret)))
(>= (car ret) 400))
(throw 'done nil)))))
((eq mech 'login)
(smtpmail-send-command process "AUTH LOGIN")
(if (or (null (car (setq ret (smtpmail-read-response process))))
(not (integerp (car ret)))
(>= (car ret) 400))
(throw 'done nil))
(smtpmail-send-command
process (base64-encode-string (smtpmail-cred-user cred) t))
(if (or (null (car (setq ret (smtpmail-read-response process))))
(not (integerp (car ret)))
(>= (car ret) 400))
(throw 'done nil))
(smtpmail-send-command process (base64-encode-string passwd t))
(if (or (null (car (setq ret (smtpmail-read-response process))))
(not (integerp (car ret)))
(>= (car ret) 400))
(throw 'done nil)))
((eq mech 'plain)
;; We used to send an empty initial request, and wait for an
;; empty response, and then send the password, but this
;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
;; is not sent if the server did not advertise AUTH PLAIN in
;; the EHLO response. See RFC 2554 for more info.
(smtpmail-send-command process
(concat "AUTH PLAIN "
(base64-encode-string
(concat "\0"
(smtpmail-cred-user cred)
"\0"
passwd) t)))
(if (or (null (car (setq ret (smtpmail-read-response process))))
(not (integerp (car ret)))
(not (equal (car ret) 235)))
(throw 'done nil)))
(t
(error "Mechanism %s not implemented" mech)))
;; Remember the password.
(when (and (not (stringp smtpmail-auth-credentials))
(null (smtpmail-cred-passwd cred)))
(setcar (cdr (cdr (cdr cred))) passwd)))))
(defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
(let ((process nil)
(host (or smtpmail-smtp-server
(error "`smtpmail-smtp-server' not defined")))
(port smtpmail-smtp-service)
;; smtpmail-mail-address should be set to the appropriate
;; buffer-local value by the caller, but in case not:
(envelope-from (or smtpmail-mail-address
(and mail-specify-envelope-from
(mail-envelope-from))
user-mail-address))
response-code
greeting
process-buffer
(supported-extensions '()))
(unwind-protect
(catch 'done
;; get or create the trace buffer
(setq process-buffer
(get-buffer-create (format "*trace of SMTP session to %s*" host)))
;; clear the trace buffer of old output
(with-current-buffer process-buffer
(setq buffer-undo-list t)
(erase-buffer))
;; open the connection to the server
(setq process (smtpmail-open-stream process-buffer host port))
(and (null process) (throw 'done nil))
;; set the send-filter
(set-process-filter process 'smtpmail-process-filter)
(with-current-buffer process-buffer
(set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
(make-local-variable 'smtpmail-read-point)
(setq smtpmail-read-point (point-min))
(if (or (null (car (setq greeting (smtpmail-read-response process))))
(not (integerp (car greeting)))
(>= (car greeting) 400))
(throw 'done nil)
)
(let ((do-ehlo t)
(do-starttls t))
(while do-ehlo
;; EHLO
(smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
(if (or (null (car (setq response-code
(smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(progn
;; HELO
(smtpmail-send-command
process (format "HELO %s" (smtpmail-fqdn)))
(if (or (null (car (setq response-code
(smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil)))
(dolist (line (cdr (cdr response-code)))
(let ((name
(with-case-table ascii-case-table
(mapcar (lambda (s) (intern (downcase s)))
(split-string (substring line 4) "[ ]")))))
(and (eq (length name) 1)
(setq name (car name)))
(and name
(cond ((memq (if (consp name) (car name) name)
'(verb xvrb 8bitmime onex xone
expn size dsn etrn
enhancedstatuscodes
help xusr
auth=login auth starttls))
(setq supported-extensions
(cons name supported-extensions)))
(smtpmail-warn-about-unknown-extensions
(message "Unknown extension %s" name)))))))
(if (and do-starttls
(smtpmail-find-credentials smtpmail-starttls-credentials host port)
(member 'starttls supported-extensions)
(numberp (process-id process)))
(progn
(smtpmail-send-command process (format "STARTTLS"))
(if (or (null (car (setq response-code (smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil))
(starttls-negotiate process)
(setq do-starttls nil))
(setq do-ehlo nil))))
(smtpmail-try-auth-methods process supported-extensions host port)
(if (or (member 'onex supported-extensions)
(member 'xone supported-extensions))
(progn
(smtpmail-send-command process (format "ONEX"))
(if (or (null (car (setq response-code (smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil))))
(if (and smtpmail-debug-verb
(or (member 'verb supported-extensions)
(member 'xvrb supported-extensions)))
(progn
(smtpmail-send-command process (format "VERB"))
(if (or (null (car (setq response-code (smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil))))
(if (member 'xusr supported-extensions)
(progn
(smtpmail-send-command process (format "XUSR"))
(if (or (null (car (setq response-code (smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil))))
;; MAIL FROM:<sender>
(let ((size-part
(if (or (member 'size supported-extensions)
(assoc 'size supported-extensions))
(format " SIZE=%d"
(with-current-buffer smtpmail-text-buffer
;; size estimate:
(+ (- (point-max) (point-min))
;; Add one byte for each change-of-line
;; because of CR-LF representation:
(count-lines (point-min) (point-max)))))
""))
(body-part
(if (member '8bitmime supported-extensions)
;; FIXME:
;; Code should be added here that transforms
;; the contents of the message buffer into
;; something the receiving SMTP can handle.
;; For a receiver that supports 8BITMIME, this
;; may mean converting BINARY to BASE64, or
;; adding Content-Transfer-Encoding and the
;; other MIME headers. The code should also
;; return an indication of what encoding the
;; message buffer is now, i.e. ASCII or
;; 8BITMIME.
(if nil
" BODY=8BITMIME"
"")
"")))
; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
(smtpmail-send-command process (format "MAIL FROM:<%s>%s%s"
envelope-from
size-part
body-part))
(if (or (null (car (setq response-code (smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil)
))
;; RCPT TO:<recipient>
(let ((n 0))
(while (not (null (nth n recipient)))
(smtpmail-send-command process (format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient))))
(setq n (1+ n))
(setq response-code (smtpmail-read-response process))
(if (or (null (car response-code))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil)
)
))
;; DATA
(smtpmail-send-command process "DATA")
(if (or (null (car (setq response-code (smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil)
)
;; Mail contents
(smtpmail-send-data process smtpmail-text-buffer)
;;DATA end "."
(smtpmail-send-command process ".")
(if (or (null (car (setq response-code (smtpmail-read-response process))))
(not (integerp (car response-code)))
(>= (car response-code) 400))
(throw 'done nil)
)
;;QUIT
; (smtpmail-send-command process "QUIT")
; (and (null (car (smtpmail-read-response process)))
; (throw 'done nil))
t ))
(if process
(with-current-buffer (process-buffer process)
(smtpmail-send-command process "QUIT")
(smtpmail-read-response process)
; (if (or (null (car (setq response-code (smtpmail-read-response process))))
; (not (integerp (car response-code)))
; (>= (car response-code) 400))
; (throw 'done nil)
; )
(delete-process process)
(unless smtpmail-debug-info
(kill-buffer process-buffer)))))))
(defun smtpmail-process-filter (process output)
(with-current-buffer (process-buffer process)
(goto-char (point-max))
(insert output)))
(defun smtpmail-read-response (process)
(let ((case-fold-search nil)
(response-strings nil)
(response-continue t)
(return-value '(nil ()))
match-end)
(catch 'done
(while response-continue
(goto-char smtpmail-read-point)
(while (not (search-forward "\r\n" nil t))
(unless (memq (process-status process) '(open run))
(throw 'done nil))
(accept-process-output process)
(goto-char smtpmail-read-point))
(setq match-end (point))
(setq response-strings
(cons (buffer-substring smtpmail-read-point (- match-end 2))
response-strings))
(goto-char smtpmail-read-point)
(if (looking-at "[0-9]+ ")
(let ((begin (match-beginning 0))
(end (match-end 0)))
(if smtpmail-debug-info
(message "%s" (car response-strings)))
(setq smtpmail-read-point match-end)
;; ignore lines that start with "0"
(if (looking-at "0[0-9]+ ")
nil
(setq response-continue nil)
(setq return-value
(cons (string-to-number
(buffer-substring begin end))
(nreverse response-strings)))))
(if (looking-at "[0-9]+-")
(progn (if smtpmail-debug-info
(message "%s" (car response-strings)))
(setq smtpmail-read-point match-end)
(setq response-continue t))
(progn
(setq smtpmail-read-point match-end)
(setq response-continue nil)
(setq return-value
(cons nil (nreverse response-strings)))))))
(setq smtpmail-read-point match-end))
return-value))
(defun smtpmail-send-command (process command)
(goto-char (point-max))
(if (= (aref command 0) ?P)
(insert "PASS <omitted>\r\n")
(insert command "\r\n"))
(setq smtpmail-read-point (point))
(process-send-string process command)
(process-send-string process "\r\n"))
(defun smtpmail-send-data-1 (process data)
(goto-char (point-max))
(if (and (multibyte-string-p data)
smtpmail-code-conv-from)
(setq data (string-as-multibyte
(encode-coding-string data smtpmail-code-conv-from))))
(if smtpmail-debug-info
(insert data "\r\n"))
(setq smtpmail-read-point (point))
;; Escape "." at start of a line
(if (eq (string-to-char data) ?.)
(process-send-string process "."))
(process-send-string process data)
(process-send-string process "\r\n")
)
(defun smtpmail-send-data (process buffer)
(let ((data-continue t) sending-data)
(with-current-buffer buffer
(goto-char (point-min)))
(while data-continue
(with-current-buffer buffer
(setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
(end-of-line 2)
(setq data-continue (not (eobp))))
(smtpmail-send-data-1 process sending-data))))
(defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
"Get address list suitable for smtp RCPT TO: <address>."
(unwind-protect
(with-current-buffer smtpmail-address-buffer
(erase-buffer)
(let
((case-fold-search t)
(simple-address-list "")
this-line
this-line-end
addr-regexp)
(insert-buffer-substring smtpmail-text-buffer header-start header-end)
(goto-char (point-min))
;; RESENT-* fields should stop processing of regular fields.
(save-excursion
(setq addr-regexp
(if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
header-end t)
"^Resent-\\(to\\|cc\\|bcc\\):"
"^\\(To:\\|Cc:\\|Bcc:\\)")))
(while (re-search-forward addr-regexp header-end t)
(replace-match "")
(setq this-line (match-beginning 0))
(forward-line 1)
;; get any continuation lines
(while (and (looking-at "^[ \t]+") (< (point) header-end))
(forward-line 1))
(setq this-line-end (point-marker))
(setq simple-address-list
(concat simple-address-list " "
(mail-strip-quoted-names (buffer-substring this-line this-line-end))))
)
(erase-buffer)
(insert " " simple-address-list "\n")
(subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
(subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
(subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
(goto-char (point-min))
;; tidyness in case hook is not robust when it looks at this
(while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
(goto-char (point-min))
(let (recipient-address-list)
(while (re-search-forward " \\([^ ]+\\) " (point-max) t)
(backward-char 1)
(setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
recipient-address-list))
)
(setq smtpmail-recipient-address-list recipient-address-list))
)
)
)
)
(defun smtpmail-do-bcc (header-end)
"Delete [Resent-]BCC: and their continuation lines from the header area.
There may be multiple BCC: lines, and each may have arbitrarily
many continuation lines."
(let ((case-fold-search t))
(save-excursion
(goto-char (point-min))
;; iterate over all BCC: lines
(while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
(delete-region (match-beginning 0)
(progn (forward-line 1) (point)))
;; get rid of any continuation lines
(while (and (looking-at "^[ \t].*\n") (< (point) header-end))
(replace-match ""))))))
(provide 'smtpmail)
;;; arch-tag: a76992df-6d71-43b7-9e72-4bacc6c05466
;;; smtpmail.el ends here
author | Bastien Guerry <bzg@altern.org> |
---|---|
date | Wed, 13 Feb 2008 20:58:26 +0000 |
parents | 107ccd98fa12 |
children | 606f2d163a64 1e3a407766b9 |
line wrap: on
line source
;;; calc-funcs.el --- well-known functions for Calc ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004, ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. ;; Author: David Gillespie <daveg@synaptics.com> ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com> ;; This file is part of GNU Emacs. ;; GNU Emacs 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. ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;;; Code: ;; This file is autoloaded from calc-ext.el. (require 'calc-ext) (require 'calc-macs) (defun calc-inc-gamma (arg) (interactive "P") (calc-slow-wrapper (if (calc-is-inverse) (if (calc-is-hyperbolic) (calc-binary-op "gamG" 'calcFunc-gammaG arg) (calc-binary-op "gamQ" 'calcFunc-gammaQ arg)) (if (calc-is-hyperbolic) (calc-binary-op "gamg" 'calcFunc-gammag arg) (calc-binary-op "gamP" 'calcFunc-gammaP arg))))) (defun calc-erf (arg) (interactive "P") (calc-slow-wrapper (if (calc-is-inverse) (calc-unary-op "erfc" 'calcFunc-erfc arg) (calc-unary-op "erf" 'calcFunc-erf arg)))) (defun calc-erfc (arg) (interactive "P") (calc-invert-func) (calc-erf arg)) (defun calc-beta (arg) (interactive "P") (calc-slow-wrapper (calc-binary-op "beta" 'calcFunc-beta arg))) (defun calc-inc-beta () (interactive) (calc-slow-wrapper (if (calc-is-hyperbolic) (calc-enter-result 3 "betB" (cons 'calcFunc-betaB (calc-top-list-n 3))) (calc-enter-result 3 "betI" (cons 'calcFunc-betaI (calc-top-list-n 3)))))) (defun calc-bessel-J (arg) (interactive "P") (calc-slow-wrapper (calc-binary-op "besJ" 'calcFunc-besJ arg))) (defun calc-bessel-Y (arg) (interactive "P") (calc-slow-wrapper (calc-binary-op "besY" 'calcFunc-besY arg))) (defun calc-bernoulli-number (arg) (interactive "P") (calc-slow-wrapper (if (calc-is-hyperbolic) (calc-binary-op "bern" 'calcFunc-bern arg) (calc-unary-op "bern" 'calcFunc-bern arg)))) (defun calc-euler-number (arg) (interactive "P") (calc-slow-wrapper (if (calc-is-hyperbolic) (calc-binary-op "eulr" 'calcFunc-euler arg) (calc-unary-op "eulr" 'calcFunc-euler arg)))) (defun calc-stirling-number (arg) (interactive "P") (calc-slow-wrapper (if (calc-is-hyperbolic) (calc-binary-op "str2" 'calcFunc-stir2 arg) (calc-binary-op "str1" 'calcFunc-stir1 arg)))) (defun calc-utpb () (interactive) (calc-prob-dist "b" 3)) (defun calc-utpc () (interactive) (calc-prob-dist "c" 2)) (defun calc-utpf () (interactive) (calc-prob-dist "f" 3)) (defun calc-utpn () (interactive) (calc-prob-dist "n" 3)) (defun calc-utpp () (interactive) (calc-prob-dist "p" 2)) (defun calc-utpt () (interactive) (calc-prob-dist "t" 2)) (defun calc-prob-dist (letter nargs) (calc-slow-wrapper (if (calc-is-inverse) (calc-enter-result nargs (concat "ltp" letter) (append (list (intern (concat "calcFunc-ltp" letter)) (calc-top-n 1)) (calc-top-list-n (1- nargs) 2))) (calc-enter-result nargs (concat "utp" letter) (append (list (intern (concat "calcFunc-utp" letter)) (calc-top-n 1)) (calc-top-list-n (1- nargs) 2)))))) ;;; Sources: Numerical Recipes, Press et al; ;;; Handbook of Mathematical Functions, Abramowitz & Stegun. ;;; Gamma function. (defun calcFunc-gamma (x) (or (math-numberp x) (math-reject-arg x 'numberp)) (calcFunc-fact (math-add x -1))) (defun math-gammap1-raw (x &optional fprec nfprec) "Compute gamma(1+X) to the appropriate precision." (or fprec (setq fprec (math-float calc-internal-prec) nfprec (math-float (- calc-internal-prec)))) (cond ((math-lessp-float (calcFunc-re x) fprec) (if (math-lessp-float (calcFunc-re x) nfprec) (math-neg (math-div (math-pi) (math-mul (math-gammap1-raw (math-add (math-neg x) '(float -1 0)) fprec nfprec) (math-sin-raw (math-mul (math-pi) x))))) (let ((xplus1 (math-add x '(float 1 0)))) (math-div (math-gammap1-raw xplus1 fprec nfprec) xplus1)))) ((and (math-realp x) (math-lessp-float '(float 736276 0) x)) (math-overflow)) (t ; re(x) now >= 10.0 (let ((xinv (math-div 1 x)) (lnx (math-ln-raw x))) (math-mul (math-sqrt-two-pi) (math-exp-raw (math-gamma-series (math-sub (math-mul (math-add x '(float 5 -1)) lnx) x) xinv (math-sqr xinv) '(float 0 0) 2))))))) (defun math-gamma-series (sum x xinvsqr oterm n) (math-working "gamma" sum) (let* ((bn (math-bernoulli-number n)) (term (math-mul (math-div-float (math-float (nth 1 bn)) (math-float (* (nth 2 bn) (* n (1- n))))) x)) (next (math-add sum term))) (if (math-nearly-equal sum next) next (if (> n (* 2 calc-internal-prec)) (progn ;; Need this because series eventually diverges for large enough n. (calc-record-why "*Gamma computation stopped early, not all digits may be valid") next) (math-gamma-series next (math-mul x xinvsqr) xinvsqr term (+ n 2)))))) ;;; Incomplete gamma function. (defvar math-current-gamma-value nil) (defun calcFunc-gammaP (a x) (if (equal x '(var inf var-inf)) '(float 1 0) (math-inexact-result) (or (Math-numberp a) (math-reject-arg a 'numberp)) (or (math-numberp x) (math-reject-arg x 'numberp)) (if (and (math-num-integerp a) (integerp (setq a (math-trunc a))) (> a 0) (< a 20)) (math-sub 1 (calcFunc-gammaQ a x)) (let ((math-current-gamma-value (calcFunc-gamma a))) (math-div (calcFunc-gammag a x) math-current-gamma-value))))) (defun calcFunc-gammaQ (a x) (if (equal x '(var inf var-inf)) '(float 0 0) (math-inexact-result) (or (Math-numberp a) (math-reject-arg a 'numberp)) (or (math-numberp x) (math-reject-arg x 'numberp)) (if (and (math-num-integerp a) (integerp (setq a (math-trunc a))) (> a 0) (< a 20)) (let ((n 0) (sum '(float 1 0)) (term '(float 1 0))) (math-with-extra-prec 1 (while (< (setq n (1+ n)) a) (setq term (math-div (math-mul term x) n) sum (math-add sum term)) (math-working "gamma" sum)) (math-mul sum (calcFunc-exp (math-neg x))))) (let ((math-current-gamma-value (calcFunc-gamma a))) (math-div (calcFunc-gammaG a x) math-current-gamma-value))))) (defun calcFunc-gammag (a x) (if (equal x '(var inf var-inf)) (calcFunc-gamma a) (math-inexact-result) (or (Math-numberp a) (math-reject-arg a 'numberp)) (or (Math-numberp x) (math-reject-arg x 'numberp)) (math-with-extra-prec 2 (setq a (math-float a)) (setq x (math-float x)) (if (or (math-negp (calcFunc-re a)) (math-lessp-float (calcFunc-re x) (math-add-float (calcFunc-re a) '(float 1 0)))) (math-inc-gamma-series a x) (math-sub (or math-current-gamma-value (calcFunc-gamma a)) (math-inc-gamma-cfrac a x)))))) (defun calcFunc-gammaG (a x) (if (equal x '(var inf var-inf)) '(float 0 0) (math-inexact-result) (or (Math-numberp a) (math-reject-arg a 'numberp)) (or (Math-numberp x) (math-reject-arg x 'numberp)) (math-with-extra-prec 2 (setq a (math-float a)) (setq x (math-float x)) (if (or (math-negp (calcFunc-re a)) (math-lessp-float (calcFunc-re x) (math-add-float (math-abs-approx a) '(float 1 0)))) (math-sub (or math-current-gamma-value (calcFunc-gamma a)) (math-inc-gamma-series a x)) (math-inc-gamma-cfrac a x))))) (defun math-inc-gamma-series (a x) (if (Math-zerop x) '(float 0 0) (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x)) (math-with-extra-prec 2 (let ((start (math-div '(float 1 0) a))) (math-inc-gamma-series-step start start a x)))))) (defun math-inc-gamma-series-step (sum term a x) (math-working "gamma" sum) (setq a (math-add a '(float 1 0)) term (math-div (math-mul term x) a)) (let ((next (math-add sum term))) (if (math-nearly-equal sum next) next (math-inc-gamma-series-step next term a x)))) (defun math-inc-gamma-cfrac (a x) (if (Math-zerop x) (or math-current-gamma-value (calcFunc-gamma a)) (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x)) (math-inc-gamma-cfrac-step '(float 1 0) x '(float 0 0) '(float 1 0) '(float 1 0) '(float 1 0) '(float 0 0) a x)))) (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x) (let ((ana (math-sub n a)) (anf (math-mul n fac))) (setq n (math-add n '(float 1 0)) a0 (math-mul (math-add a1 (math-mul a0 ana)) fac) b0 (math-mul (math-add b1 (math-mul b0 ana)) fac) a1 (math-add (math-mul x a0) (math-mul anf a1)) b1 (math-add (math-mul x b0) (math-mul anf b1))) (if (math-zerop a1) (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x) (setq fac (math-div '(float 1 0) a1)) (let ((next (math-mul b1 fac))) (math-working "gamma" next) (if (math-nearly-equal next g) next (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x)))))) ;;; Error function. (defun calcFunc-erf (x) (if (equal x '(var inf var-inf)) '(float 1 0) (if (equal x '(neg (var inf var-inf))) '(float -1 0) (if (Math-zerop x) x (let ((math-current-gamma-value (math-sqrt-pi))) (math-to-same-complex-quad (math-div (calcFunc-gammag '(float 5 -1) (math-sqr (math-to-complex-quad-one x))) math-current-gamma-value) x)))))) (defun calcFunc-erfc (x) (if (equal x '(var inf var-inf)) '(float 0 0) (if (math-posp x) (let ((math-current-gamma-value (math-sqrt-pi))) (math-div (calcFunc-gammaG '(float 5 -1) (math-sqr x)) math-current-gamma-value)) (math-sub 1 (calcFunc-erf x))))) (defun math-to-complex-quad-one (x) (if (eq (car-safe x) 'polar) (setq x (math-complex x))) (if (eq (car-safe x) 'cplx) (list 'cplx (math-abs (nth 1 x)) (math-abs (nth 2 x))) x)) (defun math-to-same-complex-quad (x y) (if (eq (car-safe y) 'cplx) (if (eq (car-safe x) 'cplx) (list 'cplx (if (math-negp (nth 1 y)) (math-neg (nth 1 x)) (nth 1 x)) (if (math-negp (nth 2 y)) (math-neg (nth 2 x)) (nth 2 x))) (if (math-negp (nth 1 y)) (math-neg x) x)) (if (math-negp y) (if (eq (car-safe x) 'cplx) (list 'cplx (math-neg (nth 1 x)) (nth 2 x)) (math-neg x)) x))) ;;; Beta function. (defun calcFunc-beta (a b) (if (math-num-integerp a) (let ((am (math-add a -1))) (or (math-numberp b) (math-reject-arg b 'numberp)) (math-div 1 (math-mul b (calcFunc-choose (math-add b am) am)))) (if (math-num-integerp b) (calcFunc-beta b a) (math-div (math-mul (calcFunc-gamma a) (calcFunc-gamma b)) (calcFunc-gamma (math-add a b)))))) ;;; Incomplete beta function. (defvar math-current-beta-value nil) (defun calcFunc-betaI (x a b) (cond ((math-zerop x) '(float 0 0)) ((math-equal-int x 1) '(float 1 0)) ((or (math-zerop a) (and (math-num-integerp a) (math-negp a))) (if (or (math-zerop b) (and (math-num-integerp b) (math-negp b))) (math-reject-arg b 'range) '(float 1 0))) ((or (math-zerop b) (and (math-num-integerp b) (math-negp b))) '(float 0 0)) ((not (math-numberp a)) (math-reject-arg a 'numberp)) ((not (math-numberp b)) (math-reject-arg b 'numberp)) ((math-inexact-result)) (t (let ((math-current-beta-value (calcFunc-beta a b))) (math-div (calcFunc-betaB x a b) math-current-beta-value))))) (defun calcFunc-betaB (x a b) (cond ((math-zerop x) '(float 0 0)) ((math-equal-int x 1) (calcFunc-beta a b)) ((not (math-numberp x)) (math-reject-arg x 'numberp)) ((not (math-numberp a)) (math-reject-arg a 'numberp)) ((not (math-numberp b)) (math-reject-arg b 'numberp)) ((math-zerop a) (math-reject-arg a 'nonzerop)) ((math-zerop b) (math-reject-arg b 'nonzerop)) ((and (math-num-integerp b) (if (math-negp b) (math-reject-arg b 'range) (Math-natnum-lessp (setq b (math-trunc b)) 20))) (and calc-symbolic-mode (or (math-floatp a) (math-floatp b)) (math-inexact-result)) (math-mul (math-with-extra-prec 2 (let* ((i 0) (term 1) (sum (math-div term a))) (while (< (setq i (1+ i)) b) (setq term (math-mul (math-div (math-mul term (- i b)) i) x) sum (math-add sum (math-div term (math-add a i)))) (math-working "beta" sum)) sum)) (math-pow x a))) ((and (math-num-integerp a) (if (math-negp a) (math-reject-arg a 'range) (Math-natnum-lessp (setq a (math-trunc a)) 20))) (math-sub (or math-current-beta-value (calcFunc-beta a b)) (calcFunc-betaB (math-sub 1 x) b a))) (t (math-inexact-result) (math-with-extra-prec 2 (setq x (math-float x)) (setq a (math-float a)) (setq b (math-float b)) (let ((bt (math-exp-raw (math-add (math-mul a (math-ln-raw x)) (math-mul b (math-ln-raw (math-sub '(float 1 0) x))))))) (if (Math-lessp x (math-div (math-add a '(float 1 0)) (math-add (math-add a b) '(float 2 0)))) (math-div (math-mul bt (math-beta-cfrac a b x)) a) (math-sub (or math-current-beta-value (calcFunc-beta a b)) (math-div (math-mul bt (math-beta-cfrac b a (math-sub 1 x))) b)))))))) (defun math-beta-cfrac (a b x) (let ((qab (math-add a b)) (qap (math-add a '(float 1 0))) (qam (math-add a '(float -1 0)))) (math-beta-cfrac-step '(float 1 0) (math-sub '(float 1 0) (math-div (math-mul qab x) qap)) '(float 1 0) '(float 1 0) '(float 1 0) qab qap qam a b x))) (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x) (let* ((two-m (math-mul m '(float 2 0))) (d (math-div (math-mul (math-mul (math-sub b m) m) x) (math-mul (math-add qam two-m) (math-add a two-m)))) (ap (math-add az (math-mul d am))) (bp (math-add bz (math-mul d bm))) (d2 (math-neg (math-div (math-mul (math-mul (math-add a m) (math-add qab m)) x) (math-mul (math-add qap two-m) (math-add a two-m))))) (app (math-add ap (math-mul d2 az))) (bpp (math-add bp (math-mul d2 bz))) (next (math-div app bpp))) (math-working "beta" next) (if (math-nearly-equal next az) next (math-beta-cfrac-step next '(float 1 0) (math-div ap bpp) (math-div bp bpp) (math-add m '(float 1 0)) qab qap qam a b x)))) ;;; Bessel functions. ;;; Should generalize this to handle arbitrary precision! (defun calcFunc-besJ (v x) (or (math-numberp v) (math-reject-arg v 'numberp)) (or (math-numberp x) (math-reject-arg x 'numberp)) (let ((calc-internal-prec (min 8 calc-internal-prec))) (math-with-extra-prec 3 (setq x (math-float (math-normalize x))) (setq v (math-float (math-normalize v))) (cond ((math-zerop x) (if (math-zerop v) '(float 1 0) '(float 0 0))) ((math-inexact-result)) ((not (math-num-integerp v)) (let ((start (math-div 1 (calcFunc-fact v)))) (math-mul (math-besJ-series start start 0 (math-mul '(float -25 -2) (math-sqr x)) v) (math-pow (math-div x 2) v)))) ((math-negp (setq v (math-trunc v))) (if (math-oddp v) (math-neg (calcFunc-besJ (math-neg v) x)) (calcFunc-besJ (math-neg v) x))) ((eq v 0) (math-besJ0 x)) ((eq v 1) (math-besJ1 x)) ((Math-lessp v (math-abs-approx x)) (let ((j 0) (bjm (math-besJ0 x)) (bj (math-besJ1 x)) (two-over-x (math-div 2 x)) bjp) (while (< (setq j (1+ j)) v) (setq bjp (math-sub (math-mul (math-mul j two-over-x) bj) bjm) bjm bj bj bjp)) bj)) (t (if (Math-lessp 100 v) (math-reject-arg v 'range)) (let* ((j (logior (+ v (math-isqrt-small (* 40 v))) 1)) (two-over-x (math-div 2 x)) (jsum nil) (bjp '(float 0 0)) (sum '(float 0 0)) (bj '(float 1 0)) bjm ans) (while (> (setq j (1- j)) 0) (setq bjm (math-sub (math-mul (math-mul j two-over-x) bj) bjp) bjp bj bj bjm) (if (> (nth 2 (math-abs-approx bj)) 10) (setq bj (math-mul bj '(float 1 -10)) bjp (math-mul bjp '(float 1 -10)) ans (and ans (math-mul ans '(float 1 -10))) sum (math-mul sum '(float 1 -10)))) (or (setq jsum (not jsum)) (setq sum (math-add sum bj))) (if (= j v) (setq ans bjp))) (math-div ans (math-sub (math-mul 2 sum) bj)))))))) (defun math-besJ-series (sum term k zz vk) (math-working "besJ" sum) (setq k (1+ k) vk (math-add 1 vk) term (math-div (math-mul term zz) (math-mul k vk))) (let ((next (math-add sum term))) (if (math-nearly-equal next sum) next (math-besJ-series next term k zz vk)))) (defun math-besJ0 (x &optional yflag) (cond ((and (not yflag) (math-negp (calcFunc-re x))) (math-besJ0 (math-neg x))) ((Math-lessp '(float 8 0) (math-abs-approx x)) (let* ((z (math-div '(float 8 0) x)) (y (math-sqr z)) (xx (math-add x (math-read-number-simple "-0.785398164"))) (a1 (math-poly-eval y (list (math-read-number-simple "0.0000002093887211") (math-read-number-simple "-0.000002073370639") (math-read-number-simple "0.00002734510407") (math-read-number-simple "-0.001098628627") '(float 1 0)))) (a2 (math-poly-eval y (list (math-read-number-simple "-0.0000000934935152") (math-read-number-simple "0.0000007621095161") (math-read-number-simple "-0.000006911147651") (math-read-number-simple "0.0001430488765") (math-read-number-simple "-0.01562499995")))) (sc (math-sin-cos-raw xx))) (if yflag (setq sc (cons (math-neg (cdr sc)) (car sc)))) (math-mul (math-sqrt (math-div (math-read-number-simple "0.636619722") x)) (math-sub (math-mul (cdr sc) a1) (math-mul (car sc) (math-mul z a2)))))) (t (let ((y (math-sqr x))) (math-div (math-poly-eval y (list (math-read-number-simple "-184.9052456") (math-read-number-simple "77392.33017") (math-read-number-simple "-11214424.18") (math-read-number-simple "651619640.7") (math-read-number-simple "-13362590354.0") (math-read-number-simple "57568490574.0"))) (math-poly-eval y (list '(float 1 0) (math-read-number-simple "267.8532712") (math-read-number-simple "59272.64853") (math-read-number-simple "9494680.718") (math-read-number-simple "1029532985.0") (math-read-number-simple "57568490411.0")))))))) (defun math-besJ1 (x &optional yflag) (cond ((and (math-negp (calcFunc-re x)) (not yflag)) (math-neg (math-besJ1 (math-neg x)))) ((Math-lessp '(float 8 0) (math-abs-approx x)) (let* ((z (math-div '(float 8 0) x)) (y (math-sqr z)) (xx (math-add x (math-read-number-simple "-2.356194491"))) (a1 (math-poly-eval y (list (math-read-number-simple "-0.000000240337019") (math-read-number-simple "0.000002457520174") (math-read-number-simple "-0.00003516396496") '(float 183105 -8) '(float 1 0)))) (a2 (math-poly-eval y (list (math-read-number-simple "0.000000105787412") (math-read-number-simple "-0.00000088228987") (math-read-number-simple "0.000008449199096") (math-read-number-simple "-0.0002002690873") (math-read-number-simple "0.04687499995")))) (sc (math-sin-cos-raw xx))) (if yflag (setq sc (cons (math-neg (cdr sc)) (car sc))) (if (math-negp x) (setq sc (cons (math-neg (car sc)) (math-neg (cdr sc)))))) (math-mul (math-sqrt (math-div (math-read-number-simple "0.636619722") x)) (math-sub (math-mul (cdr sc) a1) (math-mul (car sc) (math-mul z a2)))))) (t (let ((y (math-sqr x))) (math-mul x (math-div (math-poly-eval y (list (math-read-number-simple "-30.16036606") (math-read-number-simple "15704.4826") (math-read-number-simple "-2972611.439") (math-read-number-simple "242396853.1") (math-read-number-simple "-7895059235.0") (math-read-number-simple "72362614232.0"))) (math-poly-eval y (list '(float 1 0) (math-read-number-simple "376.9991397") (math-read-number-simple "99447.43394") (math-read-number-simple "18583304.74") (math-read-number-simple "2300535178.0") (math-read-number-simple "144725228442.0"))))))))) (defun calcFunc-besY (v x) (math-inexact-result) (or (math-numberp v) (math-reject-arg v 'numberp)) (or (math-numberp x) (math-reject-arg x 'numberp)) (let ((calc-internal-prec (min 8 calc-internal-prec))) (math-with-extra-prec 3 (setq x (math-float (math-normalize x))) (setq v (math-float (math-normalize v))) (cond ((not (math-num-integerp v)) (let ((sc (math-sin-cos-raw (math-mul v (math-pi))))) (math-div (math-sub (math-mul (calcFunc-besJ v x) (cdr sc)) (calcFunc-besJ (math-neg v) x)) (car sc)))) ((math-negp (setq v (math-trunc v))) (if (math-oddp v) (math-neg (calcFunc-besY (math-neg v) x)) (calcFunc-besY (math-neg v) x))) ((eq v 0) (math-besY0 x)) ((eq v 1) (math-besY1 x)) (t (let ((j 0) (bym (math-besY0 x)) (by (math-besY1 x)) (two-over-x (math-div 2 x)) byp) (while (< (setq j (1+ j)) v) (setq byp (math-sub (math-mul (math-mul j two-over-x) by) bym) bym by by byp)) by)))))) (defun math-besY0 (x) (cond ((Math-lessp (math-abs-approx x) '(float 8 0)) (let ((y (math-sqr x))) (math-add (math-div (math-poly-eval y (list (math-read-number-simple "228.4622733") (math-read-number-simple "-86327.92757") (math-read-number-simple "10879881.29") (math-read-number-simple "-512359803.6") (math-read-number-simple "7062834065.0") (math-read-number-simple "-2957821389.0"))) (math-poly-eval y (list '(float 1 0) (math-read-number-simple "226.1030244") (math-read-number-simple "47447.2647") (math-read-number-simple "7189466.438") (math-read-number-simple "745249964.8") (math-read-number-simple "40076544269.0")))) (math-mul (math-read-number-simple "0.636619772") (math-mul (math-besJ0 x) (math-ln-raw x)))))) ((math-negp (calcFunc-re x)) (math-add (math-besJ0 (math-neg x) t) (math-mul '(cplx 0 2) (math-besJ0 (math-neg x))))) (t (math-besJ0 x t)))) (defun math-besY1 (x) (cond ((Math-lessp (math-abs-approx x) '(float 8 0)) (let ((y (math-sqr x))) (math-add (math-mul x (math-div (math-poly-eval y (list (math-read-number-simple "8511.937935") (math-read-number-simple "-4237922.726") (math-read-number-simple "734926455.1") (math-read-number-simple "-51534381390.0") (math-read-number-simple "1275274390000.0") (math-read-number-simple "-4900604943000.0"))) (math-poly-eval y (list '(float 1 0) (math-read-number-simple "354.9632885") (math-read-number-simple "102042.605") (math-read-number-simple "22459040.02") (math-read-number-simple "3733650367.0") (math-read-number-simple "424441966400.0") (math-read-number-simple "24995805700000.0"))))) (math-mul (math-read-number-simple "0.636619772") (math-sub (math-mul (math-besJ1 x) (math-ln-raw x)) (math-div 1 x)))))) ((math-negp (calcFunc-re x)) (math-neg (math-add (math-besJ1 (math-neg x) t) (math-mul '(cplx 0 2) (math-besJ1 (math-neg x)))))) (t (math-besJ1 x t)))) (defun math-poly-eval (x coefs) (let ((accum (car coefs))) (while (setq coefs (cdr coefs)) (setq accum (math-add (car coefs) (math-mul accum x)))) accum)) ;;;; Bernoulli and Euler polynomials and numbers. (defun calcFunc-bern (n &optional x) (if (and x (not (math-zerop x))) (if (and calc-symbolic-mode (math-floatp x)) (math-inexact-result) (math-build-polynomial-expr (math-bernoulli-coefs n) x)) (or (math-num-natnump n) (math-reject-arg n 'natnump)) (if (consp n) (progn (math-inexact-result) (math-float (math-bernoulli-number (math-trunc n)))) (math-bernoulli-number n)))) (defun calcFunc-euler (n &optional x) (or (math-num-natnump n) (math-reject-arg n 'natnump)) (if x (let* ((n1 (math-add n 1)) (coefs (math-bernoulli-coefs n1)) (fac (math-div (math-pow 2 n1) n1)) (k -1) (x1 (math-div (math-add x 1) 2)) (x2 (math-div x 2))) (if (math-numberp x) (if (and calc-symbolic-mode (math-floatp x)) (math-inexact-result) (math-mul fac (math-sub (math-build-polynomial-expr coefs x1) (math-build-polynomial-expr coefs x2)))) (calcFunc-collect (math-reduce-vec 'math-add (cons 'vec (mapcar (function (lambda (c) (setq k (1+ k)) (math-mul (math-mul fac c) (math-sub (math-pow x1 k) (math-pow x2 k))))) coefs))) x))) (math-mul (math-pow 2 n) (if (consp n) (progn (math-inexact-result) (calcFunc-euler n '(float 5 -1))) (calcFunc-euler n '(frac 1 2)))))) (defvar math-bernoulli-b-cache (list (list 'frac -174611 (math-read-number-simple "802857662698291200000")) (list 'frac 43867 (math-read-number-simple "5109094217170944000")) (list 'frac -3617 (math-read-number-simple "10670622842880000")) (list 'frac 1 (math-read-number-simple "74724249600")) (list 'frac -691 (math-read-number-simple "1307674368000")) (list 'frac 1 (math-read-number-simple "47900160")) (list 'frac -1 (math-read-number-simple "1209600")) (list 'frac 1 30240) (list 'frac -1 720) (list 'frac 1 12) 1 )) (defvar math-bernoulli-B-cache '((frac -174611 330) (frac 43867 798) (frac -3617 510) (frac 7 6) (frac -691 2730) (frac 5 66) (frac -1 30) (frac 1 42) (frac -1 30) (frac 1 6) 1 )) (defvar math-bernoulli-cache-size 11) (defun math-bernoulli-coefs (n) (let* ((coefs (list (calcFunc-bern n))) (nn (math-trunc n)) (k nn) (term nn) coef (calc-prefer-frac (or (integerp n) calc-prefer-frac))) (while (>= (setq k (1- k)) 0) (setq term (math-div term (- nn k)) coef (math-mul term (math-bernoulli-number k)) coefs (cons (if (consp n) (math-float coef) coef) coefs) term (math-mul term k))) (nreverse coefs))) (defun math-bernoulli-number (n) (if (= (% n 2) 1) (if (= n 1) '(frac -1 2) 0) (setq n (/ n 2)) (while (>= n math-bernoulli-cache-size) (let* ((sum 0) (nk 1) ; nk = n-k+1 (fact 1) ; fact = (n-k+1)! ofact (p math-bernoulli-b-cache) (calc-prefer-frac t)) (math-working "bernoulli B" (* 2 math-bernoulli-cache-size)) (while p (setq nk (+ nk 2) ofact fact fact (math-mul fact (* nk (1- nk))) sum (math-add sum (math-div (car p) fact)) p (cdr p))) (setq ofact (math-mul ofact (1- nk)) sum (math-sub (math-div '(frac 1 2) ofact) sum) math-bernoulli-b-cache (cons sum math-bernoulli-b-cache) math-bernoulli-B-cache (cons (math-mul sum ofact) math-bernoulli-B-cache) math-bernoulli-cache-size (1+ math-bernoulli-cache-size)))) (nth (- math-bernoulli-cache-size n 1) math-bernoulli-B-cache))) ;;; Bn = n! bn ;;; bn = - sum_k=0^n-1 bk / (n-k+1)! ;;; A faster method would be to use "tangent numbers", c.f., Concrete ;;; Mathematics pg. 273. ;;; Probability distributions. ;;; Binomial. (defun calcFunc-utpb (x n p) (if math-expand-formulas (math-normalize (list 'calcFunc-betaI p x (list '+ (list '- n x) 1))) (calcFunc-betaI p x (math-add (math-sub n x) 1)))) (put 'calcFunc-utpb 'math-expandable t) (defun calcFunc-ltpb (x n p) (math-sub 1 (calcFunc-utpb x n p))) (put 'calcFunc-ltpb 'math-expandable t) ;;; Chi-square. (defun calcFunc-utpc (chisq v) (if math-expand-formulas (math-normalize (list 'calcFunc-gammaQ (list '/ v 2) (list '/ chisq 2))) (calcFunc-gammaQ (math-div v 2) (math-div chisq 2)))) (put 'calcFunc-utpc 'math-expandable t) (defun calcFunc-ltpc (chisq v) (if math-expand-formulas (math-normalize (list 'calcFunc-gammaP (list '/ v 2) (list '/ chisq 2))) (calcFunc-gammaP (math-div v 2) (math-div chisq 2)))) (put 'calcFunc-ltpc 'math-expandable t) ;;; F-distribution. (defun calcFunc-utpf (f v1 v2) (if math-expand-formulas (math-normalize (list 'calcFunc-betaI (list '/ v2 (list '+ v2 (list '* v1 f))) (list '/ v2 2) (list '/ v1 2))) (calcFunc-betaI (math-div v2 (math-add v2 (math-mul v1 f))) (math-div v2 2) (math-div v1 2)))) (put 'calcFunc-utpf 'math-expandable t) (defun calcFunc-ltpf (f v1 v2) (math-sub 1 (calcFunc-utpf f v1 v2))) (put 'calcFunc-ltpf 'math-expandable t) ;;; Normal. (defun calcFunc-utpn (x mean sdev) (if math-expand-formulas (math-normalize (list '/ (list '+ 1 (list 'calcFunc-erf (list '/ (list '- mean x) (list '* sdev (list 'calcFunc-sqrt 2))))) 2)) (math-mul (math-add '(float 1 0) (calcFunc-erf (math-div (math-sub mean x) (math-mul sdev (math-sqrt-2))))) '(float 5 -1)))) (put 'calcFunc-utpn 'math-expandable t) (defun calcFunc-ltpn (x mean sdev) (if math-expand-formulas (math-normalize (list '/ (list '+ 1 (list 'calcFunc-erf (list '/ (list '- x mean) (list '* sdev (list 'calcFunc-sqrt 2))))) 2)) (math-mul (math-add '(float 1 0) (calcFunc-erf (math-div (math-sub x mean) (math-mul sdev (math-sqrt-2))))) '(float 5 -1)))) (put 'calcFunc-ltpn 'math-expandable t) ;;; Poisson. (defun calcFunc-utpp (n x) (if math-expand-formulas (math-normalize (list 'calcFunc-gammaP x n)) (calcFunc-gammaP x n))) (put 'calcFunc-utpp 'math-expandable t) (defun calcFunc-ltpp (n x) (if math-expand-formulas (math-normalize (list 'calcFunc-gammaQ x n)) (calcFunc-gammaQ x n))) (put 'calcFunc-ltpp 'math-expandable t) ;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.) (defun calcFunc-utpt (tt v) (if math-expand-formulas (math-normalize (list 'calcFunc-betaI (list '/ v (list '+ v (list '^ tt 2))) (list '/ v 2) '(float 5 -1))) (calcFunc-betaI (math-div v (math-add v (math-sqr tt))) (math-div v 2) '(float 5 -1)))) (put 'calcFunc-utpt 'math-expandable t) (defun calcFunc-ltpt (tt v) (math-sub 1 (calcFunc-utpt tt v))) (put 'calcFunc-ltpt 'math-expandable t) (provide 'calc-funcs) ;;; arch-tag: 421ddb7a-550f-4dda-a31c-06638ebfc43a ;;; calc-funcs.el ends here