w3mail

program to send a web page by email
git clone https://logand.com/git/w3mail.git/
Log | Files | Refs | README | LICENSE

commit f790e6436c15df6e4352da38818f85fb68042e27
parent e098ced796e2c048854e02e1d3154d60c6071e55
Author: Tomas Hlavaty <tom@logand.com>
Date:   Thu, 18 Nov 2010 00:17:37 +0100

config ~/.w3mail added

Diffstat:
MREADME | 53++++++++++++++++++++++++++++-------------------------
Mw3mail.c | 48++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 68 insertions(+), 33 deletions(-)

diff --git a/README b/README @@ -47,43 +47,46 @@ Switch to the new directory and make the w3mail executable: $ cd w3mail $ make -Invocation from shell -===================== +Configuration +============= + +The command-line arguments of w3mail are: [cmd from to host] url. -1) Create w3mail script in your ~/bin directory. +In case all arguments are supplied, no configuration is necessary. -2) Paste the following text into the created file ~/bin/w3mail +A configuration file ~/.w3mail needs to be created in case w3mail is +called with one argument only. The configuration file must contain +four lines, each line for one omited argument in the command-line +order. ---- start of ~/bin/w3mail --- -#!/bin/sh -SENDMAIL='ssh username@host.name -e none /usr/lib/sendmail -t' -FROM='email@address' -TO=$FROM -HOST='host.name' -$HOME/git/w3mail/w3mail "$SENDMAIL" "$FROM" "$TO" "$HOST" "$1" ---- end of ~/bin/w3mail --- +--- start of ~/.w3mail --- +ssh user.name@host.name -e none /usr/lib/sendmail -t +email@address +email@address +host.name +--- end of ~/.w3mail --- -This script defines some configuration parameters that are specific to -my set up. Change the user name, host name and email address to suit -your needs. You could define SENDMAIL='sendmail -t' if you have +Change the user name, host name and email addresses to suit your +needs. You could set the first line to "sendmail -t" if you have sendmail installed locally. In my case, I use ssh to my server and invoke sendmail there. -3) Set executable permissions: +It is convenient to put the w3mail program somewhere reachable from +$PATH, e.g. create a symlink to the w3mail executable file in your +~/bin directory or add the w3mail git directory into your $PATH. - $ chown +x ~/bin/w3mail - -4) Use it +Invocation from shell +===================== - a) Send single web page: +a) Send single web page: - $ ~/bin/w3mail 'http://logand.com/' + $ w3mail 'http://logand.com/' - b) Send many web pages: +b) Send many web pages: - First save the URLs into a file, one URL per line. Then run: + First save the URLs into a file, one URL per line. Then run: - $ cat file | xargs -n1 ~/bin/w3mail + $ cat file | xargs -n1 w3mail Using w3mail with Emacs ======================= @@ -93,7 +96,7 @@ Put the following emacs-lisp code into your ~/.emacs file: --- begin cut--- (defun w3mail (url &optional new-window) (interactive (browse-url-interactive-arg "URL: ")) - (shell-command (format "~/bin/w3mail '%s' &" (browse-url-encode-url url)))) + (shell-command (format "w3mail '%s' &" (browse-url-encode-url url)))) (defun w3m-w3mail (url) (interactive (list (w3m-input-url nil nil nil nil 'feeling-lucky))) diff --git a/w3mail.c b/w3mail.c @@ -16,6 +16,8 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <ctype.h> #include <stdarg.h> #include <errno.h> #include <unistd.h> @@ -26,6 +28,14 @@ const int BLEN = 1024; static void quit(int code, char *fmt, ...) { va_list v; va_start(v, fmt); + vfprintf(stderr, fmt, v); + exit(code); + va_end(v); +} + +static void die(int code, char *fmt, ...) { + va_list v; + va_start(v, fmt); char msg[BLEN]; vsnprintf(msg, BLEN, fmt, v); perror(msg); @@ -39,21 +49,21 @@ typedef void (*tmpf_cb)(void *udata, char *fname); static void in(void *udata, in_cb cb, char *cmd) { FILE *pipe = popen(cmd, "r"); - if(!pipe) quit(1, "in(): cannot open pipe to '%s'", cmd); + if(!pipe) die(1, "in(): cannot open pipe to '%s'", cmd); cb(udata, pipe); pclose(pipe); } static void out(void *udata, out_cb cb, char *cmd) { FILE *pipe = popen(cmd, "w"); - if(!pipe) quit(1, "out(): cannot open pipe to '%s'", cmd); + if(!pipe) die(1, "out(): cannot open pipe to '%s'", cmd); cb(udata, pipe); pclose(pipe); } static void tmpf(void *udata, tmpf_cb cb, char *fname) { int fd = mkstemp(fname); - if(fd == -1) quit(errno, "tmpf(): cannot create temporary file '%s'", fname); + if(fd == -1) die(errno, "tmpf(): cannot create temporary file '%s'", fname); close(fd); cb(udata, fname); unlink(fname); @@ -69,7 +79,7 @@ static void fpr(FILE *out, ...) { static void wget(char *url, char *fname) { int pid = fork(); - if(pid < 0) quit(pid, "wget(): fork failed"); + if(pid < 0) die(pid, "wget(): fork failed"); else if(pid == 0) execlp("wget", "wget", "-q", "-O", fname, "--", url, NULL); else wait(NULL); } @@ -87,7 +97,7 @@ static void md5sum(char *fname, char *sum) { static void echo(char *fname, FILE *out) { FILE *in = fopen(fname, "r"); - if(!in) quit(1, "echo(): cannot open input file '%s'", fname); + if(!in) die(1, "echo(): cannot open input file '%s'", fname); char buf[BLEN]; int n; while(0 < (n = fread(buf, sizeof(char), BLEN, in))) @@ -141,11 +151,33 @@ static void cb_w3mail(void *udata, char *fname) { out(&y, cb_sendmail, x->cmd); } +static void rtrim(char *s) { + if(!s) die(1, "rtrim(): nothing to trim"); + char *e = s + strlen(s) - 1; + while(s < e && isspace(*e)) *e-- = '\0'; +} + +static void cb_config(void *udata, FILE* in) { + struct w3mail *x = udata; + rtrim(fgets(x->cmd, BLEN, in)); + rtrim(fgets(x->from, BLEN, in)); + rtrim(fgets(x->to, BLEN, in)); + rtrim(fgets(x->host, BLEN, in)); +} + int main(int argc, char *argv[]) { - if(argc != 6) quit(1, "usage: %s cmd from to host url", argv[0]); char fname[BLEN]; snprintf(fname, BLEN, "/tmp/w3mail-XXXXXX"); - struct w3mail x = {argv[1], argv[2], argv[3], argv[4], argv[5]}; - tmpf(&x, cb_w3mail, fname); + if(argc == 2) { + char cmd[BLEN], from[BLEN], to[BLEN], host[BLEN]; + struct w3mail x = {cmd, from, to, host, argv[1]}; + in(&x, cb_config, "cat ~/.w3mail"); + tmpf(&x, cb_w3mail, fname); + } + else if(argc == 6) { + struct w3mail x = {argv[1], argv[2], argv[3], argv[4], argv[5]}; + tmpf(&x, cb_w3mail, fname); + } + else quit(1, "usage: %s [cmd from to host] url\n", argv[0]); return 0; }