PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/Snapshot_1_0b5/mailman/Mailman/MailList.py

#
Python | 937 lines | 763 code | 106 blank | 68 comment | 92 complexity | 3ef0b0cb4e1192de96e9b70804f31a3e MD5 | raw file
Possible License(s): GPL-2.0
  1. # Copyright (C) 1998 by the Free Software Foundation, Inc.
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. """The class representing a Mailman mailing list.
  17. Mixes in many feature classes.
  18. """
  19. try:
  20. import mm_cfg
  21. except ImportError:
  22. raise RuntimeError, ('missing mm_cfg - has Config_dist been configured '
  23. 'for the site?')
  24. import sys, os, marshal, string, posixfile, time
  25. import re
  26. import Utils
  27. import Errors
  28. from ListAdmin import ListAdmin
  29. from Deliverer import Deliverer
  30. from MailCommandHandler import MailCommandHandler
  31. from HTMLFormatter import HTMLFormatter
  32. from Archiver import Archiver
  33. from Digester import Digester
  34. from SecurityManager import SecurityManager
  35. from Bouncer import Bouncer
  36. from GatewayManager import GatewayManager
  37. from Mailman.Logging.StampedLogger import StampedLogger
  38. # Note:
  39. # an _ in front of a member variable for the MailList class indicates
  40. # a variable that does not save when we marshal our state.
  41. # Use mixins here just to avoid having any one chunk be too large.
  42. class MailList(MailCommandHandler, HTMLFormatter, Deliverer, ListAdmin,
  43. Archiver, Digester, SecurityManager, Bouncer, GatewayManager):
  44. def __init__(self, name=None, lock=1):
  45. if name and name not in Utils.list_names():
  46. raise Errors.MMUnknownListError, 'list not found: %s' % name
  47. MailCommandHandler.__init__(self)
  48. self.InitTempVars(name, lock)
  49. if name:
  50. self._full_path = os.path.join(mm_cfg.LIST_DATA_DIR, name)
  51. self.Load()
  52. def __del__(self):
  53. for f in self._log_files.values():
  54. f.close()
  55. def GetAdminEmail(self):
  56. return '%s-admin@%s' % (self._internal_name, self.host_name)
  57. def GetRequestEmail(self):
  58. return '%s-request@%s' % (self._internal_name, self.host_name)
  59. def GetListEmail(self):
  60. return '%s@%s' % (self._internal_name, self.host_name)
  61. def GetRelativeScriptURL(self, script_name):
  62. prefix = '../'*Utils.GetNestingLevel()
  63. return '%s%s/%s' % (prefix,script_name, self._internal_name)
  64. def GetAbsoluteScriptURL(self, script_name):
  65. if self.web_page_url:
  66. prefix = self.web_page_url
  67. else:
  68. prefix = mm_cfg.DEFAULT_URL
  69. return os.path.join(prefix, '%s/%s' % (script_name,
  70. self._internal_name))
  71. def GetAbsoluteOptionsURL(self, addr, obscured=0,):
  72. options = self.GetAbsoluteScriptURL('options')
  73. if obscured:
  74. treated = Utils.ObscureEmail(addr, for_text=0)
  75. else:
  76. treated = addr
  77. return os.path.join(options, treated)
  78. def GetUserOption(self, user, option):
  79. if option == mm_cfg.Digests:
  80. return user in self.digest_members
  81. if not self.user_options.has_key(user):
  82. return 0
  83. return not not self.user_options[user] & option
  84. def SetUserOption(self, user, option, value):
  85. if not self.user_options.has_key(user):
  86. self.user_options[user] = 0
  87. if value:
  88. self.user_options[user] = self.user_options[user] | option
  89. else:
  90. self.user_options[user] = self.user_options[user] & ~(option)
  91. if not self.user_options[user]:
  92. del self.user_options[user]
  93. self.Save()
  94. def FindUser(self, email):
  95. matches = Utils.FindMatchingAddresses(email,
  96. (self.members
  97. + self.digest_members))
  98. if not matches or not len(matches):
  99. return None
  100. return matches[0]
  101. def InitTempVars(self, name, lock):
  102. """Set transient variables of this and inherited classes."""
  103. self._tmp_lock = lock
  104. self._lock_file = None
  105. self._internal_name = name
  106. self._ready = 0
  107. self._log_files = {} # 'class': log_file_obj
  108. if name:
  109. self._full_path = os.path.join(mm_cfg.LIST_DATA_DIR, name)
  110. Digester.InitTempVars(self)
  111. def InitVars(self, name=None, admin='', crypted_password=''):
  112. """Assign default values - some will be overriden by stored state."""
  113. # Non-configurable list info
  114. if name:
  115. self._internal_name = name
  116. # Must save this state, even though it isn't configurable
  117. self.volume = 1
  118. self.members = [] # self.digest_members is initted in mm_digest
  119. self.data_version = mm_cfg.VERSION
  120. self.last_post_time = 0
  121. self.post_id = 1. # A float so it never has a chance to overflow.
  122. self.user_options = {}
  123. # This stuff is configurable
  124. self.filter_prog = mm_cfg.DEFAULT_FILTER_PROG
  125. self.dont_respond_to_post_requests = 0
  126. self.num_spawns = mm_cfg.DEFAULT_NUM_SPAWNS
  127. self.advertised = mm_cfg.DEFAULT_LIST_ADVERTISED
  128. self.max_num_recipients = mm_cfg.DEFAULT_MAX_NUM_RECIPIENTS
  129. self.max_message_size = mm_cfg.DEFAULT_MAX_MESSAGE_SIZE
  130. self.web_page_url = mm_cfg.DEFAULT_URL
  131. self.owner = [admin]
  132. self.reply_goes_to_list = mm_cfg.DEFAULT_REPLY_GOES_TO_LIST
  133. self.posters = []
  134. self.forbidden_posters = []
  135. self.admin_immed_notify = mm_cfg.DEFAULT_ADMIN_IMMED_NOTIFY
  136. self.moderated = mm_cfg.DEFAULT_MODERATED
  137. self.require_explicit_destination = \
  138. mm_cfg.DEFAULT_REQUIRE_EXPLICIT_DESTINATION
  139. self.acceptable_aliases = mm_cfg.DEFAULT_ACCEPTABLE_ALIASES
  140. self.reminders_to_admins = mm_cfg.DEFAULT_REMINDERS_TO_ADMINS
  141. self.send_reminders = mm_cfg.DEFAULT_SEND_REMINDERS
  142. self.send_welcome_msg = mm_cfg.DEFAULT_SEND_WELCOME_MSG
  143. self.bounce_matching_headers = \
  144. mm_cfg.DEFAULT_BOUNCE_MATCHING_HEADERS
  145. self.anonymous_list = mm_cfg.DEFAULT_ANONYMOUS_LIST
  146. self.real_name = '%s%s' % (string.upper(self._internal_name[0]),
  147. self._internal_name[1:])
  148. self.description = ''
  149. self.info = ''
  150. self.welcome_msg = ''
  151. self.goodbye_msg = ''
  152. self.open_subscribe = mm_cfg.DEFAULT_OPEN_SUBSCRIBE
  153. self.private_roster = mm_cfg.DEFAULT_PRIVATE_ROSTER
  154. self.obscure_addresses = mm_cfg.DEFAULT_OBSCURE_ADDRESSES
  155. self.member_posting_only = mm_cfg.DEFAULT_MEMBER_POSTING_ONLY
  156. self.web_subscribe_requires_confirmation = \
  157. mm_cfg.DEFAULT_WEB_SUBSCRIBE_REQUIRES_CONFIRMATION
  158. self.host_name = mm_cfg.DEFAULT_HOST_NAME
  159. # Analogs to these are initted in Digester.InitVars
  160. self.nondigestable = mm_cfg.DEFAULT_NONDIGESTABLE
  161. Digester.InitVars(self) # has configurable stuff
  162. SecurityManager.InitVars(self, crypted_password)
  163. Archiver.InitVars(self) # has configurable stuff
  164. ListAdmin.InitVars(self)
  165. Bouncer.InitVars(self)
  166. GatewayManager.InitVars(self)
  167. HTMLFormatter.InitVars(self)
  168. # These need to come near the bottom because they're dependent on
  169. # other settings.
  170. self.subject_prefix = mm_cfg.DEFAULT_SUBJECT_PREFIX % self.__dict__
  171. self.msg_header = mm_cfg.DEFAULT_MSG_HEADER
  172. self.msg_footer = mm_cfg.DEFAULT_MSG_FOOTER
  173. def GetConfigInfo(self):
  174. config_info = {}
  175. config_info['digest'] = Digester.GetConfigInfo(self)
  176. config_info['archive'] = Archiver.GetConfigInfo(self)
  177. config_info['gateway'] = GatewayManager.GetConfigInfo(self)
  178. # XXX: Should this text be migrated into the templates dir?
  179. config_info['general'] = [
  180. "Fundamental list characteristics, including descriptive"
  181. " info and basic behaviors.",
  182. ('real_name', mm_cfg.String, 50, 0,
  183. 'The public name of this list (make case-changes only).',
  184. "The capitalization of this name can be changed to make it"
  185. " presentable in polite company as a proper noun, or to make an"
  186. " acronym part all upper case, etc. However, the name"
  187. " will be advertised as the email address (e.g., in subscribe"
  188. " confirmation notices), so it should <em>not</em> be otherwise"
  189. " altered. (Email addresses are not case sensitive, but"
  190. " they are sensitive to almost everything else:-)"),
  191. ('owner', mm_cfg.EmailList, (3,30), 0,
  192. "The list admin's email address - having multiple"
  193. " admins/addresses (on separate lines) is ok."),
  194. ('description', mm_cfg.String, 50, 0,
  195. 'A terse phrase identifying this list.',
  196. "This description is used when the maillist is listed with"
  197. " other maillists, or in headers, and so forth. It should"
  198. " be as succinct as you can get it, while still identifying"
  199. " what the list is."),
  200. ('info', mm_cfg.Text, (7, 50), 0,
  201. ' An introductory description - a few paragraphs - about the'
  202. ' list. It will be included, as html, at the top of the'
  203. ' listinfo page. Carriage returns will end a paragraph - see'
  204. ' the details for more info.',
  205. "The text will be treated as html <em>except</em> that newlines"
  206. " newlines will be translated to &lt;br&gt; - so you can use"
  207. " links, preformatted text, etc, but don't put in carriage"
  208. " returns except where you mean to separate paragraphs. And"
  209. " review your changes - bad html (like some unterminated HTML"
  210. " constructs) can prevent display of the entire listinfo page."),
  211. ('subject_prefix', mm_cfg.String, 10, 0,
  212. 'Prefix for subject line of list postings.',
  213. "This text will be prepended to subject lines of messages"
  214. " posted to the list, to distinguish maillist messages in"
  215. " in mailbox summaries. Brevity is premium here, it's ok"
  216. " to shorten long maillist names to something more concise,"
  217. " as long as it still identifies the maillist."),
  218. ('welcome_msg', mm_cfg.Text, (4, 50), 0,
  219. 'List-specific text prepended to new-subscriber welcome message',
  220. "This value, if any, will be added to the front of the"
  221. " new-subscriber welcome message. The rest of the"
  222. " welcome message already describes the important addresses"
  223. " and URLs for the maillist, so you don't need to include"
  224. " any of that kind of stuff here. This should just contain"
  225. " mission-specific kinds of things, like etiquette policies"
  226. " or team orientation, or that kind of thing."),
  227. ('goodbye_msg', mm_cfg.Text, (4, 50), 0,
  228. 'Text sent to people leaving the list. If empty, no special'
  229. ' text will be added to the unsubscribe message.'),
  230. ('reply_goes_to_list', mm_cfg.Radio, ('Poster', 'List'), 0,
  231. 'Are replies to a post directed to the original poster'
  232. ' or to the list? <tt>Poster</tt> is <em>strongly</em>'
  233. ' recommended.',
  234. "There are many reasons not to introduce headers like reply-to"
  235. " into other peoples messages - one is that some posters depend"
  236. " on their own reply-to setting to convey their valid email"
  237. " addr. See"
  238. ' <a href="http://www.unicom.com/pw/reply-to-harmful.html">'
  239. '"Reply-To" Munging Considered Harmful</a> for a general.'
  240. " discussion of this issue."),
  241. ('reminders_to_admins', mm_cfg.Radio, ('No', 'Yes'), 0,
  242. 'Send password reminders to "-admin" address instead of'
  243. ' directly to user.',
  244. "Set this to yes when this list is intended only to cascade to"
  245. " other maillists. When set, the password reminders will be"
  246. " directed to an address derived from the member's address"
  247. ' - it will have "-admin" appended to the member\'s account'
  248. " name."),
  249. ('send_reminders', mm_cfg.Radio, ('No', 'Yes'), 0,
  250. 'Send monthly password reminders or no? Overrides the previous '
  251. 'option.'),
  252. ('send_welcome_msg', mm_cfg.Radio, ('No', 'Yes'), 0,
  253. 'Send welcome message when people subscribe?',
  254. "Turn this on only if you plan on subscribing people manually "
  255. "and don't want them to know that you did so. This option "
  256. "is most useful for transparently migrating lists from "
  257. "some other mailing list manager to Mailman."),
  258. ('admin_immed_notify', mm_cfg.Radio, ('No', 'Yes'), 0,
  259. 'Should administrator get immediate notice of new requests, '
  260. 'as well as daily notices about collected ones?',
  261. "List admins are sent daily reminders of pending admin approval"
  262. " requests, like subscriptions to a moderated list or postings"
  263. " that are being held for one reason or another. Setting this"
  264. " option causes notices to be sent immediately on the arrival"
  265. " of new requests, as well."),
  266. ('dont_respond_to_post_requests', mm_cfg.Radio, ('Yes', 'No'), 0,
  267. 'Send mail to poster when their posting is held for approval?',
  268. "Approval notices are sent when mail triggers certain of the"
  269. " limits <em>except</em> routine list moderation and spam"
  270. " filters, for which notices are <em>not</em> sent. This"
  271. " option overrides ever sending the notice."),
  272. # XXX UNSAFE! Perhaps more selective capability could be
  273. # offered, with some kind of super-admin option, but for now
  274. # let's not even expose this. (Apparently was never
  275. # implemented, anyway.)
  276. ## ('filter_prog', mm_cfg.String, 40, 0,
  277. ## 'Program for pre-processing text, if any? '
  278. ## '(Useful, eg, for signature auto-stripping, etc...)'),
  279. ('max_message_size', mm_cfg.Number, 3, 0,
  280. 'Maximum length in Kb of a message body. Use 0 for no limit.'),
  281. ('num_spawns', mm_cfg.Number, 3, 0,
  282. 'Number of outgoing connections to open at once '
  283. '(expert users only).',
  284. "This determines the maximum number of batches into which"
  285. " a mass posting will be divided."),
  286. ('host_name', mm_cfg.Host, 50, 0, 'Host name this list prefers.',
  287. "The host_name is the preferred name for email to mailman-related"
  288. " addresses on this host, and generally should be the mail"
  289. " host's exchanger address, if any. This setting can be useful"
  290. " for selecting among alternative names of a host that has"
  291. " multiple addresses."),
  292. ('web_page_url', mm_cfg.String, 50, 0,
  293. 'Base URL for Mailman web interface',
  294. "This is the common root for all mailman URLs concerning this"
  295. " list. It can be useful for selecting a particular URL"
  296. " of a host that has multiple addresses."),
  297. ]
  298. config_info['privacy'] = [
  299. "List access policies, including anti-spam measures,"
  300. " covering members and outsiders."
  301. ' (See also the <a href="%s">Archival Options section</a> for'
  302. ' separate archive-privacy settings.)'
  303. % os.path.join(self.GetRelativeScriptURL('admin'), 'archive'),
  304. "Subscribing",
  305. ('advertised', mm_cfg.Radio, ('No', 'Yes'), 0,
  306. 'Advertise this list when people ask what lists are on '
  307. 'this machine?'),
  308. ('open_subscribe', mm_cfg.Radio, ('No', 'Yes'), 0,
  309. 'Are subscribes done without admins approval (ie, is this'
  310. ' an <em>open</em> list)?',
  311. "Disabling this option makes the list <em>closed</em>, where"
  312. " members are admitted only at the discretion of the list"
  313. " administrator."),
  314. ('web_subscribe_requires_confirmation', mm_cfg.Radio,
  315. ('None', 'Requestor confirms via email', 'Admin approves'), 0,
  316. 'What confirmation is required for on-the-web subscribes?',
  317. "This option determines whether web-initiated subscribes"
  318. " require further confirmation, either from the subscribed"
  319. " address or from the list administrator. Absence of"
  320. " <em>any</em> confirmation makes web-based subscription a"
  321. " tempting opportunity for mischievous subscriptions by third"
  322. " parties."),
  323. "Membership exposure",
  324. ('private_roster', mm_cfg.Radio,
  325. ('Anyone', 'List members', 'List admin only'), 0,
  326. 'Who can view subscription list?',
  327. "When set, the list of subscribers is protected by"
  328. " member or admin password authentication."),
  329. ('obscure_addresses', mm_cfg.Radio, ('No', 'Yes'), 0,
  330. "Show member addrs so they're not directly recognizable"
  331. ' as email addrs?',
  332. "Setting this option causes member email addresses to be"
  333. " transformed when they are presented on list web pages (both"
  334. " in text and as links), so they're not trivially"
  335. " recognizable as email addresses. The intention is to"
  336. " to prevent the addresses from being snarfed up by"
  337. " automated web scanners for use by spammers."),
  338. "General posting filters",
  339. ('moderated', mm_cfg.Radio, ('No', 'Yes'), 0,
  340. 'Must posts be approved by a moderator?',
  341. "If the 'posters' option has any entries then it supercedes"
  342. " this setting."),
  343. ('member_posting_only', mm_cfg.Radio, ('No', 'Yes'), 0,
  344. 'Restrict posting privilege to only list members?'),
  345. ('posters', mm_cfg.EmailList, (5, 30), 1,
  346. 'Addresses blessed for posting to this list. (Adding'
  347. ' anyone to this list implies moderation of everyone else.)',
  348. "Adding any entries to this list supercedes the setting of"
  349. " the list-moderation option."),
  350. "Spam-specific posting filters",
  351. ('require_explicit_destination', mm_cfg.Radio, ('No', 'Yes'), 0,
  352. 'Must posts have list named in destination (to, cc) field'
  353. ' (or be among the acceptable alias names, specified below)?',
  354. "Many (in fact, most) spams do not explicitly name their myriad"
  355. " destinations in the explicit destination addresses - in fact,"
  356. " often the to field has a totally bogus address for"
  357. " obfuscation. The constraint applies only to the stuff in"
  358. " the address before the '@' sign, but still catches all such"
  359. " spams."
  360. "<p>The cost is that the list will not accept unhindered any"
  361. " postings relayed from other addresses, unless <ol>"
  362. " <li>The relaying address has the same name, or"
  363. " <li>The relaying address name is included on the options that"
  364. " specifies acceptable aliases for the list. </ol>"),
  365. ('acceptable_aliases', mm_cfg.Text, ('4', '30'), 0,
  366. 'Alias names (regexps) which qualify as explicit to or cc'
  367. ' destination names for this list.',
  368. "Alternate list names (the stuff before the '@') that are to be"
  369. " accepted when the explicit-destination constraint (a prior"
  370. " option) is active. This enables things like cascading"
  371. " maillists and relays while the constraint is still"
  372. " preventing random spams."),
  373. ('max_num_recipients', mm_cfg.Number, 3, 0,
  374. 'Ceiling on acceptable number of recipients for a posting.',
  375. "If a posting has this number, or more, of recipients, it is"
  376. " held for admin approval. Use 0 for no ceiling."),
  377. ('forbidden_posters', mm_cfg.EmailList, (5, 30), 1,
  378. 'Addresses whose postings are always held for approval.',
  379. "Email addresses whose posts should always be held for"
  380. " approval, no matter what other options you have set."
  381. " See also the subsequent option which applies to arbitrary"
  382. " content of arbitrary headers."),
  383. ('bounce_matching_headers', mm_cfg.Text, ('6', '50'), 0,
  384. 'Hold posts with header value matching a specified regexp.',
  385. "Use this option to prohibit posts according to specific header"
  386. " values. The target value is a regular-expression for"
  387. " matching against the specified header. The match is done"
  388. " disregarding letter case. Lines beginning with '#' are"
  389. " ignored as comments."
  390. "<p>For example:<pre>to: .*@public.com </pre> says"
  391. " to hold all postings with a <em>to</em> mail header"
  392. " containing '@public.com' anywhere among the addresses."
  393. "<p>Note that leading whitespace is trimmed from the"
  394. " regexp. This can be circumvented in a number of ways, eg"
  395. " by escaping or bracketing it."
  396. "<p> See also the <em>forbidden_posters</em> option for"
  397. " a related mechanism."),
  398. ('anonymous_list', mm_cfg.Radio, ('No', 'Yes'), 0,
  399. 'Hide the sender of a message, replacing it with the list '
  400. 'address (Removes From, Sender and Reply-To fields)'),
  401. ]
  402. config_info['nondigest'] = [
  403. "Policies concerning immediately delivered list traffic.",
  404. ('nondigestable', mm_cfg.Toggle, ('No', 'Yes'), 1,
  405. 'Can subscribers choose to receive mail immediately,'
  406. ' rather than in batched digests?'),
  407. ('msg_header', mm_cfg.Text, (4, 55), 0,
  408. 'Header added to mail sent to regular list members',
  409. "Text prepended to the top of every immediately-delivery"
  410. " message. <p>" + Errors.MESSAGE_DECORATION_NOTE),
  411. ('msg_footer', mm_cfg.Text, (4, 55), 0,
  412. 'Footer added to mail sent to regular list members',
  413. "Text appended to the bottom of every immediately-delivery"
  414. " message. <p>" + Errors.MESSAGE_DECORATION_NOTE),
  415. ]
  416. config_info['bounce'] = Bouncer.GetConfigInfo(self)
  417. return config_info
  418. def Create(self, name, admin, crypted_password):
  419. if name in Utils.list_names():
  420. raise ValueError, 'List %s already exists.' % name
  421. else:
  422. Utils.MakeDirTree(os.path.join(mm_cfg.LIST_DATA_DIR, name))
  423. self._full_path = os.path.join(mm_cfg.LIST_DATA_DIR, name)
  424. self._internal_name = name
  425. self.Lock()
  426. self.InitVars(name, admin, crypted_password)
  427. self._ready = 1
  428. self.InitTemplates()
  429. self.Save()
  430. self.CreateFiles()
  431. def CreateFiles(self):
  432. # Touch these files so they have the right dir perms no matter what.
  433. # A "just-in-case" thing. This shouldn't have to be here.
  434. ou = os.umask(002)
  435. try:
  436. ## import mm_archive
  437. ## open(os.path.join(self._full_path,
  438. ## mm_archive.ARCHIVE_PENDING), "a+").close()
  439. ## open(os.path.join(self._full_path,
  440. ## mm_archive.ARCHIVE_RETAIN), "a+").close()
  441. open(os.path.join(mm_cfg.LOCK_DIR, '%s.lock' %
  442. self._internal_name), 'a+').close()
  443. open(os.path.join(self._full_path, "next-digest"), "a+").close()
  444. open(os.path.join(self._full_path, "next-digest-topics"),
  445. "a+").close()
  446. finally:
  447. os.umask(ou)
  448. def Save(self):
  449. # If more than one client is manipulating the database at once, we're
  450. # pretty hosed. That's a good reason to make this a daemon not a
  451. # program.
  452. self.IsListInitialized()
  453. ou = os.umask(002)
  454. try:
  455. fname = os.path.join(self._full_path, 'config.db')
  456. fname_last = fname + ".last"
  457. if os.path.exists(fname_last):
  458. os.unlink(fname_last)
  459. if os.path.exists(fname):
  460. os.link(fname, fname_last)
  461. os.unlink(fname)
  462. file = open(fname, 'w')
  463. finally:
  464. os.umask(ou)
  465. dict = {}
  466. for (key, value) in self.__dict__.items():
  467. if key[0] <> '_':
  468. dict[key] = value
  469. marshal.dump(dict, file)
  470. file.close()
  471. def Load(self, check_version = 1):
  472. if self._tmp_lock:
  473. self.Lock()
  474. try:
  475. file = open(os.path.join(self._full_path, 'config.db'), 'r')
  476. except IOError:
  477. raise mm_cfg.MMBadListError, 'Failed to access config info'
  478. try:
  479. dict = marshal.load(file)
  480. except (EOFError, ValueError, TypeError):
  481. raise mm_cfg.MMBadListError, 'Failed to unmarshal config info'
  482. for (key, value) in dict.items():
  483. setattr(self, key, value)
  484. file.close()
  485. self._ready = 1
  486. if check_version:
  487. self.CheckValues()
  488. self.CheckVersion(dict)
  489. def LogMsg(self, kind, msg, *args):
  490. """Append a message to the log file for messages of specified kind."""
  491. # For want of a better fallback, we use sys.stderr if we can't get
  492. # a log file. We need a better way to warn of failed log access...
  493. if self._log_files.has_key(kind):
  494. logf = self._log_files[kind]
  495. else:
  496. logf = self._log_files[kind] = StampedLogger(kind)
  497. logf.write("%s\n" % (msg % args))
  498. logf.flush()
  499. def CheckVersion(self, stored_state):
  500. """Migrate prior version's state to new structure, if changed."""
  501. if (self.data_version >= mm_cfg.DATA_FILE_VERSION and
  502. type(self.data_version) == type(mm_cfg.DATA_FILE_VERSION)):
  503. return
  504. else:
  505. self.InitVars() # Init any new variables,
  506. self.Load(check_version = 0) # then reload the file
  507. from versions import Update
  508. Update(self, stored_state)
  509. self.data_version = mm_cfg.DATA_FILE_VERSION
  510. self.Save()
  511. def CheckValues(self):
  512. """Normalize selected values to known formats."""
  513. if self.web_page_url and self.web_page_url[-1] != '/':
  514. self.web_page_url = self.web_page_url + '/'
  515. def IsListInitialized(self):
  516. if not self._ready:
  517. raise Errors.MMListNotReady
  518. def AddMember(self, name, password, digest=0, web_subscribe=0):
  519. self.IsListInitialized()
  520. # Remove spaces... it's a common thing for people to add...
  521. name = string.join(string.split(string.lower(name)), '')
  522. # Validate the e-mail address to some degree.
  523. if not Utils.ValidEmail(name):
  524. raise Errors.MMBadEmailError
  525. if self.IsMember(name):
  526. raise Errors.MMAlreadyAMember
  527. if digest and not self.digestable:
  528. raise Errors.MMCantDigestError
  529. elif not digest and not self.nondigestable:
  530. raise Errors.MMMustDigestError
  531. if self.open_subscribe:
  532. if (web_subscribe and self.web_subscribe_requires_confirmation):
  533. if self.web_subscribe_requires_confirmation == 1:
  534. # Requester confirmation required.
  535. raise Errors.MMWebSubscribeRequiresConfirmation
  536. else:
  537. # Admin approval required.
  538. self.AddRequest('add_member', digest, name, password)
  539. else:
  540. # No approval required.
  541. self.ApprovedAddMember(name, password, digest)
  542. else:
  543. # Blanket admin approval requred...
  544. self.AddRequest('add_member', digest, name, password)
  545. def ApprovedAddMember(self, name, password, digest, noack=0):
  546. # XXX klm: It *might* be nice to leave the case of the name alone,
  547. # but provide a common interface that always returns the
  548. # lower case version for computations.
  549. name = string.lower(name)
  550. if self.IsMember(name):
  551. raise Errors.MMAlreadyAMember
  552. if digest:
  553. self.digest_members.append(name)
  554. kind = " (D)"
  555. else:
  556. self.members.append(name)
  557. kind = ""
  558. self.LogMsg("subscribe", "%s: new%s %s",
  559. self._internal_name, kind, name)
  560. self.passwords[name] = password
  561. self.Save()
  562. if not noack:
  563. self.SendSubscribeAck(name, password, digest)
  564. def DeleteMember(self, name, whence=None):
  565. self.IsListInitialized()
  566. # FindMatchingAddresses *should* never return more than 1 address.
  567. # However, should log this, just to make sure.
  568. aliases = Utils.FindMatchingAddresses(name, self.members +
  569. self.digest_members)
  570. if not len(aliases):
  571. raise Errors.MMNoSuchUserError
  572. def DoActualRemoval(alias, me=self):
  573. kind = "(unfound)"
  574. try:
  575. del me.passwords[alias]
  576. except KeyError:
  577. pass
  578. if me.user_options.has_key(alias):
  579. del me.user_options[alias]
  580. try:
  581. me.members.remove(alias)
  582. kind = "regular"
  583. except ValueError:
  584. pass
  585. try:
  586. me.digest_members.remove(alias)
  587. kind = "digest"
  588. except ValueError:
  589. pass
  590. map(DoActualRemoval, aliases)
  591. if self.goodbye_msg and len(self.goodbye_msg):
  592. self.SendUnsubscribeAck(name)
  593. self.ClearBounceInfo(name)
  594. self.Save()
  595. if whence: whence = "; %s" % whence
  596. else: whence = ""
  597. self.LogMsg("subscribe", "%s: deleted %s%s",
  598. self._internal_name, name, whence)
  599. def IsMember(self, address):
  600. return len(Utils.FindMatchingAddresses(address, self.members +
  601. self.digest_members))
  602. def HasExplicitDest(self, msg):
  603. """True if list name or any acceptable_alias is included among the
  604. to or cc addrs."""
  605. # Note that qualified host can be different! This allows, eg, for
  606. # relaying from remote lists that have the same name. Still
  607. # stringent, but offers a way to provide for remote exploders.
  608. lowname = string.lower(self.real_name)
  609. recips = []
  610. # First check all dests against simple name:
  611. for recip in msg.getaddrlist('to') + msg.getaddrlist('cc'):
  612. curr = string.lower(string.split(recip[1], '@')[0])
  613. if lowname == curr:
  614. return 1
  615. recips.append(curr)
  616. # ... and only then try the regexp acceptable aliases.
  617. for recip in recips:
  618. for alias in string.split(self.acceptable_aliases, '\n'):
  619. stripped = string.strip(alias)
  620. if stripped and re.match(stripped, recip):
  621. return 1
  622. return 0
  623. def parse_matching_header_opt(self):
  624. """Return a list of triples [(field name, regex, line), ...]."""
  625. # - Blank lines and lines with '#' as first char are skipped.
  626. # - Leading whitespace in the matchexp is trimmed - you can defeat
  627. # that by, eg, containing it in gratuitous square brackets.
  628. all = []
  629. for line in string.split(self.bounce_matching_headers, '\n'):
  630. stripped = string.strip(line)
  631. if not stripped or (stripped[0] == "#"):
  632. # Skip blank lines and lines *starting* with a '#'.
  633. continue
  634. else:
  635. try:
  636. h, e = re.split(":[ ]*", stripped)
  637. all.append((h, e, stripped))
  638. except ValueError:
  639. # Whoops - some bad data got by:
  640. self.LogMsg("config", "%s - "
  641. "bad bounce_matching_header line %s"
  642. % (self.real_name, `stripped`))
  643. return all
  644. def HasMatchingHeader(self, msg):
  645. """True if named header field (case-insensitive) matches regexp.
  646. Case insensitive.
  647. Returns constraint line which matches or empty string for no
  648. matches."""
  649. pairs = self.parse_matching_header_opt()
  650. for field, matchexp, line in pairs:
  651. fragments = msg.getallmatchingheaders(field)
  652. subjs = []
  653. l = len(field)
  654. for f in fragments:
  655. # Consolidate header lines, stripping header name & whitespace.
  656. if (len(f) > l
  657. and f[l] == ":"
  658. and string.lower(field) == string.lower(f[0:l])):
  659. # Non-continuation line - trim header name:
  660. subjs.append(f[l+2:])
  661. elif not subjs:
  662. # Whoops - non-continuation that matches?
  663. subjs.append(f)
  664. else:
  665. # Continuation line.
  666. subjs[-1] = subjs[-1] + f
  667. for s in subjs:
  668. if re.search(matchexp, s, re.I):
  669. return line
  670. return 0
  671. #msg should be an IncomingMessage object.
  672. def Post(self, msg, approved=0):
  673. self.IsListInitialized()
  674. # Be sure to ExtractApproval, whether or not flag is already set!
  675. msgapproved = self.ExtractApproval(msg)
  676. if not approved:
  677. approved = msgapproved
  678. sender = msg.GetSender()
  679. # If it's the admin, which we know by the approved variable,
  680. # we can skip a large number of checks.
  681. if not approved:
  682. from_lists = msg.getallmatchingheaders('x-beenthere')
  683. if self.GetListEmail() in from_lists:
  684. self.AddRequest('post', Utils.SnarfMessage(msg),
  685. Errors.LOOPING_POST,
  686. msg.getheader('subject'))
  687. if len(self.forbidden_posters):
  688. addrs = Utils.FindMatchingAddresses(sender,
  689. self.forbidden_posters)
  690. if len(addrs):
  691. self.AddRequest('post', Utils.SnarfMessage(msg),
  692. Errors.FORBIDDEN_SENDER_MSG,
  693. msg.getheader('subject'))
  694. if len(self.posters):
  695. addrs = Utils.FindMatchingAddresses(sender, self.posters)
  696. if not len(addrs):
  697. self.AddRequest('post', Utils.SnarfMessage(msg),
  698. 'Only approved posters may post without '
  699. 'moderator approval.',
  700. msg.getheader('subject'))
  701. elif self.moderated:
  702. self.AddRequest('post', Utils.SnarfMessage(msg),
  703. Errors.MODERATED_LIST_MSG,
  704. msg.getheader('subject'))
  705. if self.member_posting_only and not self.IsMember(sender):
  706. self.AddRequest('post', Utils.SnarfMessage(msg),
  707. 'Postings from member addresses only.',
  708. msg.getheader('subject'))
  709. if self.max_num_recipients > 0:
  710. recips = []
  711. toheader = msg.getheader('to')
  712. if toheader:
  713. recips = recips + string.split(toheader, ',')
  714. ccheader = msg.getheader('cc')
  715. if ccheader:
  716. recips = recips + string.split(ccheader, ',')
  717. if len(recips) > self.max_num_recipients:
  718. self.AddRequest('post', Utils.SnarfMessage(msg),
  719. 'Too many recipients.',
  720. msg.getheader('subject'))
  721. if (self.require_explicit_destination and
  722. not self.HasExplicitDest(msg)):
  723. self.AddRequest('post', Utils.SnarfMessage(msg),
  724. Errors.IMPLICIT_DEST_MSG,
  725. msg.getheader('subject'))
  726. if self.bounce_matching_headers:
  727. triggered = self.HasMatchingHeader(msg)
  728. if triggered:
  729. # Darn - can't include the matching line for the admin
  730. # message because the info would also go to the sender.
  731. self.AddRequest('post', Utils.SnarfMessage(msg),
  732. Errors.SUSPICIOUS_HEADER_MSG,
  733. msg.getheader('subject'))
  734. if self.max_message_size > 0:
  735. if len(msg.body)/1024. > self.max_message_size:
  736. self.AddRequest('post', Utils.SnarfMessage(msg),
  737. 'Message body too long (>%dk)' %
  738. self.max_message_size,
  739. msg.getheader('subject'))
  740. # Prepend the subject_prefix to the subject line.
  741. subj = msg.getheader('subject')
  742. prefix = self.subject_prefix
  743. if not subj:
  744. msg.SetHeader('Subject', '%s(no subject)' % prefix)
  745. elif not re.match("(re:? *)?" + re.escape(self.subject_prefix),
  746. subj, re.I):
  747. msg.SetHeader('Subject', '%s%s' % (prefix, subj))
  748. if self.anonymous_list:
  749. del msg['reply-to']
  750. del msg['sender']
  751. msg.SetHeader('From', self.GetAdminEmail())
  752. if self.digestable:
  753. self.SaveForDigest(msg)
  754. if self.archive:
  755. self.ArchiveMail(msg)
  756. if self.gateway_to_news:
  757. self.SendMailToNewsGroup(msg)
  758. dont_send_to_sender = 0
  759. ack_post = 0
  760. # Try to get the address the list thinks this sender is
  761. sender = self.FindUser(msg.GetSender())
  762. if sender:
  763. if self.GetUserOption(sender, mm_cfg.DontReceiveOwnPosts):
  764. dont_send_to_sender = 1
  765. if self.GetUserOption(sender, mm_cfg.AcknowlegePosts):
  766. ack_post = 1
  767. # Deliver the mail.
  768. recipients = self.members[:]
  769. if dont_send_to_sender:
  770. recipients.remove(sender)
  771. def DeliveryEnabled(x, s=self, v=mm_cfg.DisableDelivery):
  772. return not s.GetUserOption(x, v)
  773. recipients = filter(DeliveryEnabled, recipients)
  774. self.DeliverToList(msg, recipients,
  775. header = self.msg_header % self.__dict__,
  776. footer = self.msg_footer % self.__dict__)
  777. if ack_post:
  778. self.SendPostAck(msg, sender)
  779. self.last_post_time = time.time()
  780. self.post_id = self.post_id + 1
  781. self.Save()
  782. def Locked(self):
  783. try:
  784. return self._lock_file and 1
  785. except AttributeError:
  786. return 0
  787. def Lock(self):
  788. try:
  789. if self._lock_file:
  790. return
  791. except AttributeError:
  792. return
  793. ou = os.umask(0)
  794. try:
  795. self._lock_file = posixfile.open(
  796. os.path.join(mm_cfg.LOCK_DIR, '%s.lock' % self._internal_name),
  797. 'a+')
  798. finally:
  799. os.umask(ou)
  800. self._lock_file.lock('w|', 1)
  801. def Unlock(self):
  802. if self.Locked():
  803. self._lock_file.lock('u')
  804. self._lock_file.close()
  805. self._lock_file = None
  806. def __repr__(self):
  807. if self.Locked(): status = " (locked)"
  808. else: status = ""
  809. return ("<%s.%s %s%s at %s>"
  810. % (self.__module__, self.__class__.__name__,
  811. `self._internal_name`, status, hex(id(self))[2:]))