all repos — honk @ 6efd4cb5d646e926f486e660ec1e59120fb64bfe

my fork of honk

deliverator.go (view raw)

 1//
 2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
 3//
 4// Permission to use, copy, modify, and distribute this software for any
 5// purpose with or without fee is hereby granted, provided that the above
 6// copyright notice and this permission notice appear in all copies.
 7//
 8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19	"log"
20	"math/rand"
21	"time"
22)
23
24func sayitagain(goarounds int, username string, rcpt string, msg []byte) {
25	var drift time.Duration
26	switch goarounds {
27	case 1:
28		drift = 5 * time.Minute
29	case 2:
30		drift = 1 * time.Hour
31	case 3:
32		drift = 12 * time.Hour
33	case 4:
34		drift = 24 * time.Hour
35	default:
36		log.Printf("he's dead jim: %s", rcpt)
37		return
38	}
39	drift += time.Duration(rand.Int63n(int64(drift / 16)))
40	when := time.Now().UTC().Add(drift)
41	log.Print(when.Format(dbtimeformat), goarounds, username, rcpt, msg)
42}
43
44func deliverate(goarounds int, username string, rcpt string, msg []byte) {
45	keyname, key := ziggy(username)
46	inbox, _, err := getboxes(rcpt)
47	if err != nil {
48		log.Printf("error getting inbox %s: %s", rcpt, err)
49		sayitagain(goarounds+1, username, rcpt, msg)
50		return
51	}
52	err = PostMsg(keyname, key, inbox, msg)
53	if err != nil {
54		log.Printf("failed to post json to %s: %s", inbox, err)
55		sayitagain(goarounds+1, username, rcpt, msg)
56	}
57}