def reset(self): HTMLParser.HTMLParser.reset(self) self.__max_hit = 10 self.inner_div = False self.in_ul = False self.o = IrcStringIO("%s(%s):" % (self.package, self.branch)) self.li = 0 self.li_over = False self.after_br = False
def reset(self): HTMLParser.HTMLParser.reset(self) self.o = IrcStringIO("%s(%s):" % (self.file, self.version)) # s stands for stats, True means inside that tag # p stands for parent # c stands for children self.tag_structs = {'div': {'s': False, 'p': None, 'c': ('table',), 'id': 'pcontentsres'}, 'table': {'s': False, 'p': 'div', 'c': ('tr',)}, 'tr': {'s': False, 'p': 'table', 'c': ('td',)}, 'td': {'s': False, 'p': 'tr', 'c': ('span', 'a')}, 'span': {'s': False, 'p': 'td', 'c': None, 'class': 'keyword'}, 'a': {'s': False, 'p': 'td', 'c': None}} # first raw is table header <th> self.hit = -1 self.__max_hit = 10 self.in_file_td_head = False self.file_td_head = '' self.in_file_td_tail = False self.file_td_tail = '' self.file_keyword = ''
def handler(self, **args): """ gives a nick/host mask a privileve """ from irclib import Event, IrcStringIO import database if checkPriv(args["source"], "list_priv") == 0: return Event("privmsg", "", self.return_to_sender(args), [ "You don't have permission to do that." ]) privs = database.doSQL("SELECT priv_type,hostmask FROM grants ORDER BY priv_type,hostmask") buffer = IrcStringIO("Privileges List:") last = None for i in privs: if last == i[0]: buffer.write(' ' + i[1] + ' ;;') else: last = i[0] buffer.write(' [' + i[0] + '] ' + i[1] + ' ;;') return Event("privmsg", "", self.return_to_sender(args), [ buffer.getvalue() ])
def handler(self, **args): """ gives a nick/host mask a privileve """ from irclib import Event, IrcStringIO import database if checkPriv(args["source"], "list_priv") == 0: return Event("privmsg", "", self.return_to_sender(args), ["You don't have permission to do that."]) privs = database.doSQL( "SELECT priv_type,hostmask FROM grants ORDER BY priv_type,hostmask" ) buffer = IrcStringIO("Privileges List:") last = None for i in privs: if last == i[0]: buffer.write(' ' + i[1] + ' ;;') else: last = i[0] buffer.write(' [' + i[0] + '] ' + i[1] + ' ;;') return Event("privmsg", "", self.return_to_sender(args), [buffer.getvalue()])
def handler(self, **args): from irclib import Event import priv argv = self.spaces.split((args["text"]).strip()) # case: ~ab if len(argv) < 3: cmd = None argv = [] argc = 0 # case ~ab ... else: cmd = argv[2].lower() argv = argv[3:] argc = len(argv) reply = None channel = self.return_to_sender(args) from irclib import nm_to_n yournick = nm_to_n(args['source']) try: from irclib import is_channel from ValueModifier import IntValueModifier if not is_channel(channel): reply = "this is a channel only command" elif cmd == "grant": if argc != 3: raise StopReply() import priv nick, ability, score = argv try: score = IntValueModifier(score) except ValueError: reply = "%s: score must be number" % yournick raise StopReply() curscore = self.getUserAbilityScore(nick, channel, ability) or 0 score = score.apply(curscore) # normal users? if priv.checkPriv(args["source"], "ab_admin_priv") == 0 \ and priv.checkPriv(args["source"], "all_priv") == 0: if nick == yournick: reply = "You're so proud, %s" % yournick raise StopReply() yourscore = self.getUserAbilityScore( yournick, channel, ability) # apply rules if not yourscore or yourscore < score: yourscore = self.getUserAbilityScore( yournick, channel, '*') if not yourscore or yourscore < score: reply = "You don't have enough ability score, %s" % yournick raise StopReply() if curscore >= score: reply = "%s's %s ability is %d already" % ( nick, ability, curscore) raise StopReply() abilityid = self.getAbilityId(ability, True) self.setUserAbilityScore(nick, channel, abilityid, score, yournick) reply = "%s's %s ability is set to %d" % (nick, ability, score) elif cmd == "despise": if argc != 2: raise StopReply() ability, score = argv try: score = IntValueModifier(score) except ValueError: reply = "%s: score must be number" % yournick raise StopReply() curscore = self.getUserAbilityScore(yournick, channel, ability) if not curscore: reply = "%s: You have no %s ability despise for" % ( ability, yournick) raise StopReply() score = score.apply(curscore) if curscore == score: reply = "%s: You're yourself" % yournick elif curscore <= score: reply = "%s: Are you conceited?" % yournick else: abilityid = self.getAbilityId(ability, True) self.setUserAbilityScore(yournick, channel, abilityid, score, yournick) reply = "%s has despise him/herself to %d on %s ability" % ( yournick, score, ability) elif cmd == "top" or cmd == "help": if cmd == "help": if argc != 1: # module help raise StopReply() reply = "This function is in TODO" raise StopReply() from irclib import IrcStringIO if argc == 1: ability = argv[0] user_scores = self.getTopAbilityUsers(channel, ability) if not user_scores: reply = "no one is capable of %s" % ability raise StopReply() buffer = IrcStringIO("top user(s) that capable of %s:" % ability) for user_score in user_scores: buffer.write(" %s*%s" % user_score) elif argc == 0: abilityScoreUsers = self.getTopAbilities(channel) if not abilityScoreUsers: reply = "no one is superman" raise StopReply() from irclib import IrcStringIO buffer = IrcStringIO("top abilities:") for abilityScoreUser in abilityScoreUsers: buffer.write(" %s*%s/%s" % abilityScoreUser) reply = buffer.getvalue() else: nick = None ability = None name = None # name to guess if argc == 0: # quick query if cmd: name = cmd else: nick = yournick elif cmd != "query": raise StopReply() elif argc == 1: # "query $nick" name = argv[0] elif argc == 2: # "query $nick $ability" [nick, ability] = argv else: raise StopReply() # guess if it's nick or ability if name: abilityid = self.getAbilityId(name) if abilityid: ability = name nick = yournick else: nick = name ability = None if nick: if ability: score = self.getUserAbilityScore( nick, channel, ability) or 0 if yournick == nick: reply = "Your %s ability is %d" % (ability, score) if score < 0: reply = reply + ". rats!" else: reply = "%s's %s ability is %d" % (nick, ability, score) else: ability_scores = self.getUserAbilities(nick, channel) if not ability_scores: reply = nick + " doesn't have any ability, is he/she disabled?" else: from irclib import IrcStringIO buffer = IrcStringIO(nick + " is capable of") for ability_score in ability_scores: buffer.write(" %s*%s" % ability_score) reply = buffer.getvalue() else: reply = nick + " not found" except StopReply: pass if not reply: if cmd == "help" and argc == 0 or not self.helps.has_key(cmd): reply = "AbilityProfile: " + " ;; ".join(self.helps.values()) else: reply = "AbilityProfile: " + self.helps[cmd] result = Event("privmsg", "", channel, [reply]) return result
class weathercn(MooBotModule): """weather module to get weather forecast infomation This module depends on weather.py, it use weather.py to parse the html page of weathercn.com. Note, we must ignore the HTMLParser.HTMLParseError because of the malformed html pages. """ def __init__(self): """ >>> import re >>> from weblookup import weathercn >>> a = weathercn() >>> r = re.compile(a.regex) >>> r.match("w") and True or False True >>> r.match("weather") and True or False True >>> r.match("w 0335") and True or False True >>> r.match("weather 0335") and True or False True >>> r.match("wo03335") and True or False False >>> r.match("w 10086") and True or False True >>> r.match(u"w 通化") and True or False True >>> r.match(u"weather 通辽") and True or False True >>> r.match(u"w 北京") and True or False True >>> r.match(u"w 通化 8") and True or False True >>> r.match(u"weather 通辽 9") and True or False True >>> r.match(u"w 北京 37") and True or False True >>> r.match("who") and True or False False """ self.regex = "^(weather|w)($|( [^ ]+){1,2})" def handler(self, **args): """Parse the received commandline arguments case length of the real arguments: zero, check database to see if the user already have a record. y: use it to get the forcast and setting change tips; n: print help message. 1, get and print the citylist. 2, get the citylist, and then get the forecast accroding the second argument. print the result, save settings to database. and any any any better throughts ;) --save to control database operation? """ # TODO: ... save settings to database # TODO: ... get settings from database self.result = {'notice': '', 'msg': ''} tmp_args_list = args["text"].strip().split(" ") del(tmp_args_list[0]) lenlist = len(tmp_args_list) if lenlist in (2, 3): len1 = len(tmp_args_list[1]) if lenlist == 3\ and not tmp_args_list[2].isdigit(): self.result['notice'] = u"区域索引”n“必须是数字,"\ u"请重新输入" elif len1 < 2: self.result['notice'] = u"“城市名称”不可少于两个字符,"\ u"请重新输入" elif tmp_args_list[1].isdigit(): if len1 not in (3, 4, 6) or tmp_args_list[1][:2] == '00': self.result['notice'] = u"非法的区号或邮政编码,"\ u"请重新输入" elif len1 == 6 and tmp_args_list[1][3:] != '000': self.result['notice'] = u"请使用市级以上邮政编码,"\ u"TIP:将您的邮编后三位改为“000”" elif len1 in (3, 4) \ and tmp_args_list[1][0] != '0': self.result['notice'] = u"非法的电话区号, "\ u"请重新输入" else: self.gogetit(tmp_args_list) elif tmp_args_list[1].isalpha() and len1 > 4: self.result['notice'] = u"请给我一个“城市名或拼音缩写”多于"\ u" 4 个字符的理由" else: self.gogetit(tmp_args_list) else: self.result['notice'] = self._help() # print self.result['notice'].encode('utf8') # print self.result['msg'].encode('utf8') if self.result['notice'] and not self.result['msg']: target = self.return_to_sender(args, 'nick') return Event("notice", "", target, [self.result['notice']]) if self.result['msg']: target = self.return_to_sender(args) return Event("privmsg", "", target, [self.result['msg']]) def gogetit(self, l): """get back the citylist or recursively invoke getforecast get the citylist, setup notice message it if there is no 3rd arg. check length against the 3rd arg, invoke getforcast if valid. """ citykeyword = l[1].lower() search_parm = urllib.urlencode({"searchname": citykeyword.encode("gbk")}) print search_parm try: response = urllib.urlopen("http://www.weathercn.com/search/search.jsp", search_parm) except IOError, e: print e rp = weather.WeatherCNCITYParser() try: rp.feed(response.read().decode("gbk")) print response.read() except HTMLParser.HTMLParseError: pass response.close() regionlist = rp.o() if len(l) < 3: if len(regionlist) == 1: c, u = regionlist[0] self.getforcast(u) else: i = 1 result = IrcStringIO('%s: ' % citykeyword, 200) for c, u in regionlist: result.write("".join(("=", str(i),"=>", c))) i += 1 self.result['notice'] = result.getvalue() elif len(regionlist) < int(l[2]): self.result['notice'] = u"地区索引”n”大于地区总个数" else: c, u = regionlist[int(l[2])-1] self.getforcast(u)
def handler(self, **args): from irclib import Event import priv argv = self.spaces.split((args["text"]).strip()) # case: ~ab if len(argv) < 3: cmd = None argv = [] argc = 0 # case ~ab ... else: cmd = argv[2].lower() argv = argv[3:] argc = len(argv) reply = None channel = self.return_to_sender(args) from irclib import nm_to_n yournick = nm_to_n(args['source']) try: from irclib import is_channel from ValueModifier import IntValueModifier if not is_channel(channel): reply = "this is a channel only command" elif cmd == "grant": if argc != 3: raise StopReply() import priv nick, ability, score = argv try: score = IntValueModifier(score) except ValueError: reply = "%s: score must be number" % yournick raise StopReply() curscore = self.getUserAbilityScore(nick, channel, ability) or 0 score = score.apply(curscore) # normal users? if priv.checkPriv(args["source"], "ab_admin_priv") == 0 \ and priv.checkPriv(args["source"], "all_priv") == 0: if nick == yournick: reply = "You're so proud, %s" % yournick raise StopReply() yourscore = self.getUserAbilityScore(yournick, channel, ability) # apply rules if not yourscore or yourscore < score: yourscore = self.getUserAbilityScore(yournick, channel, '*') if not yourscore or yourscore < score: reply = "You don't have enough ability score, %s" % yournick raise StopReply() if curscore >= score: reply = "%s's %s ability is %d already" % (nick, ability, curscore) raise StopReply() abilityid = self.getAbilityId(ability, True) self.setUserAbilityScore(nick, channel, abilityid, score, yournick) reply = "%s's %s ability is set to %d" % (nick, ability, score) elif cmd == "despise": if argc != 2: raise StopReply() ability, score = argv try: score = IntValueModifier(score) except ValueError: reply = "%s: score must be number" % yournick raise StopReply() curscore = self.getUserAbilityScore(yournick, channel, ability) if not curscore: reply = "%s: You have no %s ability despise for" % (ability, yournick) raise StopReply() score = score.apply(curscore) if curscore == score: reply = "%s: You're yourself" % yournick elif curscore <= score: reply = "%s: Are you conceited?" % yournick else: abilityid = self.getAbilityId(ability, True) self.setUserAbilityScore(yournick, channel, abilityid, score, yournick) reply = "%s has despise him/herself to %d on %s ability" % (yournick, score, ability) elif cmd == "top" or cmd == "help": if cmd == "help": if argc != 1: # module help raise StopReply() reply = "This function is in TODO" raise StopReply() from irclib import IrcStringIO if argc == 1: ability = argv[0] user_scores = self.getTopAbilityUsers(channel, ability) if not user_scores: reply = "no one is capable of %s" % ability raise StopReply() buffer = IrcStringIO("top user(s) that capable of %s:" % ability) for user_score in user_scores: buffer.write(" %s*%s" % user_score) elif argc == 0: abilityScoreUsers = self.getTopAbilities(channel) if not abilityScoreUsers: reply = "no one is superman" raise StopReply() from irclib import IrcStringIO buffer = IrcStringIO("top abilities:") for abilityScoreUser in abilityScoreUsers: buffer.write(" %s*%s/%s" % abilityScoreUser) reply = buffer.getvalue() else: nick = None ability = None name = None # name to guess if argc == 0: # quick query if cmd: name = cmd else: nick = yournick elif cmd != "query": raise StopReply() elif argc == 1: # "query $nick" name = argv[0] elif argc == 2: # "query $nick $ability" [nick, ability] = argv else: raise StopReply() # guess if it's nick or ability if name: abilityid = self.getAbilityId(name) if abilityid: ability = name nick = yournick else: nick = name ability = None if nick: if ability: score = self.getUserAbilityScore(nick, channel, ability) or 0 if yournick == nick: reply = "Your %s ability is %d" % (ability, score) if score < 0: reply = reply + ". rats!" else: reply = "%s's %s ability is %d" % (nick, ability, score) else: ability_scores = self.getUserAbilities(nick, channel) if not ability_scores: reply = nick + " doesn't have any ability, is he/she disabled?" else: from irclib import IrcStringIO buffer = IrcStringIO(nick + " is capable of") for ability_score in ability_scores: buffer.write(" %s*%s" % ability_score) reply = buffer.getvalue() else: reply = nick + " not found" except StopReply: pass if not reply: if cmd == "help" and argc == 0 or not self.helps.has_key(cmd): reply = "AbilityProfile: " + " ;; ".join(self.helps.values()) else: reply = "AbilityProfile: " + self.helps[cmd] result = Event("privmsg", "", channel, [reply]) return result