Debian Codegraph @

examples/login_shell

raw
1 #! /usr/bin/perl
2 #########################################################################
3 # This Perl script is Copyright (c) 2002, Peter J Billam #
4 # c/o P J B Computing, www.pjb.com.au #
5 # #
6 # This script is free software; you can redistribute it and/or #
7 # modify it under the same terms as Perl itself. #
8 #########################################################################
9
10 use Term::Clui;
11 use Term::Clui::FileSelect;
12 my $home = $ENV{'HOME'} || $ENV{'LOGDIR'} || (getpwuid($<))[7];
13 my $user = $ENV{'USER'} || $ENV{'LOGNAME'} || (getpwuid($<))[0];
14 my @path = split ':', $ENV{PATH};
15 my $browser = '';
16 my $mailer = '';
17 use Cwd; my $dir = cwd();
18
19 while (1) {
20 my @tasks = ('Browser','Config','ChDir','Edit','FTP', 'IRC','Mail');
21 if (-r './Makefile') { push (@tasks, 'Make'); }
22 push (@tasks, 'Manual', 'News');
23 if (-d "$home/.ssh") { push (@tasks, 'SSH'); }
24 push (@tasks, 'View');
25
26 my $shortdir = $dir;
27 if ((index $shortdir,$home)==0) {substr($shortdir,0,(length $home))= '~';}
28 my $task = &choose ("$user $shortdir", @tasks);
29 if (! $task) { exit 0;
30 } else { $task =~ s/ /_/g; eval "&$task";
31 }
32 }
33 sub Config {
34 my @files = grep (-w "$home/$_",
35 '.cshrc', '.efaxrc', '.emacspeak', '.edbrowse', '.exrc',
36 '.fvwmrc', '.html2psrc', '.jnewsrc',
37 '.login', '.lynxrc', 'lynx_bookmarks.html',
38 '.mailrc', '.mplayer/config', '.netrc', '.newsrc',
39 '.perltidyrc', '.pinerc', '.popslurp', '.profile',
40 '.rhosts', '.sig', '.slrnrc', '.ssh/config',
41 '.tiprc', '.xauth', '.Xauthority', '.xinitrc',
42 );
43 my $file;
44 $file = &choose("Edit which file ?", @files);
45 return unless $file;
46 &edit("$home/$file");
47 }
48 sub Browser {
49 if (! $browser) {
50 my ($bin, $prog);
51 DIR: foreach $bin (@path) {
52 foreach $prog ('lynx', 'w3m', 'links') {
53 if (-e "$bin/$prog") { $browser = "$bin/$prog"; last DIR; }
54 }
55 }
56 }
57 if (! $browser) {
58 &sorry("can't find a browser: tried lynx, w3m and links") ; return;
59 }
60 system $browser;
61 }
62 sub ChDir {
63 my $newdir = &ask('to which directory ?');
64 return unless $newdir;
65 $newdir =~ s/^~\//$home\//;
66 if (! -d $newdir) { &sorry("$newdir isn't a directory"); return; }
67 if (! chdir $newdir) { &sorry("can't chdir to $newdir: $!"); return; }
68 $dir = cwd();
69 }
70 sub Edit {
71 if (!opendir(D,'.')) { &sorry("can't open current directory: $!"); return; }
72 my @textfiles = sort grep (-T && !/^\./, readdir D); closedir D;
73 my $file = 'Create new file';
74 if (@textfiles) {
75 $file = &choose('Edit which file ?',@textfiles,'Create new file');
76 return unless $file;
77 }
78 if ($file eq 'Create new file') {
79 $file = &ask('New file name ?'); return unless $file;
80 system "touch $file";
81 }
82 &edit($file);
83 }
84 sub FTP {
85 if (!open(F,"$home/.netrc")) {
86 &sorry("can't open $home/.netrc: $!"); return;
87 }
88 my (%login, %password);
89 while (<F>) {
90 if (/^machine\s+(\S+)\s+login\s+(\S+)\s+password\s+(\S+)\s*$/) {
91 $login{$1} = $2; $password{$1} = $3;
92 }
93 }
94 close F;
95 my $task = &choose('FTP to :',keys %login,'Somewhere else','Edit ~/.netrc');
96 if (! $task) { return 1;
97 } elsif ($task eq 'Somewhere else') {
98 my $host = &ask('FTP to where ?'); if ($host) { system "ftp $host"; }
99 } elsif ($task eq 'Edit ~/.netrc') { &edit("$home/.netrc");
100 } else { system "ftp $task";
101 }
102 }
103 sub IRC {
104 if (! $ircclient) {
105 my ($bin, $prog);
106 DIR: foreach $bin (@path) {
107 foreach $prog ('sirc','tinyirc') {
108 if (-e "$bin/$prog") { $ircclient = "$bin/$prog"; last DIR; }
109 }
110 }
111 }
112 if (! $ircclient) {
113 &sorry("can't find a ircclient: tried sirc and tinyirc") ; return;
114 }
115 system $ircclient;
116 }
117 sub Mail {
118 if (! $mailer) {
119 my ($bin, $prog);
120 DIR: foreach $bin (@path) {
121 foreach $prog ('alpine','pine', 'elm') {
122 if (-e "$bin/$prog") { $mailer = "$bin/$prog"; last DIR; }
123 }
124 }
125 }
126 if (! $mailer) {
127 &sorry("can't find a mailer: tried alpine, pine and elm") ; return;
128 }
129 system $mailer;
130 }
131 sub Make {
132 if (!open(F,'./Makefile')) { &sorry("can't open Makefile: $!"); return; }
133 my (%vars, @targets, $target);
134 while (<F>) {
135 if (/^\s*(\S+)\s*=\s*(\S+)/) { $vars{$1} = $2; }
136 if (/^\s*(\S+)\s*:/) {
137 $target = $1;
138 while ($target =~ /\$[({](\w+)[)}]/ && $vars{$1}) {
139 $target =~ s/\$[({](\w+)[)}]/$vars{$1}/;
140 }
141 push (@targets, $target);
142 }
143 }
144 close F;
145 $target = &choose("$dir : Make what ?", @targets);
146 return unless $target;
147 system "make $target";
148 }
149 sub Manual {
150 my $topic = &ask('Topic ?'); return unless $topic;
151 if (system "man '$topic'") {
152 my $txt = `man -k '$topic'`;
153 &view("Keyword search on $topic", $txt);
154 }
155 }
156 sub News {
157 system 'slrn'; # /usr/bin/trn ?
158 }
159 sub SSH {
160 # make sure ~/.ssh/config contains: HashKnownHosts no
161 if (! open(F,"$home/.ssh/known_hosts")) {
162 &sorry("can't open $home/.ssh/known_hosts: $!"); return;
163 }
164 my (%hosts);
165 while (<F>) { if (/^([.\w]+)/) { $hosts{$1}++; } }
166 close F;
167 my @hosts = sort keys %hosts;
168 my $host = &choose('SSH to :', @hosts, 'Somewhere else');
169 if (! $host) { return 1;
170 } elsif ($host eq 'Somewhere else') {
171 $host = &ask('ssh to which host ?');
172 if ($host) { system "ssh $host"; }
173 } else { system "ssh $host";
174 }
175 }
176 sub View {
177 my $file = select_file(-Readable=>1);
178 &view($file);
179 }
180
181 =pod
182
183 =head1 NAME
184
185 login_shell - wrapper for text-based browser, mail, news, ssh, ftp etc.
186
187
188 =head1 SYNOPSIS
189
190 $ login_shell
191
192 =head1 DESCRIPTION
193
194 This script offers the naive user arrow-key-and-return access to a
195 text-based browser, a mail client, a news client, ssh, ftp, man
196 and various other stuff.
197
198 =head1 AUTHOR
199
200 Peter J Billam www.pjb.com.au/comp/contact.html
201
202 =head1 CREDITS
203
204 Based on Term::Clui
205
206 =head1 SEE ALSO
207
208 http://www.pjb.com.au/ ,
209 http://search.cpan.org/~pjb ,
210 Term::Clui,
211 Term::Clui::FileSelect,
212 lynx(1),
213 slrn(1),
214 pine(1),
215 ssh(1),
216 ftp(1),
217 make(1),
218 man(1)
219
220 =cut
221