#!/usr/bin/perl
#
# Catch22 (llama@osoal.org.nz)
# One Short Of A Llama [http://web.osoal.org.nz]

use strict;
use DBI;
use OneShortOfALlama;

#
#
#

my($comments, $news, $boxes, @leftside, @rightside, %finger);

$comments  = 5;
$news      = 1;
$boxes     = 'Lastposts Vote Finger Horoscopes Login';

@leftside  = (
	      \&lastposts_form,
	      \&voting_form,
	      \&search_form
	      );
@rightside = (
	      \&horoscopes_form,
	      \&finger_form,
	      \&login_form,
	      \&misc_form
	      );

%finger    = (
	      'DeadBeef'    => 'lincoln',
	      'Kismet'      => 'david',
	      'Catch22'     => 'llama',
	      'maharG'      => 'graham',
  	      'ZenSandwich' => 'colin'
	      );

#
#
#

sub voting_form
{
    my($dbh, $sth, $pid, $question, $aid, $answer, $boxes);

	$dbh   = shift(@_);
	$boxes = shift(@_);

    print('<table width="160" cellspacing="0" cellpadding="0" border="0">
           <tr>
           <td>
               <a href="/vote.pl">
               <img src="/pics/icons/vote.png" alt="Vote" border="0"></a>
           </td>
           </tr>', "\n\n");

	if ($boxes =~ m/Vote/)
	{
		($pid, $question) = $dbh->selectrow_array('select pid, question from osoal.pollquestions
                                                   order by time desc limit 1');

		if ($pid)
		{
			print('<tr>
                   <td>
                       <form method="GET" action="/vote.pl">
                       <input type="hidden" name="pid" value="', $pid, '">
                       <em><small>', $question, '</small></em></td>
                   </tr>', "\n\n");

			$sth = $dbh->prepare('select aid, answer from osoal.pollanswers
                                  where (pid = ' . $dbh->quote($pid) . ') order by aid');
			$sth->execute();
			$sth->bind_columns(undef(), \$aid, \$answer);
		
			while ($sth->fetch())
			{
				print('<tr>
                       <td>
                           <input type="radio" name="aid" value="', $aid, '"');

				if ($aid == 1)
				{
					print(' checked');
				}
			
				print('> <small>', $answer, '</small></td>
                       </tr>', "\n\n");
			}
		}

		print('<tr>
               <td>
                   <small><input type="submit" value="Vote"></small>
                   [ <a href="/vote.pl?pid=', $pid, '"><small>Results</small></a>
                   | <a href="/vote.pl"><small>Polls</small></a> ]</td>
               <tr>
               <td>
                   </form>
                   <hr noshade></td>
               </tr>');
	}
	
	print('</table>', "\n\n");
}

sub lastposts_form
{
    my($dbh, $sth, $pid, $nickname, $time, $boxes, $match);

	$dbh   = shift(@_);
	$boxes = shift(@_);
	$match = ($boxes =~ m/Lastposts/);

    print('<table width="160" cellspacing="0" cellpadding="0" border="0">
           <tr>
           <td colspan="2">
               <a href="/comments.pl">
               <img src="/pics/icons/comments.png" alt="Comments" border="0"></a>
           </td>
           </tr>', "\n\n");

	if ($match)
	{
		$sth = $dbh->prepare('select pid, nickname, from_unixtime(time, "%a %d %b %H:%i")
                              from osoal.comments order by pid desc limit ' . $comments);
		$sth->execute();

		if ($sth->rows())
		{
			$sth->bind_columns(undef(), \$pid, \$nickname, \$time);

			while ($sth->fetch())
			{
				print('<tr>
                       <td align="left">
                           <a href="/comments.pl?pid=', $pid, '#', $pid, '">', shrink($nickname, 10), '</a></td>
                        <td align="right">
                        <small>', $time, '</small></td>
                        </tr>', "\n\n");
			}
		}
	}

	print('<tr>
           <td colspan="2">');

	if ($match)
	{
		print('<br>');
	}

	print('<a href="/news.pl">
               <img src="/pics/icons/news.png" alt="News" border="0"></a></td>
           </tr>', "\n\n");

	if ($match)
	{
		$sth = $dbh->prepare('select pid, nickname, from_unixtime(time, "%a %d %b %H:%i")
                              from osoal.news order by pid desc limit ' . $news);
		$sth->execute();

		if ($sth->rows())
		{
			$sth->bind_columns(undef, \$pid, \$nickname, \$time);
			
			while ($sth->fetch())
			{
				print('<tr>
                       <td align="left">
                           <a href="/news.pl?#', $pid, '">', shrink($nickname, 10), '</a></td>
                        <td align="right">
                           <small>', $time, '</small></td>
                        </tr>', "\n\n");
			}
		}
	
		print('<tr>
               <td colspan="2">
                   <hr noshade></td>
               </tr>');
	}

	print('</table>', "\n\n");
}

sub finger_form
{
    my($dbh, $boxes, $nickname, $dir, $time, $month, $date, $day, @days, @months);

	$dbh   = shift(@_);
	$boxes = shift(@_);

	@days   = qw/Sun Mon Tue Wed Thu Fri Sat/;
	@months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;

    print('<table width="160" cellspacing="0" cellpadding="0" border="0">
           <tr>
           <td colspan="2">
               <a href="/finger.pl">
               <img src="/pics/icons/finger.png" alt="Finger" border="0"></a>
           </td>
           </tr>', "\n\n");

	if ($boxes =~ m/Finger/)
	{
		while (($nickname, $dir) = each(%finger))
		{
			if (-e "/home/$dir/.plan")
			{
				(undef, undef, undef, undef, undef, undef, undef,
				 undef, undef, $time, undef, undef, undef) = stat("/home/$dir/.plan");

				(undef, undef, undef, $date, $month, undef, $day, undef, undef) = localtime($time);
				$time = sprintf("%s %.2d %s", $days[$day], $date, $months[$month]);
			}
			else
			{
				next;
			}

			print('<tr>
                   <td align="left">
                       <a href="/finger.pl?user=', $nickname, '">', $nickname, '</a></td>
                    <td align="right">
                        <small>', $time, '</small></td>
                   </tr>', "\n\n");
		}


		print('<tr>
               <td colspan="2">
                   <hr noshade></td>
               </tr>');
	}

	print('</table>', "\n\n");
}

sub horoscopes_form
{
	my($date, $id, $dbh, $boxes);

	$dbh   = shift(@_);
	$boxes = shift(@_);

	print('<table width="160" cellpadding="0" cellspacing="0" border="0">
           <tr>
               <td>
                <a href="/horoscopes.pl">
                <img src="/pics/icons/horoscopes.png" alt="Horoscopes" border="0"></a>
               </td>
            </tr>', "\n\n");

	if ($boxes =~ m/Horoscopes/)
	{
		print('<tr>
                   <td>');

		($id, $date) = $dbh->selectrow_array('select id, from_unixtime(time, "%W %D %M")
                                              from osoal.horoscopes order by id desc limit 1');

		if ($id)
		{
			print('<a href="/horoscopes.pl?id=', $id, '">', $date, '</a>');
		}
	
		print('</td>
               </tr>

               <tr>
                   <td><hr noshade></td>
               </tr>');
	}

	print('</table>', "\n\n");
}

sub login_form
{
	my($dbh, $uid, $nickname, $boxes, $i, $cookie);

	foreach $i ($dbh, $boxes, $cookie, $uid)
	{
		$i = shift(@_);
	}

	print('<table width="160" cellspacing="0" cellpadding="0" border="0">
           <tr>
               <td colspan="2">
                   <a href="/login.pl">
                   <img src="/pics/icons/login.png" alt="Login" border="0"></a>
                </td>
           </tr>', "\n\n");

	if ($boxes =~ m/Login/)
	{
		if ($uid)
		{
			print('<tr>
                   <td align="left" colpspan="2">
                   ', shrink($cookie->{'nickname'}, 10), '
                   [ <a href="/login.pl"><small>Menu</small></a> | <a href="/login.pl?select=Logout"><small>Logout</small></a> ]
                   </td>
                   </tr>', "\n\n");
		}
		else
		{
			print('<tr>
                   <td align="left">
                       <form method="POST" action="/login.pl">
                       <small>Nickname</small></td>
                   <td align="right">
                       <input type="text" name="nickname" value="" size="12"></td>
                   </tr>

                   <tr>
                   <td align="left">
                       <small>Password</small></td>
                   <td align="right">
                       <input type="password" name="passwd" value="" size="12"></td>
                   </tr>

                   <tr>
                   <td colspan="2">
                          <small><input type="submit" name="select" value="Login"></small>
                          [ <a href="/login.pl"><small>Menu</small></a>
                          | <a href="/login.pl?select=Create"><small>Create</small></a> ]
                   
                   </td>
               </tr>', "\n\n");
		}

		print('<tr>
               <td colspan="2">
                   </form>
                   <hr noshade></td>
           </tr>');
	}

	print('</table>', "\n\n");
}

sub search_form
{
	print('<table width="160" cellspacing="0" cellpadding="0" border="0">
           <tr>
               <td><form method="GET" action="/search.pl">
                   <a href="/search.pl">
                   <img src="/pics/icons/search.png" alt="Search" border="0"></a>
               </td>
           </tr>
           <tr>
               <td align="center">
                    <input type="hidden" name="location" value="News Finger Comments">
                    <input type="text" name="search" value="" size="16"></td>
                </tr>
           <tr>
               <td></form><hr noshade></td>
           </tr>
           </table>', "\n\n");
}

sub misc_form
{
	print('<table width="160" cellspacing="0" cellpadding="0" border="0">
           <tr>
               <td><a href="/history.pl">
                   <img src="/pics/icons/history.png" alt="History" border="0"></a>
                </td>
           </tr>

           <tr>
               <td><a href="/manifest.pl">
                   <img src="/pics/icons/manifest.png" alt="File Manifest" border="0"></a>
                </td>
           </tr>

           <tr>
               <td><a href="/pics/bohemian/includes/acts.pl">
                   <img src="/pics/icons/pictures.png" alt="Pictures" border="0"></a>
               </td>
           </tr>

           <tr>
               <td><a href="/contact.html">
                   <img src="/pics/icons/contact.png" alt="Contact" border="0"></a>
               </td>
           </tr>

           <tr>
               <td><a href="/homepages.html">
                   <img src="/pics/icons/homepages.png" alt="Homepages" border="0"></a>
               </td>
           </tr>

           <tr>
               <td><a href="http://rfc.osoal.org.nz
                   <img src="/pics/icons/rfc.png" alt="RFCs" border="0"></a>
               </td>
           </tr>

           <tr>
               <td><a href="/links.html">
                   <img src="/pics/icons/links.png" alt="Links" border="0"></a>
               </td>
           </tr>

           <tr>
               <td><hr noshade></td>
           </tr>
           </table>', "\n\n");
}

sub cartoon
{
    my($dbh, $i, $id, $title);

    foreach $i ($dbh)
    {
	$i = shift(@_);
    }

    if (($id, $title) = $dbh->selectrow_array('select id, title from cartoons order by id desc limit 1'))
    {
	print('<br>
               <font face="Arial, Helvetica, Sans Serif" size="+1"><em><b>',
               quote($title),
               '</b></em></font>
	       <a href="/cartoons.pl">
	       <img src="/pics/cartoons/', $id, '.png" alt="cartoon" border="0">
               </a>',
               "\n");
    }
}

sub thought
{
	my($dbh, $entries, $id, $buf);

	$dbh     = shift(@_);
	$entries = $dbh->selectrow_array('select count(*) from babble');

	$id      = (rand(1 << 31) % $entries) + 1;
	$buf     = $dbh->selectrow_array("select message from babble where (id = $id)");
 
	print(quote($buf));
}

sub shrink
{
    my($buf, $size);

    $buf  = shift(@_);
    $size = shift(@_);

    if (length($buf) > $size)
    {
		$buf = substr($buf, 0, $size - 3) . '...';
    }

    return ($buf);
}

sub sidebar
{
	my($buf, $i, $cookie, $dbh, $uid, $boxes);

	foreach $i ($dbh, $boxes, $cookie, $uid, $buf)
	{
		$i = shift(@_);
	}

	print('<td width="160" cellpadding="0" cellspacing="0" border="0" valign="top">', "\n\n");

	foreach $i (@$buf)
	{
		&$i($dbh, $boxes, $cookie, $uid);
	}

	print('</td>', "\n\n");
}

#
#
#

sub main
{
	my($cookie, $uid, $gid, $dbh, $buf);

    #
	header('', 'oneshortofallama.png', '');

	if ($dbh = DBI->connect("dbi:mysql:$MySQLserver;database=osoal",
				$MySQLuser, $MySQLpasswd))
	{
		$cookie      = cookie('osoal');
		($uid, $gid) = authenticate($cookie->{'nickname'}, $cookie->{'passwd'});

		if ($uid)
		{
			$buf = $dbh->selectrow_array("select boxes from osoal.users where (uid = $uid)");
		}
		else
		{
			$buf = $boxes;
		}

		print('<table align="center" border="0" cellspacing="0" cellpadding="5">
               <tr>', "\n\n");

		#
		sidebar($dbh, $buf, $cookie, $uid, \@leftside);

		print('<td width="300" align="center" valign="top">

               <br>

               <img src="/pics/misc/osoal.jpg" alt="One Short Of A Llama"><br>
               <em>Powered By Geeks</em><br>

               <br>
               <br><small>');

		thought($dbh);
		print('</small><br><br>', "\n\n");

		cartoon($dbh);
		print('<br></td>', "\n\n");

		#
		sidebar($dbh, $buf, $cookie, $uid, \@rightside);

		print('</tr>
               </table>', "\n\n");
		$dbh->disconnect();
	}

	footer();
}

main();
