'Submit a link to a subreddit.
Submit will create a link or self-post in the subreddit `sr` with the
title `title`. If `kind` is `"link"`, then `url` is expected to be a
valid URL to link to. Otherwise, `text`, if present, will be the
body of the self-post.
If a link with the same URL has already been submitted to the specified
subreddit an error will be returned unless `resubmit` is true.
`extension` is used for determining which view-type (e.g. `json`,
`compact` etc.) to use for the redirect that is generated if the
`resubmit` error occurs.'

@require_oauth2_scope('submit')
@validatedForm(VUser(), VModhash(), VCaptcha(), VRatelimit(rate_user=True, rate_ip=True, prefix='rate_submit_'), VShamedDomain('url'), sr=VSubmitSR('sr', 'kind'), url=VUrl('url'), title=VTitle('title'), sendreplies=VBoolean('sendreplies'), selftext=VMarkdown('text'), kind=VOneOf('kind', ['link', 'self']), extension=VLength('extension', 20, docs={'extension': 'extension    used    for    redirects'}), resubmit=VBoolean('resubmit'))
@api_doc(api_section.links_and_comments)
def POST_submit(self, form, jquery, url, selftext, kind, title, sr, extension, sendreplies, resubmit):

    from r2.models.admintools import is_banned_domain
    if url:
        if (url.lower() == 'self'):
            url = kind = 'self'
        form.set_inputs(url=url)
    is_self = (kind == 'self')
    if ((not kind) or form.has_errors('sr', errors.INVALID_OPTION)):
        return
    if form.has_errors('captcha', errors.BAD_CAPTCHA):
        return
    if form.has_errors('sr', errors.SUBREDDIT_NOEXIST, errors.SUBREDDIT_NOTALLOWED, errors.SUBREDDIT_REQUIRED, errors.INVALID_OPTION, errors.NO_SELFS, errors.NO_LINKS, errors.IN_TIMEOUT):
        return
    if ((not sr.can_submit_text(c.user)) and is_self):
        c.errors.add(errors.NO_SELFS, field='sr')
        form.has_errors('sr', errors.NO_SELFS)
        return
    if form.has_errors('title', errors.NO_TEXT, errors.TOO_LONG):
        return
    if (not sr.should_ratelimit(c.user, 'link')):
        c.errors.remove((errors.RATELIMIT, 'ratelimit'))
    elif form.has_errors('ratelimit', errors.RATELIMIT):
        return
    if (not is_self):
        if form.has_errors('url', errors.NO_URL, errors.BAD_URL):
            return
        if form.has_errors('url', errors.DOMAIN_BANNED):
            g.stats.simple_event('spam.shame.link')
            return
        if (not resubmit):
            listing = hot_links_by_url_listing(url, sr=sr, num=1)
            links = listing.things
            if links:
                c.errors.add(errors.ALREADY_SUB, field='url')
                form.has_errors('url', errors.ALREADY_SUB)
                u = links[0].already_submitted_link(url, title)
                if extension:
                    u = UrlParser(u)
                    u.set_extension(extension)
                    u = u.unparse()
                form.redirect(u)
                return
    if ((not c.user_is_admin) and is_self):
        if (len(selftext) > Link.SELFTEXT_MAX_LENGTH):
            c.errors.add(errors.TOO_LONG, field='text', msg_params={'max_length': Link.SELFTEXT_MAX_LENGTH})
            form.set_error(errors.TOO_LONG, 'text')
            return
    VNotInTimeout().run(action_name='submit', details_text=kind, target=sr)
    if (not request.POST.get('sendreplies')):
        sendreplies = is_self
    cleaned_title = re.sub('\\s+', '    ', title, flags=re.UNICODE)
    cleaned_title = cleaned_title.strip()
    l = Link._submit(is_self=is_self, title=cleaned_title, content=(selftext if is_self else url), author=c.user, sr=sr, ip=request.ip, sendreplies=sendreplies)
    if (not is_self):
        ban = is_banned_domain(url)
        if ban:
            g.stats.simple_event('spam.domainban.link_url')
            admintools.spam(l, banner=('domain    (%s)' % ban.banmsg))
            hooks.get_hook('banned_domain.submit').call(item=l, url=url, ban=ban)
    if sr.should_ratelimit(c.user, 'link'):
        VRatelimit.ratelimit(rate_user=True, rate_ip=True, prefix='rate_submit_')
    queries.new_link(l)
    l.update_search_index()
    g.events.submit_event(l, request=request, context=c)
    path = add_sr(l.make_permalink_slow())
    if extension:
        path += ('.%s' % extension)
    form.redirect(path)
    form._send_data(url=path)
    form._send_data(id=l._id36)
    form._send_data(name=l._fullname)
