Notices


Reply
Thread Tools
Posts: 1,522 | Thanked: 392 times | Joined on Jul 2010 @ São Paulo, Brazil
#21
Here is the script i came up with after reading cmantito's post and this shell scripting tutorial filled with typos and other writing mistakes :


Code:
# The nice msgs i'm writing without testing,
# the original code has a bunch of random phrases here and there

# run the script without parameters to see a list of the accounts 
# present, name an account (without the ID part) and it should 
# toggle the status of that account

# I make no promises this is safe and i hold no resposibility for any 
# damage it might cause

if [ $# == 0 ]
then
echo "Which one?"
gconftool-2 --all-dirs /apps/modest/accounts
echo "Use the stuff between 'accounts/' and 'ID' "
else

status=`gconftool-2 -g /apps/modest/accounts/$1ID/enabled `

if [ $status == "false" ]
then
echo "Enabling $1 now"
echo `gconftool-2 -s /apps/modest/accounts/$1ID/enabled --type bool true`
elif [ $status == "true" ]
then
echo "Disabling $1 now"
echo `gconftool-2 -s /apps/modest/accounts/$1ID/enabled --type bool false`
else
echo "Oops!"
fi
echo "Done, i think"
fi

Last edited by TiagoTiago; 2010-12-28 at 07:11.
 

The Following 2 Users Say Thank You to TiagoTiago For This Useful Post:
Posts: 17 | Thanked: 17 times | Joined on Aug 2008 @ Portsmouth UK
#22
I've written another version of TiagoTiago's script in Perl, with both a simple menu for selection and the ability to pass an account with a parameter.

Code:
#!/usr/bin/perl

$arg = shift(@ARGV);
if(!$arg){
	$data = `gconftool-2 --all-dirs /apps/modest/accounts`;
	@accounts = split(/\n/, $data);
	
	print "\nPlease choose an account from the following list:\n";
	print "(alternatively, pass an account name from below as a parameter to toggle it)\n\n";
	for($i = 0; $i < ($#accounts + 1); $i++){
		$thisAccount = $accounts[$i];
		($name) = $thisAccount =~ /accounts\/(.+?)ID/;
		
		$status = `gconftool-2 -g $thisAccount/enabled`;
		if($status =~ /true/i){ $currentState = "enabled"; }
		elsif($status =~ /false/i){ $currentState = "disabled"; }
		else{ $currentState = "state unknown"; }
		
		print "\t[".$i."] ".$name.": ".$currentState."\n";
	}
	print "\nSelection [0-".($i - 1)." or [q]uit]: ";
	$id = <STDIN>;
	if($id =~ /^q/i){
		exit(0);
	}
	print "\n";
	if($id !~ /^\d+$/ || !$accounts[$id]){
		print "Invalid selection.\n";
		exit(1);
	}
	
	$path = $accounts[$id];
	chomp($path);
}else{
	$path = "/apps/modest/accounts/".$arg."ID";
}

$status = `gconftool-2 -g $path/enabled`;
if($status =~ /true/i){
	system("gconftool-2 -s ".$path."/enabled --type bool false");
	if($? > 0){
		print "The account could not be disabled.\n";
		exit($?);
	}
	print "The account has been disabled.\n";
	exit(0);
}elsif($status =~ /false/i){
	system("gconftool-2 -s ".$path."/enabled --type bool true");
	if($? > 0){
		print "The account could not be enabled.\n";
		exit($?);
	}
	print "The account has been enabled.\n";
	exit(0);
}else{
	print "The specified account could not be found.\n";
	exit(1);
}
Example:
Code:
~ $ perl modest-account.pl 

Please choose an account from the following list: 
(alternatively, pass an account name from below as a parameter to toggle it) 

	[0] Home: enabled 
	[1] Mail@32@for@32@Exchange: enabled 
	[2] Work: enabled 

Selection [0-2 or [q]uit]: 1

The account has been disabled.


~ $ perl modest-account.pl Home
The account has been disabled. 
~ $ perl modest-account.pl Home
The account has been enabled. 
~ $
If I can ever be bothered to learn Python/Qt, I might make this into a pretty little app, but Python is not my strong point. :P

Last edited by cmantito; 2010-12-29 at 01:27.
 

The Following User Says Thank You to cmantito For This Useful Post:
Posts: 17 | Thanked: 17 times | Joined on Aug 2008 @ Portsmouth UK
#23
Progress update!

The best way (for me) to learn a new language: inability sleep, and dive in head first. I converted my Perl script to Python. Next, if I still can't sleep, I'll try my hand at GUIing.

It's identical to the Perl script, except I didn't bother with regexes, and it's in Python:

Code:
#!/usr/bin/python
import sys
import commands
import string

try:
	arg = sys.argv[1]
	path = "/apps/modest/accounts/" + arg + "ID"
except:
	print "\nPlease choose an account from the following list:"
	print "(alternatively, pass an account name from below as a parameter to toggle it)\n"

	data = commands.getoutput("gconftool-2 --all-dirs /apps/modest/accounts")
	accounts = string.split(data, '\n')
	
	i = 0
	for acct in accounts:
		# path:   /apps/modest/accounts/xxxID
		pathParts = string.split(acct, '/')
		theId = pathParts[4]
		# theId = xxxID
		nameParts = string.split(theId, 'ID')
		# ['xxx', '']
		name = nameParts[0]
		
		status = commands.getoutput("gconftool-2 -g " + acct + "/enabled")
		if status == "true":
			statusText = "enabled"
		elif status == "false":
			statusText = "disabled"
		else:
			statusText = "state unknown"
		
		print "[" + str(i) + "] " + name + ": " + statusText 
		i = i + 1
	
	try:
		id = int(raw_input("\n\tSelection [0-" + str(i) + "]: "))
		if id > i:
			print "Invalid selection."
			sys.exit(1)
		if id < 0:
			print "Invalid selection."
			sys.exit(1)
	except:
		sys.exit(0)

	path = accounts[id]
	path = string.rstrip(path)
	
status = commands.getoutput("gconftool-2 -g " + path + "/enabled")
if status == "true":
	exitState = commands.getstatusoutput("gconftool-2 -s " + path + "/enabled --type bool false")
	if exitState[0] > 0:
		print "The account could not be disabled."
		sys.exit(exitState)
	
	print "The account has been disabled."
	sys.exit(0)
elif status == "false":
	exitState = commands.getstatusoutput("gconftool-2 -s " + path + "/enabled --type bool true")
	if exitState[0] > 0:
		print "The account could not be enabled."
		sys.exit(exitState)
	
	print "The account has been enabled."
	sys.exit(0)

else:
	print "The specified account could not be found."
	sys.exit(1)
(It may be a lack of sleep talking, but I think I like Perl better.)
 

The Following User Says Thank You to cmantito For This Useful Post:
Reply

Tags
email, modest, privacy, wanking

Thread Tools

 
Forum Jump


All times are GMT. The time now is 08:07.