Ugrás a tartalomhoz

Szerkesztő:Kisbesbot/aláíró

A Wikipédiából, a szabad enciklopédiából
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
import wikipedia
import difflib
import re
import time
import datetime

def seekbpos(str1, str2): 
        i = 0 
        while i < len(str1): 
                if str1[i] != str2[i]: 
                        return i 
                i += 1 
        return i 
	 
def seekepos(str1, str2, bpos): 
        i1 = len(str1) - 1 
        i2 = len(str2) - 1 
        while i1 > -1 and i2 > -1: 
                if i1 == bpos: 
                        return i2 
                elif i1 < bpos or str1[i1] != str2[i2]: 
                        return i2 + 1 
                i1 -= 1 
                i2 -= 1 
        return -1 

class SignerBot(SingleServerIRCBot):
    def __init__(self, channel, nickname, server, port=6667):
        SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
        self.channel = channel
	self.site = wikipedia.getSite()

    def on_nicknameinuse(self, c, e):
        c.nick(c.get_nickname() + "_")

    def on_welcome(self, c, e):
        c.join(self.channel)

    def on_privmsg(self, c, e):
	pass

    def on_pubmsg(self, c, e):
        msg = unicode(e.arguments()[0],'utf-8')
	if msg.find(u'archív')==-1 and msg.find(u'Archív')==-1 and msg.find(u'botmellőz')==-1:
		if msg.startswith(u'�14[[�07Wikipédia:Kocsmafal') or msg.startswith(u'�14[[�07Wikipédia:Adminisztrátorok üzenőfala') or msg.startswith(u'�14[[�07User vita:'):
			name = msg[8:msg.find(u'�14',9)]
			page = wikipedia.Page(self.site,name)
			text = page.get()
			oldver = page.getOldVersion(page.previousRevision())
			if len(text) > len(oldver):
				bpos = seekbpos(oldver, text) 
				epos = seekepos(oldver, text, bpos) 
				before = text[:bpos] 
				diff = text[bpos:epos] 
				after = text[epos:] 
				username = msg[msg.find(u'�03')+3:msg.find(u'�',msg.find(u'�03')+1)]
				match = re.search(ur'\d{4}\.\s(január|február|március|április|május|június|július|augusztus|szeptember|október|november|december)\s\d{1,2}\.,\s\d{2}\:\d{2}\s\(CES?T\)',diff)
				if not match:
					nline = u' {{Aláíratlan|' + username + u'|' + unicode(time.strftime(u'%Y. %B %d., %H:%M (CEST)'),'utf-8') + u'}}'
					page.put(before + diff + nline + after, comment=u'Aláíratlan hozzászólás sablon',watchArticle=None,minorEdit=True)

    def on_dccmsg(self, c, e):
	pass

    def on_dccchat(self, c, e):
	pass

    def do_command(self, e, cmd):
	pass

    def on_quit(self, e, cmd):
	pass

def main():
    bot = SignerBot("#hu.wikipedia", "signer", "irc.wikimedia.org", 6667)
    bot.start()

if __name__ == "__main__":
    main()