Commit a092c1d8d5dbff407def2a0d452525d9830e3ae1

  • avatar
  • Krister Svanlund <adsum @BIG…SH.(none)>
  • Sat Jun 27 02:32:52 CEST 2009
KoFoo: Changed command handling to only deal with nickname and strip away host information right away.
MediaWiki: Added a mediawiki module for posting and getting info from a wiki.
ChangeLog
(6 / 0)
  
12009-06-27 Krister Svanlund <krister.svanlund@gmail.com>
2
3 * kofoo_mediawiki.py: Created a mediawiki module using the mwclient.
4 (add_to_page): Created a helper function for adding text in an arbitrary section
5 on a arbitrary page.
6
172009-06-26 Krister Svanlund <krister.svanlund@gmail.com>
28
39 * kofoo.py (KoFoo.load_module): Added traceback on failing to load module.
Todo
(4 / 1)
  
4444 - [X] Check if updated
4545 - [X] Store last update
4646 - [X] Add functions for adding or removing streams at runtime.
47* Wiki [0/1]
47* Wiki [0/2]
4848 - [ ] 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.
4952* URLGrabber [0/4]
5053 - [ ] Identify links in channel messages.
5154 - [ ] Filter links according to blacklist.
kofoo.py
(3 / 0)
  
549549 arg_list = arg_list[1:]
550550 sender_level = self.get_level(sender)
551551
552 ### Strip away everything but nick from sender. ###
553 sender = irclib.nm_to_n(sender)
554
552555 ### Check if the command is in the global bot alias list. ###
553556 if command_name in self.globals.bot_command_alias:
554557 ### If it is, then expand the alias and insert it into the list. ###
  
1# -*- coding: utf-8 -*-
2### A module using the MediaWiki API to add text to a wiki. ###
3### Python API: http://sourceforge.net/projects/mwclient/ ###
4### MediaWiki API: http://www.mediawiki.org/wiki/API ###
5
6import mwclient
7import re
8
9####
10#
11##
12
13mediawiki_settings = { 'wiki_url': "",
14 'wiki_basedir': "/w/",
15 'wiki_user': "",
16 'wiki_password': ""}
17
18mediawiki_requires = ['webhelpers']
19
20mediawiki_helpers = ['add_to_page']
21
22do_wiki_command = { 'description': "",
23 'long help': """\
24""",
25 'arguments': [],
26 'public': True,
27 'level': 0}
28
29def init_mediawiki(bot, server, sender = None):
30 global wiki_site
31 required_settings = [("Missing wiki url.", bot.settings.mediawiki_wiki_url),
32 ("Missing wiki bot-user.", bot.settings.mediawiki_wiki_user),
33 ("Missing wiki bot-user password.", bot.settings.mediawiki_wiki_password)]
34 missing_settings = []
35 fail = False
36 for error, setting in required_settings:
37 if not setting:
38 print " - %s" % error
39 if sender:
40 server.privmsg(sender, error)
41 fail = True
42 if fail:
43 return False
44 try:
45 wiki_site = mwclient.Site(bot.settings.mediawiki_wiki_url, bot.settings.mediawiki_wiki_basedir)
46 except:
47 print " - Failed to access the site. Could not connect to %s." % bot.settings.mediawiki_wiki_url
48 if sender:
49 server.privmsg(sender, "Failed to access the site. Could not connect to %s." % bot.settings.mediawiki_wiki_url)
50 return False
51 try:
52 wiki_site.login(bot.settings.mediawiki_wiki_user, bot.settings.mediawiki_wiki_password)
53 except Exception, e:
54 if type(e) is tuple:
55 result = e[1].get('result', '')
56 print " - Failed to login to site: %s" % result
57 else:
58 print " - Failed to login to site: %s" % str(e)
59 if sender:
60 server.privmsg(sender, "Failed to login to the site.")
61 return False
62 return True
63
64#def unload_mediawiki(bot, server, sender = None):
65# pass
66#
67#def update_mediawiki(bot, server):
68# pass
69
70def do_wiki(bot, server, sender, target, args):
71 add_to_page(sender, "Sandbox", "", u'\nDet här har lagts till med en bot.', "A summary", at_end=True)
72 #if args:
73 # pass
74 #else:
75 # bot.respond(server, sender, target, "Not enough arguments.")
76
77##
78#
79####
80
81exp_end_of_section = r'(\n\s*?=+\s*%s\s*=+\s*\n.*)(\n+\s*=+)'
82exp_start_of_section = r'(\n\s*?=+\s*%s\s*=+\s*)(\n.*?\n)'
83
84def add_to_page(sender, pagename, section, add_text, summary, at_end = True):
85 print "Add '%s' to page %s." % (add_text, pagename)
86 if pagename and add_text:
87 page = wiki_site.Pages[pagename]
88 text = page.edit()
89 new_text, subcount = re.subn((exp_start_of_section, exp_end_of_section)[at_end] % section, r'\1\n%s\2' % add_text, text)
90 if subcount == 0:
91 print "Failed to substitute."
92 if at_end:
93 new_text, subcount = re.subn(r'(\n+\s*=+)', r'\n%s\1' % add_text, text, 1)
94 if subcount > 0:
95 print "Before first title."
96 else:
97 if at_end:
98 new_text = text + "\n%s" % add_text
99 else:
100 new_text = "%s\n%s" % (add_text, text)
101 print "Added after text."
102 else:
103 new_text = "%s\n%s" % (add_text, text)
104 page.save(new_text, summary=summary+" [by %s]" % sender)
105 else:
106 print " - Not enough arguments."