Commit 8ff4e068ec923155a32a450168b2c7c5bef2207a

  • avatar
  • Krister Svanlund <adsum @BIG…SH.(none)>
  • Sat Jul 04 01:22:05 CEST 2009
MediaWiki: Added som proper in-bot documentation.
           Added a max variable to 'get url' command.
Set: Removed 'set quit' command description.
Help: Added 'level=<maxlevel>' functionality to help command.
      Fixed alot of output to be more dynamic and generally prettier.
      Added 'help commands' for listing all commands.
ChangeLog
(13 / 0)
  
12009-07-04 Krister Svanlund <krister.svanlund@gmail.com>
2
3 * kofoo_set.py: Removed 'set quit' from help description since the command has
4 been removed.
5
6 * kofoo_help.py (do_help): Added 'level=' argument for help command to show help
7 for levels below the users.
8 (do_help_for): Changed output format to be prettier.
9 (do_help_for): Added the 'help commands' command for only listing availible commands.
10 (do_general_help): Changed output to fit indention to longest command+arguments
11 string.
12
1132009-07-03 Krister Svanlund <krister.svanlund@gmail.com>
214
315 * kofoo_mediawiki.py (mw_get_url): Added function for getting the URL to a
416 specific wiki-article.
517 (do_wiki): Added search functionality for getting links to articles.
18 Completed help information for the module.
619
7202009-07-02 Krister Svanlund <krister.svanlund@gmail.com>
821
Todo
(10 / 7)
  
4444 - [X] Check if updated
4545 - [X] Store last update
4646 - [X] Add functions for adding or removing streams at runtime.
47* Wiki [0/4]
48 - [ ] Check for changes (This can be done with RSS module)
49 - [ ] Post to wikipage.
50 - [ ] Create page if not existing.
51 - [ ] Add to correct subsection.
52 - [ ] Search for page and generate a link
53 - [ ] Add things to list
47* Wiki [4/4]
48 - [X] Check for changes (This can be done with RSS module)
49 - [X] Post to wikipage.
50 - [X] Create page if not existing.
51 - [X] Add to correct subsection.
52 - [X] Add to list.
53 - [X] Add to table.
54 - [X] Create a table.
55 - [X] Search for page
56 - [X] Generate a links to pages
5457* URLGrabber [0/4]
5558 - [ ] Identify links in channel messages.
5659 - [ ] Filter links according to blacklist.
kofoo_help.py
(63 / 16)
  
44### Krister Svanlund <krister.svanlund gmail.com> ###
55
66import irclib
7import re
78
89do_help_command = {'description': "Display this help or help for specific commands",
910 'arguments': [("", "Display general help and list commands availible to your level."),
1011 ("<command>", "Display help for a specific command."),
12 ("commands", "Display a list of commands without description."),
1113 ("alias", "List all aliases currently inplace."),
1214 ("modules", "List all modules that are loaded and that can be loaded.")],
1315 'public': False,
1818def do_help(self, server, sender, target, args):
1919 print "Show help to %s." % irclib.nm_to_n(sender)
2020 sender_level = self.get_level(sender)
21 if len(args) > 0:
21 if args:
2222 for command in args:
23 m = re.match(r'level=([0-9]+)', command)
24 if m:
25 args.remove(m.group(0))
26 try: sender_level = min(int(m.group(1)), sender_level)
27 except: sender_level = 0
28 print " + Showing help for level %d." % sender_level
29 if args:
30 for command in args:
2331 do_help_for(self, server, sender, target, command, sender_level)
2432 else:
2533 do_general_help(self, server, sender, target, sender_level)
4444 if sender_level < command_level:
4545 self.respond(server, sender, target, "You are not allowed to use this command.")
4646 return
47 if command_description == "":
48 self.respond(server, sender, target, "There is no help for this command.")
49 return
5047 if not type(command_arguments) is list:
5148 command_arguments = [command_arguments]
5249 if len(command_arguments) > 0:
50 width = []
5351 for argument in command_arguments:
52 if type(argument) is not tuple:
53 width.append(len(argument))
54 else:
55 width.append(len(argument[0]))
56 if width: width = max(width)
57 else: width = 0
58 for argument in command_arguments:
5459 if type(argument) is tuple:
55 help_string += " \x02Syntax:\x02 %s %s - %s\n" % (command_name, argument[0], argument[1])
60 help_string += " \x02Syntax:\x02 {0:{2}} - {1}\n" % (command_name+' '+argument[0], argument[1], width)
61 #help_string += " \x02Syntax:\x02 %s %s - %s\n" % (command_name, argument[0], argument[1])
5662 else:
57 help_string += " \x02Syntax:\x02 %s %s\n" % (command_name, argument)
63 help_string += " \x02Syntax:\x02 {0} {1}\n" % (command_name, argument)
64 #help_string += " \x02Syntax:\x02 %s %s\n" % (command_name, argument)
5865 else:
5966 help_string += " \x02Syntax:\x02 %s\n" % command_name
60 help_string += " \x02Description:\x02 %s\n" % command_description
61 help_string += " \x02Minimum level:\x02 %d\n" % command_level
67 if command_description:
68 help_string += " \x02Description:\x02 %s\n" % command_description
69 if command_level:
70 help_string += " \x02Minimum level:\x02 %d\n" % command_level
6271 self.respond(server, sender, target, help_string)
6372 elif command_name == 'alias':
6473 help_string = "Aliases defined at this point is\n"
7979 help_string = "The following modules are currently loaded: %s" % ", ".join(self.module_list)
8080 help_string += "\nThe following modules can be loaded: %s" % ", ".join([m for m in self.found_modules if m not in self.module_list])
8181 self.respond(server, sender, target, help_string)
82 elif command_name == 'commands':
83 commands = []
84 for command_name, command in self.globals.bot_commands.iteritems():
85 command_level = command.get('level', 0)
86 if sender_level >= command_level:
87 commands.append(command_name)
88 help_string = "The following commands are available: %s" % ", ".join(commands)
89 self.respond(server, sender, target, help_string)
8290 else:
8391 self.respond(server, sender, target, "There is no \x02%s\x02 command." % command_name)
92
93def get_first(tos):
94 if type(tos) is tuple or type(tos) is list:
95 return tos[0]
96 else:
97 return tos
8498
8599def do_general_help(self, server, sender, target, sender_level):
86100 help_string = "The commands availible are:\n"
101 width = []
102 print " + Figure out width."
103 for cn, c in self.globals.bot_commands.iteritems():
104 alw = []
105 al = c.get('arguments', ())
106 if type(al) is not list: al = [al]
107 for a in al:
108 if a:
109 if type(a) is not tuple: a = (a, "")
110 alw.append(len(a[0]))
111 if alw: width.append(len(cn)+max(alw)+3)
112 else: width.append(len(cn))
113 if width: width = max(width)
114 else: width = 0
115 print " + Width is %d." % width
116
87117 for command_name, command in self.globals.bot_commands.iteritems():
88118 command_arguments = command.get('arguments', "")
89119 if not type(command_arguments) is list:
134134 # | | |-Arguments for command
135135 # | |-Command name
136136 # |-Public command indicator
137 help_string += "%(public)c\x02%(name)s\x02 %(argument)s %(space)s %(description)s %(space2)s %(level)s\n" % \
138 {'public': (' ', '*')[command_public], # If command_public is False use first value in tuple.
139 'name': command_name,
140 'argument': argument,
141 'space': " "*(30-(len(command_name)+len(argument))),
142 'description': command_description,
143 'space2': " "*(50-(len(command_description))),
144 'level': ('(>'+str(command_level)+')', '')[command_level is 0]}
137 help_string += "{public}{namearg:{width}} - {description} {level}\n".format(\
138 width = width,
139 public = (' ', '*')[command_public],
140 namearg = "\x02{0}\x02 {1}".format(command_name, argument),
141 description = command_description,
142 level = ('(>'+str(command_level)+')', '')[command_level is 0])
145143 help_string += "To see help for specific commands use \x02help command\x02."
146144 self.respond(server, sender, target, help_string)
  
2323mediawiki_requires = ['webhelpers']
2424
2525mediawiki_helpers = ['mw_add_to_page',
26 'mw_get_url',
2627 'mw_get_table',
2728 'mw_get_list',
28 'mw_add_to_list',
29 'mw_add_section',
3029 'mw_get_section_level',
3130 'mw_get_section',
31 'mw_add_table',
32 'mw_add_to_table',
33 'mw_add_to_list',
34 'mw_add_section',
3235 'mw_rewrite_wiki_links']
3336
3437do_wiki_command = { 'description': "Do things to a wiki",
3538 'long help': """\
36""",
37 'arguments': [("on <pagename> [in <sectionname>] add <Text to add>",
38 "Add text to a specific page on the wiki at the bottom of a specific section"),
39 ("add section <newsection> to <pagename> [below <parentsection>]",
40 "Add a section to a page")],
39This is an wide command to do anything the mediawiki module can do with wikipages.
40Things to take note of is that the 'add to page' and 'add section' command does not add
41text to the top of the page if at top but instead add it at the bottom of the first
42section.""",
43 'arguments': [("get table|list|section \"<itemname>\" on \"<pagename>\"", "Get a named item from a page on the wiki"),
44 ("get url \"<pagename>\"", "Get URL to a specific page"),
45 ("search \"<term>\" [max: <max results>]", "Search titels in wiki for articles"),
46 ("add section \"<sectionname>\" [level <level>|below <parentsection>] on \"<pagename>\" [at top]", "Create a section on a page."),
47 ("add table \"<tablename>\" in \"<parentsection>\" on \"<pagename>\": \"<col1>\"[, \"<col2>\"[..]] [at top]", "Add a table to a section"),
48 ("add to list \"<listname>\" on \"<pagename>\": \"<item>\" [at top]", "Add an item to a list"),
49 ("add to table \"<tablename>\" on \"<pagename>\": \"<col1>\"[, \"<col2>\"[..]] [at top]", "Add a row to a table"),
50 ("add to section \"<sectionname>\" on \"<pagename>\": \"<new text>\" [at top]", "Add text to a section"),
51 ("add to page \"<pagename>\": \"<new text>\" [at top]", "Add text to a page")],
4152 'public': True,
4253 'level': 70}
4354
145145 bot.respond(server, sender, target, "\x02%s\x02 is not a valid argument or not enough arguments." % args[1])
146146 return False
147147 elif args[0] == 'search' and args[1:]:
148 m = re.match(r'([\"\']?)(.*?)\1', ' '.join(args[1:]))
148 m = re.match(r'([\"\']?)(.*?)\1(\smax: |)([0-9]+|)', ' '.join(args[1:]))
149149 if m and m.group(2).strip():
150150 search_terms = m.group(2)
151 has_max = len(m.group(3)) > 0
152 if has_max:
153 try:
154 result_max = max(int(m.group(4)), 1)
155 except Exception, e:
156 print " - Could not convert '%s' to integer." % m.group(4)
157 result_max = 1
158 else:
159 result_max = 1
151160 if type(search_terms) is not unicode:
152161 if '\xc3' in search_terms:
153162 search_terms = search_terms.decode("utf-8")
165165 try:
166166 bot.respond(server, sender, target, "\x02Search results:\x02")
167167 results = [r['title'] for r in wiki_site.search(search_terms)]
168 if not results:
169 print " - No results."
170 bot.respond(server, sender, target, "No results.")
171 return True
168172 width = max([len(r.encode("latin-1")) for r in results])
169 for result in results:
173 for result in results[:result_max]:
170174 url = mw_get_url(result)
171175 if url:
172176 res_string = " {0:{1}} : {2}".format(result.encode("latin-1"), width, url)
  
1111 'long help': """\
1212Set values for configurations such as user access level.""",
1313 'arguments':
14 [("access <user> <newlevel>", "Set the new accesslevel for <user>."),
15 ("quit [quit message]", "Set a quit message.")],
14 [("access <user> <newlevel>", "Set the new accesslevel for <user>.")],
1615 'public': False,
1716 'level': 50}
1817