2019-04-14 21:57:14 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2019-04-16 02:47:16 +02:00
|
|
|
notrand "math/rand"
|
2019-04-17 02:34:27 +02:00
|
|
|
"sync"
|
2019-04-14 21:57:14 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-04-16 02:47:16 +02:00
|
|
|
func init() {
|
|
|
|
notrand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
2019-04-15 03:35:42 +02:00
|
|
|
type Doover struct {
|
|
|
|
ID int64
|
|
|
|
When time.Time
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:57:14 +02:00
|
|
|
func sayitagain(goarounds int, username string, rcpt string, msg []byte) {
|
|
|
|
var drift time.Duration
|
|
|
|
switch goarounds {
|
|
|
|
case 1:
|
|
|
|
drift = 5 * time.Minute
|
|
|
|
case 2:
|
|
|
|
drift = 1 * time.Hour
|
|
|
|
case 3:
|
2019-08-06 02:41:57 +02:00
|
|
|
drift = 4 * time.Hour
|
2019-04-14 21:57:14 +02:00
|
|
|
case 4:
|
2019-08-06 02:41:57 +02:00
|
|
|
drift = 12 * time.Hour
|
|
|
|
case 5:
|
2019-04-14 21:57:14 +02:00
|
|
|
drift = 24 * time.Hour
|
|
|
|
default:
|
|
|
|
log.Printf("he's dead jim: %s", rcpt)
|
|
|
|
return
|
|
|
|
}
|
2019-04-16 02:47:16 +02:00
|
|
|
drift += time.Duration(notrand.Int63n(int64(drift / 10)))
|
2019-04-14 21:57:14 +02:00
|
|
|
when := time.Now().UTC().Add(drift)
|
2019-08-16 06:15:55 +02:00
|
|
|
_, err := stmtAddDoover.Exec(when.Format(dbtimeformat), goarounds, username, rcpt, msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error saving doover: %s", err)
|
|
|
|
}
|
2019-04-15 03:35:42 +02:00
|
|
|
select {
|
|
|
|
case pokechan <- 0:
|
|
|
|
default:
|
|
|
|
}
|
2019-04-14 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 02:34:27 +02:00
|
|
|
var trucksout = 0
|
2019-08-06 02:40:03 +02:00
|
|
|
var maxtrucksout = 20
|
2019-04-17 02:34:27 +02:00
|
|
|
var garagelock sync.Mutex
|
|
|
|
var garagebell = sync.NewCond(&garagelock)
|
|
|
|
|
|
|
|
func truckgoesout() {
|
|
|
|
garagelock.Lock()
|
|
|
|
for trucksout >= maxtrucksout {
|
|
|
|
garagebell.Wait()
|
|
|
|
}
|
|
|
|
trucksout++
|
|
|
|
garagelock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func truckcomesin() {
|
|
|
|
garagelock.Lock()
|
|
|
|
trucksout--
|
|
|
|
garagebell.Broadcast()
|
|
|
|
garagelock.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-04-14 21:57:14 +02:00
|
|
|
func deliverate(goarounds int, username string, rcpt string, msg []byte) {
|
2019-04-17 02:34:27 +02:00
|
|
|
truckgoesout()
|
|
|
|
defer truckcomesin()
|
|
|
|
|
2019-04-14 21:57:14 +02:00
|
|
|
keyname, key := ziggy(username)
|
2019-04-16 20:40:23 +02:00
|
|
|
var inbox string
|
|
|
|
// already did the box indirection
|
|
|
|
if rcpt[0] == '%' {
|
|
|
|
inbox = rcpt[1:]
|
|
|
|
} else {
|
|
|
|
box, err := getboxes(rcpt)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting inbox %s: %s", rcpt, err)
|
|
|
|
sayitagain(goarounds+1, username, rcpt, msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inbox = box.In
|
2019-04-14 21:57:14 +02:00
|
|
|
}
|
2019-04-16 20:40:23 +02:00
|
|
|
err := PostMsg(keyname, key, inbox, msg)
|
2019-04-14 21:57:14 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to post json to %s: %s", inbox, err)
|
|
|
|
sayitagain(goarounds+1, username, rcpt, msg)
|
2019-04-15 03:35:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var pokechan = make(chan int)
|
|
|
|
|
|
|
|
func redeliverator() {
|
|
|
|
sleeper := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pokechan:
|
|
|
|
if !sleeper.Stop() {
|
|
|
|
<-sleeper.C
|
|
|
|
}
|
2019-04-29 20:13:34 +02:00
|
|
|
time.Sleep(5 * time.Second)
|
2019-04-15 03:35:42 +02:00
|
|
|
case <-sleeper.C:
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := stmtGetDoovers.Query()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("wat?")
|
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var doovers []Doover
|
|
|
|
for rows.Next() {
|
|
|
|
var d Doover
|
|
|
|
var dt string
|
2019-08-11 01:15:49 +02:00
|
|
|
err := rows.Scan(&d.ID, &dt)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error scanning dooverid: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-04-15 03:35:42 +02:00
|
|
|
d.When, _ = time.Parse(dbtimeformat, dt)
|
|
|
|
doovers = append(doovers, d)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
now := time.Now().UTC()
|
|
|
|
nexttime := now.Add(24 * time.Hour)
|
|
|
|
for _, d := range doovers {
|
|
|
|
if d.When.Before(now) {
|
|
|
|
var goarounds int
|
|
|
|
var username, rcpt string
|
|
|
|
var msg []byte
|
|
|
|
row := stmtLoadDoover.QueryRow(d.ID)
|
2019-08-11 01:15:49 +02:00
|
|
|
err := row.Scan(&goarounds, &username, &rcpt, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error scanning doover: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-16 06:15:55 +02:00
|
|
|
_, err = stmtZapDoover.Exec(d.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error deleting doover: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2019-04-15 03:35:42 +02:00
|
|
|
log.Printf("redeliverating %s try %d", rcpt, goarounds)
|
|
|
|
deliverate(goarounds, username, rcpt, msg)
|
|
|
|
} else if d.When.Before(nexttime) {
|
|
|
|
nexttime = d.When
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 20:13:34 +02:00
|
|
|
dur := nexttime.Sub(now).Round(time.Second) + 5*time.Second
|
2019-04-15 03:35:42 +02:00
|
|
|
sleeper.Reset(dur)
|
2019-04-14 21:57:14 +02:00
|
|
|
}
|
|
|
|
}
|