small pixel drawing of a pufferfish clist

main.go

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
package main

import (
	"bufio"
	"bytes"
	"database/sql"
	"flag"
	"fmt"
	"io"
	"log"
	"net/mail"
	"net/smtp"
	"os"
	"regexp"
	"strings"
	"time"

	parsemail "git.cyberia.club/cyberia-services/clist/mail"
	_ "github.com/mattn/go-sqlite3"
	"gopkg.in/ini.v1"
)

type Config struct {
	CommandAddress string `ini:"command_address"`
	Log            string `ini:"log"`
	Database       string `ini:"database"`
	SMTPHostname   string `ini:"smtp_hostname"`
	SMTPPort       string `ini:"smtp_port"`
	SMTPUsername   string `ini:"smtp_username"`
	SMTPPassword   string `ini:"smtp_password"`
	Lists          map[string]*List
	Debug          bool
	ConfigFile     string
}

type List struct {
	Name            string `ini:"name"`
	Archive         string `ini:"archive"`
	Owner           string `ini:"owner"`
	Description     string `ini:"description"`
	Id              string
	Address         string   `ini:"address"`
	Hidden          bool     `ini:"hidden"`
	SubscribersOnly bool     `ini:"subscribers_only"`
	Posters         []string `ini:"posters,omitempty"`
	Bcc             []string `ini:"bcc,omitempty"`
}

var gConfig *Config

// Entry point
func main() {
	gConfig = &Config{}

	flag.StringVar(&gConfig.ConfigFile, "config", "", "Load configuration from specified file")
	flag.Parse()

	loadConfig()

	if len(flag.Args()) < 1 {
		fmt.Printf("Error: Command not specified\n")
		os.Exit(1)
	}

	requireLog()

	if flag.Arg(0) == "message" {
		msg, err := parsemail.Parse(bufio.NewReader(os.Stdin))
		if err != nil {
			log.Printf("ERROR_PARSING_MESSAGE mail=%v msg=%q error=%q\n", os.Stdin, msg, err.Error())
			os.Exit(0)
		}
		log.Printf("MESSAGE_RECEIVED From=%q To=%q Cc=%q Bcc=%q Subject=%q\n",
			msg.From, msg.To, msg.Cc, msg.Bcc, msg.Subject)
		handleMessage(&msg)
	} else {
		fmt.Printf("Unknown command %s\n", flag.Arg(0))
	}
}

func checkAddress(addrs []*mail.Address, checkAddr string) bool {
	for _, a := range addrs {
		if a.Address == checkAddr {
			return true
		}
	}
	return false
}

// Figure out if this is a command, or a mailing list post
func handleMessage(msg *parsemail.Email) {
	if checkAddress(msg.To, gConfig.CommandAddress) {
		handleCommand(msg)
	} else {
		matchedLists := []*List{}
		for _, l := range gConfig.Lists {
			agg := append(msg.To, msg.Cc...)
			if checkAddress(agg, l.Address) {
				matchedLists = append(matchedLists, l)
				log.Printf("matched list: %q", l.Address)
			}
		}

		if len(matchedLists) == 1 {
			list := matchedLists[0]
			if list.CanPost(msg.From[0]) {
				msg := buildListEmail(msg, list)
				send(msg)
				log.Printf("MESSAGE_SENT ListId=%q",
					list.Id)
			} else {
				handleNotAuthorisedToPost(msg)
			}
		} else {
			log.Printf("LISTS: %q", msg)
			handleNoDestination(msg)
		}
	}
}

func subjectParser(s string) string {
	var subject string

	re := regexp.MustCompile(`[Ll]s|[Ll]ists?`)
	if re.MatchString(s) {
		subject = "lists"
	}

	re = regexp.MustCompile(`[Hh]elp`)
	if re.MatchString(s) {
		subject = "help"
	}

	re = regexp.MustCompile(`[Ss]ubscribe `)
	if re.MatchString(s) {
		subject = "subscribe"
	}

	re = regexp.MustCompile(`[Uu]nsubscribe `)
	if re.MatchString(s) {
		subject = "unsubscribe"
	}

	return subject
}

// Handle the command given by the user
func handleCommand(msg *parsemail.Email) {
	switch subjectParser(msg.Subject) {
	case "lists":
		handleShowLists(msg)
	case "help":
		handleHelp(msg)
	case "subscribe":
		handleSubscribe(msg)
	case "unsubscribe":
		handleUnsubscribe(msg)
	default:
		handleUnknownCommand(msg)
	}
}

// Reply to a message that has nowhere to go
func handleNoDestination(msg *parsemail.Email) {
	var body bytes.Buffer
	fmt.Fprintf(&body, "No mailing lists addressed. Your message has not been delivered.\r\n")
	reply := buildCommandEmail(msg, body)
	send(reply)
	log.Printf("UNKNOWN_DESTINATION From=%q To=%q Cc=%q Bcc=%q", msg.From, msg.To, msg.Cc, msg.Bcc)
}

// Reply that the user isn't authorised to post to the list
func handleNotAuthorisedToPost(msg *parsemail.Email) {
	var body bytes.Buffer
	fmt.Fprintf(&body, "You are not an approved poster for this mailing list. Your message has not been delivered.\r\n")
	reply := buildCommandEmail(msg, body)
	send(reply)
	log.Printf("UNAUTHORISED_POST From=%q To=%q Cc=%q Bcc=%q", msg.From, msg.To, msg.Cc, msg.Bcc)
}

// Reply to an unknown command, giving some help
func handleUnknownCommand(msg *parsemail.Email) {
	var body bytes.Buffer
	fmt.Fprintf(&body,
		"%s is not a valid command.\r\n\r\n"+
			"Valid commands are:\r\n\r\n"+
			commandInfo(),
		msg.Subject)
	reply := buildCommandEmail(msg, body)
	send(reply)
	log.Printf("UNKNOWN_COMMAND From=%q", msg.From)
}

// Reply to a help command with help information
func handleHelp(msg *parsemail.Email) {
	var body bytes.Buffer
	fmt.Fprintf(&body, commandInfo())
	reply := buildCommandEmail(msg, body)
	send(reply)
	log.Printf("HELP_SENT To=%q", reply.To)
}

// Reply to a show mailing lists command with a list of mailing lists
func handleShowLists(msg *parsemail.Email) {
	var body bytes.Buffer
	fmt.Fprintf(&body, "Available mailing lists\r\n")
	fmt.Fprintf(&body, "-----------------------\r\n\r\n")
	for _, list := range gConfig.Lists {
		if !list.Hidden {
			fmt.Fprintf(&body,
				"%s\r\n============\r\n"+
					"%s\r\n\r\n",
				list.Id, list.Description)
		}
	}

	fmt.Fprintf(&body,
		"\r\nTo subscribe to a mailing list, email %s with 'subscribe <list-id>' as the subject.\r\n",
		gConfig.CommandAddress)

	email := buildCommandEmail(msg, body)
	send(email)
	log.Printf("LIST_SENT To=%q", msg.From)
}

// Handle a subscribe command
func handleSubscribe(msg *parsemail.Email) {
	listId := strings.TrimPrefix(msg.Subject, "Subscribe ")
	listId = strings.TrimPrefix(listId, "subscribe ")
	list := lookupList(listId)

	if list == nil {
		handleInvalidRequest(msg, listId)
		os.Exit(0)
	}

	var body bytes.Buffer
	if isSubscribed(msg.From[0], listId) {
		fmt.Fprintf(&body, "You are already subscribed to %s\r\n", listId)
		log.Printf("DUPLICATE_SUBSCRIPTION_REQUEST User=%q List=%q\n", msg.From, listId)
	} else {
		addSubscription(msg.From[0], listId)
		fmt.Fprintf(&body, "You are now subscribed to %s\r\n", listId)
		fmt.Fprintf(&body, "To send a message to this list, send an email to %s\r\n", list.Address)
	}
	reply := buildCommandEmail(msg, body)
	send(reply)
}

// Handle an unsubscribe command
func handleUnsubscribe(msg *parsemail.Email) {
	listId := strings.TrimPrefix(msg.Subject, "Unsubscribe ")
	listId = strings.TrimPrefix(listId, "unsubscribe ")
	list := lookupList(listId)

	if list == nil {
		handleInvalidRequest(msg, listId)
		os.Exit(0)
	}

	var body bytes.Buffer
	if !isSubscribed(msg.From[0], listId) {
		fmt.Fprintf(&body, "You aren't subscribed to %s\r\n", listId)
		log.Printf("DUPLICATE_UNSUBSCRIPTION_REQUEST User=%q List=%q\n", msg.From, listId)
	} else {
		removeSubscription(msg.From[0], listId)
		fmt.Fprintf(&body, "You are now unsubscribed from %s\r\n", listId)
	}
	reply := buildCommandEmail(msg, body)
	send(reply)
}

func handleInvalidRequest(msg *parsemail.Email, listId string) {
	var body bytes.Buffer
	fmt.Fprintf(&body, "Unable to operate against %s, Invalid mailing list ID.\r\n", listId)
	reply := buildCommandEmail(msg, body)
	send(reply)
	log.Printf("INVALID_MAILING_LIST From=%q To=%q Cc=%q Bcc=%q", msg.From, msg.To, msg.Cc, msg.Bcc)
}

func badAddress(recipient string, e *parsemail.Email) bool {
	for _, list := range gConfig.Lists {
		// We are a bad address if we are part of the list
		if recipient == list.Address {
			return true
		}
	}

	// We are a bad address if we are already in to/cc
	for _, a := range append(append(e.To, e.Cc...), e.Bcc...) {
		if recipient == a.Address {
			return true
		}
	}
	return false
}

func buildCommandEmail(e *parsemail.Email, t bytes.Buffer) *parsemail.Email {
	response := parsemail.Email{}
	response.Bcc = e.From // return to sender

	mail := "Sender: " + gConfig.CommandAddress + "\r\n" +
		"From: " + gConfig.CommandAddress + "\r\n" +
		"To: " + e.Header.Get("From") + "\r\n" +
		"Precedence: list\r\n" +
		"Subject: Re: " + e.Subject + "\r\n" +
		"Date: " + time.Now().Format("Mon, 2 Jan 2006 15:04:05 -0700") + "\r\n\r\n" +
		t.String()

	response.Bytes = []byte(mail)
	return &response
}

func lookupList(l string) *List {
	for _, list := range gConfig.Lists {
		if l == list.Id {
			return list
		}
	}
	return nil
}

func buildListEmail(e *parsemail.Email, l *List) *parsemail.Email {
	// Build recipient list, stripping garbage
	var recipients []*mail.Address
	for _, subscriber := range fetchSubscribers(l.Id) {
		if !badAddress(subscriber, e) {
			recipients = append(recipients, &mail.Address{Name: "", Address: subscriber})
		}
	}
	email := e
	email.Bcc = recipients

	// Set extra headers
	xtra := "Sender: " + l.Address + "\r\n" +
		"Return-Path: bounce-" + l.Address + "\r\n" +
		"Precedence: list\r\n" +
		"List-Id: <" + strings.Replace(l.Address, "@", ".", -1) + ">\r\n" +
		"List-Post: <mailto:" + l.Address + ">\r\n" +
		"List-Subscribe: <mailto:" + gConfig.CommandAddress + "?subject=subscribe%20" + l.Id + ">\r\n" +
		"List-Unsubscribe: <mailto:" + gConfig.CommandAddress + "?subject=unsubscribe%20" + l.Id + ">\r\n" +
		"List-Help: <mailto:" + l.Address + "?subject=help>\r\n" +
		"List-Archive: <" + l.Archive + ">\r\n" +
		"List-Owner: <" + l.Owner + ">\r\n"

	email.PrefixBytes = []byte(xtra)

	return email
}

func send(e *parsemail.Email) {
	// Bcc = recipients
	var recipients []string
	for _, a := range e.Bcc {
		recipients = append(recipients, a.Address)
	}

	auth := smtp.PlainAuth("", gConfig.SMTPUsername, gConfig.SMTPPassword, gConfig.SMTPHostname)
	err := smtp.SendMail(gConfig.SMTPHostname+":"+gConfig.SMTPPort, auth, e.Header.Get("Sender"), recipients, e.ToBytes())
	if err != nil {
		log.Printf("ERROR_SENDING_MAIL: Error=%q\n", err.Error())
	}
}

// MAILING LIST LOGIC /////////////////////////////////////////////////////////

// Check if the user is authorised to post to this mailing list
func (list *List) CanPost(from *mail.Address) bool {

	// Is this list restricted to subscribers only?
	if list.SubscribersOnly && !isSubscribed(from, list.Id) {
		return false
	}

	// Is there a whitelist of approved posters?
	if len(list.Posters) > 0 {
		for _, poster := range list.Posters {
			if from.Address == poster {
				return true
			}
		}
		return false
	}

	return true
}

// DATABASE LOGIC /////////////////////////////////////////////////////////////

// Open the database
func openDB() (*sql.DB, error) {
	db, err := sql.Open("sqlite3", gConfig.Database)

	if err != nil {
		return nil, err
	}

	_, err = db.Exec(`
	CREATE TABLE IF NOT EXISTS "subscriptions" (
		"list" TEXT,
		"user" TEXT
	);
	`)

	return db, err
}

// Open the database or fail immediately
func requireDB() *sql.DB {
	db, err := openDB()
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(1)
	}
	return db
}

// Fetch list of subscribers to a mailing list from database
func fetchSubscribers(listId string) []string {
	db := requireDB()
	rows, err := db.Query("SELECT user FROM subscriptions WHERE list=?", listId)

	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}

	listIds := []string{}
	defer rows.Close()
	for rows.Next() {
		var user string
		rows.Scan(&user)
		listIds = append(listIds, user)
	}

	return listIds
}

// Check if a user is subscribed to a mailing list
func isSubscribed(address *mail.Address, list string) bool {
	db := requireDB()

	exists := false
	err := db.QueryRow("SELECT 1 FROM subscriptions WHERE user=? AND list=?", address.Address, list).Scan(&exists)

	if err == sql.ErrNoRows {
		return false
	} else if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}

	return true
}

// Add a subscription to the subscription database
func addSubscription(address *mail.Address, list string) {
	db := requireDB()
	_, err := db.Exec("INSERT INTO subscriptions (user,list) VALUES(?,?)", address.Address, list)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}
	log.Printf("SUBSCRIPTION_ADDED Sender=%q List=%q\n", address, list)
}

// Remove a subscription from the subscription database
func removeSubscription(address *mail.Address, list string) {
	db := requireDB()
	_, err := db.Exec("DELETE FROM subscriptions WHERE user=? AND list=?", address.Address, list)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}
	log.Printf("SUBSCRIPTION_REMOVED Sender=%q List=%q\n", address, list)
}

// HELPER FUNCTIONS ///////////////////////////////////////////////////////////

// Open the log file for logging
func openLog() error {
	logFile, err := os.OpenFile(gConfig.Log, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
	if err != nil {
		return err
	}
	out := io.MultiWriter(logFile, os.Stderr)
	log.SetOutput(out)
	return nil
}

// Open the log, or fail immediately
func requireLog() {
	err := openLog()
	if err != nil {
		log.Printf("LOG_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}
}

// Load gConfig from the on-disk config file
func loadConfig() {
	var (
		err error
		cfg *ini.File
	)

	if len(gConfig.ConfigFile) > 0 {
		cfg, err = ini.Load(gConfig.ConfigFile)
	} else {
		cfg, err = ini.LooseLoad("clist.ini", "/etc/clist.ini")
	}

	if err != nil {
		log.Printf("CONFIG_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}

	err = cfg.Section("").MapTo(gConfig)
	if err != nil {
		log.Printf("CONFIG_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}

	gConfig.Lists = make(map[string]*List)

	for _, section := range cfg.ChildSections("list") {
		list := &List{}
		err = section.MapTo(list)
		if err != nil {
			log.Printf("CONFIG_ERROR Error=%q\n", err.Error())
			os.Exit(0)
		}
		list.Id = strings.TrimPrefix(section.Name(), "list.")
		gConfig.Lists[list.Address] = list
	}
}

// Generate an email-able list of commands
func commandInfo() string {
	return fmt.Sprintf("    help\r\n"+
		"      Information about valid commands\r\n"+
		"\r\n"+
		"    list\r\n"+
		"      Retrieve a list of available mailing lists\r\n"+
		"\r\n"+
		"    subscribe <list-id>\r\n"+
		"      Subscribe to <list-id>\r\n"+
		"\r\n"+
		"    unsubscribe <list-id>\r\n"+
		"      Unsubscribe from <list-id>\r\n"+
		"\r\n"+
		"To send a command, email %s with the command as the subject.\r\n",
		gConfig.CommandAddress)
}