def process_summary_call(post):
  #special("__________________________________________________")
  #special("SUMMARY CALL: %s"%post.id)
  #TODO change summoning
  replacedbody = post.body.lower().replace('wikibot','___uawb___wikibot')
  if re.search(r'wikibot.\s*tell\s.{1,23}\sabout\s+(an\s+|a\s+|the\s+|)(.*?)$',replacedbody):
    post_body = re.sub(r'wikibot.\s*tell\s.{1,23}\sabout\s+(an\s+|a\s+|the\s+|)(.*?)$',r'\2',replacedbody).split('___uawb___')[1].split('.')[0].split('?')[0]
    term = post_body.strip()
  elif re.search(r'wikibot.\s*wh.{1,3}(\'s|\s+is|\s+are|\s+was)\s+(an\s+|a\s+|the\s+|)(.*?)$',replacedbody):
    post_body = re.sub(r'wikibot.\s*wh.{1,3}(\'s|\s+is|\s+are|\s+was)\s+(an\s+|a\s+|the\s+|)(.*?)$',r'\3',replacedbody).split('___uawb___')[1].split('.')[0].split('?')[0]
    term = post_body.strip()
  elif re.search("\?\-.*\-\?",replacedbody):
    term = re.search("\?\-.*\-\?",post.body.lower()).group(0).strip('?').strip('-').strip()
    
  special("SUMMARY CALL: %s @ %s"%(filter(lambda x: x in string.printable, term),post.id))
  if term.lower().strip() == 'love':
    #post_reply('*Baby don\'t hurt me! Now seriously, stop asking me about love so many times! O.o What were we discussing about in this thread again?*',post)
    return(False,False)
  #if term.lower().strip() == 'wikibot':
    #post_reply('*Me! I know me.*',post)
    return(False,False)
  if term.lower().strip() == 'reddit':
    #post_reply('*This place. It feels like home.*',post)
    return(False,False)
  if term.strip().__len__() < 2 or term == None:
    #log("EMPTY TERM")
    return(False,False)
  try:
    title = wikipedia.page(term,auto_suggest=False).title
    if title.lower() == term:
      bit_comment_start = ""
    elif title.lower() != term:
      try:
	discard = wikipedia.page(term,auto_suggest=False,redirect=False).title
      except Exception as e:
	if re.search('resulted in a redirect',str(e)):
	  bit_comment_start = "*\"" + term.strip() + "\" redirects to* "
    else:
      bit_comment_start = "*Nearest match for* ***" + term.strip() + "*** *is* "
    if re.search(r'#',title):
      url = wikipedia.page(title.split('#')[0],auto_suggest=False).url
      sectionurl =  url + "#" + title.split('#')[1]
      comment = "*Nearest match for* ***" + term.strip() + "*** *is the section ["+title.split('#')[1]+"]("+sectionurl.replace(')','\)')+") in article ["+title.split('#')[0]+"]("+url+").*\n\n---\n\n"
      post_reply(comment,post)
      #log("RELEVANT SECTION SUGGESTED: %s"%filter(lambda x: x in string.printable, title))
      return (False,False)
    url_string = title
    #log("INTERPRETATION: %s"%filter(lambda x: x in string.printable, title))
    return (url_string,bit_comment_start)
  except Exception as e:
    if bool(re.search('.*may refer to:.*',filter(lambda x: x in string.printable, str(e)))):
      deflist = ">Definitions for few of those terms:"
      for idx, val in enumerate(filter(lambda x: x in string.printable, str(e)).split('may refer to: \n')[1].split('\n')):
	deflist = deflist + "\n\n>1. **"+val.strip()+"**: "+ wikipedia.summary(val,auto_suggest=False,sentences=1)
	if idx > 3:
	  break
      summary = "*Oops,* ***"+term.strip()+"*** *landed me on a disambiguation page.*\n\n---\n\n"+deflist+"\n\n---\n\n"
      #log("ASKING FOR DISAMBIGUATION")
    else:
      #log("INTERPRETATION FAIL: %s"%filter(lambda x: x in string.printable, term))
      try:
	terms = "\""+term+"\""
	suggesttitle = str(wikipedia.search(terms,results=1)[0])
	#log("SUGGESTING: %s"%filter(lambda x: x in string.printable, suggesttitle))
	if suggesttitle.lower() == term:
	  bit_comment_start = ""
	else:
	  bit_comment_start = "*Nearest match for* ***" + term.strip() + "*** *is* "
	if str(suggesttitle).endswith(')') and not re.search('\(',str(suggesttitle)):
	  suggesttitle = suggesttitle[0:--(suggesttitle.__len__()-1)]
	return (str(suggesttitle),bit_comment_start)
      except:
	trialtitle = wikipedia.page(term,auto_suggest=True).title
	if trialtitle.lower() == term:
	  bit_comment_start = ""
	else:
	  bit_comment_start = "*Nearest match for* ***" + term.strip() + "*** *is* "
	#log("TRIAL SUGGESTION: %s"%filter(lambda x: x in string.printable, trialtitle))  
	if str(trialtitle).endswith(')') and not re.search('\(',str(trialtitle)):
	  trialtitle = trialtitle[0:--(trialtitle.__len__()-1)]
	return (str(trialtitle),bit_comment_start)
    post_reply(summary,post)
    return (False,False)
		  if str(post.subreddit) in badsubs:
		    badsubs.remove(str(post.subreddit))
		  editsummary = 'removed '+str(post.subreddit)+', reason:mod_switch_root_off'
		  save_changing_variables(editsummary)
		  comment = "*Root only feature switched* ***OFF*** *for /r/"+str(post.subreddit)+"*\n\n---\n\n"
	      else:
		comment = False
	      
	      if comment:
		a = post_reply(comment,post)
		title = "MODSWITCH: %s"%str(post.subreddit)
		subtext = "/u/"+str(post.author.name)+": @ [comment]("+post.permalink+")\n\n"+str(post.body)+"\n\n---\n\n"+comment
		#TODO change username
    r.submit('acini',title,text=subtext)
	      if a:
		special("MODSWITCH: %s @ %s"%(comment.replace('*',''),post.id))
	      else:
		fail("MODSWITCH REPLY FAILED: %s @ %s"%(comment,post.id))
		title = "MODSWITCH REPLY FAILED: %s"%str(post.subreddit)
		subtext = "/u/"+str(post.author.name)+": @ [comment]("+post.permalink+")\n\n"+str(post.body)+"\n\n---\n\n"+comment
		#TODO change username
    r.submit('acini',title,text=subtext)
	    else:
	      if post.subreddit not in badsubs:
		comment = "*Moderator switches can only be switched ON and OFF by moderators of this subreddit.*\n\n*If you want specific feature turned ON or OFF, [ask the moderators](/message/compose?to=%2Fr%2F"+str(post.subreddit)+") and provide them with [this link](http://www.np.reddit.com/r/autowikibot/wiki/modfaqs).*\n\n---\n\n"
		post_reply(comment,post)
	  except Exception as e:
	    title = "MODSWITCH FAILURE !!: %s"%str(post.subreddit)
	    traceback.print_exc()
	    subtext = "/u/"+str(post.author.name)+": @ [comment]("+post.permalink+")\n\n"+str(post.body)+"\n\n---\n\n"+str(e)
	    #TODO chagne username
def process_summary_call(post):
    special("__________________________________________________")
    special("SUMMARY CALL: %s" % post.id)
    if re.search('wikibot.*?tell .*? about ', post.body.lower()) or re.search(
            "wikibot.*?wh.*?(\'s|is a |is an|is|are|was)", post.body.lower()):
        if re.search('wikibot.*?tell .*? about ', post.body.lower()):
            post_body = re.sub('wikibot.*?tell .*? about ', '__BODYSPLIT__',
                               post.body.lower())
        else:
            post_body = re.sub('wikibot.*?wh.*?(\'s|is a |is an|is|are|was) ',
                               '__BODYSPLIT__', post.body.lower())
        term = post_body.split('__BODYSPLIT__')[1]
        term = re.sub('\?', '\n', term)
        if term[0:2] == 'a ':
            term = term[2:term.__len__()]
        if term[0:4] == 'the ':
            term = term[4:term.__len__()]
        if term.endswith('.'):
            term = term[0:--(term.__len__() - 1)]
        try:
            term = term.split('\n')[0]
        except:
            log("COULD NOT SPLIT")
            pass
    elif re.search("\?\-.*\-\?", post.body.lower()):
        term = re.search(
            "\?\-.*\-\?",
            post.body.lower()).group(0).strip('$').strip('-').strip()

    log("TERM: %s" % filter(lambda x: x in string.printable, term))
    if term.lower().strip() == 'love':
        post_reply(
            '*Baby don\'t hurt me! Now seriously, stop asking me about love so many times! O.o What were we discussing about in this thread again?*',
            post)
        return (False, False)
    if term.lower().strip() == 'wikibot':
        post_reply('*Me! I know me.*', post)
        return (False, False)
    if term.lower().strip() == 'reddit':
        post_reply('*This place. It feels like home.*', post)
        return (False, False)
    if term.strip().__len__() < 2 or term == None:
        log("EMPTY TERM")
        return (False, False)
    try:
        title = wikipedia.page(term, auto_suggest=False).title
        if title.lower() == term:
            bit_comment_start = ""
        elif title.lower() != term:
            try:
                discard = wikipedia.page(term,
                                         auto_suggest=False,
                                         redirect=False).title
            except Exception as e:
                if re.search('resulted in a redirect', str(e)):
                    bit_comment_start = "*\"" + term.strip(
                    ) + "\" redirects to* "
        else:
            bit_comment_start = "*Couldn't find Wikipedia article titled \"" + term.strip(
            ) + "\". Closest match is* "
        if re.search(r'#', title):
            url = wikipedia.page(title.split('#')[0], auto_suggest=False).url
            sectionurl = url + "#" + title.split('#')[1]
            comment = "*Couldn't find Wikipedia article titled \"" + term.strip(
            ) + "\". But I found a relevant section [" + title.split(
                '#'
            )[1] + "](" + sectionurl.replace(
                ')', '\)'
            ) + ") in article [" + title.split(
                '#'
            )[0] + "](" + url + ") that might interest you.*\n\n---\n\n[^(about)](http://) ^| *^(/u/" + post.author.name + " can reply with 'delete'. Will also delete if comment's score is -1 or less.)*  ^| ^[**Summon**](http://www.reddit.com/r/autowikibot/comments/1ux484/ask_wikibot/)"
            post_reply(comment, post)
            log("RELEVANT SECTION SUGGESTED: %s" %
                filter(lambda x: x in string.printable, title))
            return (False, False)
        url_string = title
        log("INTERPRETATION: %s" %
            filter(lambda x: x in string.printable, title))
        return (url_string, bit_comment_start)
    except Exception as e:
        if bool(
                re.search('.*may refer to:.*',
                          filter(lambda x: x in string.printable, str(e)))):
            deflist = "\n\nI found 3 most common meanings for you:\n\n"
            for idx, val in enumerate(
                    filter(lambda x: x in string.printable,
                           str(e)).split('may refer to: \n')[1].split('\n')):
                deflist = deflist + "\n\n>* " + wikipedia.summary(
                    val, auto_suggest=False, sentences=1)
                if idx > 1:
                    break
            summary = "*Oh, there's too many of \"" + term.strip(
            ) + "\".*\n\n---" + deflist + "\n\n---\n\nOtherwise, " + str(
                e
            ).replace(
                '\n', '\n\n>'
            ) + "\n\n---\n\n[^(about)](http://) ^| *^(/u/" + post.author.name + " can reply with 'delete'. Will also delete if comment's score is -1 or less.)*  ^| ^[**Summon**](http://www.reddit.com/r/autowikibot/comments/1ux484/ask_wikibot/)"
            log("ASKING FOR DISAMBIGUATION")
        else:
            log("INTERPRETATION FAIL: %s" %
                filter(lambda x: x in string.printable, term))
            try:
                terms = "\"" + term + "\""
                suggesttitle = str(wikipedia.search(terms, results=1)[0])
                log("SUGGESTING: %s" %
                    filter(lambda x: x in string.printable, suggesttitle))
                if suggesttitle.lower() == term:
                    bit_comment_start = ""
                else:
                    bit_comment_start = "*Couldn't find Wikipedia article titled \"" + term.strip(
                    ) + "\". Here\'s the closest match:*"
                if str(suggesttitle).endswith(')') and not re.search(
                        '\(', str(suggesttitle)):
                    suggesttitle = suggesttitle[0:--(suggesttitle.__len__() -
                                                     1)]
                return (str(suggesttitle), bit_comment_start)
            except:
                trialtitle = wikipedia.page(term, auto_suggest=True).title
                if trialtitle.lower() == term:
                    bit_comment_start = ""
                else:
                    bit_comment_start = "*Couldn't find Wikipedia article titled \"" + term.strip(
                    ) + "\". By long shot, here's the closest match:*"
                log("TRIAL SUGGESTION: %s" %
                    filter(lambda x: x in string.printable, trialtitle))
                if str(trialtitle).endswith(')') and not re.search(
                        '\(', str(trialtitle)):
                    trialtitle = trialtitle[0:--(trialtitle.__len__() - 1)]
                return (str(trialtitle), bit_comment_start)
        post_reply(summary, post)
        return (False, False)
def process_summary_call(post):
  special("__________________________________________________")
  special("SUMMARY CALL: %s"%post.id)
  if re.search('wikibot.*?tell .*? about ',post.body.lower()) or re.search("wikibot.*?wh.*?(\'s|is a |is an|is|are|was)",post.body.lower()):
    if re.search('wikibot.*?tell .*? about ',post.body.lower()):
      post_body = re.sub('wikibot.*?tell .*? about ','__BODYSPLIT__',post.body.lower())
    else:
      post_body = re.sub('wikibot.*?wh.*?(\'s|is a |is an|is|are|was) ','__BODYSPLIT__',post.body.lower())
    term = post_body.split('__BODYSPLIT__')[1]
    term = re.sub('\?','\n',term)
    if term[0:2] == 'a ':
      term = term[2:term.__len__()]
    if term[0:4] == 'the ':
      term = term[4:term.__len__()]
    if term.endswith('.'):
      term = term[0:--(term.__len__()-1)]
    try:
      term = term.split('\n')[0]
    except:
      log("COULD NOT SPLIT")
      pass
  elif re.search("\?\-.*\-\?",post.body.lower()):
    term = re.search("\?\-.*\-\?",post.body.lower()).group(0).strip('$').strip('-').strip()
    
  log("TERM: %s"%filter(lambda x: x in string.printable, term))
  if term.lower().strip() == 'love':
    post_reply('*Baby don\'t hurt me! Now seriously, stop asking me about love so many times! O.o What were we discussing about in this thread again?*',post)
    return(False,False)
  if term.lower().strip() == 'wikibot':
    post_reply('*Me! I know me.*',post)
    return(False,False)
  if term.lower().strip() == 'reddit':
    post_reply('*This place. It feels like home.*',post)
    return(False,False)
  if term.strip().__len__() < 2 or term == None:
    log("EMPTY TERM")
    return(False,False)
  try:
    title = wikipedia.page(term,auto_suggest=False).title
    if title.lower() == term:
      bit_comment_start = ""
    elif title.lower() != term:
      try:
	discard = wikipedia.page(term,auto_suggest=False,redirect=False).title
      except Exception as e:
	if re.search('resulted in a redirect',str(e)):
	  bit_comment_start = "*\"" + term.strip() + "\" redirects to* "
    else:
      bit_comment_start = "*Couldn't find Wikipedia article titled \"" + term.strip() + "\". Closest match is* "
    if re.search(r'#',title):
      url = wikipedia.page(title.split('#')[0],auto_suggest=False).url
      sectionurl =  url + "#" + title.split('#')[1]
      comment = "*Couldn't find Wikipedia article titled \"" + term.strip() + "\". But I found a relevant section ["+title.split('#')[1]+"]("+sectionurl.replace(')','\)')+") in article ["+title.split('#')[0]+"]("+url+") that might interest you.*\n\n---\n\n[^(about)](http://) ^| *^(/u/"+post.author.name+" can reply with 'delete'. Will also delete if comment's score is -1 or less.)*  ^| ^[**Summon**](http://www.reddit.com/r/autowikibot/comments/1ux484/ask_wikibot/)"
      post_reply(comment,post)
      log("RELEVANT SECTION SUGGESTED: %s"%filter(lambda x: x in string.printable, title))
      return (False,False)
    url_string = title
    log("INTERPRETATION: %s"%filter(lambda x: x in string.printable, title))
    return (url_string,bit_comment_start)
  except Exception as e:
    if bool(re.search('.*may refer to:.*',filter(lambda x: x in string.printable, str(e)))):
      deflist = "\n\nI found 3 most common meanings for you:\n\n"
      for idx, val in enumerate(filter(lambda x: x in string.printable, str(e)).split('may refer to: \n')[1].split('\n')):
	deflist = deflist + "\n\n>* " + wikipedia.summary(val,auto_suggest=False,sentences=1)
	if idx > 1:
	  break
      summary = "*Oh, there's too many of \""+term.strip()+"\".*\n\n---"+deflist+"\n\n---\n\nOtherwise, "+str(e).replace('\n','\n\n>')+"\n\n---\n\n[^(about)](http://) ^| *^(/u/"+post.author.name+" can reply with 'delete'. Will also delete if comment's score is -1 or less.)*  ^| ^[**Summon**](http://www.reddit.com/r/autowikibot/comments/1ux484/ask_wikibot/)"
      log("ASKING FOR DISAMBIGUATION")
    else:
      log("INTERPRETATION FAIL: %s"%filter(lambda x: x in string.printable, term))
      try:
	terms = "\""+term+"\""
	suggesttitle = str(wikipedia.search(terms,results=1)[0])
	log("SUGGESTING: %s"%filter(lambda x: x in string.printable, suggesttitle))
	if suggesttitle.lower() == term:
	  bit_comment_start = ""
	else:
	  bit_comment_start = "*Couldn't find Wikipedia article titled \"" + term.strip() + "\". Here\'s the closest match:*"
	if str(suggesttitle).endswith(')') and not re.search('\(',str(suggesttitle)):
	  suggesttitle = suggesttitle[0:--(suggesttitle.__len__()-1)]
	return (str(suggesttitle),bit_comment_start)
      except:
	trialtitle = wikipedia.page(term,auto_suggest=True).title
	if trialtitle.lower() == term:
	  bit_comment_start = ""
	else:
	  bit_comment_start = "*Couldn't find Wikipedia article titled \"" + term.strip() + "\". By long shot, here's the closest match:*"
	log("TRIAL SUGGESTION: %s"%filter(lambda x: x in string.printable, trialtitle))  
	if str(trialtitle).endswith(')') and not re.search('\(',str(trialtitle)):
	  trialtitle = trialtitle[0:--(trialtitle.__len__()-1)]
	return (str(trialtitle),bit_comment_start)
    post_reply(summary,post)
    return (False,False)
def process_summary_call(post):
  special("__________________________________________________")
  special("SUMMARY CALL: %s"%post.id)
  if re.search('wikibot.*?tell .*? about ',post.body.lower()):
    post_body = re.sub('wikibot.*?tell .*? about ','__BODYSPLIT__',post.body.lower())
  else:
    post_body = re.sub('wikibot.*?wh.*?(\'s|is a |is|are) ','__BODYSPLIT__',post.body.lower())
  term = post_body.split('__BODYSPLIT__')[1]
  term = re.sub('\?','\n',term)
  if term[0:2] == 'a ':
    term = term[2:term.__len__()]
  if term[0:4] == 'the ':
    term = term[4:term.__len__()]
  if term.endswith('.'):
    term = term[0:--(term.__len__()-1)]
  try:
    term = term.split('\n')[0]
  except:
    log("COULD NOT SPLIT")
    pass
  log("TERM: %s"%filter(lambda x: x in string.printable, term))
  if term.strip().__len__() < 2 or term == None:
    log("EMPTY TERM")
    return(False,False)
  try:
    title = wikipedia.page(term,auto_suggest=False).title
    if title.lower() == term:
      bit_comment_start = ""
    elif title.lower() != term:
      try:
	discard = wikipedia.page(term,auto_suggest=False,redirect=False).title
      except Exception as e:
	if re.search('resulted in a redirect',str(e)):
	  bit_comment_start = "*\"" + term.strip() + "\" redirects to* "
    else:
      bit_comment_start = "*No Wikipedia article exists with heading \"" + term.strip() + "\". Closest match is* "
    if re.search(r'#',title):
      url = wikipedia.page(title.split('#')[0],auto_suggest=False).url
      sectionurl =  url + "#" + title.split('#')[1]
      comment = "*Sorry, no Wikipedia article exists with the heading \"" + term.strip() + "\". But I found a relevant section ["+title.split('#')[1]+"]("+sectionurl.replace(')','\)')+") in article ["+title.split('#')[0]+"]("+url+") that might interest you.*\n\n---\n\n[^(about)](http://www.reddit.com/r/autowikibot/wiki/index) ^| *^(/u/"+post.author.name+" can reply with 'delete'. Will also delete if comment's score is -1 or less.)*  ^| ^(**To summon**: wikibot, what is something?)"
      post_reply(comment,post)
      log("RELEVANT SECTION SUGGESTED: %s"%filter(lambda x: x in string.printable, title))
      return (False,False)
    url_string = title
    log("INTERPRETATION: %s"%filter(lambda x: x in string.printable, title))
    return (url_string,bit_comment_start)
  except Exception as e:
    if bool(re.search('.*may refer to:.*',filter(lambda x: x in string.printable, str(e)))):
      deflist = "\n\nI found 3 most common meanings for you:\n\n"
      for idx, val in enumerate(filter(lambda x: x in string.printable, str(e)).split('may refer to: \n')[1].split('\n')):
	deflist = deflist + "\n\n>* " + wikipedia.summary(val,auto_suggest=True,sentences=1)
	if idx > 1:
	  break
      summary = "*Oh, there's too many of \""+term.strip()+"\".*\n\n---"+deflist+"\n\n---\n\nOtherwise, "+str(e).replace('\n','\n\n>')+"\n\n---\n\n[^(about)](http://www.reddit.com/r/autowikibot/wiki/index) ^| *^(/u/"+post.author.name+" can reply with 'delete'. Will also delete if comment's score is -1 or less.)*  ^| ^(**To summon**: wikibot, what is something?)"
      log("ASKING FOR DISAMBIGUATION")
    else:
      log("INTERPRETATION FAIL: %s"%filter(lambda x: x in string.printable, term))
      try:
	terms = "\""+term+"\""
	suggesttitle = str(wikipedia.search(terms,results=1)[0])
	log("SUGGESTING: %s"%filter(lambda x: x in string.printable, suggesttitle))
	if suggesttitle.lower() == term:
	  bit_comment_start = ""
	else:
	  bit_comment_start = "*Sorry, no Wikipedia article exists with the heading \"" + term.strip() + "\". Here\'s the closest match I could find for you from Wikipedia.*\n\n"
	if str(suggesttitle).endswith(')') and not re.search('\(',str(suggesttitle)):
	  suggesttitle = suggesttitle[0:--(suggesttitle.__len__()-1)]
	return (str(suggesttitle),bit_comment_start)
      except:
	trialtitle = wikipedia.page(term,auto_suggest=True).title
	if trialtitle.lower() == term:
	  bit_comment_start = ""
	else:
	  bit_comment_start = "*Sorry, no Wikipedia article exists with the heading \"" + term.strip() + "\". By long shot, the closest match I could find for you is this.*\n\n"
	log("TRIAL SUGGESTION: %s"%filter(lambda x: x in string.printable, trialtitle))  
	if str(trialtitle).endswith(')') and not re.search('\(',str(trialtitle)):
	  trialtitle = trialtitle[0:--(trialtitle.__len__()-1)]
	return (str(trialtitle),bit_comment_start)
    post_reply(summary,post)
    return (False,False)