#!/usr/bin/python # -*- coding: utf-8 -*- # --------------------------------------------------------------------------- # # # # Plugin for iSida Jabber Bot # # Copyright (C) diSabler # # Copyright (C) Vit@liy # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # # --------------------------------------------------------------------------- # filename_chars_limit = 48 last_url_watch = '' url_watch_ignore = ['pdf','sig','spl','class','ps','torrent','dvi','gz','pac','swf','tar','tgz','tar','zip','mp3','m3u','wma',\ 'wax','ogg','wav','gif','jar','jpg','jpeg','png','xbm','xpm','xwd','css','asc','c','cpp','log','conf','text',\ 'txt','dtd','xml','mpeg','mpg','mov','qt','avi','asf','asx','wmv','bz2','tbz','tar','so','dll','exe','bin',\ 'img','usbimg','rar','deb','rpm','iso','ico','apk','patch','svg','7z','tcl'] def rss_search(type, jid, nick, text): if text: if not re.findall('^http(s?)://',text[:10]): text = 'http://%s' % text text = enidna(text) msg, result = get_opener(text) if result: msg = L('Bad url or rss/atom not found!','%s/%s'%(jid,nick)) page = remove_sub_space(html_encode(load_page(text))) page = get_tag(page,'head') l = [] while ''~'] for r in str(tt)]))) body = '%s\n%s' % (text,'\n'.join(res)) if regex: try: mt = re.findall(regex, body, re.S|re.I|re.U) if mt != []: body = ''.join(mt[0]) else: body = L('RegExp not found!','%s/%s'%(jid,nick)) except: body = L('Error in RegExp!','%s/%s'%(jid,nick)) body = deidna(body) else: body = L('What?','%s/%s'%(jid,nick)) send_msg(type, jid, nick, body) def netwww(type, jid, nick, text): if text: try: regex = text.split('\n')[0].replace('*','*?') tmp = regex.split(' ', 1) if tmp[0].strip().isdigit(): n = int(tmp[0]) regex = tmp[1] else: n = 1 text = text.split('\n')[1] except: regex = None if not re.findall('^http(s?)://',text[:10]): text = 'http://%s' % text text = enidna(text) msg, result = get_opener(text) if result: page = remove_sub_space(html_encode(load_page(text))) if regex: try: mt = re.findall(regex, page, re.S|re.I|re.U) if mt != []: if n: msg = unhtml_hard('\n'.join([''.join(t) for t in mt[:n]])) else: msg = unhtml_hard('\n'.join([''.join(t) for t in mt])) else: msg = L('RegExp not found!','%s/%s'%(jid,nick)) except: msg = L('Error in RegExp!','%s/%s'%(jid,nick)) else: msg = urllib.unquote(unhtml_hard(page).encode('utf8')).decode('utf8', 'ignore') if '= len(text)/3: text = remove_sub_space(html_encode(get_tag(original_page,tag)).replace('\n',' ').replace('\r',' ').replace('\t',' ')) except: text = '' return text.strip() def parse_url_in_message(room,jid,nick,type,text): global last_url_watch if type != 'groupchat' or text == 'None' or nick == '' or getRoom(jid) == getRoom(selfjid): return if get_level(room,nick)[0] < 4: return content_title = None if get_config(getRoom(room),'store_users_url'): rjid = getRoom(jid) for t in text.split(): link = re.findall(r'(http[s]?://.*)',t) if link: link = link[0].split(' ')[0].split('"')[0].split('\'')[0] if not cur_execute_fetchone('select * from url where room=%s and jid=%s and url=%s',(room,rjid,link)): ttext = get_content_title(link) if ttext: ttext = to_censore(rss_del_html(rss_replace(ttext)),room) content_title = [link,ttext] else: is_file,ttext = False,'' ll = link.lower() for t in url_watch_ignore: if ll.endswith('.%s' % t): is_file = True break if is_file: body, result = get_opener(enidna(link)) if result: mt = float(body.headers.get('Content-Length',0)) if mt: ttext = L('Content length %s','%s/%s'%(jid,nick)) % get_size_human(mt) else: ttext = '' pprint('Store url: %s in %s/%s' % (link,room,nick),'white') cur_execute('insert into url values (%s,%s,%s,%s,%s,%s);',(room,rjid,nick,int(time.time()),link,ttext)) was_shown = False if get_config(getRoom(room),'url_title'): try: link = re.findall(r'(http[s]?://.*)',text)[0].split(' ')[0].split('"')[0].split('\'')[0] if link and last_url_watch != link and pasteurl not in link: if content_title and content_title[0] == link: ttext = content_title[1] else: ttext = get_content_title(link) if ttext: pprint('Show url-title: %s in %s' % (link,room),'white') was_shown = True send_msg(type, room, '', L('Title: %s','%s/%s'%(jid,nick)) % to_censore(rss_del_html(rss_replace(ttext)),room)) last_url_watch = link except: pass if not was_shown and get_config(getRoom(room),'content_length'): try: link = re.findall(u'(http[s]?://[-0-9a-zа-я.]+\/[-a-zа-я0-9._?#=@%/]+\.[a-z0-9]{2,7})',text,re.I+re.U+re.S)[0] if link and last_url_watch != link and pasteurl not in link: is_file = False ll = link.lower() for t in url_watch_ignore: if ll.endswith('.%s' % t): is_file = True break if is_file: last_url_watch = enidna(link) body, result = get_opener(last_url_watch) pprint('Show content length: %s in %s' % (link,room),'white') if result: mt = float(body.headers.get('Content-Length',0)) if mt: link_end = urllib2.unquote(last_url_watch.rsplit('/',1)[-1]).decode('utf-8') link_end = u'…%s%s' % (['/',''][len(link_end)>filename_chars_limit], link_end[-filename_chars_limit:]) send_msg(type, room, '', L('Length of %s is %s','%s/%s'%(jid,nick)) % (to_censore(link_end,room),get_size_human(mt))) except: pass global execute message_act_control = [parse_url_in_message] execute = [(3, 'www', netwww, 2, 'Show web page.\nwww [count (0 for all)] regexp\n[http://]url - page after regexp\nwww [http://]url - without html tags'), (3, 'header', netheader, 2, 'Show net header'), (3, 'isdown', www_isdown, 2, 'Check works site'), (4, 'rss_search', rss_search, 2, 'Search RSS/ATOM feeds')]