1. Jameel Haffejee
  2. longtail

Commits

rc1140  committed a883e2c

Something Changed

  • Participants
  • Parent commits e27826e
  • Branches default

Comments (0)

Files changed (1)

File main.go

View file
  • Ignore whitespace
 import (
 	"fmt"
 	"log"
+    "bytes"
+    "strings"
+    "errors"
+	"crypto/sha1"
+	"io"
+	"math/rand"
+	"net/smtp"
+	"net/mail"
+	"time"
+    "strconv"
+
 
 	"github.com/bradfitz/go-smtpd/smtpd"
 	irc "github.com/fluffle/goirc/client"
     r *redis.Client
 )
 
+type Message struct {
+	// HTML-escaped fields sent to the client
+	From, To string
+	Subject  string
+	Body     string // includes images (via data URLs)
+
+	// internal state
+	bodies []string
+	buf    bytes.Buffer // for accumulating email as it comes in
+	msg    interface{}  // alternate message to send
+}
+
+func validateMessage(m *Message){
+    //Store the object values because im too lazy to rewrite
+    from := m.From
+    subject := m.Subject
+    userCountIndex := fmt.Sprintf("user:%q:index", from)
+    exists, _ := r.Exists(fmt.Sprintf("user:%q", from))
+	if !exists {
+		//Generate a random hash
+		randomSeed := rand.New(rand.NewSource(time.Now().UnixNano()))
+		hash := sha1.New()
+		io.WriteString(hash, fmt.Sprintf("%q", randomSeed.Int63()))
+		finalhash := fmt.Sprintf("%x", hash.Sum(nil))
+		replymessage := fmt.Sprintf(registerMessage, finalhash)
+		r.Set(fmt.Sprintf("user:%q", from), finalhash)
+		smtp.SendMail(
+			"localhost:2500",
+			nil,
+			"longtail@superuser.co.za",
+			[]string{from},
+			[]byte(replymessage))
+	} else {
+		hash,_ := r.Get(fmt.Sprintf("user:%q", from))
+        if hash != subject {
+            smtp.SendMail(
+                "localhost:2500",
+                nil,
+                "longtail@superuser.co.za",
+                []string{from},
+                []byte("Invalid Passcode"))
+                return
+        }
+        channelCount ,_ := r.LLen("chan:zacon")
+        indexExists , _ := r.Exists(userCountIndex)
+        var loadIndex int64 = 0
+        if indexExists {
+            tempCountIndex ,_ := r.Get(userCountIndex)
+            loadIndex,_ = strconv.ParseInt(tempCountIndex,0,16)
+        }
+        chanData,_ := r.LRange("chan:zacon",loadIndex,-1)
+        finalBody := strings.Join(chanData,"\n")
+        r.Set(fmt.Sprintf("user:%q:index", from),channelCount)
+        smtp.SendMail(
+                "localhost:2500",
+                nil,
+                "longtail@superuser.co.za",
+                []string{from},
+                []byte(finalBody))
+	}
+}
+
+func (m *Message) parse(r io.Reader) error {
+	msg, err := mail.ReadMessage(r)
+	if err != nil {
+		return err
+	}
+	m.Subject = msg.Header.Get("Subject")
+	m.To = msg.Header.Get("To")
+    validateMessage(m)
+	// If we didn't find a text/plain body, pick the first body we did find.
+	if m.Body == "" {
+		for _, body := range m.bodies {
+			if body != "" {
+				m.Body = body
+				break
+			}
+		}
+	}
+	return nil
+}
+
+func removeNewlines(p []byte) []byte {
+	return bytes.Map(func(r rune) rune {
+		switch r {
+		case '\n', '\r':
+			return -1
+		}
+		return r
+	}, p)
+}
+
+func (m *Message) AddRecipient(rcpt smtpd.MailAddress) error {
+	m.To = strings.ToLower(rcpt.Email())
+	if !strings.HasSuffix(m.To, "@"+domain) {
+		return errors.New("Invalid recipient domain")
+	}
+	return nil
+}
+
+func (m *Message) BeginData() error { return nil }
+
+const maxMessageSize = 5 << 20
+
+func (m *Message) Write(line []byte) error {
+	m.buf.Write(line)
+	if m.buf.Len() > maxMessageSize {
+		return errors.New("too big, yo")
+	}
+	return nil
+}
+
+func (m *Message) Close() error {
+	if err := m.parse(&m.buf); err != nil {
+		return err
+	}
+	return nil
+}
+
 func onNewMail (c smtpd.Connection, from smtpd.MailAddress) (smtpd.Envelope, error) {
     e := &Message{
         From: from.Email(),