--- checkgmail-1.13/checkgmail	2008-01-17 07:30:23.000000000 +0100
+++ checkgmail/checkgmail	2008-07-22 21:57:51.000000000 +0200
@@ -5,7 +5,7 @@
 # system tray; optionally saves password in encrypted form using
 # machine-unique passphrase
 
-# version 1.12.1svn (25/6/2007)
+# version 1.13svn (14/2/2008)
 # Copyright © 2005-7 Owen Marshall
 
 # This program is free software; you can redistribute it and/or modify
@@ -31,9 +31,12 @@
 # Command-line processing
 #
 
-my ($version, $silent, $nocrypt, $update, $notsexy, $profile, $disable_monitors_check, $private, $cookies, $popup_size, $hosted_tmp, $show_popup_delay, $popup_persistence, $usekwallet, $libsexy);
+# global variables (can't be set global in the BEGIN block)
+my ($version, $silent, $nocrypt, $update, $notsexy, $profile, $disable_monitors_check, 
+		$private, $cookies, $popup_size, $hosted_tmp, $show_popup_delay, 
+		$popup_persistence, $usekwallet, $libsexy, $nologin, $mailno, $debug);
 BEGIN {
-	$version = "1.12.1svn";
+	$version = "1.13svn";
 	$silent = 1;
 	$profile = "";
 	$cookies = 1;
@@ -64,6 +67,10 @@
 				$notsexy = 1;
 				last };
 				
+			/no-login/ && do {
+				$nologin = 1;
+				last };
+				
 			/cookie_login/ && do {
 				$cookies = 1;
 				last };
@@ -105,6 +112,18 @@
 				$update = 1;
 				last };
 				
+				
+			/numbers/ && do {
+				$mailno = 1;
+				last };
+				
+			/debug/ && do {
+				$debug = 1;
+				$silent = 0;
+				last };
+				
+				
+				
 			print "CheckGmail v$version\nCopyright © 2005-7 Owen Marshall\n\n";
 			print "usage: checkgmail [-profile=profile_name] [-popup_delay=millisecs] [-hosted=hosted_domain] [-no_cookies] [-popup_persistence=millisecs] [-private] [-v | -verbose] [-nocrypt] [-no-libsexy] [-disable-monitors-check] [-update] [-h]\n\n";
 			exit 1;
@@ -158,6 +177,7 @@
 	my $failed_packages;
 		
 	my $eval_sub = sub {
+		print "$_\n" if $debug;
 		eval $_;
 		if ($@) {
 			return unless m/(use|require)\s*([\w:-]+).*?\;/;
@@ -176,6 +196,7 @@
 	use Thread::Queue;
 	use Thread::Semaphore;
 	use threads::shared;
+	use Encode;
 	use XML::Simple;
 	use FileHandle;
 	use LWP::UserAgent;
@@ -355,14 +376,17 @@
 my $fat_lady = new Thread::Semaphore(0);
 my $child_exit : shared = 0; # to signal exit to child
 
+print "About to start new thread ...\n" if $debug;
 # Start http checking thread ...
 my $http_check = new threads(\&http_check);
+print "Parent: Process now continues ...\n" if $debug;
 
 
 #######################
 # Prefs and Variables
 #
 
+print "Parent: Setting up global variables ...\n" if $debug;
 # Prefs hash
 my %pref_variables = (
 	user => \$user,
@@ -430,6 +454,7 @@
 my $status_label;
 # my $message_flag;
 
+print "Parent: Checking the existence of ~/.checkgmail ...\n" if $debug;
 # Create the default .checkgmail directory and migrate prefs from users of older versions
 unless (-d $prefs_dir) {
 	if (-e "$prefs_dir") {
@@ -452,6 +477,7 @@
 # Icons
 #
 
+print "Parent: Loading icon data ...\n" if $debug;
 # we load the pixmaps as uuencoded data
 my ($error_data, $no_mail_data, $mail_data, $compose_mail_data);
 load_icon_data();
@@ -462,10 +488,12 @@
 
 my $image = Gtk2::Image->new_from_pixbuf($no_mail_pixbuf);
 
+
 ##############
 # Setup tray
 #
 
+print "Parent: Setting up system tray ...\n" if $debug;
 my $tray = Gtk2::TrayIcon->new("gmail");
 my $eventbox = Gtk2::EventBox->new;
 my $tray_hbox = Gtk2::HBox->new(0,0);
@@ -488,14 +516,25 @@
 $eventbox->add($tray_hbox);
 $tray_hbox->pack_start($image,0,0,0);
 
+# number of new mail messages (use -numbers)
+my $number_label;
+if ($mailno) {
+	$number_label = Gtk2::Label->new;
+	$number_label->set_markup("0");
+	$tray_hbox->pack_start($number_label,0,0,0);
+	$number_label->hide;
+}
+
 
 ########################
 # Read prefs and login
 #
 
+print "Parent: Reading translations ...\n" if $debug;
 # Read translations if they exist ...
 read_translations();
 
+print "Parent: Reading prefs ...\n" if $debug;
 # First time configuration ...
 unless (read_prefs()) {
 	show_prefs();
@@ -532,7 +571,7 @@
 
 $tray->add($eventbox);
 $tray->show_all;
-
+print "Parent: System tray now complete ...\n" if $debug;
 
 ############################
 # enter/leave notification
@@ -603,6 +642,7 @@
 # All set? Let's login ...
 #
 
+print "Parent: Sending semaphore to child process ...\n" if $debug;
 # She's singing ...
 $fat_lady->up;
 
@@ -675,6 +715,7 @@
 
 sub http_check {
 	# Threaded process for sending HTTP requests ...
+	print "Child: Checking thread now starting ... waiting for semaphore to continue\n" if $debug;
 	
 	# Variable initialisation isn't over until the fat lady sings ...
 	$fat_lady->down;
@@ -716,14 +757,21 @@
 			unless ($hosted) {
 				# Normal Gmail login action ...
 				$error = http_get("https://www.google.com/accounts/ServiceLoginAuth?ltmpl=yj_wsad&ltmplcache=2&continue=https%3A%2F%2Fmail.google.com%2Fmail%3F&service=mail&rm=false&ltmpl=yj_wsad&Email=$URI_user&Passwd=$URI_passwd&rmShown=1&null=Sign+in", "LOGIN");
-				# $cookie_jar->scan(\&scan_at);
+
+				$cookie_jar->scan(\&scan_at);
+				unless ($error || !$gmail_sid || !$gmail_gausr) {
+					$error = http_get("https://mail.google.com/mail/?auth=$gmail_sid&gausr=$gmail_gausr", 'GET');
+				}
+
 				# $error = http_get("https://mail.google.com/mail?nsr=0&auth=$gmail_sid&gausr=$gmail_gausr", "LOGIN");
 	
 			} else {
 				# hosted domains work differently ...
 				# First we POST a login
-				$error = http_get("https://www.google.com/a/$hosted/LoginAction|at=null&continue=http%3A%2F%2Fmail.google.com%2Fa%2F$hosted&service=mail&userName=$URI_user&password=$URI_passwd", "POST");
-				
+				# $error = http_get("https://www.google.com/a/$hosted/LoginAction|at=null&continue=http%3A%2F%2Fmail.google.com%2Fa%2F$hosted&service=mail&userName=$URI_user&password=$URI_passwd", "POST");
+				# thanks to Olinto Neto for this fix for hosted domains:
+				$error = http_get("https://www.google.com/a/$hosted/LoginAction2|at=null&continue=http%3A%2F%2Fmail.google.com%2Fa%2F$hosted&service=mail&Email=$URI_user&Passwd=$URI_passwd", "POST");
+
 				# Then we grab the HID ("Hosted ID"?) cookie
 				$cookie_jar->scan(\&scan_at);
 				
@@ -1045,9 +1093,9 @@
 			my $author = $author_name || $author_mail;
 						
 			# clean text for Pango compatible display ...
-			$title = clean_text($title);
-			$author = clean_text($author);
-			$summary = clean_text($summary);
+			$title = clean_text_and_decode($title);
+			$author = clean_text_and_decode($author);
+			$summary = clean_text_and_decode($summary);
 			
 			my ($year, $month, $day, $hour, $min, $sec) = ($issued =~ m/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)Z/);
 			my $time_abs = $sec+$min*60+$hour*3600+$day*86400+$month*2678400+$year*32140800;
@@ -1089,7 +1137,7 @@
 		# Create popup text
 		if ($popup_authors) {
 			$popup_authors =~ s/, ([\w\s-]+)$/ $trans{notify_and} $1/; # replace final comma with "and"
-			$popup_authors = clean_text($popup_authors);
+			$popup_authors = clean_text_and_decode($popup_authors);
 			$popup_text = "<span foreground=\"#000000\"><small>$trans{notify_new_mail}$popup_authors ...</small></span>";
 		}
 		
@@ -1111,6 +1159,32 @@
 	return 1;
 }
 
+# Note -- for some reason (?? why ??) the title does not need decoding; all other data apparently does.  Very strange ...
+sub clean_text_and_decode {
+	($_) = @_;
+	# some basic replacements so that the text is readable ...
+	# (these aren't used by pango markup, unlike other HTML escapes)
+	s/&hellip;/\.\.\./g;
+	s/&\s/\&amp; /g;
+	s/\\u003c/</g;
+	# s/\\n//g;
+	s/<br\s*\/*\\*>/\n/g;
+	s/</\&lt;/g;
+	s/>/\&gt;/g;
+	s/&(?>([\w\d\,\.]+))(?!;)/\&amp;$1/g; #not a great fix, but at least it's simple (heavy irony ... :)
+	s/&nbsp;/ /g;
+	# s/\\n/\n/g;
+	# Encode::from_to($body, 'utf-8', 'iso-8859-15');
+	# eval { decode("utf8", $_, Encode::FB_CROAK); };
+	# my $body_decode = $@ ? $_ : decode("utf8", $_);
+	#my $body_decode= decode("utf-8", $_);
+	
+	# I have no idea why this works ...
+	my $body_encode = encode("utf8", $_);
+	my $body_decode = decode("utf8", $body_encode);
+	
+	return $body_decode;
+}
 
 sub clean_text {
 	($_) = @_;
@@ -1129,6 +1203,7 @@
 	return $_;
 }
 
+
 sub clean_text_body {
 	($_) = @_;
 	# some basic replacements so that the text is readable ...
@@ -1164,9 +1239,13 @@
 	# s/&(?>([\w\d\,\.]+))(?!;)/\&amp;$1/g; #not a great fix, but at least it's simple (heavy irony ... :)
 	s/&nbsp;/ /g;
 	# s/\\n/\n/g;
-	use Encode;
 	# Encode::from_to($body, 'utf-8', 'iso-8859-15');
-	my $body_decode= decode("utf-8", $_);
+	# eval { decode("utf8", $_, Encode::FB_CROAK); };
+	# my $body_decode = $@ ? $_ : decode("utf8", $_);
+	# my $body_encode = encode("utf8", $_);
+	
+	# I have no idea why this works either ...
+	my $body_decode = decode("utf8", $_);
 
 	return $body_decode;
 }
@@ -1335,6 +1414,14 @@
 	my ($status, $text) = @_;	
 	my $new_mail = @messages;
 	
+	# display number of messages
+	if ($mailno) {
+		print "setting \"$new_mail\"\n";
+		$number_label->set_markup("$new_mail");
+		$new_mail ? $number_label->show : $number_label->hide;
+		# $new_mail ? $number_label->set_markup("$new_mail") : $number_label->set_markup(" ")
+	}
+	
 	my @sorted_messages = sort {$b->{time} <=> $a->{time}} @messages;
 	@messages = @sorted_messages;
 	
@@ -1497,8 +1584,7 @@
 			$title_l_ebox->signal_handler_disconnect($s2);
 			unless ($mb) {
 				# yep, here's the magic code.  This accesses the datapack, which we read with a little hack in the check routine ...
-				$request->enqueue("GET:".gen_prefix_url()."/?ui=1&view=cv&search=all&th=$id&ww=1173&qt=&prf=1&pft=undefined&rq=xm&at=$gmail_at");
-				# $request->enqueue("GET:".gen_prefix_url()."/?ui=2&ik=b8bb7ca6af&view=cv&search=all&th=$id&rt=j&prf=1");
+				$request->enqueue("GET:".gen_prefix_url()."/?ui=1&view=cv&search=all&th=$id&qt=&prf=1&pft=undefined&rq=xm&at=$gmail_at");
 			} else {
 				# this allows the message text to be toggled ...
 				# oh yes, we're all about usability here, folks! :)
@@ -1684,6 +1770,7 @@
 		show_notify($win_notify_temp);
 	} else {
 		$win_notify->destroy if $win_notify;
+		$win_notify="";
 		$win_notify = $win_notify_temp;
 	}
 }
@@ -1742,6 +1829,11 @@
 sub get_login_href {
 	# Login directly to gmail ...
 	$_ = shift;
+	
+	# The following is for people who like to stay permanently logged in ...
+	# (enable with -no-login on the command-line for now ...)
+	return $_ if $nologin;
+	
 	my ($options) = m/.*?\?(.*)/;
 	my $options_uri = $options ? "?&$options" : "";
 	
@@ -1908,6 +2000,7 @@
 
 	if ($win_notify_temp) {
 		$win_notify->destroy;
+		$win_notify="";
 		$win_notify=$new_win;
 	}