⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions.in

📁 geektalkd是一个简单的聊天服务器
💻 IN
📖 第 1 页 / 共 3 页
字号:
#!/usr/bin/perl -wuse strict 'vars';##@use Net::Ident;use vars qw($VERSION $FUNCVERS $FUNCDATE				%stats %handles %users				$bits $rehash $filenum $order				$logging $debug $defaultport $password $codeon $access $aclfile);# These flags can be accessed from either the %handles hash, or from the %users# hash (as soon as a username and hostname have been provided, the values will# be placed in %users).  To access a particular flag, use:# $handles{<filehandlename>}{<flagname>}# or# $users{<username>}{<flagname>}# i.e. $handles{FILE87}{channel} == $users{plumpy}{channel}# FLAG      : definition##IDENTIFICATION# handle      The name of the filehandle associated with the user.#             gethostbyaddr().# username    The username (as specified by the user).# hostname    The hostname (as specified by the user).##CONNECTION INFO##@# ident       The result of an identd query on the connection.# rhostname   The real hostname (or undef) as returned by gethostbyaddr();# ipaddr      The real IP address of the connection.# rport       The (remote) port used for the connection.##USER SETTINGS# channel     The channel the user is currently on.# echo        The users' echoing setting (1 or 0)# times       The users' timestamp setting (1 or 0)# read        Whether or not the user has read permissions.# write       Whether or not the user has write permissions.# %{ignore}   Users that this user is ignoring.#             i.e.: $users{plumpy}{ignore}{meanie}#                   $users{plumpy}{ignore}{jerkz}##USER SETTINGS# spoke       The last time() the user spoke publically.# connect     The time the user connected.# lines       The number of lines processed for this handle.# spoken      The number of lines publically spoken by this handle.##SIGNON INFO# flag        A value from 0 - 4 indicating:#             0: User has just connected.#             1: User has sent a username with "u=username" or a hostname with#                "h=hostname", but not both.  Awaiting the other part, and then#                the flag will be switched to 4.#             2: User has issued a /signon command without setting a username.#                The user was asked for a username, which we are now awaiting,#                and then we will jump to flag 3.#             3: User has issued a /signon command without setting a hostname.#                The user was asked for a hostname, which we are now awaiting,#                and then we will jump to flag 5.#             4: User is signed off.#             5: User is signed on.# Catch this bastard signal.$SIG{PIPE} = sub {  logmsg("SERVER: SIGPIPE received and promptly ignored.") };# Some globals.$FUNCVERS = "1.24";$FUNCDATE = "10-Feb-2003";# Set these yourself!!!# perl -e 'print crypt("password", "m0"), "\n";'$password = "rUycwUSRsoKxs" if(!defined $password);# ***  about()  ***# Prints author and copyright info.sub about {	my $handle = shift;	toHandle($handle, "*", " " . dateStr());	toHandle($handle, "* Geektalkd version $VERSION");	toHandle($handle, "* functions version $FUNCVERS ($FUNCDATE)");	toHandle($handle,"* Copyright (C) 1999 Michael Plump <plumpy\@skylab.org>");	toHandle($handle, "*");}# ***  broadcastChan()  ***# Sends a line of data to every client who is on the current channel.sub broadcastChan {	my ($handle, $channel, $pretime, $time, $posttime) = @_;	foreach my $chandle (keys %handles) {		# if the user is in the same channel		if($handles{$chandle}{channel} == $channel			# and is signed in...			&& $handles{$chandle}{flag} == 5			# and has read permission (or this is his own message)			&& ($handles{$chandle}{read} != 0 || $handle eq $chandle)			# and if he is not ignoring the user			&& (!exists $handles{$chandle}{ignore}{$handles{$handle}{username}})			# and if this is his message and he has echo on			&& ($handle ne $chandle || $handles{$chandle}{echo} == 1)) {				# then we can send the message!				toHandle($chandle,$pretime,$time,$posttime)		}	}}# ***  broadcastMsg()  ***# Sends a line of data to every client who is signed on.sub broadcastMsg {	my ($pretime, $time, $posttime) = @_;	foreach my $chandle (keys %handles) {		if($handles{$chandle}{flag} == 5) {			toHandle($chandle,$pretime,$time,$posttime)		}	}}# ***  channel()  ***# Accepts a handle and a channel number as arguments, and changes the users# Current channel, or prints errors where applicible.sub channel {	my ($handle, $channel) = @_;	if($handles{$handle}{flag} != 5) {		notOn($handle);	} elsif(!defined $channel) {		$channel = $handles{$handle}{channel};		toHandle($handle, "", "<".dateStr()."> ",					"* You are on channel $channel.");	} elsif($channel !~ /^-?\d+$/ || $channel < -2147483648			|| $channel > 2147483647 || $channel == 0) {		toHandle($handle, "", "<".dateStr()."> ",					"* Invalid channel number.");	} elsif($channel == $handles{$handle}{channel}) {		toHandle($handle, "", "<".dateStr()."> ",					"* You are already on channel $channel.");	} else {		my $oldchannel = $handles{$handle}{channel};		my $username = $handles{$handle}{username};		my $hostname = $handles{$handle}{hostname};		broadcastChan($handle,$channel,"|", dateStr() . " ",					"change| $username@$hostname ($username) has joined this "					. "channel.");		$handles{$handle}{channel} = $channel;		broadcastChan($handle,$oldchannel,"|", dateStr() . " ",					"change| $username@$hostname ($username) has left this "					. "channel.");		toHandle($handle, "", "<".dateStr()."> ",					"* You are now on channel $channel.");	}}# ***  cmd()  ***# Pass a command name and a line as arguments.  This function will return true# if the line contained the /command.sub cmd {	my ($cmd, $line) = @_;	return 1 if($line =~ /^\/(.+?)\b/ && $1 eq substr($cmd, 0, length($1)));}# ***  dateStr()  ***# Constructs and returns a date string in the format hh:mmPM for broadcast to# the user.sub dateStr {	# Patch for perl 5.00404 by Jade Nicoletti <phaethon@phaethon.ch>:	my ($min,$hour) = (localtime)[1,2];	# End	my $apm = "AM";	$apm = "PM" if $hour >= 12;	$hour -= 12 if $hour > 12;	$hour = 12 if $hour == 0;	my $datestr = sprintf("%02d:%02d%s", $hour, $min, $apm);	return $datestr;}# *** doLoginProcessing()  ***# This is called from processInput if the user hasn't set a username and# hostname yet (IOW, when the flag < 4).  It steps through the various methods# for getting a username and hostname, maintaining both backwards compatibility# with chatd and trying to make it a little easier (so if they don't know the# proper protocol, they can still set a username with "/signon")sub doLoginProcessing {	my($handle, $line) = @_;	my $temp;	if($handles{$handle}{flag} < 2) {		if($line =~ /^u=(.*)$/) {			$temp = $1;			if($temp =~ /^[A-Za-z0-9_\-\.]{1,15}$/) {				if(!exists $users{$temp}) {					$handles{$handle}{username} = $temp;					$users{$temp} = $handles{$handle};					$handles{$handle}{flag} = 						($handles{$handle}{flag} == 0) ? 1 : 4;					logmsg("USER: Handle $handle now has the username $temp.");				} else {					toHandle($handle, "", "<".dateStr()."> ",								"* That username is taken; please specify a new one "								. "with \"u=username\".");				}			} else {				toHandle($handle, "", "<".dateStr()."> ",							"* Invalid username.  Please specify a new one "							. "with \"u=username\".");				toHandle($handle, "", "<".dateStr()."> ",							"* Valid usernames are from the set: "							. "/^[A-Za-z0-9_\\-\\.]{1,15}\$/");			} 		} elsif($line =~ /^h=(.*)$/) {			$temp = $1;			if($temp =~ /^[A-Za-z0-9\-\.]{1,26}$/) {				$handles{$handle}{hostname} = $temp;				$handles{$handle}{flag} = 					($handles{$handle}{flag} == 0) ? 1 : 4;			} else {				toHandle($handle, "", "<".dateStr()."> ",							"* Invalid hostname.  Please specify a new "							. "one with \"h=hostname\".");				toHandle($handle, "", "<".dateStr()."> ",							"* Valid hostnames are from the set: "							. "/^[A-Za-z0-9\\-\\.]{1,26}\$/");			}		} elsif(cmd("signon", $line)) {			my @ARGV = split /\s+/, $line, 3;			my $error = 0;			if(defined $ARGV[1]) {				if($ARGV[1] !~ /^-?\d+$/ || $ARGV[1] < -2147483648					|| $ARGV[1] > 2147483647 || $ARGV[1] == 0) {					toHandle($handle, "", "<".dateStr()."> ",								"* Invalid channel number.");					$error = 1;				} else {						$handles{$handle}{channel} = $ARGV[1];				}			}			if($error == 0) {				if(!defined $handles{$handle}{username}) {					toHandle($handle, "", "<".dateStr()."> ",								"* Please enter a username to signon.");					$handles{$handle}{flag} = 2;				} elsif(!defined $handles{$handle}{hostname}) {					toHandle($handle, "", "<".dateStr()."> ",								"* Please enter a hostname to signon.");					$handles{$handle}{flag} = 3;				}			}		} else {			toHandle($handle, "", "<".dateStr()."> ",						"* Use /signon to sign on.");		}	} elsif($handles{$handle}{flag} == 2) {		if($line =~ /^h=(.*)?$/) {			$temp = $1;			if($temp =~ /^[A-Za-z0-9\-\.]{1,26}$/) {				$handles{$handle}{hostname} = $temp;				toHandle($handle, "", "<".dateStr()."> ",							"* Please enter a username to signon.");			} else {				toHandle($handle, "", "<".dateStr()."> ",							"* Invalid hostname. Use the set: "							. "/^[A-Za-z0-9\\-\\.]{1,26}\$/");				toHandle($handle, "", "<".dateStr()."> ",							"* Please enter a username to signon.");			}		} elsif($line =~ /^(u=)?([A-Za-z0-9_\-\.]{1,15})$/) {			if(!exists $users{$2}) {				$handles{$handle}{username} = $2;				$users{$2} = $handles{$handle};				logmsg("USER: Handle $handle now has the username $2.");				if(defined $handles{$handle}{hostname}) {					signon($handles{$handle}{username});				} else {					$handles{$handle}{flag} = 3;					toHandle($handle, "", "<".dateStr()."> ",								"* Please enter a hostname to signon.");				}			} else {				toHandle($handle, "", "<".dateStr()."> ",							"* That username is taken; please specify another one.");			}		} else {			toHandle($handle, "", "<".dateStr()."> ",						"* Invalid username.  Use the set: " .						"/^[A-Za-z0-9_\\-\\.]{1,15}\$/");		}	} elsif($handles{$handle}{flag} == 3) {		if($line =~ /^(h=)?([A-Za-z0-9-\.]{1,26})$/) {			$handles{$handle}{hostname} = $2;			signon($handles{$handle}{username});		} else {			toHandle($handle, "", "<".dateStr()."> ",						"* Invalid hostname.  Use the set: " .						"/^[A-Za-z0-9\\-\\.]{1,26}\$/");		}	}}# ***  echo()  ***# Accepts a handle and another argument, and turns echoing on or off.sub echo {	my ($handle, $boolean) = @_;	$boolean = lc($boolean);	if($handles{$handle}{flag} != 5) {		notOn($handle);	} elsif((!defined $boolean || $boolean eq "") && $boolean ne "on"				&& $boolean ne "off" && $boolean ne "1" && $boolean ne "0") {		if($handles{$handle}{echo} == 0) {			$handles{$handle}{echo} = 1;			toHandle($handle, "", "<".dateStr()."> ", "* Echoing is now ON.");		} else {			$handles{$handle}{echo} = 0;			toHandle($handle, "", "<".dateStr()."> ", "* Echoing is now OFF.");		}	} elsif($boolean eq "on" || $boolean eq "1") {		$handles{$handle}{echo} = 1;		toHandle($handle, "", "<".dateStr()."> ", "* Echoing is now ON.");	} elsif($boolean eq "off" || $boolean eq "0") {		$handles{$handle}{echo} = 0;		toHandle($handle, "", "<".dateStr()."> ", "* Echoing is now OFF.");	} else {		toHandle($handle, "", "<".dateStr()."> ", "* Invalid argument.");	}}# *** getConnect()  ***# Called from processClients() whenever incoming activity is detected on the# socket.  Opens and set up a new filehandle for an incoming connection.sub getConnect {	my $paddr;	my $fhandle = "FILE" . ++$filenum;	$paddr = accept("$fhandle",Server);	my($port,$iaddr) = sockaddr_in($paddr);	my $name = gethostbyaddr($iaddr,AF_INET);	my $ipaddr = inet_ntoa($iaddr);##@	my $ident = Net::Ident::lookup($fhandle, 5);##@	$ident = "unknown user" if(!defined $ident);	if(defined $access) {		my $allowed = verifyHost($iaddr);		if(!$allowed) {			toHandle($fhandle, "|geektalkd $FUNCDATE| Access forbidden.");			close($fhandle);			logmsg("REFUSED CONNECTION",##@						"by $ident",						"from $name [$ipaddr] at port $port using handle $fhandle.");			return;		}	}	logmsg("CONNECTION",##@				"by $ident",				"from $name [$ipaddr] at port $port using handle $fhandle.");	toHandle($fhandle, "|geektalkd $FUNCDATE| Type /help for help.");		$fhandle->autoflush();	$handles{$fhandle}{handle} = $fhandle;	$handles{$fhandle}{ipaddr} = $ipaddr;	$handles{$fhandle}{rhostname} = $name;##@	$handles{$fhandle}{ident} = $ident;	$handles{$fhandle}{rport} = $port;	$handles{$fhandle}{flag} = 0;	$handles{$fhandle}{connect} = time();	$handles{$fhandle}{spoke} = time();	$handles{$fhandle}{lines} = 0;	$handles{$fhandle}{spoken} = 0;	$handles{$fhandle}{channel} = 0;	$handles{$fhandle}{echo} = 1;	$handles{$fhandle}{times} = 1;	$handles{$fhandle}{read} = 1;	$handles{$fhandle}{write} = 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -