hbbs

[archived] bbs.hlirc.net
git clone https://hhvn.uk/hbbs
git clone git://hhvn.uk/hbbs
Log | Files | Refs | README | LICENSE

userinfo.sh (3042B)


      1 #!/bin/sh
      2 
      3 . $cwd/include/lock.sh
      4 
      5 userset(){
      6 	touch $datadir/users/$1
      7 	lock $datadir/users/$1
      8 	awk -v "var=$2" -v "val=$3" -F"	" '
      9 		$1 == var {
     10 			;
     11 		}
     12 
     13 		$1 != var {
     14 			print
     15 		}
     16 
     17 		END {
     18 			print var "\t" val
     19 		}' < $datadir/users/$1 > $datadir/users/$1.new 2>/dev/null
     20 	mv $datadir/users/$1.new $datadir/users/$1 2>/dev/null
     21 	unlock $datadir/users/$1
     22 }
     23 
     24 userget(){
     25 	awk -v "var=$2" -F"	" '
     26 		$1 == var {
     27 			print $2
     28 		}' < $datadir/users/$1 2>/dev/null
     29 }
     30 
     31 userlist(){
     32 	for u in $datadir/users/*
     33 	do
     34 		u2=$(basename $u)
     35 		[ "$u2" != "$1" ] && printf "%s\t" "$u2"
     36 	done
     37 }
     38 
     39 markread(){
     40 	lock $datadir/boards/$1/list
     41 	awk -v "user=$2" -v "id1=${3%%.*}" -v "id2=${3##*.}" -F"	" '
     42 	BEGIN {OFS="\t"}
     43 	$1 != id1 || $2 != id2
     44 	$1 == id1 && $2 == id2 {
     45 		n = ""
     46 		for (i=8; i <= NF; i++)
     47 			if ($i != user)
     48 				n = sprintf("%s\t%s", n, $i)
     49 		print $1, $2, $3, $4, $5, $6, $7, n
     50 	}' < $datadir/boards/$1/list > $datadir/boards/$1/list.new
     51 	mv $datadir/boards/$1/list.new $datadir/boards/$1/list
     52 	unlock $datadir/boards/$1/list
     53 }
     54 
     55 markread_board(){
     56 	lock $datadir/boards/$1/list
     57 	awk -v "user=$2" -F "	" '
     58 	BEGIN {OFS="\t"}
     59 	{
     60 		n = ""
     61 		for (i=8; i <= NF; i++)
     62 			if ($i != user)
     63 				n = sprintf("%s\t%s", n, $i)
     64 		print $1, $2, $3, $4, $5, $6, $7, n
     65 	}' < $datadir/boards/$1/list > $datadir/boards/$1/list.new
     66 	mv $datadir/boards/$1/list.new $datadir/boards/$1/list
     67 	unlock $datadir/boards/$1/list
     68 }
     69 
     70 markread_all(){
     71 	for d in $datadir/boards/*
     72 	do
     73 		lock $d/list
     74 		awk -v "user=$1" -F "	" '
     75 		BEGIN {OFS="\t"}
     76 		{
     77 			n = ""
     78 			for (i=8; i <= NF; i++)
     79 				if ($i != user)
     80 					n = sprintf("%s\t%s", n, $i)
     81 			print $1, $2, $3, $4, $5, $6, $7, n
     82 		}' < $d/list > $d/list.new
     83 		mv $d/list.new $d/list
     84 		unlock $d/list
     85 	done
     86 }
     87 
     88 checknuser(){
     89 	[ "$(userget $1 expiry)" != "" ] && [ $(datediff $(userget $1 expiry) 2021-02-02) -gt -1 ] && rm $datadir/users/$1
     90 	[ -f $datadir/users/$1 ] && error "user already exists" && return 1
     91 	[ "$1" = "new" ] && error "user \"new\" reserved" && return 1
     92 	[ "$1" = "admin" ] && error "user \"admin\" reserved" && return 1
     93 	[ "$1" = "root" ] && error "user \"root\" reserved" && return 1
     94 	[ "$1" = "recover" ] && error "user \"recover\" reserved" && return 1
     95 	return 0
     96 }
     97 
     98 checknmail(){
     99 	echo "$1" | grep -Ev '^..*@.*' >/dev/null && error "doesn't like like an email addr" && return 1
    100 	for u in $datadir/users/*
    101 	do
    102 		[ "$(userget "$(basename $u)" email)" = "$1" ] && error "addr already in use" && return 1
    103 	done
    104 	return 0
    105 }
    106 
    107 check(){
    108 	[ ! -f $datadir/users/$1 ] && {
    109 		error "no such account" 
    110 		return 1
    111 	}
    112 	[ "$(userget $1 passwd)" != "$2" ] && error "incorrect passwd" && return 1
    113 	[ "$(userget $1 expiry)" != "" ] && [ $(datediff $(userget $1 expiry) $(date +%Y-%m-%d)) -gt -1 ] && {
    114 		error "user has expired"
    115 		rm $datadir/users/$1
    116 		return 1
    117 	}
    118 	[ "$(userget $1 expiry)" != "" ] && {
    119 		prompt "verification code" vercode 
    120 		[ "$(userget $1 vercode)" = "$vercode" ] && ok "verification successful" && userset $1 expiry "" || error "incorrect verification code"
    121 	}
    122 	add_log "login" "$1"
    123 	ok "login success"
    124 }