mcabber/contrib/mcwizz.pl
changeset 1148 e86483cb8c39
parent 1147 fcef5d34b7d4
child 1149 13d125a2020f
equal deleted inserted replaced
1147:fcef5d34b7d4 1148:e86483cb8c39
     1 #!/usr/bin/perl
       
     2 #
       
     3 #  Copyright (C) 2006 Adam Wolk "Mulander" <netprobe@gmail.com>
       
     4 #  Copyright (C) 2006 Mateusz Karkula "Karql"
       
     5 #  A few tweaks by Mikael Berthe
       
     6 #
       
     7 # This script is provided under the terms of the GNU General Public License,
       
     8 # see the file COPYING in the root mcabber source directory.
       
     9 #
       
    10 
       
    11 use warnings;
       
    12 use strict;
       
    13 
       
    14 use Getopt::Long;
       
    15 use Term::ReadKey;
       
    16 
       
    17 my %options;
       
    18 
       
    19 Getopt::Long::Configure qw(bundling);
       
    20 my $result = GetOptions(
       
    21 		"help|h"	=> \$options{help},
       
    22 		"version|V"	=> \$options{version},
       
    23 		"ignore|i"	=> \$options{ignore},
       
    24 		"ignore-auto|I"	=> \$options{ignore_auto},
       
    25 		"output|o"	=> \$options{output},
       
    26 		"passwd|p"	=> \$options{passwd},
       
    27 		"color|c"	=> \$options{color},
       
    28 		"ssl|s"		=> \$options{ssl},
       
    29 		"status|S"	=> \$options{status},
       
    30 		"proxy|P"	=> \$options{proxy},
       
    31 		"keep|k"	=> \$options{keep},
       
    32 		"log|l"		=> \$options{log},
       
    33 		"tracelog|t"	=> \$options{tracelog},
       
    34 );
       
    35 
       
    36 ##
       
    37 # Allowed colors
       
    38 my @bg_color	= qw( black red green yellow blue magenta cyan white );
       
    39 my @fg_color	= (@bg_color, map("bright$_", @bg_color), map("bold$_", @bg_color));
       
    40 
       
    41 ##
       
    42 # info for specific settings
       
    43 my %info = (
       
    44 	# not grouped
       
    45 	beep_on_message => { msg => 'Should mcabber beep when you receive a message?', allow => undef, type => 'yesno', anwsers => undef, default => 0},
       
    46 	password	=> { msg => 'Enter your password (at your own risk, this will be saved in plain text)', allow=>'.+', type=>'pass',anwsers => undef, default => undef },
       
    47 	pinginterval	=> { msg => 'Enter pinginterval in seconds for keepalive settings, set this to 0 to disable, ', allow =>'\d+', type=>'edit', anwsers => undef, default => 40 },
       
    48 	hide_offline_buddies => {msg => 'Display only connected buddies in the roster?', allow => undef, type => 'yesno', anwsers => undef, default => 0 },
       
    49 	iq_version_hide_os => { msg => 'Hide Your OS information?', allow => undef, type => 'yesno', anwsers => undef, default => 0 },
       
    50 	# server settings
       
    51 	username 	=> { msg => 'Your username', allow => '[^\s\@:<>&\'"]+', type => 'edit', anwsers => undef, default => undef },
       
    52 	server		=> { msg => 'Your jabber server', allow => '\S+', type => 'edit', anwsers => undef, default => undef },
       
    53 	resource	=> { msg => 'Resource (If your don\'t know what a resource is, use the default setting)', allow => '.{1,1024}', type => 'edit', anwsers => undef, default => 'mcabber' },
       
    54 	nickname	=> { msg => 'Conference nickname (if you skip this setting your username will be used as a nickname in MUC chatrooms)', allow => '.+', type => 'edit', anwsers => undef, default => undef },
       
    55 	# ssl settings
       
    56 	ssl		=> { msg => 'Enable ssl?', allow => undef, type => 'yesno', anwsers => undef, default => 0 },
       
    57 	port		=> { msg => 'Enter ssl port', allow => '\d+', type => 'edit', anwsers => undef, default => 5222 },
       
    58 	# proxy settings
       
    59 	proxy_host	=> { msg => 'Proxy host', allow => '\S+?\.\S+?', type => 'edit', anwsers => undef, default => undef },
       
    60 	proxy_port	=> { msg => 'Proxy port', allow => '\d+', type => 'edit', anwsers => undef, default => 3128 },
       
    61 	proxy_user	=> { msg => 'Proxy user (optional, you can skip this if not required)', allow => '.+', type => 'edit', anwsers => undef, default => undef },
       
    62 	proxy_pass	=> { msg => 'Proxy pass (optional, you can skip this if not required)', allow => '.+', type => 'pass', anwsers => undef, default => undef },
       
    63 	# trace logs
       
    64 	tracelog_level => { msg => 'Specify level of advanced traces', allow => undef, type => 'multi', anwsers => ['lvl0: I don\'t want advanced tracing','lvl1: most events of the log window are written to the file','lvl2: debug logging (XML etc.)'], default => 0 },
       
    65 	tracelog_file => { msg => 'Specify a file to which the logs will be written', allow => undef, type => 'edit', anwsers => undef, default => undef },
       
    66 	# logging settings
       
    67 	log_win_height => { msg => 'Set log window height (minimum 1)', allow => '[1-9]\d*', type => 'edit', anwsers => undef, default => 5 },
       
    68 	log_display_sender => { msg => 'Display the message sender\'s jid in the log window?', allow => undef, type => 'yesno', anwsers => undef, default => 0 },
       
    69 	logging		=> { msg => 'Enable logging?', allow => undef, type => 'yesno', anwsers => undef, default => 1 },
       
    70 	load_logs 	=> { msg => 'Enable loading logs?', allow => undef, type => 'yesno', anwsers => undef, default => 1 },
       
    71 	logging_dir	=> { msg => 'Enter logging directory', allow => '.+' , type => 'edit', anwsers => undef, default => undef },
       
    72 	log_muc_conf	=> { msg => 'Log MUC chats?', allow => undef, type => 'yesno', anwsers => undef, default => 1 },
       
    73 	load_muc_logs	=> { msg => 'Load MUC chat logs?', allow => undef, type => 'yesno', default => 0 },
       
    74 	# status settings
       
    75 	roster_width => { msg => 'Set buddylist window width (minimum 2)', allow => '[2-9]\d*', type => 'edit', anwsers => undef, default => 24 },
       
    76 	buddy_format => { msg => 'What buddy name format (in status window) do you prefer?', allow => undef, type => 'multi', anwsers => ['<jid/resource>','name <jid/resource> (name is omitted if same as the jid)','name/resource (if the name is same as the jid, use <jid/res>','name (if the name is the same as the jid, use <jid/res>'], default => 0 },
       
    77 	show_status_in_buffer => { msg => 'What status changes should be displayed in the buffer?', allow => undef, type => 'multi', anwsers => ['none','connect/disconnect','all'], default => 2 },
       
    78 	autoaway => { msg => 'After how many seconds of inactivity should You become auto away? (0 for never)', allow => '\d+', type => 'edit', anwsers => undef, default => 0 },
       
    79 	message		=> { msg => 'Skip this setting unless you want to override all other status messages', allow => undef, type => 'edit', default => 'Unique message status'},
       
    80 	message_avail	=> { msg => 'Set avaible status', allow => undef, type =>'edit',anwsers => undef, default =>'I\'m avaible'},
       
    81 	message_free	=> { msg => 'Set free for chat status', allow => undef, type =>'edit', anwsers => undef, default => 'I\'m free for chat'},
       
    82 	message_dnd	=> { msg => 'Set do not disturb status', allow => undef, type => 'edit', anwsers => undef, default => 'Please do not disturb'},
       
    83 	message_notavail=> { msg => 'Set not avaible status', allow => undef, type => 'edit', anwsers => undef, default => 'I\'m not avaible'},
       
    84 	message_away	=> { msg => 'Set away status', allow => undef, type => 'edit', anwsers => undef, default => 'I\'m away' },
       
    85 	message_autoaway=> { msg => 'Set auto-away status', allow => undef, type => 'edit', anwsers => undef, default => 'Auto-away'},
       
    86 	# color settings
       
    87 	color_background=> { msg => 'Select background color of the chat window and the log window', allow => undef, type => 'multi', anwsers => \@bg_color, default => 'black' },
       
    88 	color_general	=> { msg => 'Select text color in the chat window and the log window', allow => undef, type =>'multi', anwsers => \@fg_color , default => 'white' },
       
    89 	color_msgout	=> { msg => 'Select text color in the chat window for outgoing messages', allow => undef,  type => 'multi', anwsers => \@fg_color, default => 'cyan'},
       
    90 	color_bgstatus	=> { msg => 'Select background color of the status lines', allow => undef, type => 'multi', anwsers => \@bg_color, default =>'blue'},
       
    91 	color_status	=> { msg => 'Select text color of the status lines', allow => undef, type => 'multi', anwsers => \@fg_color, default => 'white' },
       
    92 	color_roster	=> { msg => 'Select text color of the roster (buddylist) normal items', allow => undef, type => 'multi', anwsers => \@fg_color, default => 'green' },
       
    93 	color_bgrostersel=>{ msg => 'Select background color of the selected roster item', allow => undef, type => 'multi', anwsers => \@bg_color, default => 'cyan' },
       
    94 	color_rostersel	=> { msg => 'Select text color of the selected roster item', allow => undef, type => 'multi', anwsers => \@fg_color, default => 'blue' },
       
    95 	color_rosterselmsg=>{ msg => 'Select text color of the selected roster item, if there is a new message', allow => undef, type => 'multi', anwsers => \@fg_color, default => 'red' },
       
    96 	color_rosternewmsg=>{ msg => 'Select text color of items with unread messages', allow => undef, type => 'multi', anwsers => \@fg_color , default => 'red' },
       
    97 );
       
    98 
       
    99 ##
       
   100 # question groups
       
   101 my %groups = ( 	required => [qw(username server resource nickname ssl port)],
       
   102 		proxy_settings  => [qw(proxy_host proxy_port proxy_user proxy_pass)],
       
   103 		logging_settings=> [qw(logging log_win_height log_display_sender load_logs logging_dir log_muc_conf load_muc_logs)],
       
   104 		status_settings => [qw(buddy_format roster_width show_status_in_buffer autoaway message message_avail message_free message_dnd message_notavail message_away message_autoaway )],
       
   105 		color_settings  => [qw(color_background color_general color_msgout color_bgstatus color_status color_roster color_bgrostersel color_rostersel color_rosterselmsg color_rosternewmsg)],
       
   106 		tracelog_settings => [qw(tracelog_level tracelog_file)],
       
   107 );
       
   108 
       
   109 my (%conf,@old);
       
   110 ##
       
   111 # regexp for valid keys
       
   112 my $key_reg = join '|', keys %info;
       
   113 
       
   114 help() 		if $options{help};
       
   115 version() 	if $options{version};
       
   116 
       
   117 prepare();
       
   118 ask('password') if $options{passwd};
       
   119 
       
   120 ask($_) for @{ $groups{required} };
       
   121 if($options{proxy}) { ask($_) for @{ $groups{proxy_settings} } };
       
   122 ask('pinginterval') if $options{keep};
       
   123 ask('beep_on_message');
       
   124 ask('hide_offline_buddies');
       
   125 ask('iq_version_hide_os');
       
   126 ask('autoaway');
       
   127 if($options{log}   	)   { ask($_) for @{ $groups{logging_settings} } };
       
   128 if($options{status}	)   { ask($_) for @{ $groups{status_settings}  } };
       
   129 if($options{color} 	)   { ask($_) for @{ $groups{color_settings}   } };
       
   130 if($options{tracelog}	)   { ask($_) for @{ $groups{tracelog_settings}} };
       
   131 build_config();
       
   132 
       
   133 ##
       
   134 # Prepare for work
       
   135 sub prepare
       
   136 {
       
   137 	mkdir "$ENV{HOME}/.mcabber", 0700 unless ( -d "$ENV{HOME}/.mcabber" );
       
   138 
       
   139 	parse_config() if ( -e "$ENV{HOME}/.mcabber/mcabberrc" && !$options{ignore} );
       
   140 }
       
   141 
       
   142 ##
       
   143 # Parse current user configuration and save it
       
   144 sub parse_config
       
   145 {
       
   146 	my $conf_file = "$ENV{HOME}/.mcabber/mcabberrc";
       
   147 	my $flag = 1;
       
   148 
       
   149 	open CONF, "<$conf_file" or return;
       
   150 
       
   151 	my ($key,$value);
       
   152 	while(<CONF>)
       
   153 	{
       
   154 		push @old, $_;
       
   155 		$flag = 0 if $options{ignore_auto} && m/^#BEGIN AUTO GENERATED SECTION/;
       
   156 		$flag = 1 if $options{ignore_auto} && m/^#END AUTO GENERATED SECTION/;
       
   157 		if ( $flag && m/^set\s+($key_reg)\s*=\s*(.+)$/ )
       
   158 		{
       
   159 			($key,$value) = ($1,$2);
       
   160 
       
   161 			$conf{$key} = $value if ( exists $info{$key} );
       
   162 		}
       
   163 	}
       
   164 
       
   165 	close CONF;
       
   166 	return 1;
       
   167 }
       
   168 
       
   169 ##
       
   170 # Ask the user for a setting
       
   171 sub ask
       
   172 {
       
   173 	my ($key) = @_;
       
   174 
       
   175 	my %dispatch = (
       
   176 		edit 	=> \&_ask_edit,
       
   177 		yesno 	=> \&_ask_yesno,
       
   178 		multi	=> \&_ask_multi,
       
   179 		pass	=> \&_ask_pass
       
   180 	);
       
   181 
       
   182 	my $lp = 1;
       
   183 
       
   184 	print 	"\n'$key'\n",
       
   185 		$info{$key}->{msg},"\n",
       
   186 		( defined $info{$key}->{default} ) ?
       
   187 			( $lp++, '. ', ( exists $conf{$key} ) ? 'Reset' : 'Set', " to Default [",show($key,'default'),"]\n" ) : '',
       
   188 		( exists $conf{$key} ) ? ( $lp++, ". Leave Current setting [",show($key,'current'),"]\n", $lp++ ) : $lp++ , ". ",
       
   189 		( $info{$key}->{type} eq 'pass') ? 'Enter Passowrd' : ( ( $info{$key}->{type} eq 'edit' ) ? 'Edit' : 'Set' ),
       
   190 		"\n$lp. Skip\n[choice]: ";
       
   191 
       
   192 	chomp(my $action = <STDIN>);
       
   193 	unless ( $action =~ /^\d$/ && $action >= 1 && $action <= $lp ) {
       
   194 		ask($key);
       
   195 		return;
       
   196 	}
       
   197 
       
   198 	##
       
   199 	# Edit
       
   200 	if ( $lp -1  == $action )
       
   201 	{
       
   202 		&{ $dispatch{ $info{$key}->{type} } }($key);
       
   203 	}
       
   204 
       
   205 	##
       
   206 	# Default
       
   207 	elsif ( $action == 1 )
       
   208 	{
       
   209 		$conf{$key} = $info{$key}->{default}  if defined $info{$key}->{default};
       
   210 	}
       
   211 
       
   212 	##
       
   213 	# Skip
       
   214 	elsif ( $lp == $action )
       
   215 	{
       
   216 		delete $conf{$key};
       
   217 	}
       
   218 
       
   219 	##
       
   220 	# Nothing for Leave Current setting
       
   221 
       
   222 	return 1;
       
   223 }
       
   224 
       
   225 sub _ask_yesno
       
   226 {
       
   227 	my ($key) = @_;
       
   228 
       
   229 	print "1. yes\n2. no\n[choice]: ";
       
   230 
       
   231 	chomp(my $set = <STDIN>);
       
   232 	unless ( $set =~ /^[12]$/ ) {
       
   233 		ask($key);
       
   234 		return;
       
   235 	}
       
   236 
       
   237 	$conf{$key} = $set;
       
   238 	$conf{$key} = 0 if $set eq 2;
       
   239 }
       
   240 
       
   241 sub _ask_multi
       
   242 {
       
   243 	my ($key) = @_;
       
   244 	my $count = scalar @{$info{$key}->{anwsers}};
       
   245 	my $row = sprintf("%0.f",($count/3+0.5));
       
   246 
       
   247 	for (my $i = 0; $i < $row; ++$i)
       
   248 	{
       
   249 		printf("%-25s", ($i+1) . ". " . $info{$key}->{anwsers}->[$i]);
       
   250 		printf("%-25s", ($i+$row+1) . ". " . $info{$key}->{anwsers}->[$i+$row]) if ($i+$row < $count);
       
   251 		printf("%-25s", ($i+2*$row+1) . ". " . $info{$key}->{anwsers}->[$i+2*$row]) if ($i+2*$row < $count);
       
   252 		print "\n";
       
   253 	}
       
   254 
       
   255 	print '[choice]: ';
       
   256 	chomp(my $set = <STDIN>);
       
   257 	unless ( $set =~ /^\d+$/ && $set >= 1 && $set <= $count ) {
       
   258 		ask($key);
       
   259 		return;
       
   260 	}
       
   261 	$conf{$key} = $info{$key}->{anwsers}->[$set-1];
       
   262 }
       
   263 
       
   264 sub _ask_edit
       
   265 {
       
   266 	my ($key) = @_;
       
   267 	print '[edit]: ';
       
   268 	chomp(my $set = <STDIN>);
       
   269 	unless ( $set =~ /^$info{$key}->{allow}$/ ) {
       
   270 		ask($key);
       
   271 		return;
       
   272 	}
       
   273 
       
   274 	$conf{$key} = $set;
       
   275 }
       
   276 
       
   277 sub _ask_pass
       
   278 {
       
   279 	my ($key) = @_;
       
   280 	print "Characters you type in will not be shown\n[password]: ";
       
   281 
       
   282 	ReadMode(2);
       
   283 	my $anws = ReadLine(0);
       
   284 	ReadMode(0);
       
   285 
       
   286 	ask($key) unless $anws =~ /^$info{$key}->{allow}$/;
       
   287 	chomp($anws);
       
   288 
       
   289 	$anws =~ s/^((?:\s.+)|(?:.+\s))$/"$1"/;
       
   290 	$conf{$key} = $anws;
       
   291 }
       
   292 
       
   293 ##
       
   294 # Build configuration file
       
   295 sub build_config
       
   296 {
       
   297 	my $config_file = "$ENV{HOME}/.mcabber/mcabberrc";
       
   298 
       
   299 	local *STDOUT unless $options{output} ;
       
   300 
       
   301 	unless($options{output})
       
   302 	{
       
   303 		open STDOUT,">$config_file" or die "Can't create config file";
       
   304 		chmod 0600, $config_file
       
   305 	}
       
   306 
       
   307 	my ($flag,$dumped) = (1,0);
       
   308 	for (@old)
       
   309 	{
       
   310 		$flag = 0 if m/^#BEGIN AUTO GENERATED SECTION/;
       
   311 		$flag = 1 if m/^#END AUTO GENERATED SECTION/;
       
   312 		if ( $flag )
       
   313 		{
       
   314 			print
       
   315 		}
       
   316 		elsif( !$flag && !$dumped )
       
   317 		{
       
   318 			print "#BEGIN AUTO GENERATED SECTION\n\n";
       
   319 			print "set $_ = $conf{$_}\n" for sort keys %conf;
       
   320 			print "\n";
       
   321 			$dumped = 1;
       
   322 		}
       
   323 	}
       
   324 
       
   325 	unless($dumped)
       
   326 	{
       
   327 		print "\n#BEGIN AUTO GENERATED SECTION\n\n";
       
   328 		print "set $_ = $conf{$_}\n" for sort keys %conf;
       
   329 		print "\n#END AUTO GENERATED SECTION\n";
       
   330 	}
       
   331 
       
   332 	close STDOUT unless $options{output};
       
   333 }
       
   334 
       
   335 sub show
       
   336 {
       
   337 	my ($key,$name) = @_;
       
   338 	my $value;
       
   339 
       
   340 	$value = $info{$key}->{default} 	if $name eq 'default';
       
   341 	$value = $conf{$key}		 	if $name eq 'current';
       
   342 
       
   343 	if ( $info{$key}->{type} eq 'yesno' )
       
   344 	{
       
   345 		return ( $value ) ? 'yes' : 'no';
       
   346 	}
       
   347 
       
   348 	elsif ( $info{$key}->{type} eq 'multi' )
       
   349 	{
       
   350 		return $info{$key}->{anwsers}->[$value];
       
   351 	}
       
   352 
       
   353 	else
       
   354 	{
       
   355 		return $value;
       
   356 	}
       
   357 }
       
   358 
       
   359 sub help
       
   360 {
       
   361 print<<EOF;
       
   362 Usage: $0 options
       
   363 
       
   364 This script generates configuration files for mcabber jabber client
       
   365 
       
   366 -h, --help		display this help screen
       
   367 -v, --version		display version information
       
   368 -i, --ignore		ignore previous user configuration
       
   369 -I, --ignore-auto	ignore auto generated section
       
   370 -o, --output		output to stdout instead of file
       
   371 -p, --passwd		save password in the config file (not recommended)
       
   372 -s, --ssl		ask for ssl settings
       
   373 -c, --color		ask for color settings
       
   374 -S, --status		ask for status settings
       
   375 -P, --proxy		ask for proxy settings
       
   376 -k, --keep		ping/keepalive connection settings
       
   377 -l, --log		ask for logging settings
       
   378 -t, --tracelog		ask for trace log settings
       
   379 EOF
       
   380 exit;
       
   381 }
       
   382 
       
   383 sub version
       
   384 {
       
   385 print<<EOF;
       
   386 mcwizz v0.02 coded by Karql & mulander <netprobe\@gmail.com>
       
   387 EOF
       
   388 exit;
       
   389 }
       
   390 
       
   391 # vim: set noexpandtab sts=8: