Category: other
Author/Contact Info Amir M. Mahmoodi
Description: this is a realy simple program ,that manage wvdial(for connect to the internet for dialup)...you can swich between your connection, creat connections...

#!/usr/bin/perl -w
######################################################################
+###
# Program name: wvdial manager                                        
+    #
# Version: 0.0.1                                                      
+  #
# Programmer: Amir m. mahmoodi.                                       
+     #
# E-mail: white_fox_ir@yahoo.com                                      
+  #
#                                                                     
+   #
#                                                                     
+   #
# This program is free software; you can redistribute it and/or modify
+    #
# it under the terms of the GNU General Public License as published by
+    #
# the Free Software Foundation; either version 2 of the License, or   
+     #
# (at your option) any later version.                                 
+   #
#                                                                     
+   #
# This program is distributed in the hope that it will be useful,     
+   #
# but WITHOUT ANY WARRANTY; without even the implied warranty of      
+  #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       
+     #
# GNU Library General Public License for more details.                
+    #
######################################################################
+###
use strict;
use warnings;
use Term::ANSIColor;


system("clear");
#try to introduce my command program, this is a sample of hash
my %commands = (
    help   =>  \&help,
    dial   =>  \&dial,
    config =>  \&config,
    "q"    =>  \&quit,
    creat  =>  \&creat,
    ver    =>  \&version,
    connections => \&connections,
    which  =>    \&which
);


#main
print color 'bold blue';
print "*********************************\n";
print "*         wvdial-manager        *\n";
print "*         version: 0.0.1        *\n";
print "*********************************\n";
print color 'reset';


#this >> is my command program,where you cant type your command.
while(1){
print ">>";
my $command=<stdin>;chomp $command;

#let's check our commands and if it dosn't exist report that
    if (exists $commands{lc($command)}) {
        &{$commands{$command}};
    } else {
        print "this command dose not exist, type \"help\" for more inf
+ormation\n";
    }

}


#Subroutins
#this place is for write my subroutin
sub version{
        print color 'bold yellow';
        print "This is wvdial-manager, v0.0.1 (Beta).\n";
        print "Written by:Amir m. Mahmoodi<white_fox_ir\@yahoo.com>.\n
+";
        print "This program is free released under GNU/GPL.\n";
        print "Use \"help\" to see the commands.\n";
        print "This program released without any warranty!.\n";
        print "Reaport Bugs to my eamil and if you want to change this
+ program pleas contact me.\n";
        print color 'reset';
}

sub help{
        print color 'bold red';
        print "use these commands for managing wvdial:\n\n";print colo
+r 'reset';
        print "dial: for starting wvdial.\n";
        print "creat: for create connection.\n";
        print "config: for configurtion this program.\n";
        print "connections: to see your connections list.\n";
        print "which: use it,to select the one you want to connect wit
+h.\n";
        print "help: for show this tip.\n";
        print "ver: to see the version.\n";
        print "q: for quit this program\n";
}

sub dial{
        print color 'bold red';
        print "For disconnect use Ctrl-C...\n";print color 'reset';
        system ('wvdial');
        return;
}
sub creat{
        print color 'bold red';
        print "In order to use the mwvdial for the first time,\nyou sh
+uld use \"config\" and then \"create\".\n\n";
        print color 'reset';
        print "Enter Connection name:";
           my $connection_name=<stdin>;chomp $connection_name;
        print "Enter phone number:";
        my $phone=<stdin>;chomp $phone;
        print "Enter username:";
        my $username=<stdin>;chomp $username;
        print "Enter password:";
        my $pass=<stdin>;chomp $pass;

            my $path = $ENV{HOME};
            open (INFILEHANDLE, "< $path/.MWVDIAL/mwvdial.config") or 
+die "Can't open input file because: $! \n";
            open (OUTFILEHANDLE, ">$path/.MWVDIAL/$connection_name\.co
+nection") or die "Can't open output file because: $! \n";
            while(<INFILEHANDLE>){
                $_ =~ s/^#.*1/Phone = $phone/;
                   $_ =~ s/^#.*2/Username =  $username/;
                   $_ =~ s/^#.*3/Password = $pass/;

                print OUTFILEHANDLE $_;
}

close(INFILEHANDLE);
close(OUTFILEHANDLE);

}

sub config{
            my $path = $ENV{HOME};
            if (!opendir(MWLOG, '$path/.MWVDIAL')){
                &log_folder;
            }
            if(!open(MWVDIAL_LOG, '$path/.MWVDIAL/mwvdial.config')){
                print "Enter the path of wvdial.conf[/etc/wvdial.conf]
+:";
                my $infile = <stdin>;chomp $infile;
                my $outfile = "$path/.MWVDIAL/mwvdial.config";
                open (INFILEHANDLE, "< $infile") or die "Can't open in
+put file because: $! \n";
                open (OUTFILEHANDLE, "> $outfile") or die "Can't open 
+output file because: $! \n";
                while(<INFILEHANDLE>){
                $_ =~ s/^[p,P]hone.*\S/#1/;
                $_ =~ s/^[u,U]sername.*\S/#2/;
                $_ =~ s/^[p,P]assword.*\S/#3/;
                print OUTFILEHANDLE $_;
                close(MWVDIAL_LOG);
}

            close(INFILEHANDLE);
            close(OUTFILEHANDLE);
                
}
}

sub log_folder{
            chdir;
            if (!opendir(MWLOG, '.MWVDIAL')){
            mkdir (".MWVDIAL", 0755) || die "Faild to creat this direc
+tory: $!";
            return 0;
}
}

    sub connections{
                my $path = $ENV{HOME};
                opendir(CONNECTION, "$path/.MWVDIAL") || die;
                my @connection_name = grep(s/\.conection$//, readdir C
+ONNECTION);            
                my $index = 0;
                my $i = 0;
                for($i=0;$i <= $#connection_name; $i++){
                    print $connection_name[$index];
                    print "\n";
                    $index++;
                }
                closedir(CONNECTION);
}

sub which {
        my $user = $ENV{USER};
        if ("$user" ne "root") {
        print color 'bold red';
        print "You must be root to configure, if your not!, Exit and t
+ry as root again.\n\n";print color 'reset';
        &quit
        }else {
        my $path = $ENV{HOME};
        print color 'bold red';
        print "to see your connections use connections command.\n";
        print color 'reset';
        print "Which your connection: ";
        my $connect_name = <stdin>;chomp $connect_name;
        my $input = "$path/.MWVDIAL/$connect_name\.conection";
        open(SOURCE, "$input") || die "Can't use this connection_name:
+ $!";
        open(DEST, "> /etc/wvdial.conf") || die "$!";
        my @contents=<SOURCE>;
        print DEST @contents;
        close(DEST);
        close(SOURCE);        
}
}
sub quit{
        exit;
        closedir(MWLOG);
        
}