# Task: form trigger that refuses "p4 protect" entries that
# give permissions to "user *"
#
# will work on 2004.2 (and later) Perforce servers, but you
# need to remember to install the trigger thusly:
# (p4 triggers line follows - the '#' is a comment char for Ruby.)
# example in protect "ruby /Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/trig_protect_paranoid.p4ruby --formfile %formfile% --formname %formname%"
#
# num of calls to 'p4': 0
# status: tested on Darwin Mac OS X using ruby (no P4API needed!)
#
# Copyright 2004 Piccolo Engineering, Inc. All rights reserved.
require "getoptlong"
options = GetoptLong.new(
[ "--formfile", "--ff", GetoptLong::OPTIONAL_ARGUMENT],
[ "--formname", "--fn", GetoptLong::OPTIONAL_ARGUMENT]
)
defaultFormFile = nil
defaultFormName = nil
options.each do |opt, arg|
case opt
when "--formfile", "--ff"
defaultFormFile = arg
when "--formname", "--fn"
defaultFormName = arg
end
end
if defaultFormFile.nil?
then
puts "'p4 triggers' line is wrong: need argument '--formfile XXX'"
exit(1)
end
# fd = open("/Users/jeffbowles/work/ruby-work/contracts/tmp-trigger-scripts/debug.out", "w")
# fd.puts defaultFormFile unless defaultFormFile.nil?
# fd.puts defaultFormName unless defaultFormName.nil?
# fd.puts "Test Message to stdout!"
errorList = []
IO.readlines(defaultFormFile).each { |ln|
next unless (ln =~ /^\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)/)
perm = $1
entity = $3
entityType = $2
ipAddr = $4
pathName = $5
# fd.puts "entity = #{entity}"
if (entityType == "user" && entity == "*" && pathName !~ /^-/)
then
errorList << "Cannot add reference to 'user *'. Sorry."
# fd.puts "(error for entity=#{entity} pathName[0]='#{pathName[0]}')"
end
}
if errorList.length > 0
then
puts errorList
# fd.puts errorList
exit(1)
else
exit(0)
end