############################################################################## # # Zope Public License (ZPL) Version 0.9.7 # --------------------------------------- # # Copyright (c) Digital Creations. All rights reserved. # # This license has been certified as Open Source(tm). # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions in source code must retain the above copyright # notice, this list of conditions, and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions, and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # # 3. Digital Creations requests that attribution be given to Zope # in any manner possible. Zope includes a "Powered by Zope" # button that is installed by default. While it is not a license # violation to remove this button, it is requested that the # attribution remain. A significant investment has been put # into Zope, and this effort will continue if the Zope community # continues to grow. This is one way to assure that growth. # # 4. All advertising materials and documentation mentioning # features derived from or use of this software must display # the following acknowledgement: # # "This product includes software developed by Digital Creations # for use in the Z Object Publishing Environment # (http://www.zope.org/)." # # In the event that the product being advertised includes an # intact Zope distribution (with copyright and license included) # then this clause is waived. # # 5. Names associated with Zope or Digital Creations must not be used to # endorse or promote products derived from this software without # prior written permission from Digital Creations. # # 6. Modified redistributions of any form whatsoever must retain # the following acknowledgment: # # "This product includes software developed by Digital Creations # for use in the Z Object Publishing Environment # (http://www.zope.org/)." # # Intact (re-)distributions of any official Zope release do not # require an external acknowledgement. # # 7. Modifications are encouraged but must be packaged separately as # patches to official Zope releases. Distributions that do not # clearly separate the patches from the original work must be clearly # labeled as unofficial distributions. Modifications which do not # carry the name Zope may be packaged in any form, as long as they # conform to all of the clauses above. # # # Disclaimer # # THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # # This software consists of contributions made by Digital Creations and # many individuals on behalf of Digital Creations. Specific # attributions are listed in the accompanying credits file. # ############################################################################## """Confera - a web-based discussion system.""" __version__='$Revision: 1.2 $'[11:-2] #import sys, ts_regex, regsub import sys, regsub from Globals import Persistent from Globals import PersistentMapping from Globals import HTMLFile ,MessageDialog from OFS.ObjectManager import ObjectManager from AccessControl.Role import RoleManager from SearchIndex.TextIndex import TextIndex from SearchIndex.Index import Index from IOBTree import BTree from intSet import intSet from FileObject import FileObject from OFS.Folder import Folder from Acquisition import Implicit from time import time, localtime from string import strip,split,join from string import lower,rfind,atoi #crlf=ts_regex.compile('\r\n\|\n\r') #dpat=ts_regex.compile('[\\/]') class Topic(Folder, RoleManager): """ """ meta_type ='Confera Topic' description='Confera Topic' icon ='misc_/Confera/topic.gif' root =1 _properties=({'id':'title', 'type':'string'}, ) manage_messages=HTMLFile('Topic_manage_messages', globals()) manage_editForm=HTMLFile('Topic_editForm', globals()) manage_options=({'label':'Contents', 'icon':icon, 'action':'manage_main', 'target':'manage_main'}, {'label':'Messages', 'icon':'', 'action':'manage_messages', 'target':'manage_main'}, {'label':'Options', 'icon':'', 'action':'manage_editForm', 'target':'manage_main'}, {'label':'Properties', 'icon':'', 'action':'manage_propertiesForm', 'target':'manage_main'}, {'label':'Security', 'icon':'', 'action':'manage_access', 'target':'manage_main'}, {'label':'Undo', 'icon':'', 'action':'manage_UndoForm','target':'manage_main'}, ) __ac_permissions__=Folder.__ac_permissions__+( ('Manage messages', ['manage_edit', 'manage_delete']), ('Add messages', ['addMessage'], ('Anonymous', 'Manager')), ) def __init__(self, id, title, mhost, exp, mod, default): t=time() self.id =id self.title =title self.created =t self.modified =t self.mail_host=mhost self.expire =exp self.moderated=mod self.data =BTree() # id -> Message self.ids =intSet() # ids of children self.indices=PersistentMapping({'author': Index(), 'body' : TextIndex(), } ) for k in self.indices.keys(): self.indices[k]._init(self.data,None,k) if default: self.defaultDocFile('index_html','Topic Homepage', 'Confera/Topic_index_html') self.defaultDocFile('message_html','Message', 'Confera/Topic_message_html') self.defaultDocFile('searchForm','Search Form', 'Confera/Topic_searchForm') self.defaultDocFile('searchResults','Search Results', 'Confera/Topic_searchResults') self.defaultDocFile('mail_html','Notification Email', 'Confera/Topic_mail_html') self.defaultDocFile('addMessageForm','Add Message Form', 'Confera/addMessageForm') def __len__(self): return 1 def __getitem__(self,n): try: id=atoi(n) except: raise AttributeError, n if self.ids.has_key(id): return self.data[id].__of__(self) raise AttributeError, n def setItem(self,id,obj): # Make sure the object we store is not wrapped with # an acquisition wrapper, since we will be wrapping # it again manually in __getitem__ when it is needed. if hasattr(obj, 'aq_base'): obj=obj.aq_base self.ids.insert(id) self.data[id]=obj for k in self.indices.keys(): self.indices[k].index_item(id) def delItem(self,id): data=self.data item=data[id] if self.ids.has_key(id): self.ids.remove(id) else: data[item.thread[-1]].ids.remove(id) if self.moderated: if not item.reviewed: for t in item.thread: obj=self.data[t] if obj.revsub > 0: obj.revsub=obj.revsub-1 ids=intSet() ids.insert(id) ids=item.sub_ids(ids,data) item=None for index in self.indices.values(): for id in ids: try: index.unindex_item(id) except: pass for id in ids: del data[id] def createId(self): id=int(time()) while 1: try: o=self.data[id] except: break id=id+1 return id def defaultDocFile(self,id,title,file): f=open('%s/Products/%s.dtml' % (SOFTWARE_HOME,file)) file=f.read() f.close() self.manage_addDTMLMethod(id,title,file) def messageValues(self): """ """ return map(lambda x, p=self: x.__of__(p), self.data.map(self.ids)) def tpId(self): return self.id def tpURL(self): return self.id def this(self): return self def topic(self): return (self,) def topic_url(self): return self.REQUEST['URL1'] def has_items(self): return len(self.ids) def item_count(self): return len(self.data) def mailhost_list(self): try: return self.superValues(('Mail Host',)) except: return [] def expire_items(self): if self.expire: d=self.data t=int(time()-(self.expire * 86400.0)) ids=[] for id in d.keys(): if d[id].modified < t: ids.append(id) for id in ids: try: self.delItem(id) except: pass return '' def addMessage(self,title,author,body,email='',notify='',file='', submit='Cancel',REQUEST=None,RESPONSE=None): """ """ if submit=='Cancel': RESPONSE.redirect('%s/index_html' % self.topic_url()) title =strip(title) author=strip(author) email =strip(email) # workaround by JMF body=regsub.gsub('\n\r', '\n', body) body=regsub.gsub('\r\n', '\n', body) body = split(body,'\n') #body =split(regsub.gsub(crlf,'\n',body),'\n') notify=notify reviewed=(not self.moderated) and 1 or 0 try: file=FileObject(file) except: file='' if notify and not email: return MessageDialog( title='Data Missing', message='You must enter an email address for notification!', action=REQUEST['URL1']+'/addMessageForm') if not title or not author or not body: return MessageDialog( title='Data Missing', message='You must enter a title, author and body!', action=REQUEST['URL1']+'/addMessageForm') id=self.createId() msg=Message(id,intSet(),title,author,body,email,notify,reviewed,file) msg=msg.__of__(self) self.setItem(id,msg) self.expire_items() if REQUEST: p='%s%s' % (REQUEST['SCRIPT_NAME'],REQUEST['PATH_INFO']) t=self.id p=p[:(rfind(p,t)+len(t))] e='Friday, 31-Dec-99 23:59:59 GMT' resp=REQUEST['RESPONSE'] resp.setCookie('suggest_author',author,path=p,expires=e) resp.setCookie('suggest_email',email,path=p,expires=e) resp.setCookie('suggest_notify',notify,path=p,expires=e) return MessageDialog(title='Message Posted', message='Your message has been posted', action=REQUEST['URL1']) def search(self,REQUEST): """ """ sr=self.__call__(REQUEST,1) rc=len(sr) return self.searchResults(self,REQUEST,search_results=sr, result_count=rc) def manage_edit(self,mailhost='',exp=0,expire='0',moderated=0, REQUEST=None): """ """ if exp: expire=atoi(expire) else: expire=0 if mailhost: try: v=getattr(self, mailhost) except: return MessageDialog(title='Invalid Mail Host', message='Cannot find the named mail host!', action=REQUEST['URL']+'/manage_main') self.mail_host=mailhost self.expire =expire self.moderated=moderated return self.manage_main(self, REQUEST) def manage_delete(self,REQUEST,ids=[],): """ """ ids=map(atoi, ids) while ids: self.delItem(ids[-1]) del ids[-1] return self.manage_messages(self, REQUEST) # Searchable interface def __call__(self, REQUEST=None, internal=0): if REQUEST is None: REQUEST=self.REQUEST myid=self.id rset=None used={} for i in self.indices.values(): try: r=i._apply_index(REQUEST,myid) if r is not None: r,u=r for n in u: used[n]=1 if rset is None: rset=r else: rset=rs.intersection(r) except: pass if rset is None: r=[] else: r=self.data.map(rset) if self.moderated: r=filter(lambda x: x.reviewed, r) r=map(lambda x, p=self: x.__of__(p), r) if internal: return r return ResultSet(r) def _searchable_result_columns(self): return ({'name':'title', 'type':'s'}, {'name':'author','type':'s'}, {'name':'body', 'type':'s'}, {'name':'email', 'type':'s'}, ) def _searchable_arguments(self): return {'author': {'type':'string', 'optional':1, 'indexed':1}, 'body': {'type':'string', 'optional':0, 'indexed':1}, } class ResultItem: def __init__(self,data): self._data=data _schema=['title','author','body','email'] def __getattr__(self,key): if key=='body': return join(self._data.body,'
') return getattr(self._data, key) def __getitem__(self,key,inttype=type(0)): try: if type(key) is inttype: return getattr(self._data,self._schema[key]) return getattr(self._data, key) except (IndexError, KeyError): raise AttributeError, key def __len__(self): return len(self._schema) class ResultSet: def __init__(self,data): self._data=map(ResultItem, data) def __getitem__(self,index): return self._data[index] def __len__(self): return len(self._data) def __getslice__(self,i1,i2): return self._data[i1:i2] slice=__getslice__ _searchable_result_columns=Topic._searchable_result_columns class Message(Persistent, Implicit): """ """ manage_editForm=HTMLFile('editMessageForm', globals()) manage =manage_editForm manage_main =manage_editForm meta_type='Message' icon ='misc_/Confera/message.gif' root=0 def __init__(self, id, thread, title, author, body, email, notify, reviewed, file): self.id =str(id) self.ids =intSet() self.thread =thread self.title =title self.author =author self.body =body self.email =email self.notify =notify self.created =id self.modified=id self.reviewed=reviewed if file: setattr(self,file._name,file) self.file=file else: self.file='' self.revsub =0 def __len__(self): return 1 def __getitem__(self,n): try: id=atoi(n) except: raise AttributeError, n if self.ids.has_key(id): return self.data[id].__of__(self) raise AttributeError, n def setItem(self,id,obj): # Make sure the object we store is not wrapped with # an acquisition wrapper, since we will be wrapping # it again manually in __getitem__ when it is needed. if hasattr(obj, 'aq_base'): obj=obj.aq_base self.ids.insert(id) self.data[id]=obj for k in self.indices.keys(): self.indices[k].index_item(id) def messageValues(self): return map(lambda x, p=self: x.__of__(p), self.data.map(self.ids)) def tpId(self): return self.id def tpURL(self): return self.id def this(self): return self def has_items(self): return len(self.ids) def sub_ids(self,ids,data): map(ids.insert, self.ids) for item in data.map(self.ids): ids=item.sub_ids(ids,data) return ids def date_created(self): t=localtime(self.created) return '%d/%d/%d' % (t[1],t[2],t[0]) def time_created(self): t=localtime(self.created) return '%02d:%02d' % (t[3],t[4]) def attachment(self): file=self.file return file and (file,) or None def suggest_title(self): t=self.title return (lower(t[:3])=='re:') and t or 'Re: %s' % t def topic_url(self): req=self.REQUEST par=req['PARENTS'] for p in par: if p.root: return req['URL%d' % (par.index(p)+1)] return '' def thread_path(self): return join(map(lambda x: '/%s' % x, self.thread), '') def index_html(self,REQUEST): """ """ return self.message_html(self,REQUEST) def doNotify(self, msg, REQUEST): if self.notify and self.email and self.mail_host: mail =self.mail_html(self, REQUEST, newItem=(msg,)) mhost=getattr(self,self.mail_host) mhost.send(mail) def cancelNotify(self, REQUEST): """ """ self.notify='' return MessageDialog(title='Cancelled Notification', message='You will no longer be notified of ' \ 'replies to this message', action=self.topic_url()) def addMessage(self,title,author,body,email='',notify='',file='', submit='Cancel', REQUEST=None,RESPONSE=None): """ """ if submit=='Cancel': RESPONSE.redirect('%s/index_html' % self.topic_url()) title =strip(title) author =strip(author) email =strip(email) # workaround by JMF body=regsub.gsub('\n\r', '\n', body) body=regsub.gsub('\r\n', '\n', body) body = split(body,'\n') #body =split(regsub.gsub(crlf,'\n',body),'\n') # body =split(regsub.gsub(crlf,'\n',body),'\n') notify =notify reviewed=(not self.moderated) and 1 or 0 try: file=FileObject(file) except: file='' if notify and not email: return MessageDialog( title='Data Missing', message='You must enter an email address for notification!', action=REQUEST['URL1']+'/addMessageForm') if not title or not author or not body: return MessageDialog( title='Data Missing', message='You must enter a title, author and body!', action=REQUEST['URL1']+'/addMessageForm') id=self.createId() thread=intSet() map(thread.insert, self.thread) thread.insert(atoi(self.id)) for t in thread: obj=self.data[t] obj.modified=id if not reviewed: obj.revsub=obj.revsub+1 msg=Reply(id, thread, title, author, body, email, notify, reviewed, file) msg=msg.__of__(self) self.setItem(id, msg) if REQUEST: try: self.doNotify(msg,REQUEST) except: pass p='%s%s' % (REQUEST['SCRIPT_NAME'],REQUEST['PATH_INFO']) t=self.topic()[0].id p=p[:(rfind(p,t)+len(t))] e='Friday, 31-Dec-99 23:59:59 GMT' resp=REQUEST['RESPONSE'] resp.setCookie('suggest_author',author,path=p,expires=e) resp.setCookie('suggest_email',email,path=p,expires=e) resp.setCookie('suggest_notify',notify,path=p,expires=e) return MessageDialog(title='Message Posted', message='Your message has been posted', action=p) def manage_edit(self,title,author,body,email='',notify='',reviewed='', REQUEST=None,RESPONSE=None): """ """ if not title or not author or not body: return MessageDialog( title='Data Missing', message='You must enter a title, author and body!', action=REQUEST['URL1']+'/manage_editForm') if notify and not email: return MessageDialog( title='Data Missing', message='You must enter an email address for notification!', action=REQUEST['URL1']+'/manage_editForm') self.title =strip(title) self.author=strip(author) self.email =strip(email) # workaround by JMF body=regsub.gsub('\n\r', '\n', body) body=regsub.gsub('\r\n', '\n', body) self.body = split(body,'\n') #body =split(regsub.gsub(crlf,'\n',body),'\n') #self.body =split(regsub.gsub(crlf,'\n',body),'\n') self.notify=notify if self.moderated and (not self.reviewed) and reviewed: self.reviewed=1 for t in self.thread: obj=self.data[t] if obj.revsub > 0: obj.revsub=obj.revsub-1 if RESPONSE: RESPONSE.redirect('%s/manage_messages' % self.topic_url()) class Reply(Message): """ """ meta_type ='Reply' icon ='misc_/Confera/reply.gif' def manage_addConferaTopic(self,id,title='',mailhost='',exp=0,expire='0', moderated=0, default=1, REQUEST=None): """Create Confera topic""" if exp: try: expire=atoi(expire) except: expire=0 else: expire=0 if mailhost: try: v=getattr(self, mailhost) except: return MessageDialog(title='Invalid Mail Host', message='Cannot find the named mail host!', action=REQUEST['URL1']+'/manage_main') ob=Topic(id,title,mailhost,expire,moderated,default) self._setObject(id, ob) if REQUEST: return self.manage_main(self,REQUEST,update_menu=1)