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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
package main

import (
	"bufio"
	"bytes"
	"database/sql"
	"flag"
	"fmt"
	"git.cyberia.club/cyberia-services/clist/mail"
	_ "github.com/mattn/go-sqlite3"
	"gopkg.in/ini.v1"
	"io"
	"log"
	"net/mail"
	"net/smtp"
	"os"
	"regexp"
	"strings"
	"time"
)

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 := email.NewEmail()
		msg, err := email.NewEmailFromReader(bufio.NewReader(os.Stdin))
		if err != nil {
			log.Printf("ERROR_PARSING_MESSAGE Error=%q\n", 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 []string, checkAddr string) bool {
	for _, to := range addrs {
		t, _ := mail.ParseAddress(to)
		if t.Address == checkAddr {
			return true
		}
	}
	return false
}

// Figure out if this is a command, or a mailing list post
func handleMessage(msg *email.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("matchedLists: %q", matchedLists)
		if len(matchedLists) == 1 {
			list := matchedLists[0]
			if list.CanPost(msg.From) {
				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 = "ls"
	}

	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 *email.Email) {
	switch subjectParser(msg.Subject) {
	case "ls":
		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 *email.Email) {
	reply := reply(msg)
	reply.From = gConfig.CommandAddress
	reply.Text = []byte("No mailing lists addressed. Your message has not been delivered.\r\n")
	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 *email.Email) {
	reply := reply(msg)
	reply.From = gConfig.CommandAddress
	reply.Text = []byte("You are not an approved poster for this mailing list. Your message has not been delivered.\r\n")
	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 *email.Email) {
	reply := reply(msg)
	reply.From = gConfig.CommandAddress
	reply.Text = []byte(fmt.Sprintf(
		"%s is not a valid command.\r\n\r\n"+
			"Valid commands are:\r\n\r\n"+
			commandInfo(),
		msg.Subject))
	send(reply)
	log.Printf("UNKNOWN_COMMAND From=%q", msg.From)
}

// Reply to a help command with help information
func handleHelp(msg *email.Email) {
	var body bytes.Buffer
	fmt.Fprintf(&body, commandInfo())
	reply := reply(msg)
	reply.From = gConfig.CommandAddress
	reply.Text = []byte(body.String())
	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 *email.Email) {
	log.Printf("HANDLEHSOWLISTS")
	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)
		}
	}

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

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

// Handle a subscribe command
func handleSubscribe(msg *email.Email) {
	listId := strings.TrimPrefix(msg.Subject, "subscribe ")
	list := List{}

	// Switch to id - in case we were passed address
	listId = list.Id

	if isSubscribed(msg.From, listId) {
		reply := reply(msg)
		reply.From = gConfig.CommandAddress
		reply.Text = []byte(fmt.Sprintf("You are already subscribed to %s\r\n", listId))
		send(reply)
		log.Printf("DUPLICATE_SUBSCRIPTION_REQUEST User=%q List=%q\n", msg.From, listId)
		os.Exit(0)
	}

	addSubscription(msg.From, listId)
	reply := reply(msg)
	reply.Text = []byte(fmt.Sprintf("You are now subscribed to %s\r\n", listId))
	send(reply)
}

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

	// Switch to id - in case we were passed address
	listId = list.Id

	if !isSubscribed(msg.From, listId) {
		reply := reply(msg)
		reply.Text = []byte(fmt.Sprintf("You aren't subscribed to %s\r\n", listId))
		send(reply)
		log.Printf("DUPLICATE_UNSUBSCRIPTION_REQUEST User=%q List=%q\n", msg.From, listId)
		os.Exit(0)
	}

	removeSubscription(msg.From, listId)
	reply := reply(msg)
	reply.Text = []byte(fmt.Sprintf("You are now unsubscribed from %s\r\n", listId))
	send(reply)
}

// Create a new message that replies to this message
func reply(msg *email.Email) *email.Email {
	reply := email.NewEmail()
	reply.Subject = "Re: " + msg.Subject
	reply.From = msg.To[0]
	reply.To = []string{msg.From}
	reply.Headers["Date"] = []string{time.Now().Format("Mon, 2 Jan 2006 15:04:05 -0700")}
	return reply
}

func badAddress(a string, list []string) bool {
	for _, l := range list {
		if l == a {
			return true
		}
	}
	return false
}

func buildCommandEmail(e *email.Email, t bytes.Buffer) *email.Email {
	from, err := mail.ParseAddress(e.From)
	if err != nil {
		log.Printf("WARN: CommandEmail: couldn't parse from address")
	}

	email := email.NewEmail()
	email.Sender = gConfig.CommandAddress
	email.From = "<" + gConfig.CommandAddress + ">"
	email.To = []string{from.Name + "<" + from.Address + ">"}
	email.Recipients = []string{from.Address}
	email.Subject = "RE: " + e.Subject
	email.Text = []byte(t.String())
	email.Headers["Date"] = []string{time.Now().Format("Mon, 2 Jan 2006 15:04:05 -0700")}
	email.Headers["Precedence"] = []string{"list"}
	email.Headers["List-Help"] = []string{"<mailto:" + gConfig.CommandAddress + "?subject=help>"}
	return email
}

func buildListEmail(e *email.Email, l *List) *email.Email {
	addresses := []string{}
	m, _ := mail.ParseAddress(e.From)
	addresses = append(addresses, m.Address)
	for _, list := range gConfig.Lists {
		addresses = append(addresses, list.Address)
	}

	cc := []string{}
	recipients := []string{}

	for _, a := range e.Cc {
		if !badAddress(a, addresses) {
			cc = append(cc, a)
		}
	}

	for _, a := range fetchSubscribers(l.Id) {
		if !badAddress(a, addresses) {
			recipients = append(recipients, a)
		}
	}

	// Copy the message
	// Add headers
	// Return the new message
	newEmail := email.NewEmail()
	newEmail.Sender = l.Address
	newEmail.From = getNameOrAddress(e.From) + "<" + l.Address + ">"
	newEmail.To = []string{l.Name + "<" + l.Address + ">"}
	newEmail.Cc = cc
	newEmail.Recipients = recipients
	newEmail.Subject = e.Subject
	newEmail.Text = e.Text
	newEmail.Headers["Date"] = e.Headers["Date"]
	newEmail.Headers["Reply-To"] = []string{e.From}
	newEmail.Headers["Precedence"] = []string{"list"}
	newEmail.Headers["List-Id"] = []string{"<" + l.Id + ">"}
	newEmail.Headers["List-Post"] = []string{"<mailto:" + l.Address + ">"}
	newEmail.Headers["List-Help"] = []string{"<mailto:" + l.Address + "?subject=help>"}
	newEmail.Headers["List-Subscribe"] = []string{"<mailto:" + gConfig.CommandAddress + "?subject=subscribe>"}
	newEmail.Headers["List-Unsubscribe"] = []string{"<mailto:" + gConfig.CommandAddress + "?subject=unsubscribe>"}
	newEmail.Headers["List-Archive"] = []string{"<" + l.Archive + ">"}
	newEmail.Headers["List-Owner"] = []string{"<" + l.Owner + ">"}
	return newEmail
}

func getNameOrAddress(a string) string {
	r, err := mail.ParseAddress(a)
	if err != nil {
		log.Printf("couldn't parse from address")
		return ""
	}
	if r.Name != "" {
		return r.Name
	}
	return r.Address
}

func send(e *email.Email) {
	log.Printf("MESSAGE:\n")
	log.Printf("%q\n", e)
	e.Send("mail.c3f.net:587", smtp.PlainAuth("", gConfig.SMTPUsername, gConfig.SMTPPassword, "mail.c3f.net"))
	//for _, r := range recipients {
	//	sender, _ := mail.ParseAddress(msg.From)
	//	if sender.Address != r {
	//		msg.Bcc = append(msg.Bcc, r)
	//	}
	//	}
}

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

// Check if the user is authorised to post to this mailing list
func (list *List) CanPost(from string) 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 == 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(user string, list string) bool {
	addressObj, err := mail.ParseAddress(user)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}
	db := requireDB()

	exists := false
	err = db.QueryRow("SELECT 1 FROM subscriptions WHERE user=? AND list=?", addressObj.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(user string, list string) {
	addressObj, err := mail.ParseAddress(user)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}

	db := requireDB()
	_, err = db.Exec("INSERT INTO subscriptions (user,list) VALUES(?,?)", addressObj.Address, list)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}
	log.Printf("SUBSCRIPTION_ADDED User=%q List=%q\n", user, list)
}

// Remove a subscription from the subscription database
func removeSubscription(user string, list string) {
	addressObj, err := mail.ParseAddress(user)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}

	db := requireDB()
	_, err = db.Exec("DELETE FROM subscriptions WHERE user=? AND list=?", addressObj.Address, list)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}
	log.Printf("SUBSCRIPTION_REMOVED User=%q List=%q\n", user, list)
}

// Remove all subscriptions from a given mailing list
func clearSubscriptions(list string) {
	db := requireDB()
	_, err := db.Exec("DELETE FROM subscriptions WHERE AND list=?", list)
	if err != nil {
		log.Printf("DATABASE_ERROR Error=%q\n", err.Error())
		os.Exit(0)
	}
}

// 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("nanolist.ini", "/usr/local/etc/nanolist.ini", "/etc/nanolist.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)
}