+(: prog Prog)
+(define prog
+ (Prog "tt" (info:#%info-lookup 'version)))
+
+(: user-default User)
+(define user-default
+ (User "https://github.com/xandkar/tt" #f))
+
+(: user->str (-> User String))
+(define (user->str user)
+ (match-define (User u n) user)
+ (if n
+ (format "+~a; @~a" u n)
+ (format "+~a" u )))
+
+(: user-agent->str (-> User-Agent String))
+(define (user-agent->str ua)
+ (match-define (User-Agent u p) ua)
+ (format "~a/~a (~a)" (Prog-name p) (Prog-version p) (user->str u)))
+
+(: user->user-agent User)
+(define (user->user-agent user)
+ (User-Agent user prog))
+
+(: user-agent-str String)
+(define user-agent-str
+ (user-agent->str (user->user-agent user-default)))
+
+(: set-user-agent-str (-> Path-String Void))
+(define (set-user-agent-str filename)
+ (set! user-agent-str (user-agent->str (user->user-agent (file->user filename))))
+ (log-info "User-Agent string is now set to: ~v" user-agent-str))
+
+(: file->user (-> Path-String User))
+(define (file->user filename)
+ (if (file-exists? filename)
+ (match (set->list (file->peers filename))
+ [(list p)
+ (log-info
+ "User-Agent. Found one peer in file: ~v. Using the found peer: ~a"
+ filename
+ (peer->str p))
+ (peer->user p)]
+ [(list* p _)
+ (log-warning
+ "User-Agent. Multiple peers in file: ~v. Picking arbitrary: ~a"
+ filename
+ (peer->str p))
+ (peer->user p)]
+ ['()
+ (log-warning
+ "User-Agent. No peers found in file: ~v. Using the default user: ~a"
+ filename
+ user-default)
+ user-default])
+ (begin
+ (log-warning
+ "User-Agent. File doesn't exist: ~v. Using the default user: ~a"
+ filename
+ user-default)
+ user-default)))
+
+(define (peer->user p)
+ (match-define (Peer n _ u _) p)
+ (User u n))
+