3 # 50-motd-news - print the live news from the Ubuntu wire
4 # Copyright (C) 2016-2017 Canonical Ltd.
5 # Copyright (C) 2016-2017 Dustin Kirkland
7 # Authors: Dustin Kirkland <kirkland@canonical.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 ##############################################################################
24 # This program could be rewritten in C or Golang for faster performance.
25 # Or it could be rewritten in Python or another higher level language
26 # for more modularity.
27 # However, I've insisted on shell here for transparency!
29 ##############################################################################
31 # Source the local configuration
32 [ -r /etc/default/motd-news ] && . /etc/default/motd-news
34 # Exit immediately, unless we're enabled
35 # This makes this script very easy to disable in /etc/default/motd-news configuration
36 [ "$ENABLED" = "1" ] || exit 0
38 # Ensure sane defaults
39 [ -n "$URLS" ] || URLS="https://motd.ubuntu.com"
40 [ -n "$WAIT" ] || WAIT=5
41 [ -n "$CACHE" ] || CACHE="/var/cache/motd-news"
42 [ "$1" = "--force" ] && FORCED=1
44 # Ensure we print safely, maximum of the first 10 lines,
45 # maximum of the first 80 chars per line, no control chars
47 cat "$1" | head -n 10 | tr -d '\000-\011\013\014\016-\037' | cut -c -80
51 # If we're not forcing an update, and we have a cached motd-news file,
52 # then just print it and exit as quickly as possible, for login performance.
53 # Note that systemd should keep this cache file up to date, asynchronously
54 if [ "$FORCED" != "1" ]; then
55 if [ -r $CACHE ]; then
64 # If we've made it here, we've been given the --force argument,
65 # probably from the systemd motd-news.service. Let's update...
67 # Generate our temp files, clean up when done
68 NEWS=$(mktemp) || exit 1
69 ERR=$(mktemp) || exit 1
70 trap "rm -f $NEWS $ERR" HUP INT QUIT ILL TRAP KILL BUS TERM
72 # Construct a user agent, similar to Firefox/Chrome/Safari/IE to
73 # ensure a proper, tailored, accurate message of the day
75 # Curl browser version, for debug purposes
76 curl_ver="$(dpkg -l curl | awk '$1 == "ii" { print($3); exit(0); }')"
78 # Distribution version, for messages releated to this Ubuntu release
80 lsb=$(echo "$DISTRIB_DESCRIPTION" | sed -e "s/ /\//g")
81 codename="$DISTRIB_CODENAME"
83 # Kernel version and CPU type, for messages related to a particular revision or hardware
84 platform="$(uname -o)/$(uname -r)/$(uname -m)"
86 cpu="$(grep -m1 "^model name" /proc/cpuinfo | sed -e "s/.*: //" -e "s:\s\+:/:g")"
88 # Some messages may only be pertinent before or after some amount of uptime
89 read up idle < /proc/uptime
90 uptime="uptime/$up/$idle"
92 # Piece together the user agent
93 USER_AGENT="curl/$curl_ver $lsb $platform $cpu $uptime"
95 # Loop over any configured URLs
97 # Ensure https:// protocol, for security reasons
102 https://motd.ubuntu.com)
103 u="$u/$codename/$arch"
109 # If we're forced, set the wait to much higher (1 minute)
110 [ "$FORCED" = "1" ] && WAIT=60
111 # Fetch and print the news motd
112 if curl --connect-timeout "$WAIT" --max-time "$WAIT" -A "$USER_AGENT" -o- "$u" >"$NEWS" 2>"$ERR"; then
114 # At most, 10 lines of text, remove control characters, print at most 80 characters per line
116 # Try to update the cache
117 safe_print "$NEWS" 2>/dev/null >$CACHE || true