1 """Mailing list manager.
3 This is a simple mailing list manager that mimicks the ezmlm-idx mail
4 address commands. See manual page for more information.
8 PLUGIN_INTERFACE_VERSION = "1"
29 # The following values will be overriden by "make install".
30 TEMPLATE_DIRS = ["./templates"]
34 class EocException(Exception):
36 def __init__(self, arg=None):
42 class UnknownList(EocException):
43 def __init__(self, list_name):
44 self.msg = "%s is not a known mailing list" % list_name
46 class BadCommandAddress(EocException):
47 def __init__(self, address):
48 self.msg = "%s is not a valid command address" % address
50 class BadSignature(EocException):
51 def __init__(self, address):
52 self.msg = "address %s has an invalid digital signature" % address
54 class ListExists(EocException):
55 def __init__(self, list_name):
56 self.msg = "Mailing list %s alreadys exists" % list_name
58 class ListDoesNotExist(EocException):
59 def __init__(self, list_name):
60 self.msg = "Mailing list %s does not exist" % list_name
62 class MissingEnvironmentVariable(EocException):
63 def __init__(self, name):
64 self.msg = "Environment variable %s does not exist" % name
66 class MissingTemplate(EocException):
67 def __init__(self, template):
68 self.msg = "Template %s does not exit" % template
71 # Names of commands EoC recognizes in e-mail addresses.
72 SIMPLE_COMMANDS = ["help", "list", "owner", "setlist", "setlistsilently", "ignore"]
73 SUB_COMMANDS = ["subscribe", "unsubscribe"]
74 HASH_COMMANDS = ["subyes", "subapprove", "subreject", "unsubyes",
75 "bounce", "probe", "approve", "reject", "setlistyes",
77 COMMANDS = SIMPLE_COMMANDS + SUB_COMMANDS + HASH_COMMANDS
81 return md5.new(s).hexdigest()
84 def forkexec(argv, text):
85 """Run a command (given as argv array) and write text to its stdin"""
89 raise Exception("fork failed")
94 fd = os.open("/dev/null", os.O_RDWR)
97 os.execvp(argv[0], argv)
103 (pid2, exit) = os.waitpid(pid, 0)
105 raise Exception("os.waitpid for %d returned for %d" % (pid, pid2))
107 raise Exception("subprocess failed, exit=0x%x" % exit)
113 def set_environ(new_environ):
115 environ = new_environ
117 def get_from_environ(key):
124 return env[key].lower()
125 raise MissingEnvironmentVariable(key)
129 """A parser for incoming e-mail addresses."""
131 def __init__(self, lists):
132 self.set_lists(lists)
133 self.set_skip_prefix(None)
134 self.set_forced_domain(None)
136 def set_lists(self, lists):
137 """Set the list of canonical list names we should know about."""
140 def set_skip_prefix(self, skip_prefix):
141 """Set the prefix to be removed from an address."""
142 self.skip_prefix = skip_prefix
144 def set_forced_domain(self, forced_domain):
145 """Set the domain part we should force the address to have."""
146 self.forced_domain = forced_domain
148 def clean(self, address):
149 """Remove cruft from the address and convert the rest to lower case."""
151 n = self.skip_prefix and len(self.skip_prefix)
152 if address[:n] == self.skip_prefix:
153 address = address[n:]
154 if self.forced_domain:
155 parts = address.split("@", 1)
156 address = "%s@%s" % (parts[0], self.forced_domain)
157 return address.lower()
159 def split_address(self, address):
160 """Split an address to a local part and a domain."""
161 parts = address.lower().split("@", 1)
167 # Does an address refer to a list? If not, return None, else return a list
168 # of additional parts (separated by hyphens) in the address. Note that []
169 # is not the same as None.
171 def additional_address_parts(self, address, listname):
172 addr_local, addr_domain = self.split_address(address)
173 list_local, list_domain = self.split_address(listname)
175 if addr_domain != list_domain:
178 if addr_local.lower() == list_local.lower():
182 if addr_local[:n] != list_local or addr_local[n] != "-":
185 return addr_local[n+1:].split("-")
188 # Parse an address we have received that identifies a list we manage.
189 # The address may contain command and signature parts. Return the name
190 # of the list, and a sequence of the additional parts (split at hyphens).
191 # Raise exceptions for errors. Note that the command will be valid, but
192 # cryptographic signatures in the address is not checked.
194 def parse(self, address):
195 address = self.clean(address)
196 for listname in self.lists:
197 parts = self.additional_address_parts(address, listname)
201 return listname, parts
202 elif parts[0] in HASH_COMMANDS:
204 raise BadCommandAddress(address)
205 return listname, parts
206 elif parts[0] in COMMANDS:
207 return listname, parts
209 raise UnknownList(address)
212 class MailingListManager:
214 def __init__(self, dotdir, sendmail="/usr/sbin/sendmail", lists=[],
215 smtp_server=None, qmqp_server=None):
217 self.sendmail = sendmail
218 self.smtp_server = smtp_server
219 self.qmqp_server = qmqp_server
222 self.secret = self.make_and_read_secret()
225 lists = filter(lambda s: "@" in s, os.listdir(dotdir))
226 self.set_lists(lists)
228 self.simple_commands = ["help", "list", "owner", "setlist",
229 "setlistsilently", "ignore"]
230 self.sub_commands = ["subscribe", "unsubscribe"]
231 self.hash_commands = ["subyes", "subapprove", "subreject", "unsubyes",
232 "bounce", "probe", "approve", "reject",
233 "setlistyes", "setlistsilentyes"]
234 self.commands = self.simple_commands + self.sub_commands + \
241 # Create the dot directory for us, if it doesn't exist already.
242 def make_dotdir(self):
243 if not os.path.isdir(self.dotdir):
244 os.makedirs(self.dotdir, 0700)
246 # Create the "secret" file, with a random value used as cookie for
247 # verification addresses.
248 def make_and_read_secret(self):
249 secret_name = os.path.join(self.dotdir, "secret")
250 if not os.path.isfile(secret_name):
251 f = open("/dev/urandom", "r")
254 f = open(secret_name, "w")
258 f = open(secret_name, "r")
263 # Load the plugins from DOTDIR/plugins/*.py.
264 def load_plugins(self):
267 dirname = os.path.join(DOTDIR, "plugins")
269 plugins = os.listdir(dirname)
274 plugins = map(os.path.splitext, plugins)
275 plugins = filter(lambda p: p[1] == ".py", plugins)
276 plugins = map(lambda p: p[0], plugins)
278 pathname = os.path.join(dirname, name + ".py")
279 f = open(pathname, "r")
280 module = imp.load_module(name, f, pathname,
281 (".py", "r", imp.PY_SOURCE))
283 if module.PLUGIN_INTERFACE_VERSION == PLUGIN_INTERFACE_VERSION:
284 self.plugins.append(module)
286 # Call function named funcname (a string) in all plugins, giving as
287 # arguments all the remaining arguments preceded by ml. Return value
288 # of each function is the new list of arguments to the next function.
289 # Return value of this function is the return value of the last function.
290 def call_plugins(self, funcname, list, *args):
291 for plugin in self.plugins:
292 if plugin.__dict__.has_key(funcname):
293 args = apply(plugin.__dict__[funcname], (list,) + args)
294 if type(args) != type((0,)):
298 # Set the list of listnames. The list of lists needs to be sorted in
299 # length order so that test@example.com is matched before
300 # test-list@example.com
301 def set_lists(self, lists):
302 temp = map(lambda s: (len(s), s), lists)
304 self.lists = map(lambda t: t[1], temp)
306 # Return the list of listnames.
310 # Decode an address that has been encoded to be part of a local part.
311 def decode_address(self, parts):
312 return string.join(string.join(parts, "-").split("="), "@")
314 # Is local_part@domain an existing list?
315 def is_list_name(self, local_part, domain):
316 return ("%s@%s" % (local_part, domain)) in self.lists
318 # Compute the verification checksum for an address.
319 def compute_hash(self, address):
320 return md5sum_as_hex(address + self.secret)
322 # Is the verification signature in a parsed address bad? If so, return true,
323 # otherwise return false.
324 def signature_is_bad(self, dict, hash):
325 local_part, domain = dict["name"].split("@")
326 address = "%s-%s-%s@%s" % (local_part, dict["command"], dict["id"],
328 correct = self.compute_hash(address)
329 return correct != hash
331 # Parse a command address we have received and check its validity
332 # (including signature, if any). Return a dictionary with keys
333 # "command", "sender" (address that was encoded into address, if
334 # any), "id" (group ID).
336 def parse_recipient_address(self, address, skip_prefix, forced_domain):
337 ap = AddressParser(self.get_lists())
338 ap.set_lists(self.get_lists())
339 ap.set_skip_prefix(skip_prefix)
340 ap.set_forced_domain(forced_domain)
341 listname, parts = ap.parse(address)
343 dict = { "name": listname }
346 dict["command"] = "post"
348 command, args = parts[0], parts[1:]
349 dict["command"] = command
350 if command in SUB_COMMANDS:
351 dict["sender"] = self.decode_address(args)
352 elif command in HASH_COMMANDS:
355 if self.signature_is_bad(dict, hash):
356 raise BadSignature(address)
360 # Does an address refer to a mailing list?
361 def is_list(self, name, skip_prefix=None, domain=None):
363 self.parse_recipient_address(name, skip_prefix, domain)
364 except BadCommandAddress:
372 # Create a new list and return it.
373 def create_list(self, name):
374 if self.is_list(name):
375 raise ListExists(name)
376 self.set_lists(self.lists + [name])
377 return MailingList(self, name)
379 # Open an existing list.
380 def open_list(self, name):
381 if self.is_list(name):
382 return self.open_list_exact(name)
385 for list in self.lists:
386 if list[:len(x)] == x:
387 return self.open_list_exact(list)
388 raise ListDoesNotExist(name)
390 def open_list_exact(self, name):
391 for list in self.get_lists():
392 if list.lower() == name.lower():
393 return MailingList(self, list)
394 raise ListDoesNotExist(name)
396 # Process an incoming message.
397 def incoming_message(self, skip_prefix, domain, moderate, post):
398 debug("Processing incoming message.")
399 debug("$SENDER = <%s>" % get_from_environ("SENDER"))
400 debug("$RECIPIENT = <%s>" % get_from_environ("RECIPIENT"))
401 dict = self.parse_recipient_address(get_from_environ("RECIPIENT"),
404 dict["force-moderation"] = moderate
405 dict["force-posting"] = post
406 debug("List is <%(name)s>, command is <%(command)s>." % dict)
407 list = self.open_list_exact(dict["name"])
410 # Clean up bouncing address and do other janitorial work for all lists.
411 def cleaning_woman(self, send_mail=None):
413 for listname in self.lists:
414 list = self.open_list_exact(listname)
416 list.send_mail = send_mail
417 list.cleaning_woman(now)
419 # Send a mail to the desired recipients.
420 def send_mail(self, envelope_sender, recipients, text):
421 debug("send_mail:\n sender=%s\n recipients=%s\n text=\n %s" %
422 (envelope_sender, str(recipients),
423 "\n ".join(text[:text.find("\n\n")].split("\n"))))
427 smtp = smtplib.SMTP(self.smtp_server)
428 smtp.sendmail(envelope_sender, recipients, text)
431 error("Error sending SMTP mail, mail probably not sent")
433 elif self.qmqp_server:
435 q = qmqp.QMQP(self.qmqp_server)
436 q.sendmail(envelope_sender, recipients, text)
439 error("Error sending QMQP mail, mail probably not sent")
442 status = forkexec([self.sendmail, "-oi", "-f",
443 envelope_sender] + recipients, text)
445 error("%s returned %s, mail sending probably failed" %
446 (self.sendmail, status))
447 sys.exit((status >> 8) & 0xff)
449 debug("send_mail: no recipients, not sending")
455 posting_opts = ["auto", "free", "moderated"]
457 def __init__(self, mlm, name):
461 self.cp = ConfigParser.ConfigParser()
462 self.cp.add_section("list")
463 self.cp.set("list", "owners", "")
464 self.cp.set("list", "moderators", "")
465 self.cp.set("list", "subscription", "free")
466 self.cp.set("list", "posting", "free")
467 self.cp.set("list", "archived", "no")
468 self.cp.set("list", "mail-on-subscription-changes", "no")
469 self.cp.set("list", "mail-on-forced-unsubscribe", "no")
470 self.cp.set("list", "ignore-bounce", "no")
471 self.cp.set("list", "language", "")
472 self.cp.set("list", "pristine-headers", "")
474 self.dirname = os.path.join(self.mlm.dotdir, name)
476 self.cp.read(self.mkname("config"))
478 self.subscribers = SubscriberDatabase(self.dirname, "subscribers")
479 self.moderation_box = MessageBox(self.dirname, "moderation-box")
480 self.subscription_box = MessageBox(self.dirname, "subscription-box")
481 self.bounce_box = MessageBox(self.dirname, "bounce-box")
483 def make_listdir(self):
484 if not os.path.isdir(self.dirname):
485 os.mkdir(self.dirname, 0700)
487 f = open(self.mkname("subscribers"), "w")
490 def mkname(self, relative):
491 return os.path.join(self.dirname, relative)
493 def save_config(self):
494 f = open(self.mkname("config"), "w")
498 def read_stdin(self):
499 data = sys.stdin.read()
500 # Convert CRLF to plain LF
501 data = "\n".join(data.split("\r\n"))
502 # Skip Unix mbox "From " mail start indicator
503 if data[:5] == "From ":
504 data = string.split(data, "\n", 1)[1]
507 def invent_boundary(self):
508 return "%s/%s" % (md5sum_as_hex(str(time.time())),
509 md5sum_as_hex(self.name))
511 def command_address(self, command):
512 local_part, domain = self.name.split("@")
513 return "%s-%s@%s" % (local_part, command, domain)
515 def signed_address(self, command, id):
516 unsigned = self.command_address("%s-%s" % (command, id))
517 hash = self.mlm.compute_hash(unsigned)
518 return self.command_address("%s-%s-%s" % (command, id, hash))
521 return self.command_address("ignore")
523 def nice_7bit(self, str):
525 if (ord(c) < 32 and not c.isspace()) or ord(c) >= 127:
529 def mime_encode_headers(self, text):
531 headers, body = text.split("\n\n", 1)
534 for line in headers.split("\n"):
535 if line[0].isspace():
542 if self.nice_7bit(header):
543 headers.append(header)
546 name, content = header.split(": ", 1)
548 name, content = header.split(":", 1)
549 hdr = email.Header.Header(content, "utf-8")
550 headers.append(name + ": " + hdr.encode())
552 return "\n".join(headers) + "\n\n" + body
554 info("Cannot MIME encode header, using original ones, sorry")
557 def template(self, template_name, dict):
558 lang = self.cp.get("list", "language")
560 template_name_lang = template_name + "." + lang
562 template_name_lang = template_name
564 if not dict.has_key("list"):
565 dict["list"] = self.name
566 dict["local"], dict["domain"] = self.name.split("@")
567 if not dict.has_key("list"):
568 dict["list"] = self.name
570 for dir in [os.path.join(self.dirname, "templates")] + TEMPLATE_DIRS:
571 pathname = os.path.join(dir, template_name_lang)
572 if not os.path.exists(pathname):
573 pathname = os.path.join(dir, template_name)
574 if os.path.exists(pathname):
575 f = open(pathname, "r")
580 raise MissingTemplate(template_name)
582 def send_template(self, envelope_sender, sender, recipients,
583 template_name, dict):
584 dict["From"] = "EoC <%s>" % sender
585 dict["To"] = string.join(recipients, ", ")
586 text = self.template(template_name, dict)
589 if self.cp.get("list", "pristine-headers") != "yes":
590 text = self.mime_encode_headers(text)
591 self.mlm.send_mail(envelope_sender, recipients, text)
593 def send_info_message(self, recipients, template_name, dict):
594 self.send_template(self.command_address("ignore"),
595 self.command_address("help"),
601 return self.cp.get("list", "owners").split()
603 def moderators(self):
604 return self.cp.get("list", "moderators").split()
606 def is_list_owner(self, address):
607 return address in self.owners()
610 self.send_info_message([get_from_environ("SENDER")], "help", {})
613 recipient = get_from_environ("SENDER")
614 if self.is_list_owner(recipient):
615 addr_list = self.subscribers.get_all()
616 addr_text = string.join(addr_list, "\n")
617 self.send_info_message([recipient], "list",
619 "addresses": addr_text,
620 "count": len(addr_list),
623 self.send_info_message([recipient], "list-sorry", {})
625 def obey_setlist(self, origmail):
626 recipient = get_from_environ("SENDER")
627 if self.is_list_owner(recipient):
628 id = self.moderation_box.add(recipient, origmail)
629 if self.parse_setlist_addresses(origmail) == None:
630 self.send_bad_addresses_in_setlist(id)
631 self.moderation_box.remove(id)
633 confirm = self.signed_address("setlistyes", id)
634 self.send_info_message(self.owners(), "setlist-confirm",
637 "origmail": origmail,
638 "boundary": self.invent_boundary(),
642 self.send_info_message([recipient], "setlist-sorry", {})
644 def obey_setlistsilently(self, origmail):
645 recipient = get_from_environ("SENDER")
646 if self.is_list_owner(recipient):
647 id = self.moderation_box.add(recipient, origmail)
648 if self.parse_setlist_addresses(origmail) == None:
649 self.send_bad_addresses_in_setlist(id)
650 self.moderation_box.remove(id)
652 confirm = self.signed_address("setlistsilentyes", id)
653 self.send_info_message(self.owners(), "setlist-confirm",
656 "origmail": origmail,
657 "boundary": self.invent_boundary(),
660 self.send_info_message([recipient], "setlist-sorry", {})
662 def parse_setlist_addresses(self, text):
663 body = text.split("\n\n", 1)[1]
664 lines = body.split("\n")
665 lines = filter(lambda line: line != "", lines)
666 badlines = filter(lambda line: "@" not in line, lines)
672 def send_bad_addresses_in_setlist(self, id):
673 addr = self.moderation_box.get_address(id)
674 origmail = self.moderation_box.get(id)
675 self.send_info_message([addr], "setlist-badlist",
677 "origmail": origmail,
678 "boundary": self.invent_boundary(),
682 def obey_setlistyes(self, dict):
683 if self.moderation_box.has(dict["id"]):
684 text = self.moderation_box.get(dict["id"])
685 addresses = self.parse_setlist_addresses(text)
686 if addresses == None:
687 self.send_bad_addresses_in_setlist(id)
689 removed_subscribers = []
690 self.subscribers.lock()
691 old = self.subscribers.get_all()
693 if address.lower() not in map(string.lower, addresses):
694 self.subscribers.remove(address)
695 removed_subscribers.append(address)
698 if x.lower() == address.lower():
700 self.subscribers.add_many(addresses)
701 self.subscribers.save()
703 for recipient in addresses:
704 self.send_info_message([recipient], "sub-welcome", {})
705 for recipient in removed_subscribers:
706 self.send_info_message([recipient], "unsub-goodbye", {})
707 self.send_info_message(self.owners(), "setlist-done", {})
709 self.moderation_box.remove(dict["id"])
711 def obey_setlistsilentyes(self, dict):
712 if self.moderation_box.has(dict["id"]):
713 text = self.moderation_box.get(dict["id"])
714 addresses = self.parse_setlist_addresses(text)
715 if addresses == None:
716 self.send_bad_addresses_in_setlist(id)
718 self.subscribers.lock()
719 old = self.subscribers.get_all()
721 if address not in addresses:
722 self.subscribers.remove(address)
724 addresses.remove(address)
725 self.subscribers.add_many(addresses)
726 self.subscribers.save()
727 self.send_info_message(self.owners(), "setlist-done", {})
729 self.moderation_box.remove(dict["id"])
731 def obey_owner(self, text):
732 sender = get_from_environ("SENDER")
733 recipients = self.cp.get("list", "owners").split()
734 self.mlm.send_mail(sender, recipients, text)
736 def obey_subscribe_or_unsubscribe(self, dict, template_name, command,
739 requester = get_from_environ("SENDER")
740 subscriber = dict["sender"]
742 subscriber = requester
743 if subscriber.find("@") == -1:
744 info("Trying to (un)subscribe address without @: %s" % subscriber)
746 if self.cp.get("list", "ignore-bounce") == "yes":
747 info("Will not (un)subscribe address: %s from static list" %subscriber)
749 if requester in self.owners():
750 confirmers = self.owners()
752 confirmers = [subscriber]
754 id = self.subscription_box.add(subscriber, origmail)
755 confirm = self.signed_address(command, id)
756 self.send_info_message(confirmers, template_name,
759 "origmail": origmail,
760 "boundary": self.invent_boundary(),
763 def obey_subscribe(self, dict, origmail):
764 self.obey_subscribe_or_unsubscribe(dict, "sub-confirm", "subyes",
767 def obey_unsubscribe(self, dict, origmail):
768 self.obey_subscribe_or_unsubscribe(dict, "unsub-confirm", "unsubyes",
771 def obey_subyes(self, dict):
772 if self.subscription_box.has(dict["id"]):
773 if self.cp.get("list", "subscription") == "free":
774 recipient = self.subscription_box.get_address(dict["id"])
775 self.subscribers.lock()
776 self.subscribers.add(recipient)
777 self.subscribers.save()
778 sender = self.command_address("help")
779 self.send_template(self.ignore(), sender, [recipient],
781 self.subscription_box.remove(dict["id"])
782 if self.cp.get("list", "mail-on-subscription-changes")=="yes":
783 self.send_info_message(self.owners(),
784 "sub-owner-notification",
786 "address": recipient,
789 recipients = self.cp.get("list", "owners").split()
790 confirm = self.signed_address("subapprove", dict["id"])
791 deny = self.signed_address("subreject", dict["id"])
792 subscriber = self.subscription_box.get_address(dict["id"])
793 origmail = self.subscription_box.get(dict["id"])
794 self.send_template(self.ignore(), deny, recipients,
799 "subscriber": subscriber,
800 "origmail": origmail,
801 "boundary": self.invent_boundary(),
803 recipient = self.subscription_box.get_address(dict["id"])
804 self.send_info_message([recipient], "sub-wait", {})
806 def obey_subapprove(self, dict):
807 if self.subscription_box.has(dict["id"]):
808 recipient = self.subscription_box.get_address(dict["id"])
809 self.subscribers.lock()
810 self.subscribers.add(recipient)
811 self.subscribers.save()
812 self.send_info_message([recipient], "sub-welcome", {})
813 self.subscription_box.remove(dict["id"])
814 if self.cp.get("list", "mail-on-subscription-changes")=="yes":
815 self.send_info_message(self.owners(), "sub-owner-notification",
817 "address": recipient,
820 def obey_subreject(self, dict):
821 if self.subscription_box.has(dict["id"]):
822 recipient = self.subscription_box.get_address(dict["id"])
823 self.send_info_message([recipient], "sub-reject", {})
824 self.subscription_box.remove(dict["id"])
826 def obey_unsubyes(self, dict):
827 if self.subscription_box.has(dict["id"]):
828 recipient = self.subscription_box.get_address(dict["id"])
829 self.subscribers.lock()
830 self.subscribers.remove(recipient)
831 self.subscribers.save()
832 self.send_info_message([recipient], "unsub-goodbye", {})
833 self.subscription_box.remove(dict["id"])
834 if self.cp.get("list", "mail-on-subscription-changes")=="yes":
835 self.send_info_message(self.owners(),
836 "unsub-owner-notification",
838 "address": recipient,
841 def store_into_archive(self, text):
842 if self.cp.get("list", "archived") == "yes":
843 archdir = os.path.join(self.dirname, "archive")
844 if not os.path.exists(archdir):
845 os.mkdir(archdir, 0700)
846 id = md5sum_as_hex(text)
847 f = open(os.path.join(archdir, id), "w")
851 def list_headers(self):
852 local, domain = self.name.split("@")
854 list.append("List-Id: <%s.%s>" % (local, domain))
855 list.append("List-Help: <mailto:%s-help@%s>" % (local, domain))
856 list.append("List-Unsubscribe: <mailto:%s-unsubscribe@%s>" %
858 list.append("List-Subscribe: <mailto:%s-subscribe@%s>" %
860 list.append("List-Post: <mailto:%s@%s>" % (local, domain))
861 list.append("List-Owner: <mailto:%s-owner@%s>" % (local, domain))
862 list.append("Precedence: bulk");
863 return string.join(list, "\n") + "\n"
865 def read_file(self, basename):
867 f = open(os.path.join(self.dirname, basename), "r")
874 def headers_to_add(self):
875 headers_to_add = self.read_file("headers-to-add").rstrip()
877 return headers_to_add + "\n"
881 def remove_some_headers(self, mail, headers_to_remove):
882 endpos = mail.find("\n\n")
884 endpos = mail.find("\n\r\n")
887 headers = mail[:endpos].split("\n")
890 headers_to_remove = [x.lower() for x in headers_to_remove]
893 add_continuation_lines = 0
895 for header in headers:
896 if header[0] in [' ','\t']:
897 # this is a continuation line
898 if add_continuation_lines:
899 remaining.append(header)
901 pos = header.find(":")
903 # malformed message, try to remove the junk
904 add_continuation_lines = 0
906 name = header[:pos].lower()
907 if name in headers_to_remove:
908 add_continuation_lines = 0
910 add_continuation_lines = 1
911 remaining.append(header)
913 return "\n".join(remaining) + body
915 def headers_to_remove(self, text):
916 headers_to_remove = self.read_file("headers-to-remove").split("\n")
917 headers_to_remove = map(lambda s: s.strip().lower(),
919 return self.remove_some_headers(text, headers_to_remove)
921 def append_footer(self, text):
922 if "base64" in text or "BASE64" in text:
924 for line in StringIO.StringIO(text):
925 if line.lower().startswith("content-transfer-encoding:") and \
926 "base64" in line.lower():
928 return text + self.template("footer", {})
930 def send_mail_to_subscribers(self, text):
931 text = self.remove_some_headers(text, ["list-id", "list-help",
933 "list-subscribe", "list-post",
934 "list-owner", "precedence"])
935 text = self.headers_to_add() + self.list_headers() + \
936 self.headers_to_remove(text)
937 text = self.append_footer(text)
938 text, = self.mlm.call_plugins("send_mail_to_subscribers_hook",
940 if have_email_module and \
941 self.cp.get("list", "pristine-headers") != "yes":
942 text = self.mime_encode_headers(text)
943 self.store_into_archive(text)
944 for group in self.subscribers.groups():
945 bounce = self.signed_address("bounce", group)
946 addresses = self.subscribers.in_group(group)
947 self.mlm.send_mail(bounce, addresses, text)
949 def post_into_moderate(self, poster, dict, text):
950 id = self.moderation_box.add(poster, text)
951 recipients = self.moderators()
953 recipients = self.owners()
955 confirm = self.signed_address("approve", id)
956 deny = self.signed_address("reject", id)
957 self.send_template(self.ignore(), deny, recipients, "msg-moderate",
962 "boundary": self.invent_boundary(),
964 self.send_info_message([poster], "msg-wait", {})
966 def should_be_moderated(self, posting, poster):
967 if posting == "moderated":
969 if posting == "auto":
970 if poster.lower() not in \
971 map(string.lower, self.subscribers.get_all()):
975 def obey_post(self, dict, text):
976 if dict.has_key("force-moderation") and dict["force-moderation"]:
980 if dict.has_key("force-posting") and dict["force-posting"]:
984 posting = self.cp.get("list", "posting")
985 if posting not in self.posting_opts:
986 error("You have a weird 'posting' config. Please, review it")
987 poster = get_from_environ("SENDER")
989 self.post_into_moderate(poster, dict, text)
991 self.send_mail_to_subscribers(text)
992 elif self.should_be_moderated(posting, poster):
993 self.post_into_moderate(poster, dict, text)
995 self.send_mail_to_subscribers(text)
997 def obey_approve(self, dict):
998 if self.moderation_box.lock(dict["id"]):
999 if self.moderation_box.has(dict["id"]):
1000 text = self.moderation_box.get(dict["id"])
1001 self.send_mail_to_subscribers(text)
1002 self.moderation_box.remove(dict["id"])
1003 self.moderation_box.unlock(dict["id"])
1005 def obey_reject(self, dict):
1006 if self.moderation_box.lock(dict["id"]):
1007 if self.moderation_box.has(dict["id"]):
1008 self.moderation_box.remove(dict["id"])
1009 self.moderation_box.unlock(dict["id"])
1011 def split_address_list(self, addrs):
1014 userpart, domain = addr.split("@")
1015 if domains.has_key(domain):
1016 domains[domain].append(addr)
1018 domains[domain] = [addr]
1020 if len(domains.keys()) == 1:
1022 result.append([addr])
1024 result = domains.values()
1027 def obey_bounce(self, dict, text):
1028 if self.subscribers.has_group(dict["id"]):
1029 self.subscribers.lock()
1030 addrs = self.subscribers.in_group(dict["id"])
1032 if self.cp.get("list", "ignore-bounce") == "yes":
1033 info("Address <%s> bounced, ignoring bounce as configured." %
1035 self.subscribers.unlock()
1037 debug("Address <%s> bounced, setting state to bounce." %
1039 bounce_id = self.bounce_box.add(addrs[0], text[:4096])
1040 self.subscribers.set(dict["id"], "status", "bounced")
1041 self.subscribers.set(dict["id"], "timestamp-bounced",
1043 self.subscribers.set(dict["id"], "bounce-id",
1046 debug("Group %s bounced, splitting." % dict["id"])
1047 for new_addrs in self.split_address_list(addrs):
1048 self.subscribers.add_many(new_addrs)
1049 self.subscribers.remove_group(dict["id"])
1050 self.subscribers.save()
1052 debug("Ignoring bounce, group %s doesn't exist (anymore?)." %
1055 def obey_probe(self, dict, text):
1057 if self.subscribers.has_group(id):
1058 self.subscribers.lock()
1059 if self.subscribers.get(id, "status") == "probed":
1060 self.subscribers.set(id, "status", "probebounced")
1061 self.subscribers.save()
1063 def obey(self, dict):
1064 text = self.read_stdin()
1066 if dict["command"] in ["help", "list", "subscribe", "unsubscribe",
1067 "subyes", "subapprove", "subreject",
1068 "unsubyes", "post", "approve"]:
1069 sender = get_from_environ("SENDER")
1071 debug("Ignoring bounce message for %s command." %
1075 if dict["command"] == "help":
1077 elif dict["command"] == "list":
1079 elif dict["command"] == "owner":
1080 self.obey_owner(text)
1081 elif dict["command"] == "subscribe":
1082 self.obey_subscribe(dict, text)
1083 elif dict["command"] == "unsubscribe":
1084 self.obey_unsubscribe(dict, text)
1085 elif dict["command"] == "subyes":
1086 self.obey_subyes(dict)
1087 elif dict["command"] == "subapprove":
1088 self.obey_subapprove(dict)
1089 elif dict["command"] == "subreject":
1090 self.obey_subreject(dict)
1091 elif dict["command"] == "unsubyes":
1092 self.obey_unsubyes(dict)
1093 elif dict["command"] == "post":
1094 self.obey_post(dict, text)
1095 elif dict["command"] == "approve":
1096 self.obey_approve(dict)
1097 elif dict["command"] == "reject":
1098 self.obey_reject(dict)
1099 elif dict["command"] == "bounce":
1100 self.obey_bounce(dict, text)
1101 elif dict["command"] == "probe":
1102 self.obey_probe(dict, text)
1103 elif dict["command"] == "setlist":
1104 self.obey_setlist(text)
1105 elif dict["command"] == "setlistsilently":
1106 self.obey_setlistsilently(text)
1107 elif dict["command"] == "setlistyes":
1108 self.obey_setlistyes(dict)
1109 elif dict["command"] == "setlistsilentyes":
1110 self.obey_setlistsilentyes(dict)
1111 elif dict["command"] == "ignore":
1114 def get_bounce_text(self, id):
1115 bounce_id = self.subscribers.get(id, "bounce-id")
1116 if self.bounce_box.has(bounce_id):
1117 bounce_text = self.bounce_box.get(bounce_id)
1118 bounce_text = string.join(map(lambda s: "> " + s + "\n",
1119 bounce_text.split("\n")), "")
1121 bounce_text = "Bounce message not available."
1124 one_week = 7.0 * 24.0 * 60.0 * 60.0
1126 def handle_bounced_groups(self, now):
1127 for id in self.subscribers.groups():
1128 status = self.subscribers.get(id, "status")
1129 t = float(self.subscribers.get(id, "timestamp-bounced"))
1130 if status == "bounced":
1131 if now - t > self.one_week:
1132 sender = self.signed_address("probe", id)
1133 recipients = self.subscribers.in_group(id)
1134 self.send_template(sender, sender, recipients,
1136 "bounce": self.get_bounce_text(id),
1137 "boundary": self.invent_boundary(),
1139 self.subscribers.set(id, "status", "probed")
1140 elif status == "probed":
1141 if now - t > 2 * self.one_week:
1142 debug(("Cleaning woman: probe didn't bounce " +
1143 "for group <%s>, setting status to ok.") % id)
1144 self.subscribers.set(id, "status", "ok")
1145 self.bounce_box.remove(
1146 self.subscribers.get(id, "bounce-id"))
1147 elif status == "probebounced":
1148 sender = self.command_address("help")
1149 for address in self.subscribers.in_group(id):
1150 if self.cp.get("list", "mail-on-forced-unsubscribe") \
1152 self.send_template(sender, sender,
1154 "bounce-owner-notification",
1157 "bounce": self.get_bounce_text(id),
1158 "boundary": self.invent_boundary(),
1161 self.bounce_box.remove(
1162 self.subscribers.get(id, "bounce-id"))
1163 self.subscribers.remove(address)
1164 debug("Cleaning woman: removing <%s>." % address)
1165 self.send_template(sender, sender, [address],
1166 "bounce-goodbye", {})
1168 def join_nonbouncing_groups(self, now):
1170 for id in self.subscribers.groups():
1171 status = self.subscribers.get(id, "status")
1172 age1 = now - float(self.subscribers.get(id, "timestamp-bounced"))
1173 age2 = now - float(self.subscribers.get(id, "timestamp-created"))
1175 if age1 > self.one_week and age2 > self.one_week:
1176 to_be_joined.append(id)
1179 for id in to_be_joined:
1180 addrs = addrs + self.subscribers.in_group(id)
1181 self.subscribers.add_many(addrs)
1182 for id in to_be_joined:
1183 self.bounce_box.remove(self.subscribers.get(id, "bounce-id"))
1184 self.subscribers.remove_group(id)
1186 def remove_empty_groups(self):
1187 for id in self.subscribers.groups()[:]:
1188 if len(self.subscribers.in_group(id)) == 0:
1189 self.subscribers.remove_group(id)
1191 def cleaning_woman(self, now):
1192 if self.subscribers.lock():
1193 self.handle_bounced_groups(now)
1194 self.join_nonbouncing_groups(now)
1195 self.subscribers.save()
1197 class SubscriberDatabase:
1199 def __init__(self, dirname, name):
1201 self.filename = os.path.join(dirname, name)
1202 self.lockname = os.path.join(dirname, "lock")
1207 if os.system("lockfile -l 60 %s" % self.lockname) == 0:
1213 os.remove(self.lockname)
1217 if not self.loaded and not self.dict:
1218 f = open(self.filename, "r")
1219 for line in f.xreadlines():
1220 parts = line.split()
1221 self.dict[parts[0]] = {
1223 "timestamp-created": parts[2],
1224 "timestamp-bounced": parts[3],
1225 "bounce-id": parts[4],
1226 "addresses": parts[5:],
1234 f = open(self.filename + ".new", "w")
1235 for id in self.dict.keys():
1237 f.write("%s " % self.dict[id]["status"])
1238 f.write("%s " % self.dict[id]["timestamp-created"])
1239 f.write("%s " % self.dict[id]["timestamp-bounced"])
1240 f.write("%s " % self.dict[id]["bounce-id"])
1241 f.write("%s\n" % string.join(self.dict[id]["addresses"], " "))
1243 os.remove(self.filename)
1244 os.rename(self.filename + ".new", self.filename)
1247 def get(self, id, attribute):
1249 if self.dict.has_key(id) and self.dict[id].has_key(attribute):
1250 return self.dict[id][attribute]
1253 def set(self, id, attribute, value):
1256 if self.dict.has_key(id) and self.dict[id].has_key(attribute):
1257 self.dict[id][attribute] = value
1259 def add(self, address):
1260 return self.add_many([address])
1262 def add_many(self, addresses):
1265 for addr in addresses[:]:
1266 if addr.find("@") == -1:
1267 info("Address '%s' does not contain an @, ignoring it." % addr)
1268 addresses.remove(addr)
1269 for id in self.dict.keys():
1270 old_ones = self.dict[id]["addresses"]
1271 for addr in addresses:
1273 if x.lower() == addr.lower():
1275 self.dict[id]["addresses"] = old_ones
1276 id = self.new_group()
1279 "timestamp-created": self.timestamp(),
1280 "timestamp-bounced": "0",
1281 "bounce-id": "..notexist..",
1282 "addresses": addresses,
1286 def new_group(self):
1287 keys = self.dict.keys()
1289 keys = map(lambda x: int(x), keys)
1291 return "%d" % (keys[-1] + 1)
1295 def timestamp(self):
1296 return "%.0f" % time.time()
1301 for values in self.dict.values():
1302 list = list + values["addresses"]
1307 return self.dict.keys()
1309 def has_group(self, id):
1311 return self.dict.has_key(id)
1313 def in_group(self, id):
1315 return self.dict[id]["addresses"]
1317 def remove(self, address):
1320 for id in self.dict.keys():
1321 group = self.dict[id]
1322 for x in group["addresses"][:]:
1323 if x.lower() == address.lower():
1324 group["addresses"].remove(x)
1325 if len(group["addresses"]) == 0:
1328 def remove_group(self, id):
1336 def __init__(self, dirname, boxname):
1337 self.boxdir = os.path.join(dirname, boxname)
1338 if not os.path.isdir(self.boxdir):
1339 os.mkdir(self.boxdir, 0700)
1341 def filename(self, id):
1342 return os.path.join(self.boxdir, id)
1344 def add(self, address, message_text):
1345 id = self.make_id(message_text)
1346 filename = self.filename(id)
1347 f = open(filename + ".address", "w")
1350 f = open(filename + ".new", "w")
1351 f.write(message_text)
1353 os.rename(filename + ".new", filename)
1356 def make_id(self, message_text):
1357 return md5sum_as_hex(message_text)
1358 # XXX this might be unnecessarily long
1360 def remove(self, id):
1361 filename = self.filename(id)
1362 if os.path.isfile(filename):
1364 os.remove(filename + ".address")
1367 return os.path.isfile(self.filename(id))
1369 def get_address(self, id):
1370 f = open(self.filename(id) + ".address", "r")
1376 f = open(self.filename(id), "r")
1381 def lockname(self, id):
1382 return self.filename(id) + ".lock"
1385 if os.system("lockfile -l 600 %s" % self.lockname(id)) == 0:
1390 def unlock(self, id):
1392 os.remove(self.lockname(id))
1400 def write(self, str):
1404 log_file_handle = None
1406 global log_file_handle
1407 if log_file_handle is None:
1409 log_file_handle = open(os.path.join(DOTDIR, "logfile.txt"), "a")
1411 log_file_handle = DevNull()
1412 return log_file_handle
1415 tuple = time.localtime(time.time())
1416 return time.strftime("%Y-%m-%d %H:%M:%S", tuple) + " [%d]" % os.getpid()
1422 # No logging to stderr of debug messages. Some MTAs have a limit on how
1423 # much data they accept via stderr and debug logs will fill that quickly.
1425 log_file().write(timestamp() + " " + msg + "\n")
1428 # Log to log file first, in case MTA's stderr buffer fills up and we lose
1431 log_file().write(timestamp() + " " + msg + "\n")
1432 sys.stderr.write(msg + "\n")
1441 sys.stdout.write("""\
1442 Usage: enemies-of-carlotta [options] command
1443 Mailing list manager.
1446 --name=listname@domain
1447 --owner=address@domain
1448 --moderator=address@domain
1449 --subscription=free/moderated
1450 --posting=free/moderated/auto
1452 --ignore-bounce=yes/no
1453 --language=language code or empty
1454 --mail-on-forced-unsubscribe=yes/no
1455 --mail-on-subscription-changes=yes/no
1456 --skip-prefix=string
1457 --domain=domain.name
1458 --smtp-server=domain.name
1474 For more detailed information, please read the enemies-of-carlotta(1)
1480 def no_act_send_mail(sender, recipients, text):
1481 print "NOT SENDING MAIL FOR REAL!"
1482 print "Sender:", sender
1483 print "Recipients:", recipients
1485 print "\n".join(map(lambda s: " " + s, text.split("\n")))
1488 def set_list_options(list, owners, moderators, subscription, posting,
1489 archived, language, ignore_bounce,
1490 mail_on_sub_changes, mail_on_forced_unsub):
1492 list.cp.set("list", "owners", string.join(owners, " "))
1494 list.cp.set("list", "moderators", string.join(moderators, " "))
1495 if subscription != None:
1496 list.cp.set("list", "subscription", subscription)
1498 list.cp.set("list", "posting", posting)
1499 if archived != None:
1500 list.cp.set("list", "archived", archived)
1501 if language != None:
1502 list.cp.set("list", "language", language)
1503 if ignore_bounce != None:
1504 list.cp.set("list", "ignore-bounce", ignore_bounce)
1505 if mail_on_sub_changes != None:
1506 list.cp.set("list", "mail-on-subscription-changes",
1507 mail_on_sub_changes)
1508 if mail_on_forced_unsub != None:
1509 list.cp.set("list", "mail-on-forced-unsubscribe",
1510 mail_on_forced_unsub)
1515 opts, args = getopt.getopt(args, "h",
1524 "mail-on-forced-unsubscribe=",
1525 "mail-on-subscription-changes=",
1553 except getopt.GetoptError, detail:
1554 error("Error parsing command line options (see --help):\n%s" %
1564 ignore_bounce = None
1567 sendmail = "/usr/sbin/sendmail"
1575 mail_on_forced_unsub = None
1576 mail_on_sub_changes = None
1580 for opt, arg in opts:
1583 elif opt == "--owner":
1585 elif opt == "--moderator":
1586 moderators.append(arg)
1587 elif opt == "--subscription":
1589 elif opt == "--posting":
1591 elif opt == "--archived":
1593 elif opt == "--ignore-bounce":
1595 elif opt == "--skip-prefix":
1597 elif opt == "--domain":
1599 elif opt == "--sendmail":
1601 elif opt == "--smtp-server":
1603 elif opt == "--qmqp-server":
1605 elif opt == "--sender":
1607 elif opt == "--recipient":
1609 elif opt == "--language":
1611 elif opt == "--mail-on-forced-unsubscribe":
1612 mail_on_forced_unsub = arg
1613 elif opt == "--mail-on-subscription-changes":
1614 mail_on_sub_changes = arg
1615 elif opt == "--moderate":
1617 elif opt == "--post":
1619 elif opt == "--quiet":
1621 elif opt == "--no-act":
1626 if operation is None:
1627 error("No operation specified, see --help.")
1629 if list_name is None and operation not in ["--incoming", "--help", "-h",
1633 error("%s requires a list name specified with --name" % operation)
1635 if operation in ["--help", "-h"]:
1638 if sender or recipient:
1639 environ = os.environ.copy()
1641 environ["SENDER"] = sender
1643 environ["RECIPIENT"] = recipient
1644 set_environ(environ)
1646 mlm = MailingListManager(DOTDIR, sendmail=sendmail,
1647 smtp_server=smtp_server,
1648 qmqp_server=qmqp_server)
1650 mlm.send_mail = no_act_send_mail
1652 if operation == "--create":
1654 error("You must give at least one list owner with --owner.")
1655 list = mlm.create_list(list_name)
1656 set_list_options(list, owners, moderators, subscription, posting,
1657 archived, language, ignore_bounce,
1658 mail_on_sub_changes, mail_on_forced_unsub)
1660 debug("Created list %s." % list_name)
1661 elif operation == "--destroy":
1662 shutil.rmtree(os.path.join(DOTDIR, list_name))
1663 debug("Removed list %s." % list_name)
1664 elif operation == "--edit":
1665 list = mlm.open_list(list_name)
1666 set_list_options(list, owners, moderators, subscription, posting,
1667 archived, language, ignore_bounce,
1668 mail_on_sub_changes, mail_on_forced_unsub)
1670 elif operation == "--subscribe":
1671 list = mlm.open_list(list_name)
1672 list.subscribers.lock()
1673 for address in args:
1674 if address.find("@") == -1:
1675 error("Address '%s' does not contain an @." % address)
1676 list.subscribers.add(address)
1677 debug("Added subscriber <%s>." % address)
1678 list.subscribers.save()
1679 elif operation == "--unsubscribe":
1680 list = mlm.open_list(list_name)
1681 list.subscribers.lock()
1682 for address in args:
1683 list.subscribers.remove(address)
1684 debug("Removed subscriber <%s>." % address)
1685 list.subscribers.save()
1686 elif operation == "--list":
1687 list = mlm.open_list(list_name)
1688 for address in list.subscribers.get_all():
1690 elif operation == "--is-list":
1691 if mlm.is_list(list_name, skip_prefix, domain):
1692 debug("Indeed a mailing list: <%s>" % list_name)
1694 debug("Not a mailing list: <%s>" % list_name)
1696 elif operation == "--incoming":
1697 mlm.incoming_message(skip_prefix, domain, moderate, post)
1698 elif operation == "--cleaning-woman":
1699 mlm.cleaning_woman()
1700 elif operation == "--show-lists":
1701 listnames = mlm.get_lists()
1703 for listname in listnames:
1705 elif operation == "--get":
1706 list = mlm.open_list(list_name)
1708 print list.cp.get("list", name)
1709 elif operation == "--set":
1710 list = mlm.open_list(list_name)
1713 error("Error: --set arguments must be of form name=value")
1714 name, value = arg.split("=", 1)
1715 list.cp.set("list", name, value)
1717 elif operation == "--version":
1718 print "EoC, version %s" % VERSION
1719 print "Home page: http://liw.iki.fi/liw/eoc/"
1721 error("Internal error: unimplemented option <%s>." % operation)
1723 if __name__ == "__main__":
1726 except EocException, detail:
1727 error("Error: %s" % detail)