aboutsummaryrefslogtreecommitdiff
path: root/mail/fetchmail/files/fetchmail.in
blob: e1c66d40fbdf72aff691a5ce3cec4ff1f792b854 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: fetchmail
# REQUIRE: LOGIN mail
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to fetchmail in daemon
# mode.
#
# fetchmail_enable="YES"
#
# There are two variants:
#
#  * Single system-wide fetchmail daemon:
#    - It is run as user `fetchmail_user' (default: fetchmail)
#      Note: The directory /var/run/fetchmail must be writable for
#            'fetchmail_user'.
#    - All configuration is contained in one global file
#      'fetchmail_config' (default: %%PREFIX%%/etc/fetchmailrc)
#      that must be owned by 'fetchmail_user' (mode 700)
#    - The fetchmail daemon awakes to fetch mail every 
#      'fetchmail_polling_interval' seconds (default: 900).
#
#  * Per-user daemon
#    - Users for which a fetchmail daemon is to be started must be
#      listed in 'fetchmail_users', e.g. fetchmail_users="user1 user2"
#      The 'fetchmail_user' (sic!) variable is ignored in this
#      configuration variant.
#    - The config files for the individual users must be located at
#      ${fetchmail_home_prefix}/${user}/${fetchmail_config_name}. The
#      default for 'fetchmail_home_prefix' is "/home", and that for
#      'fetchmail_config_name' is ".fetchmailrc".
#    - Note that "${fetchmail_home_prefix}/${user}" must be writable
#      for ${user} since it is used to store the per-user PID files!
#    - There are user-specific versions of 'fetchmail_config' and
#      'fetchmail_polling_interval' that can be used to override the
#      defaults, i.e. for the user 'user1' there are variables
#      'fetchmail_user1_config' and 'fetchmail_user1_polling_interval'
#    - All commands (e.g. start, stop, awaken (see below)) can be either
#      passed to all instances of the daemon (if %%PREFIX%%/etc/rc.d/fetchmail)
#      is run as root), or just to the instance belonging to the respective
#      user.
#
# Extra commands:
#
#  * 'awaken': Sends a signal to the daemon(s) to check for new mail
#     immediately
#
# Fetchmail configuration:
#
# In any case, you will need a working fetchmailrc file. Please consult
# the man page fetchmail(1), the documentation in %%PREFIX%%/share/doc/fetchmail/
# and/or the material found at <http://www.fetchmail.info/>.

. /etc/rc.subr

name=fetchmail
rcvar=fetchmail_enable

command=%%PREFIX%%/bin/${name}
pidfile=/var/run/fetchmail/${name}.pid
extra_commands="awaken"
awaken_cmd="fetchmail_awaken"

fetchmail_script=%%PREFIX%%/etc/rc.d/$name

# read settings, set default values
load_rc_config "$name"
: ${fetchmail_enable="NO"}
: ${fetchmail_user="fetchmail"}
: ${fetchmail_config="%%PREFIX%%/etc/fetchmailrc"}
: ${fetchmail_polling_interval="900"}
: ${fetchmail_logging_facility="--syslog"}
: ${fetchmail_home_prefix="/home"}
: ${fetchmail_config_name=".fetchmailrc"}

# send signal to fetchmail process(es) to check for new mail immediately
fetchmail_awaken()
{
	if [ $rc_pid ]; then
		echo "Forcing fetchmail to check mailbox(es)..."
		kill -USR1 $rc_pid
	else
		echo "$name not running? (check $pidfile)"
	fi

	return
}

if [ -n "$2" ]; then
	# perform action for an instance of fetchmail daemon
        user="$2"
        if [ "x${fetchmail_users}" != "x" -o "x$3" = "xGLOBALCONFIG"  ]; then
		if [ "x${fetchmail_users}" != "x" ]; then
			# multiuser setup:  determine user specific config and pid file
			eval fetchmail_config="\${fetchmail_${user}_config:-${fetchmail_home_prefix}/${user}}/${fetchmail_config_name}"
			eval pidfile="${fetchmail_home_prefix}/${user}/.fetchmail.pid"
			eval fetchmail_user=$user
		else
			eval pidfile=/var/run/fetchmail/fetchmail.pid
		fi
		required_files=${fetchmail_config}
		eval fetchmail_polling_interval="\${fetchmail_${user}_polling_interval:-${fetchmail_polling_interval}}"
		fetchmail_flags="-f ${fetchmail_config} \
				--pidfile ${pidfile} \
				-d ${fetchmail_polling_interval} \
				${fetchmail_logging_facility}"
        else
                echo "$0: extra argument ignored"
        fi
else
	uid=`id -u`
        if [ "x${fetchmail_users}" != "x" -a "x$1" != "x" -a "$uid" = "0" ]; then
		# root mode: multiple user profiles are handled by recursive
		# calls of this script
		for user in ${fetchmail_users}; do
			echo "===> fetchmail user: ${user}"
			$fetchmail_script $1 ${user}
			retcode="$?"
			if [ "0${retcode}" -ne 0 ]; then
				failed="${user} (${retcode}) ${failed:-}"
			else
				success="${user} ${success:-}"
			fi
		done
		exit 0
	else
		if [ "x${fetchmail_users}" = "x" ]; then
			# There is only one global configuration file
			globalconfig=GLOBALCONFIG
		fi
		$fetchmail_script $1 `id -u -n` $globalconfig
		retcode="$?"
		if [ "0${retcode}" -ne 0 ]; then
			failed="${user} (${retcode}) ${failed:-}"
			exit 1
		else
			success="${user} ${success:-}"
		fi
		exit 0
        fi
fi

# actually execute the fetchmail program
export FETCHMAILUSER=$fetchmail_user
run_rc_command "$1"