<commit_before># -*- default-generic -*-
# User-related MUES::CommandShell commands.
# Time-stamp: <13-Oct-2002 16:44:01 deveiant>
# $Id: users.cmd,v 1.4 2002/10/13 23:27:38 deveiant Exp $
#
# == Authors:
# * <NAME> <<EMAIL>>
#


### 'users' command
= users

== Abstract

Display user records.

== Description

This command lists user accounts in a MUES server whose name matches a given
pattern (a regular expression), or, if no pattern is specified, all user
accounts.

== Usage

  users [<pattern>]

== Restriction

implementor

== Code

	re = nil

	# If they specified a pattern, strip leading '/' characters, if present, and
	# compile a Regexp from the rest. If the regex doesn't compile, just send
	# output describing why.
	if argString =~ /\S/
		argString.strip!
		argString.gsub!( %r{^/|/$}, '' )
		begin
			re = Regexp::new( argString )
		rescue RegexpError => e
			return [ MUES::OutputEvent::new("Malformed pattern: #{e.message}") ]
		end

	# If they didn't supply a pattern, just use on that'll match anything valid
	# username.
	else
		re = /\w/
	end

	usernames = MUES::ServerFunctions::getUserNames()
	matchedNames = usernames.grep( re )

	output = "User Accounts\n\n    " <<
		matchedNames.join("\n    ") <<
		"\n  Displayed #{matchedNames.length} of #{usernames.length} user(s)\n\n"

	return [ MUES::OutputEvent::new(output) ]



### Add a new user
= adduser

== Synonyms

useradd

== Restriction

admin

== Abstract

Create a new user account.

== Description

This command allows an admin to create a new user account for the server.

== Usage

  adduser

== Code

  # The MUES::User class has a factory method for creating Questionnaire objects
  # for building instances of itself, so just use that.
  return [ MUES::User::getCreationQuestionnaire ]


### Show a user's information
= showuser

== Synonyms
finger

== Abstract
Show a user record.

== Description

This command allows a user to view her own user record, or the record of another
user if the invoking user has implementor privileges.

== Usage

  showuser [<username>]

== Code

  # Get the target user, either from the command arguments, or from the
  # current context.
  user = nil
  if argString =~ /(\S+)/
	username = $1
	if username == context.user.login
		user = context.user
	elsif ! context.user.isImplementor?
		raise CommandError, "You cannot view other users' information."
	else
		user = MUES::ServerFunctions::getUserByName( username ) or
			raise CommandError, "No such user '#{username}'"
	end
  else
    user = context.user
  end

  # Build a detailed user record display
  output = []
  output << "User record for user '%s':\n" % user.realname
  output << "\tAccount type: %s\n" % MUES::User::AccountType::Name[user.accountType]
  output << "\tCreated: #{user.timeCreated.to_s}\n" 

  if context.user.isAdmin?
    output << "\tCrypted password: #{user.cryptedPass}\n"
  end

  output << "\tReal name: #{user.realname}\n"
  output << "\tEmail address: #{user.emailAddress}\n"
  output << "\tLast login: #{user.lastLoginDate.to_s}\n"
  output << "\tLast host: #{user.lastHost}\n"

  if context.user.isImplementor?
    output << "\tFirst login tick: #{user.firstLoginTick}\n"
  end

  output << "\tPreferences: \n" + user.preferences.collect {|k,v| "\t\t#{k} => #{v}\n"}.to_s +
		"\n\n"

  return [ MUES::OutputEvent::new(output.join('')) ]


### Change a user's password
= password

== Abstract
Change the password of a user.

== Description
Allows a user to change their account password, or the password of another user
if the invoking user has admin privileges.

== Usage

  password [<username>]

== Code

	user = nil
	if argString =~ /(\S+)/
		username = $1
		if username == context.user.login
			user = context.user
		elsif ! context.user.isAdmin?
			raise CommandError, "You cannot alter other users' passwords."
		else
			user = MUES::ServerFunctions::getUserByName( username ) or
				raise CommandError, "No such user '#{username}'"
		end
	else
		user = context.user
	end

	# The MUES::User class has a factory method for creating Questionnaire objects
	# for changing a user's password.
	return [MUES::User::getPasswordQuestionnaire(user, (context.user != user))]


### Change a user's attributes
= usermod

== Abstract
Change an attribute of a user.

== Synonyms
moduser

== Description
Allows a user to change their account information, or the account information of
another user if the invoking user has admin priviledges.

== Usage

	usermod <attribute> [<username>]

== Code

	modableAttr = %w[realname emailAddress]
	adminModableAttr = modableAttr + %w[username]
	user = nil

	# attribute + user
	if argString =~ /(\S+)\s+(\S+)/
		attribute = $1
		username = $2
		if username == context.user.login
			user = context.user
		elsif ! context.user.isAdmin?
			raise CommandError, "You cannot alter other users' accounts."
		else
			user = MUES::ServerFunctions::getUserByName( username ) or
				raise CommandError, "No such user '#{username}'"
		end

	# attribute only
	elsif argString =~ /(\S+)/
		user = context.user
		attribute = $1

	else
		raise CommandError, self.usage
	end

	# Match the attribute to change against the list of modifiable ones.
	if context.user.isAdmin?
		attributes = adminModableAttr.find_all {|attr| attr.match(attribute)}
	else
		attributes = modableAttr.find_all {|attr| attr.match(attribute)}
	end

	# Handle attribute not found
	if attributes.nil? or attributes.empty?
		raise CommandError, "Invalid attribute '%s'.  Must be one of: %s" % [
			attribute, (context.user.isAdmin? ?
				adminModableAttr :
				modableAttr).join(", ") 
		]

	# Handle ambiguous attribute
	elsif attributes.length > 1
		raise CommandError, "Ambiguous attribute: %s" % attributes.join(" ")
	end
	
	# The MUES::User class has a factory method for creating Questionnaire
	# objeccts for changing a user's attributes.
	return [MUES::User::getAttributeQuestionnaire(user, attributes[0])]
<commit_msg>Added check for admin-password changers.<commit_after># -*- default-generic -*-
# User-related MUES::CommandShell commands.
# Time-stamp: <14-Oct-2002 03:50:09 deveiant>
# $Id: users.cmd,v 1.5 2002/10/14 09:50:56 deveiant Exp $
#
# == Authors:
# * <NAME> <<EMAIL>>
#


### 'users' command
= users

== Abstract

Display user records.

== Description

This command lists user accounts in a MUES server whose name matches a given
pattern (a regular expression), or, if no pattern is specified, all user
accounts.

== Usage

  users [<pattern>]

== Restriction

implementor

== Code

	re = nil

	# If they specified a pattern, strip leading '/' characters, if present, and
	# compile a Regexp from the rest. If the regex doesn't compile, just send
	# output describing why.
	if argString =~ /\S/
		argString.strip!
		argString.gsub!( %r{^/|/$}, '' )
		begin
			re = Regexp::new( argString )
		rescue RegexpError => e
			return [ MUES::OutputEvent::new("Malformed pattern: #{e.message}") ]
		end

	# If they didn't supply a pattern, just use on that'll match anything valid
	# username.
	else
		re = /\w/
	end

	usernames = MUES::ServerFunctions::getUserNames()
	matchedNames = usernames.grep( re )

	output = "User Accounts\n\n    " <<
		matchedNames.join("\n    ") <<
		"\n  Displayed #{matchedNames.length} of #{usernames.length} user(s)\n\n"

	return [ MUES::OutputEvent::new(output) ]



### Add a new user
= adduser

== Synonyms

useradd

== Restriction

admin

== Abstract

Create a new user account.

== Description

This command allows an admin to create a new user account for the server.

== Usage

  adduser

== Code

  # The MUES::User class has a factory method for creating Questionnaire objects
  # for building instances of itself, so just use that.
  return [ MUES::User::getCreationQuestionnaire ]


### Show a user's information
= showuser

== Synonyms
finger

== Abstract
Show a user record.

== Description

This command allows a user to view her own user record, or the record of another
user if the invoking user has implementor privileges.

== Usage

  showuser [<username>]

== Code

  # Get the target user, either from the command arguments, or from the
  # current context.
  user = nil
  if argString =~ /(\S+)/
	username = $1
	if username == context.user.login
		user = context.user
	elsif ! context.user.isImplementor?
		raise CommandError, "You cannot view other users' information."
	else
		user = MUES::ServerFunctions::getUserByName( username ) or
			raise CommandError, "No such user '#{username}'"
	end
  else
    user = context.user
  end

  # Build a detailed user record display
  output = []
  output << "User record for user '%s':\n" % user.realname
  output << "\tAccount type: %s\n" % MUES::User::AccountType::Name[user.accountType]
  output << "\tCreated: #{user.timeCreated.to_s}\n" 

  if context.user.isAdmin?
    output << "\tCrypted password: #{user.cryptedPass}\n"
  end

  output << "\tReal name: #{user.realname}\n"
  output << "\tEmail address: #{user.emailAddress}\n"
  output << "\tLast login: #{user.lastLoginDate.to_s}\n"
  output << "\tLast host: #{user.lastHost}\n"

  if context.user.isImplementor?
    output << "\tFirst login tick: #{user.firstLoginTick}\n"
  end

  output << "\tPreferences: \n" + user.preferences.collect {|k,v| "\t\t#{k} => #{v}\n"}.to_s +
		"\n\n"

  return [ MUES::OutputEvent::new(output.join('')) ]


### Change a user's password
= password

== Abstract
Change the password of a user.

== Description
Allows a user to change their account password, or the password of another user
if the invoking user has admin privileges.

== Usage

  password [<username>]

== Code

	user = nil
	if argString =~ /(\S+)/
		username = $1
		if username == context.user.login
			user = context.user
		elsif ! context.user.isAdmin?
			raise CommandError, "You cannot alter other users' passwords."
		else
			user = MUES::ServerFunctions::getUserByName( username ) or
				raise CommandError, "No such user '#{username}'"
			raise CommandError, "You cannot alter another admin's password." if
				user.isAdmin?
		end
	else
		user = context.user
	end

	# The MUES::User class has a factory method for creating Questionnaire objects
	# for changing a user's password.
	return [MUES::User::getPasswordQuestionnaire(user, (context.user != user))]


### Change a user's attributes
= usermod

== Abstract
Change an attribute of a user.

== Synonyms
moduser

== Description
Allows a user to change their account information, or the account information of
another user if the invoking user has admin priviledges.

== Usage

	usermod <attribute> [<username>]

== Code

	modableAttr = %w[realname emailAddress]
	adminModableAttr = modableAttr + %w[username]
	user = nil

	# attribute + user
	if argString =~ /(\S+)\s+(\S+)/
		attribute = $1
		username = $2
		if username == context.user.login
			user = context.user
		elsif ! context.user.isAdmin?
			raise CommandError, "You cannot alter other users' accounts."
		else
			user = MUES::ServerFunctions::getUserByName( username ) or
				raise CommandError, "No such user '#{username}'"
		end

	# attribute only
	elsif argString =~ /(\S+)/
		user = context.user
		attribute = $1

	else
		raise CommandError, self.usage
	end

	# Match the attribute to change against the list of modifiable ones.
	if context.user.isAdmin?
		attributes = adminModableAttr.find_all {|attr| attr.match(attribute)}
	else
		attributes = modableAttr.find_all {|attr| attr.match(attribute)}
	end

	# Handle attribute not found
	if attributes.nil? or attributes.empty?
		raise CommandError, "Invalid attribute '%s'.  Must be one of: %s" % [
			attribute, (context.user.isAdmin? ?
				adminModableAttr :
				modableAttr).join(", ") 
		]

	# Handle ambiguous attribute
	elsif attributes.length > 1
		raise CommandError, "Ambiguous attribute: %s" % attributes.join(" ")
	end
	
	# The MUES::User class has a factory method for creating Questionnaire
	# objeccts for changing a user's attributes.
	return [MUES::User::getAttributeQuestionnaire(user, attributes[0])]

{'fraction_non_alphanumeric': 0.12221850426635436, 'fraction_numerical': 0.0058557804918855615, 'mean_word_length': 3.782, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3805512', 'n_tokens_mistral': 4041, 'n_tokens_neox': 3730, 'n_words': 1273}