def import_mail_from_message(request, message): """ Reads a message generated by the email package and imports it to the wiki. """ _ = request.getText msg = process_message(message) wiki_addrs = request.cfg.mail_import_wiki_addrs request.user = user.get_by_email_address(request, msg['from_addr'][1]) if not request.user: raise ProcessingError("No suitable user found for mail address %r" % (msg['from_addr'][1], )) d = get_pagename_content(request, msg) pagename = d['pagename'] generate_summary = d['generate_summary'] comment = u"Mail: '%s'" % (msg['subject'], ) page = PageEditor(request, pagename, do_editor_backup=0) if not request.user.may.save(page, "", 0): raise ProcessingError("Access denied for page %r" % pagename) attachments = [] for att in msg['attachments']: i = 0 while i < 1000: # do not create a gazillion attachments if something # strange happens, give up after 1000. if i == 0: fname = att.filename else: components = att.filename.split(".") new_suffix = "-" + str(i) # add the counter before the file extension if len(components) > 1: fname = u"%s%s.%s" % (u".".join( components[:-1]), new_suffix, components[-1]) else: fname = att.filename + new_suffix try: # att.data can be None for forwarded message content - we can # just ignore it, the forwarded message's text will be present # nevertheless if att.data is not None: # get the fname again, it might have changed fname, fsize = add_attachment(request, pagename, fname, att.data) attachments.append(fname) break except AttachmentAlreadyExists: i += 1 # build an attachment link table for the page with the e-mail # use relative attachment link markup here, so the page can be easily # renamed and the links don't break attachment_links = [""] + [ u'''[[attachment:%s|%s]]''' % (att, att) for att in attachments ] # assemble old page content and new mail body together old_content = Page(request, pagename).get_raw_body() if old_content: new_content = u"%s\n-----\n" % old_content else: new_content = '' #if not (generate_summary and "/" in pagename): #generate header in any case: new_content += u"'''Mail: %s (%s, <<DateTime(%s)>>)'''\n\n" % ( msg['subject'], email_to_markup(request, msg['from_addr']), msg['date']) new_content += d['content'] new_content += "\n" + u"\n * ".join(attachment_links) try: page.saveText(new_content, 0, comment=comment) except page.AccessDenied: raise ProcessingError("Access denied for page %r" % pagename) if generate_summary and "/" in pagename: splitted = pagename.split("/") parent_page = u"/".join(splitted[:-1]) child_page = splitted[-1] # here, use relative links also, but we need to include the child_page # name in the relative link as the markup gets put onto the parent_page attachment_links = [ u'''[[attachment:%s|%s]]''' % ("/%s/%s" % (child_page, att), att) for att in attachments ] old_content = Page(request, parent_page).get_raw_body().splitlines() found_table = None table_ends = None for lineno, line in enumerate(old_content): if line.startswith("## mail_overview") and old_content[ lineno + 1].startswith("||"): found_table = lineno elif found_table is not None and line.startswith("||"): table_ends = lineno + 1 elif table_ends is not None and not line.startswith("||"): break # in order to let the gettext system recognise the <<GetText>> calls used below, # we must repeat them here: [_("Date"), _("From"), _("To"), _("Content"), _("Attachments")] table_header = ( u"\n\n## mail_overview (don't delete this line)\n" + u"|| '''<<GetText(Date)>> ''' || '''<<GetText(From)>> ''' || '''<<GetText(To)>> ''' || '''<<GetText(Content)>> ''' || '''<<GetText(Attachments)>> ''' ||\n" ) from_col = email_to_markup(request, msg['from_addr']) to_col = ' '.join([ email_to_markup(request, (realname, mailaddr)) for realname, mailaddr in msg['target_addrs'] if not mailaddr in wiki_addrs ]) subj_col = '[[%s|%s]]' % (pagename, msg['subject']) date_col = msg['date'] attach_col = " ".join(attachment_links) new_line = u'|| <<DateTime(%s)>> || %s || %s || %s || %s ||' % ( date_col, from_col, to_col, subj_col, attach_col) if found_table is not None: content = "\n".join(old_content[:table_ends] + [new_line] + old_content[table_ends:]) else: content = "\n".join(old_content) + table_header + new_line page = PageEditor(request, parent_page, do_editor_backup=0) page.saveText(content, 0, comment=comment)
def execute(pagename, request): _ = request.getText def wr(fmt, *args): request.write(form_writer(fmt, *args)) # This action generates data using the user language request.setContentLanguage(request.lang) form = values_to_form(request.values) if form.has_key('cancel'): request.reset() backto = form.get('backto', [None])[0] request.theme.add_msg(_('Edit was cancelled.'), "error") if backto: page = Page(request, backto) request.http_redirect(page.url(request)) request.page = page request.page.send_page() elif form.has_key('save') or form.has_key('saveform'): if request.environ['REQUEST_METHOD'] != 'POST': request.page.send_page() return template = form.get('template', [None])[0] # MetaFormEdit is much closer to set_meta in function if form.has_key('saveform'): added, cleared = {pagename: dict()}, {pagename: list()} # Pre-create page if it does not exist, using the template specified if template: added[pagename]['gwikitemplate'] = template # Ignore form clutter ignore = set() files = dict() for key in request.files: ignore.add(key) _file = request.files.get(key) filename = _file.filename fileobj = _file.stream # if type(fileobj) != file: # continue banana = key.split(SEPARATOR) keys = files.setdefault(banana[0], dict()) values = keys.setdefault(banana[1], list()) values.append((filename, fileobj)) keys = list() for key in form: if key not in ignore and SEPARATOR in key: keys.append(key.split(SEPARATOR)[1]) # keys = [x.split(SEPARATOR)[1] for x in form if SEPARATOR in x] for key in keys: cleared[pagename].append(key) vals = [x.strip() for x in form[pagename + SEPARATOR + key] if x.strip()] if vals: added[pagename].setdefault(key, list()).extend(vals) msgs = list() # Add attachments for pname in files: for key in files[pname]: for value in files[pname][key]: name = value[0] try: t, s = add_attachment(request, pname, name, value[1]) added.setdefault(pname, dict()).setdefault( key, list()).append("[[attachment:%s]]" % name) except AttachmentAlreadyExists: msgs = ["Attachment '%s' already exists." % name] _, msgss = set_metas(request, cleared, dict(), added) msgs.extend(msgss) else: # MetaEdit pages, msgs, files = parse_editform(request, form) if pages: saved_templates = False for page, _ in pages.iteritems(): # Save the template if needed if not Page(request, page).exists() and template: msgs.append(save_template(request, page, template)) saved_templates = True # If new pages were changed we need to redo parsing # the form to know what we really need to edit if saved_templates: pages, newmsgs, files = parse_editform(request, form) for page, (oldMeta, newMeta) in pages.iteritems(): msgs.append('%s: ' % page + edit_meta(request, page, oldMeta, newMeta)) for page in files: for key in files[page]: name, content = files[page][key] t, s = add_attachment(request, page, name, content) else: msgs.append(request.getText('No pages changed')) msg = '' for line in msgs: msg += line + request.formatter.linebreak(0) request.reset() backto = form.get('backto', [None])[0] if backto: page = Page(request, backto) request.http_redirect(page.url(request)) else: page = Page(request, pagename) request.theme.add_msg(msg) request.page = page page.send_page() elif form.has_key('args'): enter_page(request, pagename, 'Metatable editor') formatter = request.page.formatter request.write(formatter.heading(1, 2)) request.write(formatter.text(_("Edit metatable"))) request.write(formatter.heading(0, 2)) args = ', '.join(form['args']) show_editform(wr, request, pagename, args) exit_page(request, pagename) else: enter_page(request, pagename, 'Metatable editor') formatter = request.page.formatter request.write(formatter.heading(1, 2)) request.write(formatter.text(_("Edit current page"))) request.write(formatter.heading(0, 2)) show_editform(wr, request, pagename, pagename) request.write(formatter.heading(1, 2)) request.write(formatter.text(_("Edit metatable"))) request.write(formatter.heading(0, 2)) show_queryform(wr, request, pagename) exit_page(request, pagename) return
def execute(pagename, request): form = values_to_form(request.values) util = form.get('util', [None])[0] if util == "format": txt = form.get('text', [None])[0] request.write(format_wikitext(request, txt)) elif util == "getTemplate": template = form.get('name', [None])[0] template_page = wikiutil.unquoteWikiname(template) if request.user.may.read(template_page): Page(request, template_page).send_raw() elif util == "newPage": page = form.get('page', [None])[0] content = form.get('content', [""])[0] request.content_type = "application/json" if request.environ['REQUEST_METHOD'] != 'POST': return if not page: msg = "Page name not defined!" json.dump(dict(status="error", msg=msg), request) return if not request.user.may.write(page): msg = "You are not allowed to edit this page" json.dump(dict(status="error", msg=msg), request) return p = Page(request, page) if p.exists(): msg = "Page already exists" json.dump(dict(status="error", msg=msg), request) return editor = PageEditor(request, page) msg = editor.saveText(content, p.get_real_rev()) json.dump(dict(status="ok", msg=msg), request) elif util == "getProperties": request.content_type = "application/json" key = form.get('key', [''])[0] json.dump(get_properties(request, key), request) return elif util == "uploadFile": request.content_type = "application/json" if not request.user.may.write(pagename): msg = u"You are not allowed to edit this page!" json.dump(dict(status="error", msg=msg), request) request.status_code = 403 return from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists try: overwrite = int(form.get('overwrite', ['0'])[0]) except: overwrite = 0 response = dict(success=list(), failed=list()) for name in request.files: _file = request.files.get(name) filename = _file.filename try: t, s = add_attachment(request, pagename, filename, _file.stream, overwrite=overwrite) response['success'].append(filename) except AttachmentAlreadyExists: response['failed'].append(filename) json.dump(response, request) return elif util == "getAttachments": request.content_type = "application/json" from MoinMoin.action.AttachFile import _get_files, getAttachUrl files = _get_files(request, pagename) response = [] for name in files: response.append( dict(url=getAttachUrl(pagename, name, request), name=name)) json.dump(response, request) return
def import_mail_from_message(request, message): """ Reads a message generated by the email package and imports it to the wiki. """ _ = request.getText msg = process_message(message) wiki_addrs = request.cfg.mail_import_wiki_addrs request.user = user.get_by_email_address(request, msg['from_addr'][1]) if not request.user: raise ProcessingError("No suitable user found for mail address %r" % (msg['from_addr'][1], )) d = get_pagename_content(request, msg) pagename = d['pagename'] generate_summary = d['generate_summary'] comment = u"Mail: '%s'" % (msg['subject'], ) page = PageEditor(request, pagename, do_editor_backup=0) if not request.user.may.save(page, "", 0): raise ProcessingError("Access denied for page %r" % pagename) attachments = [] for att in msg['attachments']: i = 0 while i < 1000: # do not create a gazillion attachments if something # strange happens, give up after 1000. if i == 0: fname = att.filename else: components = att.filename.split(".") new_suffix = "-" + str(i) # add the counter before the file extension if len(components) > 1: fname = u"%s%s.%s" % (u".".join(components[:-1]), new_suffix, components[-1]) else: fname = att.filename + new_suffix try: # att.data can be None for forwarded message content - we can # just ignore it, the forwarded message's text will be present # nevertheless if att.data is not None: # get the fname again, it might have changed fname, fsize = add_attachment(request, pagename, fname, att.data) attachments.append(fname) break except AttachmentAlreadyExists: i += 1 # build an attachment link table for the page with the e-mail attachment_links = [""] + [u'''[[attachment:%s|%s]]''' % ("%s/%s" % (pagename, att), att) for att in attachments] # assemble old page content and new mail body together old_content = Page(request, pagename).get_raw_body() if old_content: new_content = u"%s\n-----\n" % old_content else: new_content = '' #if not (generate_summary and "/" in pagename): #generate header in any case: new_content += u"'''Mail: %s (%s, <<DateTime(%s)>>)'''\n\n" % (msg['subject'], email_to_markup(request, msg['from_addr']), msg['date']) new_content += d['content'] new_content += "\n" + u"\n * ".join(attachment_links) try: page.saveText(new_content, 0, comment=comment) except page.AccessDenied: raise ProcessingError("Access denied for page %r" % pagename) if generate_summary and "/" in pagename: parent_page = u"/".join(pagename.split("/")[:-1]) old_content = Page(request, parent_page).get_raw_body().splitlines() found_table = None table_ends = None for lineno, line in enumerate(old_content): if line.startswith("## mail_overview") and old_content[lineno+1].startswith("||"): found_table = lineno elif found_table is not None and line.startswith("||"): table_ends = lineno + 1 elif table_ends is not None and not line.startswith("||"): break # in order to let the gettext system recognise the <<GetText>> calls used below, # we must repeat them here: [_("Date"), _("From"), _("To"), _("Content"), _("Attachments")] table_header = (u"\n\n## mail_overview (don't delete this line)\n" + u"|| '''<<GetText(Date)>> ''' || '''<<GetText(From)>> ''' || '''<<GetText(To)>> ''' || '''<<GetText(Content)>> ''' || '''<<GetText(Attachments)>> ''' ||\n" ) from_col = email_to_markup(request, msg['from_addr']) to_col = ' '.join([email_to_markup(request, (realname, mailaddr)) for realname, mailaddr in msg['target_addrs'] if not mailaddr in wiki_addrs]) subj_col = '[[%s|%s]]' % (pagename, msg['subject']) date_col = msg['date'] attach_col = " ".join(attachment_links) new_line = u'|| <<DateTime(%s)>> || %s || %s || %s || %s ||' % (date_col, from_col, to_col, subj_col, attach_col) if found_table is not None: content = "\n".join(old_content[:table_ends] + [new_line] + old_content[table_ends:]) else: content = "\n".join(old_content) + table_header + new_line page = PageEditor(request, parent_page, do_editor_backup=0) page.saveText(content, 0, comment=comment)
def execute(pagename, request): form = values_to_form(request.values) util = form.get('util', [None])[0] if util == "format": txt = form.get('text', [None])[0] request.write(format_wikitext(request, txt)) elif util == "getTemplate": template = form.get('name', [None])[0] template_page = wikiutil.unquoteWikiname(template) if request.user.may.read(template_page): Page(request, template_page).send_raw() elif util == "newPage": page = form.get('page', [None])[0] content = form.get('content', [""])[0] request.content_type = "application/json" if request.environ['REQUEST_METHOD'] != 'POST': return if not page: msg = "Page name not defined!" json.dump(dict(status="error", msg=msg), request) return if not request.user.may.write(page): msg = "You are not allowed to edit this page" json.dump(dict(status="error", msg=msg), request) return p = Page(request, page) if p.exists(): msg = "Page already exists" json.dump(dict(status="error", msg=msg), request) return editor = PageEditor(request, page) msg = editor.saveText(content, p.get_real_rev()) json.dump(dict(status="ok", msg=msg), request) elif util == "getProperties": request.content_type = "application/json" key = form.get('key', [''])[0] json.dump(get_properties(request, key), request) return elif util == "uploadFile": request.content_type = "application/json" if not request.user.may.write(pagename): msg = u"You are not allowed to edit this page!" json.dump(dict(status="error", msg=msg), request) request.status_code = 403 return from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists try: overwrite = int(form.get('overwrite', ['0'])[0]) except: overwrite = 0 response = dict(success=list(), failed=list()) for name in request.files: _file = request.files.get(name) filename = _file.filename try: t,s = add_attachment(request, pagename, filename, _file.stream, overwrite=overwrite) response['success'].append(filename) except AttachmentAlreadyExists: response['failed'].append(filename) json.dump(response, request) return elif util == "getAttachments": request.content_type = "application/json" from MoinMoin.action.AttachFile import _get_files, getAttachUrl files = _get_files(request, pagename) response = [] for name in files: response.append(dict(url=getAttachUrl(pagename, name, request),name=name)) json.dump(response, request) return
def execute(pagename, request): _ = request.getText def wr(fmt, *args): request.write(form_writer(fmt, *args)) # This action generates data using the user language request.setContentLanguage(request.lang) form = values_to_form(request.values) if form.has_key('cancel'): request.reset() backto = form.get('backto', [None])[0] request.theme.add_msg(_('Edit was cancelled.'), "error") if backto: page = Page(request, backto) request.http_redirect(page.url(request)) request.page = page request.page.send_page() elif form.has_key('save') or form.has_key('saveform'): if request.environ['REQUEST_METHOD'] != 'POST': request.page.send_page() return template = form.get('template', [None])[0] # MetaFormEdit is much closer to set_meta in function if form.has_key('saveform'): added, cleared = {pagename: dict()}, {pagename: list()} # Pre-create page if it does not exist, using the template specified if template: added[pagename]['gwikitemplate'] = template # Ignore form clutter ignore = set() files = dict() for key in request.files: ignore.add(key) _file = request.files.get(key) filename = _file.filename fileobj = _file.stream # if type(fileobj) != file: # continue banana = key.split(SEPARATOR) keys = files.setdefault(banana[0], dict()) values = keys.setdefault(banana[1], list()) values.append((filename, fileobj)) keys = list() for key in form: if key not in ignore and SEPARATOR in key: keys.append(key.split(SEPARATOR)[1]) # keys = [x.split(SEPARATOR)[1] for x in form if SEPARATOR in x] for key in keys: cleared[pagename].append(key) vals = [ x.strip() for x in form[pagename + SEPARATOR + key] if x.strip() ] if vals: added[pagename].setdefault(key, list()).extend(vals) msgs = list() # Add attachments for pname in files: for key in files[pname]: for value in files[pname][key]: name = value[0] try: t, s = add_attachment(request, pname, name, value[1]) added.setdefault(pname, dict()).setdefault( key, list()).append("[[attachment:%s]]" % name) except AttachmentAlreadyExists: msgs = ["Attachment '%s' already exists." % name] _, msgss = set_metas(request, cleared, dict(), added) msgs.extend(msgss) else: # MetaEdit pages, msgs, files = parse_editform(request, form) if pages: saved_templates = False for page, _ in pages.iteritems(): # Save the template if needed if not Page(request, page).exists() and template: msgs.append(save_template(request, page, template)) saved_templates = True # If new pages were changed we need to redo parsing # the form to know what we really need to edit if saved_templates: pages, newmsgs, files = parse_editform(request, form) for page, (oldMeta, newMeta) in pages.iteritems(): msgs.append('%s: ' % page + edit_meta(request, page, oldMeta, newMeta)) for page in files: for key in files[page]: name, content = files[page][key] t, s = add_attachment(request, page, name, content) else: msgs.append(request.getText('No pages changed')) msg = '' for line in msgs: msg += line + request.formatter.linebreak(0) request.reset() backto = form.get('backto', [None])[0] if backto: page = Page(request, backto) request.http_redirect(page.url(request)) else: page = Page(request, pagename) request.theme.add_msg(msg) request.page = page page.send_page() elif form.has_key('args'): enter_page(request, pagename, 'Metatable editor') formatter = request.page.formatter request.write(formatter.heading(1, 2)) request.write(formatter.text(_("Edit metatable"))) request.write(formatter.heading(0, 2)) args = ', '.join(form['args']) show_editform(wr, request, pagename, args) exit_page(request, pagename) else: enter_page(request, pagename, 'Metatable editor') formatter = request.page.formatter request.write(formatter.heading(1, 2)) request.write(formatter.text(_("Edit current page"))) request.write(formatter.heading(0, 2)) show_editform(wr, request, pagename, pagename) request.write(formatter.heading(1, 2)) request.write(formatter.text(_("Edit metatable"))) request.write(formatter.heading(0, 2)) show_queryform(wr, request, pagename) exit_page(request, pagename) return
def photoupload(pagename, request): _ = request.getText # if not wikiutil.checkTicket(request, request.form.get('ticket', '')): # return _('Please use the interactive user interface to use action %(actionname)s!') % {'actionname': 'AttachFile.upload' } # Currently we only check TextCha for upload (this is what spammers ususally do), # but it could be extended to more/all attachment write access # if not TextCha(request).check_answer_from_form(): # return _('TextCha: Wrong answer! Go back and try again...') form = request.form file_upload = request.files.get('file') if not file_upload: # This might happen when trying to upload file names # with non-ascii characters on Safari. return _("No file content. Delete non ASCII characters from the file name and try again.") try: overwrite = int(form.get('overwrite', '0')) except: overwrite = 0 if not request.user.may.write(pagename): return _('You are not allowed to attach a file to this page.') if overwrite and not request.user.may.delete(pagename): return _('You are not allowed to overwrite a file attachment of this page.') # target = form.get('target', u'').strip() # if not target: # target = file_upload.filename or u'' # target = wikiutil.clean_input(target) file_upload_ext = os.path.splitext(file_upload.filename)[-1].lower() if file_upload_ext not in ('.jpg'): return _('You can upload only following extentions. -- .jpg, but ' + file_upload_ext) # if not target: # return _("Filename of attachment not specified!") if pagename.encode('utf8') == '아메바사진': path = '/volume1/photo/webpub_amb'#/@eaDir' elif pagename.encode('utf8') == '사과나무사진관': path = '/volume1/photo/webpub_apt'#/@eaDir' else: path = '/volume1/photo/webpub_tst' try: lst = glob.glob(path + '/*.jpg') lastnumfilepath = sorted(lst)[-1] lastnumfile = os.path.split(lastnumfilepath)[1] filenm = os.path.splitext(lastnumfile)[0] filenum = int(filenm) filenum += 1 target = "%05d%s" % (filenum, file_upload_ext) except: target = "00001" + file_upload_ext print '-'*100 print path, target # add the attachment try: target, bytes = add_attachment(request, pagename, target, file_upload.stream, overwrite=overwrite) msg = "Picture '%(target)s' ('%(filename)s',%(bytes)d bytes) have been uploaded." % { 'target': target, 'filename': file_upload.filename, 'bytes': bytes} except AttachmentAlreadyExists: msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % { 'target': target, 'filename': file_upload.filename} # return attachment list #upload_form(pagename, request, msg) # move # replace illegal chars target = wikiutil.taintfilename(target) # get directory, and possibly create it # attach_dir = getAttachDir(request, pagename, create=1) if request.page and pagename == request.page.page_name: page = request.page # reusing existing page obj is faster else: page = Page(request, pagename) attach_dir = page.getPagePath("attachments", check_create=1) fpath = os.path.join(attach_dir, target).encode(request.cfg.attachment_charset) os.rename(fpath, path + os.sep + target) # msg += ' mv %s %s/%s' % (fpath, path, target) # os.system('synoindex -a ' + path + '/' + target) # os.system('synoindxe -U photo') from subprocess import Popen pid = Popen(["/volume1/photo/create_tn.sh", path, target]).pid #request.page.send_page() return msg