-- Leo's gemini proxy

-- Connecting to git.thebackupbox.net:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

repo: www_git
action: blob
revision:
path_from: git-www.cgi
revision_from: refs/heads/master:
path_to:
revision_to:

git.thebackupbox.net

www_git

git://git.thebackupbox.net/www_git

blob of:

www_git

/ git-www.cgi

refs/heads/master:/git-www.cgi
 #!/bin/bash

 ishttp() {
   case "$SERVER_PROTOCOL" in
     HTTP*) return 0 ;;
     *) return 1 ;;
   esac
 }
 isgemi() {
   case "$SERVER_PROTOCOL" in
     gemini*) return 0 ;;
     *) return 1 ;;
   esac
 }

 #if [ "$REMOTE_ADDR" = "127.0.0.1" ];then
 #  printf "HTTP 200 Ok. You asked for it.\r\n"
 ##  printf "HTTP 301 Fuck Off, Bot\r\n"
 ##  printf "Location: /robots.txt\r\n"
 ##  printf "Content-Type: text/plain\r\n\r\n"
 #  printf "Transfer-Encoding: gzip\r\n"
 #  printf "Content-Type: text/html\r\n\r\n"
 #  logger "sending a bomb"
 #  cat /var/cache/gzip.bomb
 #  logger "bomb sent. fuck off bot."
 ##  printf "get rekt, robot. at least don't like about your useragent.\n"
 ##  printf "if you're a human, say so in the your request. I'm watching them right now.\n"
 #  exit 0
 #fi

 # /gitweb.cgi/<repo>/<action>/<revision>:/<path>?<arguments>
 repo="$(printf "%s\n" "$PATH_INFO" | cut -d/ -f2 | tr -cd 'a-zA-Z0-9_.:-')"
 action="$(printf "%s\n" "$PATH_INFO" | cut -d/ -f3 | tr -cd 'a-zA-Z0-9_.:-')"
 ## revision_from can have /s
 revision_from="$(printf "%s\n" "$PATH_INFO" | cut -d/ -f4- | sed 's|\.\.|\n|g' | head -n1 | cut -d: -f1 | tr -cd 'a-zA-Z0-9_.:/-'):"
 path_from="$(printf "%s\n" "$PATH_INFO" | cut -d/ -f4- | sed 's|\.\.|\n|g' | head -n1 | grep ':' | cut -d: -f2 | sed 's|^/||')"
 revision_to="$(printf "%s\n" "$PATH_INFO" | cut -d/ -f4- | grep '\.\.' | sed 's|\.\.|\n|g' | tail -n1 | cut -d: -f1)"
 path_to="$(printf "%s\n" "$PATH_INFO" | cut -d/ -f4- | grep '\.\.' | sed 's|\.\.|\n|g' | tail -n1 | grep ':' | cut -d: -f2 | sed 's|^/||')"
 if [ "$revision" = ":" ];then
   revision="HEAD"
 fi
 if [ ! "$action" ];then
   if [ ! "$repo" ];then
     action=project_list
   else
     action=summary
   fi
 fi

 ### if we're looking at a repo, cd into it.
 if [ "$repo" ];then
   case "$PATH_INFO" in
    "/$repo/"*)
     true
    ;;
    *)
     ishttp && printf "Status: 301 Redirect\r\n"
     ishttp && printf "Location: /%s/\r\n" "$repo"
     ishttp && printf "Content-Type: text/plain\r\n\r\n"
     ishttp && printf "redirect to: /%s/" "$repo"
     isgemi && printf '30 /%s/\r\n' "$repo"
     exit 0
    ;;
   esac
   ## now to cd into gitroot
   gitroot="$(normalpath /var/git/$repo)"
   case "$gitroot" in
    /var/git/*)
      cd /"$gitroot"
      ;;
    *)
      ishttp && printf "Content-Type: text/plain\r\n\r\n"
      isgemi && printf '20 text/plain\r\n'
      echo "fuck proper error handling is a pain in the ass."
      exit 1
      ;;
   esac
 fi

 if [ "$action" = "gemfeed" ];then
   ishttp && printf 'Content-Type: text/gemini; charset=utf-8\r\n\r\n'
   isgemi && printf '20 text/gemini\r\n'
   printf '# git.thebackupbox.net commit log for %s\n' "${repo}"
   git log --format=format:'=> gemini://git.thebackupbox.net/'"${repo}"'/commit/%H %as - %s'
   exit 0
 fi

 if [ "$action" = "ass" ];then
   ishttp && printf 'Content-Type: text/plain; charset=utf-8\r\n\r\n'
   isgemi && printf '20 text/plain\r\n'
   printf '# for details see: https://tilde.town/~dzwdz/ass/\n'
   git log --format=format:'%as%x09https://git.thebackupbox.net/'"${repo}"'/commit/%H%x09%s'
   exit 0
 fi
 if [ "$action" = "twtxt" ];then
   ishttp && printf 'Content-Type: text/plain; charset=utf-8\r\n\r\n'
   isgemi && printf '20 text/plain\r\n'
   git log --date=format:'%Y-%m-%dT%H:%M:%SZ' --format=format:'%ad%x09https://git.thebackupbox.net/'"${repo}"'/commit/%H %s'
   exit 0
 fi

 if [ "$action" = "refs" ];then ### git clone over https
   printf "Status: 200 OK\r\n"
   printf "Content-Type: text/plain\r\n\r\n"
   find refs -type f -exec grep -H . {} \; | tr ':' '\t' | awk '{print $2"\t"$1}'
   exit 0
 fi
 if [ "$action" = "objects" ];then ## git clone over https
   printf "Status: 200 OK\r\n"
   printf "Content-Type: text/plain\r\n\r\n"
   cat objects/"${PI_3}"
   exit 0
 fi

 if [ "$action" = "atom" ];then
     last_modified="$(rfc7231date "$(git log --date=format:'%s' --format=format:'%ad' | head -n1)")"
     updated="$(git log --date=format:'%Y-%m-%dT%H:%M:%SZ' --format=format:'%ad' | head -n1)"
     ishttp && printf "Last-Modified: %s\r\n" "${last_modified}"
     ishttp && printf "Content-Type: application/atom+xml\r\n\r\n"
     isgemi && printf "20 application/atom+xml\r\n"
     printf '<?xml version="1.0" encoding="utf-8" ?>\n'
     printf '<feed xmlns="http://www.w3.org/2005/Atom">\n'
     printf '<link href="https://git.thebackupbox.net/%s/atom" rel="self" />\n' "${repo}"
     printf '<link href="https://git.thebackupbox.net/%s/" />\n' "${repo}"
     printf '<title>commits to %s</title>\n' "${repo}"
     printf '<updated>%s</updated><!-- RFC3339 -->\n' "$updated"
     git log --date=format:'%Y-%m-%dT%H:%M:%SZ' --format=format:'<entry><title>%s</title><link href="https://git.thebackupbox.net/'"${repo}"'/commit/%H"/><published>%ad</published><author><name>%an</name></author><summary>%s</summary></entry>\n'
             #| html_entities_encode \
             #| sed 's|%|%%|g' \
             #| sed 's|^\([^\t][^\t]*\)\t\([^\t][^\t]*\)\t\([^\t][^\t]*\)$|<entry><title>\1</title><link href="https://git.thebackupbox.net/${repo}/\1"/><published>%Y-%m-%dT%H:%M:%S+00:00</published><author><name>\3</name></author><summary>\2</summary></entry>\n--date=@\4|g' \
             #| tr '\n' '\0' \
             #| xargs -0 -n2 -L2 echo
     printf '</feed>\n'
   exit 0
 fi


 ishttp && printf "Content-Type: text/html\r\n\r\n"
 isgemi && printf "20 text/gemini\r\n"

 ishttp && printf '<html><head>\n'
 ishttp && printf '<link rel="alternate" type="application/atom+xml" title="commit feed for %s" href="https://git.thebackupbox.net/%s/atom" />\n' "${repo}" "${repo}"
 ishttp && printf '<link rel="stylesheet" type="text/css" href="/style.css" /><title>%s</title></head><body>\n' "git.thebackupbox.net"
 ishttp && printf "<details><summary>now with git-web compatible paths</summary><pre>"
 isgemi && printf '```\n'
 printf "repo: %s\n" "$repo"
 printf "action: %s\n" "$action"
 printf "revision: %s\n" "$revision"
 printf "path_from: %s\n" "$path_from"
 printf "revision_from: %s\n" "$revision_from"
 printf "path_to: %s\n" "$path_to"
 printf "revision_to: %s\n" "$revision_to"
 isgemi && printf '```\n'
 ishttp && printf "</pre></details>"

 ishttp && printf '<h2><a href="/">git.thebackupbox.net</a> / '
 isgemi && printf '=> / git.thebackupbox.net\n'
 if [ "$repo" ];then
   ishttp && printf '<a href="/%s">%s</a>' "${repo}" "${repo}"
   isgemi && printf '=> /%s/ %s\n' "${repo}" "${repo}"
 fi
 ishttp && printf '</h2>'
 if [ "$repo" ];then
   ishttp && printf '<h3><a href="git://git.thebackupbox.net/%s">git://git.thebackupbox.net/%s</a></h3>' "${repo}" "${repo}"
   isgemi && printf '=> git://git.thebackupbox.net/%s git://git.thebackupbox.net/%s\n' "${repo}" "${repo}"
 fi

 revision_from_fix="$(printf "%s\n" "$revision_from" | sed 's/:$//')"

 case "$action" in
   project_list)
     #default
     ishttp && printf "<h2>repositories</h2>"
     isgemi && printf '## repositories\n'
     cd /var/git
     ishttp && find ./* -maxdepth 0 -type d \
       | sed 's|^\./\(.*\)$|<a href="/\1/">/\1/</a><br/>|g'
     isgemi && find ./* -maxdepth 0 -type d \
       | sed 's|^\./\(.*\)$|=> /\1/ /\1/|g'
   ;;
   summary)
     printf "Display summary about given repository. This is the default command if no action is specified in URL, and only repository is specified.\n"
     ishttp && printf '<h2><a href="heads/">branches</a></h2>\n'
     isgemi && printf '=> heads/ branches\n'
     ishttp && git show-branch \
       | html_entities_encode | sed 's|^.*\[\(.*\)\] \(.*\)$|[<a href="/'"${repo}"'/shortlog/refs/heads/\1/">\1/</a> <a href="/'"${repo}"'/tree/refs/heads/\1">tree</a>] \2<br/>|g'
     isgemi && git show-branch \
       | sed 's|^\[\(.*\)\] \(.*\)$|=> /'"${repo}"'/shortlog/refs/heads/\1/ [\1/]\n=> /'"${repo}"'/tree/refs/heads/\1 [tree] \2\n|g'
 #	      [master] added a state var, and a title var. svg will now use those in its output "svg" (html)
     ishttp && echo "<pre>"
     if git show refs/heads/$(git show-branch | head -n1 | sed 's/^\[\(.*\)\].*$/\1/'):README >/dev/null 2>&1;then
       isgemi && printf '```\n'
       ishttp && git show refs/heads/$(git show-branch | head -n1 | sed 's/^\[\(.*\)\].*$/\1/'):README 2>/dev/null | html_entities_encode
       isgemi && git show refs/heads/$(git show-branch | head -n1 | sed 's/^\[\(.*\)\].*$/\1/'):README 2>/dev/null | sed 's/^/ /g' #kind of escape against gemini line injection
       ishttp && echo "</pre>"
       isgemi && echo '```\n'
     fi
 ### need to pick a revision_from to do this from...
 #      echo "<pre>"
 #      git show "${revision_from}/README" | html_entities_encode
 #      echo "</pre>"
   ;;
   heads)
     ishttp && printf '<h2><a href="heads/">branches</a></h2>'
     isgemi && printf '=> heads/ branches\n'
     ishttp && git show-branch \
       | sed 's|^\[\(.*\)\] \(.*\)$|[<a href="/'"${repo}"'/shortlog/refs/heads/\1/">\1/</a> <a href="/'"${repo}"'/tree/refs/heads/\1">tree</a>] \2<br/>|g'
     isgemi && git show-branch \
       | sed 's|^\[\(.*\)\] \(.*\)$|=> /'"${repo}"'/shortlog/refs/heads/\1/ [\1/]\n=> /'"${repo}"'/tree/refs/heads/\1 [tree] \2\n|g'
   ;;
   remotes)
     printf ""
   ;;
   tags)
   ;;
   blob)
       ishttp && printf '<h2>blob of: <a href="/%s/tree/%s/">%s</a>' "${repo}" "${revision_from}" "${repo}"
       isgemi && printf '## blob of:\n=> /%s/tree/%s/ %s\n' "${repo}" "${revision_from}" "${repo}"
       for p in $(dirname "/${path_from}" | tr '/' ' ');do
         pp="$pp/$p"
         ishttp && printf ' / <a href="/%s/tree/%s%s">%s</a>' "${repo}" "${revision_from}" "$pp" "$p"
         isgemi && printf '=> /%s/tree/%s%s / %s\n' "${repo}" "${revision_from}" "$pp" "$p"
       done
       ishttp && printf ' / %s </h2>\n' "$(basename "${path_from}")"
       isgemi && printf ' / %s\n' "$(basename "${path_from}")"
 #        printf "<h2>file contents %s</h2>" "${path_from}"
       ishttp && echo "<pre>"
       isgemi && echo '```\n'
       if [ "$path_from" ];then
         echo "${revision_from}/${path_from}"
         ishttp && git show "${revision_from}${path_from}" | html_entities_encode
         isgemi && git show "${revision_from}${path_from}" | sed 's/^/ /g'
       else
         echo "${revision_from_fix}"
         ishttp && git show "${revision_from_fix}" | html_entities_encode
         isgemi && git show "${revision_from_fix}" | sed 's/^/ /g'
       fi
       ishttp && echo "</pre>"
       isgemi && echo '```\n'
   ;;
   tree) ### example: https://sipb.mit.edu/gitweb.cgi/ikiwiki.git/tree/refs/heads/master
     ### in this example, $revision is refs, and $path is heads/master
     ### outputs links of the files of this "revision" like:
     ### for tree type: /repo/tree/refs/heads/master:/[in-repo-path]
     ### for blob type: /repo/blob/refs/heads/master:/[in-repo-path]
       ishttp && printf '<h2>tree of: <a href="/%s/tree/%s/">%s</a>' "${repo}" "${revision_from}" "${repo}"
       isgemi && printf '## tree of:\n=> /%s/tree/%s/ %s\n' "${repo}" "${revision_from}" "${repo}"
       for p in $(dirname "/${path_from}" | tr '/' ' ');do
         pp="$pp/$p"
         ishttp && printf ' / <a href="/%s/tree/%s%s">%s</a>' "${repo}" "${revision_from}" "$pp" "$p"
         isgemi && printf '=> /%s/tree/%s%s / %s\n' "${repo}" "${revision_from}" "$pp" "$p"
       done
       ishttp && printf ' / %s</h2>\n' "$(basename "${path_from}")"
       isgemi && printf ' / %s\n' "$(basename "${path_from}")"
       ishttp && printf "<pre>"
 #        if [ "$parent" != "$file_path" ];then
 #          printf "%s %s %s "
 #          printf '<a href="/%s/%s/%s/"></a>\n'
 #        fi
       if [ ! "${path_from}" ];then
         path_from=.
       fi
       git ls-tree "${revision_from}" -- "${path_from}" \
         | while read -r mode type hash name;do
             #printf "mode: %s\n" "$mode"
             ishttp && printf "%s %s %s " "$(strmode "$[0$mode]")" "$type" "$hash"
             ### these parts are... $repo, $type
             case "$type" in
               tree)
                 ishttp && printf '<a href="/%s/%s/%s/%s/">%s/</a>\n' \
                        "$repo" "$type" "$revision_from" "$name" "$name"
                 isgemi && printf '=> /%s/%s/%s/%s/ ' \
                        "$repo" "$type" "$revision_from" "$name"
                 ;;
               blob)
                 ishttp && printf '<a href="/%s/%s/%s/%s">%s</a>\n' \
                        "$repo" "$type" "$revision_from" "$name" "$name"
                 isgemi && printf '=> /%s/%s/%s/%s ' \
                        "$repo" "$type" "$revision_from" "$name"
                 ;;
               *)
                 ishttp && printf '<a href="/%s/%s/%s/%s">%s</a>\n' \
                        "$repo" "$type" "$revision_from" "$name" "$name"
                 isgemi && printf '=> /%s/%s/%s/%s ' \
                        "$repo" "$type" "$revision_from" "$name"
                 ;;
             esac
             isgemi && printf "%s %s %s %s\n" "$(strmode "$[0$mode]")" "$type" "$hash" "$name"
           done
       ishttp && printf "</pre>"
   ;;
   blob_plain)
   ;;
   blobdiff)
   ;;
   blame|blame_incremental)
   ;;
   commit|commitdiff)
     ishttp && printf '<pre>'
     isgemi && printf '```\n'
     ishttp && git show --no-abbrev-commit --full-index "$revision_from_fix" \
       | html_entities_encode \
       | sed 's|^index \([0-9a-f][0-9a-f]*\)..\([0-9a-f][0-9a-f]*\) |index <a href="/'"${repo}"'/blob/\1">\1</a>..<a href="/'"${repo}"'/blob/\2">\2</a> |g' \
       | sed 's|^\+\(.*\)$|<span class="added">+\1</span>|g;s|^-\(.*\)|<span class="removed">-\1</span>|g'
     isgemi && git show --no-abbrev-commit --full-index "$revision_from_fix" \
       | sed 's|^index \([0-9a-f][0-9a-f]*\)..\([0-9a-f][0-9a-f]*\) \(.*\)$|```\n=> /'"${repo}"'/blob/\1 index \1..\n=> /'"${repo}"'/blob/\2 index ..\2 \3\n```|g'
     ishttp && printf '</pre>'
     isgemi && printf '```\n'
     printf '%c----END OF PAGE-----\n' '-'
   ;;
   patch)
   ;;
   tag)
   ;;
   log|shortlog)
     revision_from_fix_twice="$(printf "%s\n" "$revision_from" | sed 's|/:$||')"
 #    ishttp && git log --format=format:'%as <a href="https://git.thebackupbox.net/'"${repo}"'/commit/%H">%s</a><br/>' "${revision_from_fix}"
 ### kek. just convert the gemini to html. XD
     ishttp && git log --format=format:'=> /'"${repo}"'/commit/%H %as %s' "${revision_from_fix_twice}" | html_entities_encode | sed 's|&gt;|>|' | gemini2html
     isgemi && git log --format=format:'=> /'"${repo}"'/commit/%H %as %s' "${revision_from_fix_twice}"
   ;;
   history)
   ;;
   rss|atom)
   ;;
   *)
     printf "unknown action. assuming broken old link\n"
     ishttp && printf '<a href="/%s/blob/refs/heads/%s:%s">this is probably what you were looking for</a>' "$repo" "$action" "$revision_from_fix"
     isgemi && printf '=> /%s/blob/refs/heads/%s:%s" this is probably what you were looking for\n' "$repo" "$action" "$revision_from_fix"
   ;;
 esac

 ishttp && printf '</body></html>'

-- Response ended

-- Page fetched on Sun Jun 2 17:34:47 2024