OSDN Git Service

整理した
[serene/serenelinux-system-pkg.git] / base-files.old / etc / update-motd.d / 50-motd-news
1 #!/bin/sh
2 #
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
6 #
7 #    Authors: Dustin Kirkland <kirkland@canonical.com>
8 #
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.
13 #
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.
18 #
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.
22
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!
28 #                                                                     - Dustin
29 ##############################################################################
30
31 # Source the local configuration
32 [ -r /etc/default/motd-news ] && . /etc/default/motd-news
33
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
37
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
43
44 # Ensure we print safely, maximum of the first 10 lines,
45 # maximum of the first 80 chars per line, no control chars
46 safe_print() {
47         cat "$1" | head -n 10 | tr -d '\000-\011\013\014\016-\037' | cut -c -80
48 }
49
50
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
56                 echo
57                 safe_print $CACHE
58         else
59                 : > $CACHE
60         fi
61         exit 0
62 fi
63
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...
66
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
71
72 # Construct a user agent, similar to Firefox/Chrome/Safari/IE to
73 # ensure a proper, tailored, accurate message of the day
74
75 # Curl browser version, for debug purposes
76 curl_ver="$(dpkg -l curl | awk '$1 == "ii" { print($3); exit(0); }')"
77
78 # Distribution version, for messages releated to this Ubuntu release
79 . /etc/lsb-release
80 lsb=$(echo "$DISTRIB_DESCRIPTION" | sed -e "s/ /\//g")
81 codename="$DISTRIB_CODENAME"
82
83 # Kernel version and CPU type, for messages related to a particular revision or hardware
84 platform="$(uname -o)/$(uname -r)/$(uname -m)"
85 arch="$(uname -m)"
86 cpu="$(grep -m1 "^model name" /proc/cpuinfo | sed -e "s/.*: //" -e "s:\s\+:/:g")"
87
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"
91
92 # Piece together the user agent
93 USER_AGENT="curl/$curl_ver $lsb $platform $cpu $uptime"
94
95 # Loop over any configured URLs
96 for u in $URLS; do
97         # Ensure https:// protocol, for security reasons
98         case $u in
99                 https://*)
100                         true
101                 ;;
102                 https://motd.ubuntu.com)
103                         u="$u/$codename/$arch"
104                 ;;
105                 *)
106                         continue
107                 ;;
108         esac
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
113                 echo
114                 # At most, 10 lines of text, remove control characters, print at most 80 characters per line
115                 safe_print "$NEWS"
116                 # Try to update the cache
117                 safe_print "$NEWS" 2>/dev/null >$CACHE || true
118         else
119                 : > "$CACHE"
120         fi
121 done
122 rm -f "$NEWS" "$ERR"
123 exit 0