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 (
|
2023-06-12 20:40:28 +02:00
|
|
|
"bytes"
|
|
|
|
"database/sql"
|
2019-04-16 02:47:16 +02:00
|
|
|
notrand "math/rand"
|
2023-06-12 20:40:28 +02:00
|
|
|
"sync"
|
2019-04-14 21:57:14 +02:00
|
|
|
"time"
|
2019-10-29 18:38:51 +01:00
|
|
|
|
|
|
|
"humungus.tedunangst.com/r/webs/gate"
|
2019-04-14 21:57:14 +02:00
|
|
|
)
|
|
|
|
|
2019-04-15 03:35:42 +02:00
|
|
|
type Doover struct {
|
2023-06-12 21:01:03 +02:00
|
|
|
ID int64
|
|
|
|
When time.Time
|
|
|
|
Userid int64
|
|
|
|
Tries int64
|
|
|
|
Rcpt string
|
|
|
|
Msgs [][]byte
|
2019-04-15 03:35:42 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 21:01:03 +02:00
|
|
|
func sayitagain(doover Doover) {
|
2023-06-12 20:40:28 +02:00
|
|
|
rcpt := doover.Rcpt
|
2019-04-14 21:57:14 +02:00
|
|
|
var drift time.Duration
|
2023-06-12 21:01:03 +02:00
|
|
|
doover.Tries += 1
|
|
|
|
switch doover.Tries {
|
2019-04-14 21:57:14 +02:00
|
|
|
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:
|
2022-02-06 06:42:13 +01:00
|
|
|
ilog.Printf("he's dead jim: %s", rcpt)
|
2019-04-14 21:57:14 +02:00
|
|
|
return
|
|
|
|
}
|
2019-04-16 02:47:16 +02:00
|
|
|
drift += time.Duration(notrand.Int63n(int64(drift / 10)))
|
2022-04-12 20:35:44 +02:00
|
|
|
when := time.Now().Add(drift)
|
2023-06-12 20:40:28 +02:00
|
|
|
data := bytes.Join(doover.Msgs, []byte{0})
|
2023-06-12 21:01:03 +02:00
|
|
|
_, err := stmtAddDoover.Exec(when.UTC().Format(dbtimeformat), doover.Tries, doover.Userid, rcpt, data)
|
2019-08-16 06:15:55 +02:00
|
|
|
if err != nil {
|
2022-02-06 06:42:13 +01:00
|
|
|
elog.Printf("error saving doover: %s", err)
|
2019-08-16 06:15:55 +02:00
|
|
|
}
|
2019-04-15 03:35:42 +02:00
|
|
|
select {
|
|
|
|
case pokechan <- 0:
|
|
|
|
default:
|
|
|
|
}
|
2019-04-14 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
2023-06-12 20:40:28 +02:00
|
|
|
var dqmtx sync.Mutex
|
|
|
|
|
|
|
|
func delinquent(rcpt string, msg []byte) bool {
|
|
|
|
dqmtx.Lock()
|
|
|
|
defer dqmtx.Unlock()
|
|
|
|
row := stmtDeliquentCheck.QueryRow(rcpt)
|
|
|
|
var dooverid int64
|
|
|
|
var data []byte
|
|
|
|
err := row.Scan(&dooverid, data)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
elog.Printf("error scanning deliquent check: %s", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
data = append(data, 0)
|
|
|
|
data = append(data, msg...)
|
|
|
|
_, err = stmtDeliquentUpdate.Exec(data, dooverid)
|
|
|
|
if err != nil {
|
|
|
|
elog.Printf("error updating deliquent: %s", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-06-12 21:01:03 +02:00
|
|
|
func deliverate(userid int64, rcpt string, msg []byte) {
|
2023-06-12 20:40:28 +02:00
|
|
|
if delinquent(rcpt, msg) {
|
2020-07-18 21:01:00 +02:00
|
|
|
return
|
|
|
|
}
|
2023-06-12 20:40:28 +02:00
|
|
|
var d Doover
|
2023-06-12 21:01:03 +02:00
|
|
|
d.Userid = userid
|
|
|
|
d.Tries = 0
|
2023-06-12 20:40:28 +02:00
|
|
|
d.Rcpt = rcpt
|
|
|
|
d.Msgs = append(d.Msgs, msg)
|
2023-06-12 21:01:03 +02:00
|
|
|
deliveration(d)
|
2020-07-18 21:01:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 02:05:21 +02:00
|
|
|
var garage = gate.NewLimiter(40)
|
2019-04-17 02:34:27 +02:00
|
|
|
|
2023-06-12 21:01:03 +02:00
|
|
|
func deliveration(doover Doover) {
|
2019-10-29 18:38:51 +01:00
|
|
|
garage.Start()
|
|
|
|
defer garage.Finish()
|
2019-04-17 02:34:27 +02:00
|
|
|
|
2019-10-26 00:46:27 +02:00
|
|
|
var ki *KeyInfo
|
2023-06-12 21:01:03 +02:00
|
|
|
ok := ziggies.Get(doover.Userid, &ki)
|
2019-10-26 00:46:27 +02:00
|
|
|
if !ok {
|
2022-02-06 06:42:13 +01:00
|
|
|
elog.Printf("lost key for delivery")
|
2019-10-26 00:46:27 +02:00
|
|
|
return
|
|
|
|
}
|
2019-04-16 20:40:23 +02:00
|
|
|
var inbox string
|
2023-06-12 20:40:28 +02:00
|
|
|
rcpt := doover.Rcpt
|
2019-04-16 20:40:23 +02:00
|
|
|
// already did the box indirection
|
|
|
|
if rcpt[0] == '%' {
|
|
|
|
inbox = rcpt[1:]
|
|
|
|
} else {
|
2019-10-07 20:45:02 +02:00
|
|
|
var box *Box
|
|
|
|
ok := boxofboxes.Get(rcpt, &box)
|
|
|
|
if !ok {
|
2022-02-06 06:42:13 +01:00
|
|
|
ilog.Printf("failed getting inbox for %s", rcpt)
|
2023-06-12 21:01:03 +02:00
|
|
|
sayitagain(doover)
|
2019-04-16 20:40:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
inbox = box.In
|
2019-04-14 21:57:14 +02:00
|
|
|
}
|
2023-06-12 20:40:28 +02:00
|
|
|
for i, msg := range doover.Msgs {
|
|
|
|
if i > 0 {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
}
|
|
|
|
err := PostMsg(ki.keyname, ki.seckey, inbox, msg)
|
|
|
|
if err != nil {
|
|
|
|
ilog.Printf("failed to post json to %s: %s", inbox, err)
|
|
|
|
doover.Msgs = doover.Msgs[i:]
|
2023-06-12 21:01:03 +02:00
|
|
|
sayitagain(doover)
|
2023-06-12 20:40:28 +02:00
|
|
|
return
|
|
|
|
}
|
2019-04-15 03:35:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:35:47 +02:00
|
|
|
var pokechan = make(chan int, 1)
|
2019-04-15 03:35:42 +02:00
|
|
|
|
2019-09-17 15:45:02 +02:00
|
|
|
func getdoovers() []Doover {
|
|
|
|
rows, err := stmtGetDoovers.Query()
|
|
|
|
if err != nil {
|
2022-02-06 06:42:13 +01:00
|
|
|
elog.Printf("wat?")
|
2019-09-17 15:45:02 +02:00
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var doovers []Doover
|
|
|
|
for rows.Next() {
|
|
|
|
var d Doover
|
|
|
|
var dt string
|
|
|
|
err := rows.Scan(&d.ID, &dt)
|
|
|
|
if err != nil {
|
2022-02-06 06:42:13 +01:00
|
|
|
elog.Printf("error scanning dooverid: %s", err)
|
2019-09-17 15:45:02 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.When, _ = time.Parse(dbtimeformat, dt)
|
|
|
|
doovers = append(doovers, d)
|
|
|
|
}
|
|
|
|
return doovers
|
|
|
|
}
|
|
|
|
|
2023-06-12 21:01:03 +02:00
|
|
|
func extractdoover(d *Doover) error {
|
|
|
|
dqmtx.Lock()
|
|
|
|
defer dqmtx.Unlock()
|
|
|
|
row := stmtLoadDoover.QueryRow(d.ID)
|
|
|
|
var data []byte
|
|
|
|
err := row.Scan(&d.Tries, &d.Userid, &d.Rcpt, &data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = stmtZapDoover.Exec(d.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Msgs = bytes.Split(data, []byte{0})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-15 03:35:42 +02:00
|
|
|
func redeliverator() {
|
2022-04-12 20:35:44 +02:00
|
|
|
sleeper := time.NewTimer(5 * time.Second)
|
2019-04-15 03:35:42 +02:00
|
|
|
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:
|
|
|
|
}
|
|
|
|
|
2019-09-17 15:45:02 +02:00
|
|
|
doovers := getdoovers()
|
|
|
|
|
2022-04-12 20:35:44 +02:00
|
|
|
now := time.Now()
|
2019-04-15 03:35:42 +02:00
|
|
|
nexttime := now.Add(24 * time.Hour)
|
|
|
|
for _, d := range doovers {
|
|
|
|
if d.When.Before(now) {
|
2023-06-12 21:01:03 +02:00
|
|
|
err := extractdoover(&d)
|
2019-08-16 06:15:55 +02:00
|
|
|
if err != nil {
|
2023-06-12 21:01:03 +02:00
|
|
|
elog.Printf("error extracting doover: %s", err)
|
2019-08-16 06:15:55 +02:00
|
|
|
continue
|
|
|
|
}
|
2023-06-12 21:01:03 +02:00
|
|
|
ilog.Printf("redeliverating %s try %d", d.Rcpt, d.Tries)
|
|
|
|
deliveration(d)
|
2019-04-15 03:35:42 +02:00
|
|
|
} else if d.When.Before(nexttime) {
|
|
|
|
nexttime = d.When
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 20:35:44 +02:00
|
|
|
now = time.Now()
|
|
|
|
dur := 5 * time.Second
|
|
|
|
if now.Before(nexttime) {
|
|
|
|
dur += nexttime.Sub(now).Round(time.Second)
|
|
|
|
}
|
2019-04-15 03:35:42 +02:00
|
|
|
sleeper.Reset(dur)
|
2019-04-14 21:57:14 +02:00
|
|
|
}
|
|
|
|
}
|