Skip to content

Commit

Permalink
IMPORTANT: Renamed indexes of the messages arrays! Also: fixed encoding!
Browse files Browse the repository at this point in the history
Renamed the following indexes:
uid > userid
gid > groupid
cmduser > usercmd
cmdgroup > groupcmd

improoved print_message_data (for messages) regex.
and did some code cleanup.

Did I mention:
Fuck Yeah, got the f**k encoding working!!!
fixed '\' crashing bot.
fixed encoding like 'äöü'.

It's 3 am... 😭
  • Loading branch information
luckydonald committed Nov 27, 2014
1 parent c5915b6 commit 888ebf8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 97 deletions.
3 changes: 1 addition & 2 deletions examples/pingbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
command from a specified user id.
"""

import sys
from datetime import datetime, timedelta
import pytg
from pytg.utils import coroutine, broadcast
Expand Down Expand Up @@ -65,7 +64,7 @@ def command_parser(chat_group, tg):
# Keep on polling so that messages will pass through our pipeline
tg.poll()

if QUIT == True:
if QUIT:
break

# Quit gracefully
Expand Down
23 changes: 9 additions & 14 deletions pytg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ def register_callable(self, func, *args, **kwargs):

def start(self):
def preexec_function():
import signal
def on_abort(signal, stackframe):
print("SIGINT ignored!")
return False
# Ignore the SIGINT signal by setting the handler
# to the standard signal handler SIG_IGN.
# Does this even work ?!?
import signal
signal.signal(signal.SIGINT,on_abort)
proc = subprocess.Popen([self._tg, '-R', '-N', '-W', '-k', self._pub], stdin=subprocess.PIPE, stdout=subprocess.PIPE, preexec_fn = preexec_function)
self._proc, self.tgin = proc, proc.stdin
Expand All @@ -57,41 +58,35 @@ def poll(self):
raise TelegramError('telegram not running')
try:
c = self._proc.stdout.read(1) # allways is a byte string.
except TypeError as e:
except TypeError:
return # yeah. Nothing to do here,
if c is None:
return # yeah. Nothing to do here,

try:
if(self._debug_output_file):
if self._debug_output_file:
with open(self._debug_output_file, "ab") as text_file:
text_file.write(c)
except Exception as e:
except Exception:
raise # probably file io

if len(self._buffer_char) > 0: # already begun with an imcomplete character.
self._buffer_char += c # add this part to it.
try:
u_c = u(self._buffer_char)
print("\nU_C 1")
except UnicodeDecodeError: # added character to char buffer, but is no complete character.
self._buffer_char += c # add this part to it.
print("\nADD TO CHAR")
return # better luck next time.
else: # hey, it did work!
self._buffer_char = b('') # reset chraracter buffer
print("\nRESET CHAR")
else: # no incomplete character.
try:
u_c = u(c) #try to parse current char
print("\nU_C 2")
except UnicodeDecodeError: # is not a whole char.
assert (len(self._buffer_char) == 0)
self._buffer_char += c # begin with adding incomplete character parts
print("\nNEW CHAR")
self._buffer_char = c # begin with adding incomplete character parts
return # better luck/part next time.
else: #is normal complete char
pass # wooho?
print(u_c, end="", flush=True)
self._buffer = "".join([self._buffer,u_c])
if not self._banner_found and c == b'\n' and 'conditions' in self._buffer:
self._banner_found = True
Expand All @@ -100,11 +95,11 @@ def poll(self):
if self._banner_found and c == b'\n':
if '{print_message}' in self._buffer:
if '{end_print_message}' in self._buffer:
self._pipeline.send(self._buffer)
self._pipeline.send(n(self._buffer))
self._buffer = ''
else:
if self._buffer not in self._ignore:
self._pipeline.send(self._buffer)
self._pipeline.send(n(self._buffer))
self._buffer = ''

def quit(self):
Expand Down
15 changes: 2 additions & 13 deletions pytg/encoding.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import sys
from __future__ import unicode_literals
import codecs
import sys

if sys.version < '3': # python 2.7
text_type = unicode
Expand All @@ -28,17 +27,7 @@ def to_unicode(x):
#return codecs.unicode_escape_decode(x)[0]
if x == b'\\':
return "\\"
try:
return x.decode("utf-8")
except Exception as e:
print (x)
print (e)
try:
return x.decode("unicode_escape")
except Exception as e:
print (x)
print (e)
return u''
return x.decode("utf-8")
else:
return x

18 changes: 9 additions & 9 deletions pytg/regex.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# -*- coding: utf-8 -*-
import re

unread_user = re.compile("^User\ useri?d?\#(?P<user>[^#]+)\#?(?P<uid>\d+)?\:\ (?P<unread>\d+)\ unread", re.U)
unread_chat = re.compile("^Chat\ chati?d?\#(?P<group>[^#]+)\#?(?P<gid>\d+)?\:\ (?P<unread>\d+)\ unread", re.U)
chat_info_header = re.compile("^Chat\ chati?d?\#(?P<group>[^#]+)\#?(?P<gid>\d+)?\ members\:", re.U)
chat_info_body = re.compile("^\t?\t?useri?d?\#(?P<user>[^#]+)\#?(?P<uid>\d+)?\ invited\ by\ useri?d?\#(?P<iuser>[^#]+)\#?(?P<iuid>\d+)?\ at \[(?P<yr>\d{4})\/(?P<mth>\d{2})\/(?P<day>\d{2})\ (?P<hr>\d{2})\:(?P<min>\d{2})\:(?P<sec>\d{2})\]", re.U|re.M|re.S)
user_status_data = re.compile('^\[\d{2}\:\d{2}\]\ \ \{user_status\}\ User\ useri?d?\#(?P<user>[^#]+)\#?(?P<uid>\d+)?\ is\ now\ (?P<status>online|offline)', re.U)
user_info_header = re.compile("^User\ useri?d?\#(?P<user>[^#]+)\#?(?P<uid>\d+)?\:", re.U)
unread_user = re.compile("^User\ useri?d?\#(?P<user>[^#]+)\#?(?P<userid>\d+)?\:\ (?P<unread>\d+)\ unread", re.U)
unread_chat = re.compile("^Chat\ chati?d?\#(?P<group>[^#]+)\#?(?P<groupid>\d+)?\:\ (?P<unread>\d+)\ unread", re.U)
chat_info_header = re.compile("^Chat\ chati?d?\#(?P<group>[^#]+)\#?(?P<groupid>\d+)?\ members\:", re.U)
chat_info_body = re.compile("^\t?\t?useri?d?\#(?P<user>[^#]+)\#?(?P<userid>\d+)?\ invited\ by\ useri?d?\#(?P<iuser>[^#]+)\#?(?P<iuserid>\d+)?\ at \[(?P<yr>\d{4})\/(?P<mth>\d{2})\/(?P<day>\d{2})\ (?P<hr>\d{2})\:(?P<min>\d{2})\:(?P<sec>\d{2})\]", re.U|re.M|re.S)
user_status_data = re.compile('^\[\d{2}\:\d{2}\]\ \ \{user_status\}\ User\ useri?d?\#(?P<user>[^#]+)\#?(?P<userid>\d+)?\ is\ now\ (?P<status>online|offline)', re.U)
user_info_header = re.compile("^User\ useri?d?\#(?P<user>[^#]+)\#?(?P<userid>\d+)?\:", re.U)
user_info_peerid = re.compile("peer\ id\:\ (?P<peerid>\d+)", re.U)
user_info_realname = re.compile("real\ name\:\ (?P<realname>.+)", re.U)
user_info_phone = re.compile("phone\:\ (?P<phone>\d+)", re.U)
contact_list_data = re.compile("^User\ \#(?P<uid>\d+)\:\ user\#(?P<user>.+)\#\d+\ \((?P<cmduser>[^\ ]+)\ (?P<phone>\d+).+", re.U)
print_message_data = re.compile('^(?P<msgid>\d+)\ \[(?P<timestamp>.{5,6})\]\ \[?(?P<media>.+)?\]?\ ?(?P<geolink>https\:\/\/map\.google\.com\/\?=[\d\.\,])?\ \{print_message}\ (chati?d?\#(?P<chat>.+)\ )?useri?d?\#(?P<user>.+)\ (?P<dir>[<>«»]{3})\ (?P<message>.*)\{end_print_message\}', re.U|re.M|re.S)
contact_list_data = re.compile("^User\ \#(?P<userid>\d+)\:\ user\#(?P<user>.+)\#\d+\ \((?P<usercmd>[^\ ]+)\ (?P<phone>\d+).+", re.U)
print_message_data = re.compile('^(?P<msgid>\d+)\ \[(?P<timestamp>.{5,6})\]\ \[?(?P<media>.+)?\]?\ ?(?P<geolink>https\:\/\/map\.google\.com\/\?=[\d\.\,])?\ \{print_message}\ (chati?d?\#(?P<chat>.+)\#(?P<chatid>\d+)\ )?useri?d?\#(?P<user>.+)\#(?P<userid>\d+)\ (?P<dir>[<>«»]{3})\ (?P<message>.*)\{end_print_message\}', re.U|re.M|re.S)
print_message_data_media = re.compile('^\[(?P<media>photo|video|audio|document|photo|cideo|geo|contact|unsupported)(?:\ (?P<caption>.+?))?(?:\:\ type (?P<type>.+))?\]$', re.U)
service_message_data = re.compile("^(?P<msgid>\d+)\ \[(?P<timestamp>.+)\]\ \ ?\{service_message\}\ (chati?d?\#(?P<group>[^#]+)\#?(?P<gid>\d+)?\ )??useri?d?\#(?P<user>[^#]+)\#?(?P<uid>\d+)?\ (?P<action>changed\ title\ to|added\ user|deleted\ user)\ (?P<arg>.+)", re.U)
service_message_data = re.compile("^(?P<msgid>\d+)\ \[(?P<timestamp>.+)\]\ \ ?\{service_message\}\ (chati?d?\#(?P<group>[^#]+)\#?(?P<groupid>\d+)?\ )??useri?d?\#(?P<user>[^#]+)\#?(?P<userid>\d+)?\ (?P<action>changed\ title\ to|added\ user|deleted\ user)\ (?P<arg>.+)", re.U)
108 changes: 51 additions & 57 deletions pytg/tg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ def dialog_list(target):
continue
m = unread_user.search(clear_prompt(remove_color(line)).strip())
if m:
if m.group('uid'):
user, uid = m.group('user'), m.group('uid')
cmduser = user.replace(' ', '_')
if m.group('userid'):
user, userid = m.group('user'), m.group('userid')
usercmd = user.replace(' ', '_')
else:
user, cmduser, uid = None, None, m.group('user')
user, usercmd, userid = None, None, m.group('user')
unread = m.group('unread')
arg = {'type': 'dialog_list_user', 'uid': uid, 'user': user, 'cmduser': cmduser, 'unread': unread}
arg = {'type': 'dialog_list_user', 'userid': userid, 'user': user, 'usercmd': usercmd, 'unread': unread}
target.send(arg)
continue
m = unread_chat.search(clear_prompt(remove_color(line)).strip())
if m:
if m.group('gid'):
group, gid = m.group('group'), m.group('gid')
cmdgroup = group.replace(' ', '_')
if m.group('groupid'):
group, groupid = m.group('group'), m.group('groupid')
groupcmd = group.replace(' ', '_')
else:
group, cmdgroup, gid = None, None, m.group('group')
group, groupcmd, groupid = None, None, m.group('group')
unread = m.group('unread')
arg = {'type': 'dialog_list_group', 'gid': gid, 'group': group, 'cmdgroup': cmdgroup, 'unread': unread}
arg = {'type': 'dialog_list_group', 'groupid': groupid, 'group': group, 'groupcmd': groupcmd, 'unread': unread}
target.send(arg)
except GeneratorExit:
pass
Expand All @@ -58,34 +58,34 @@ def chat_info(target):
continue
m = chat_info_header.search(clear_prompt(remove_color(line)).strip())
if m:
if m.group('gid'):
group, gid = m.group('group'), m.group('gid')
cmdgroup = group.replace(' ', '_')
if m.group('groupid'):
group, groupid = m.group('group'), m.group('groupid')
groupcmd = group.replace(' ', '_')
else:
group, cmdgroup, gid = None, None, m.group('group')
group, groupcmd, groupid = None, None, m.group('group')
arg = {'type': 'chat_info', 'group': group,
'cmdgroup': cmdgroup, 'gid': gid}
'groupcmd': groupcmd, 'groupid': groupid}
target.send(arg)
continue
m = chat_info_body.search(clear_prompt(remove_color(line)).strip())
if m and len(arg) > 0:
if m.group('uid'):
user, uid = m.group('user'), m.group('uid')
cmduser = user.replace(' ', '_')
if m.group('userid'):
user, userid = m.group('user'), m.group('userid')
usercmd = user.replace(' ', '_')
else:
user, usercmd, uid = None, None, m.group('user')
if m.group('iuid'):
iuser, iuid = m.group('iuser'), m.group('iuid')
user, usercmd, userid = None, None, m.group('user')
if m.group('iuserid'):
iuser, iuserid = m.group('iuser'), m.group('iuserid')
cmdiuser = iuser.replace(' ', '_')
else:
iuser, cmdiuser, iuid = None, None, m.group('iuser')
iuser, cmdiuser, iuserid = None, None, m.group('iuser')
timestamp = datetime(int(m.group('yr')), int(m.group('mth')),
int(m.group('day')), int(m.group('hr')),
int(m.group('min')), int(m.group('sec')))
arg = {
'type': 'chat_info_member', 'group': group, 'gid': gid,
'user': user, 'cmduser': cmduser, 'uid': uid,
'iuser': iuser, 'cmdiuser': cmdiuser, 'iuid': uid,
'type': 'chat_info_member', 'group': group, 'groupid': groupid,
'user': user, 'usercmd': usercmd, 'userid': userid,
'iuser': iuser, 'cmdiuser': cmdiuser, 'iuserid': userid,
'timestamp': timestamp
}
target.send(arg)
Expand All @@ -111,12 +111,12 @@ def user_info(target):
continue
m = user_info_peerid.search(clear_prompt(remove_color(line)).strip())
if m and header_found:
arg = {'type': 'user_info', 'uid': m.group('peerid')}
arg = {'type': 'user_info', 'userid': m.group('peerid')}
continue
m = user_info_realname.search(clear_prompt(remove_color(line)).strip())
if m and header_found:
arg['user'] = m.group('realname')
arg['cmduser'] = arg['user'].replace(' ', '_')
arg['usercmd'] = arg['user'].replace(' ', '_')
continue
m = user_info_phone.search(clear_prompt(remove_color(line)).strip())
if m and header_found:
Expand All @@ -137,11 +137,11 @@ def user_status(target):
continue
m = user_status_data.search(clear_prompt(remove_color(line)).strip())
if m:
user, uid, status = m.group('user'), m.group('uid'), m.group('status')
if not uid:
uid, user = user, None
user, userid, status = m.group('user'), m.group('userid'), m.group('status')
if not userid:
userid, user = user, None
arg = {
'type': 'user_status', 'uid': uid, 'user': user, 'status': status
'type': 'user_status', 'userid': userid, 'user': user, 'status': status
}
target.send(arg)
except GeneratorExit:
Expand All @@ -159,8 +159,8 @@ def contact_list(target):
m = contact_list_data.search(clear_prompt(remove_color(line)).strip())
if m:
arg = {
'type': 'contact_list', 'uid': m.group('uid'),
'user': m.group('user'), 'cmduser': m.group('cmduser'),
'type': 'contact_list', 'userid': m.group('userid'),
'user': m.group('user'), 'usercmd': m.group('usercmd'),
'phone': m.group('phone')
}
target.send(arg)
Expand All @@ -176,26 +176,18 @@ def message(target):
line = (yield)
if '{print_message}' not in line or '{end_print_message}' not in line:
continue
m = print_message_data.search(clear_prompt(remove_color(line)).strip())
m = print_message_data.search(clear_prompt(remove_color(line)).strip()) # http://regex101.com/r/aZ1pU3/2
if m:
arg = {
'type': 'message', 'msgid': m.group('msgid'),
'timestamp': m.group('timestamp'),
'message': m.group('message'), 'media': None
}
tmpchat, tmpuser = m.group('chat'), m.group('user')
arg['peer'] = 'group' if tmpchat else 'user'
if tmpchat:
if '#' in tmpchat:
arg['group'], arg['gid'] = tmpchat.split('#')
arg['cmdgroup'] = arg['group'].replace(' ', '_')
else:
arg['group'], arg['cmdgroup'], arg['gid'] = None, None, tmpchat
if '#' in tmpuser:
arg['user'], arg['uid'] = tmpuser.split('#')
arg['cmduser'] = arg['user'].replace(' ', '_')
else:
arg['user'], arg['cmduser'], arg['uid'] = None, None, tmpuser
arg['peer'] = 'group' if (m.group('chatid')) else 'user'
arg['group'], arg['groupid'] = m.group('chat'),m.group('chatid')
arg['groupcmd'] = arg['group'].replace(' ', '_') if arg['group'] else None
arg['user'], arg['userid'] = m.group('user'), m.group('userid')
arg['usercmd'] = arg['user'].replace(' ', '_') if arg['user'] else None
# if arg['peer'] == 'user':
arg['ownmsg'] = True if m.group('dir') in ['«««','<<<'] else False
if m.group('media'):
Expand All @@ -222,6 +214,8 @@ def message(target):
elif 'geo' in m.group('media'):
arg['media'] = {'type': 'geo', 'link': m.group('geolink')}
target.send(arg)
else:
print("not accepted:>" + line)
except GeneratorExit:
pass

Expand All @@ -241,24 +235,24 @@ def service_message(target):
'timestamp': m.group('timestamp'),
'message': m.group('message'), 'action': m.group('action'),
}
if m.group('gid'):
arg['group'], arg['gid'] = m.group('group'), m.group('gid')
arg['cmdgroup'] = arg['group'].replace(' ', '_')
if m.group('groupid'):
arg['group'], arg['groupid'] = m.group('group'), m.group('groupid')
arg['groupcmd'] = arg['group'].replace(' ', '_')
else:
arg['group'], arg['cmdgroup'], arg['gid'] = None, None, m.group('group')
if m.group('uid'):
arg['user'], arg['uid'] = m.group('user'), m.group('uid')
arg['cmduser'] = arg['user'].replace(' ', '_')
arg['group'], arg['groupcmd'], arg['groupid'] = None, None, m.group('group')
if m.group('userid'):
arg['user'], arg['userid'] = m.group('user'), m.group('userid')
arg['usercmd'] = arg['user'].replace(' ', '_')
else:
arg['user'], arg['cmduser'], arg['uid'] = None, None, m.group('user')
arg['user'], arg['usercmd'], arg['userid'] = None, None, m.group('user')
if arg['action'] == 'changed title to':
arg['actionarg'] = m.group('arg')
else:
tmp = m.group('arg').split('#')
if len(tmp) == 2:
arg['actionarg'] = {'user': None, 'uid': tmp[1]}
arg['actionarg'] = {'user': None, 'userid': tmp[1]}
else:
arg['actionarg'] = {'user': tmp[1], 'uid': tmp[2]}
arg['actionarg'] = {'user': tmp[1], 'userid': tmp[2]}
target.send(arg)
except GeneratorExit:
pass
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-

from distutils.core import setup
from distutils.extension import Extension

long_description = """A Python module that wraps around Telegram messenger CLI, Version 2, forked and updated from https://github.com/efaisal/pytg"""

Expand All @@ -18,7 +17,7 @@
long_description = long_description,
url = 'https://bitbucket.org/luckydonald/pytg2',
cmdclass = {'build_py': build_py},
version = '0.2',
version = '0.2.1',
author = 'luckydonald (forked from E A Faisal)',
author_email = 'luckydonald@flutterb.at',
license = 'MIT',
Expand Down

0 comments on commit 888ebf8

Please sign in to comment.