def regUser(self, jid, login, priority = 50, protocol = 'xmpp', force = None): try: self.db.regUser(jid = jid, login = login, priority = priority, protocol = protocol, force = force) return self.getText('MSG3') % login except dmbErrorRepeat as exc: log.debug('%s, %s' % (self.getText(exc.getText(), 'en'), jid)) raise dmbErrorRepeat('MSG1')
def adminParsing(service, login, text, jid = None): try: command, args = text.split(' ', 1) except ValueError: command, args = text.split(' ', 1)[0], None log.debug('%s %s' % (command, args)) if command == 'exit': sys.exit() elif command == 'registry': new_server = args.split(' ')[0] dmb_servers = service.addServer(new_server) service.queue_to_send.append({'jid': (new_server,), 'message': 'registry', 'send': True, 'extra': {'dmb': 'server'}}) elif command == 'add_server': try: dmb_servers = service.addServer(args.split()[0]) return 'ok' except dmbErrorRepeat: return 'already exists' elif command == 'del_server': try: dmb_servers = service.delServer(args.split()[0]) return 'ok' except dmbErrorNotFound: return 'not found' elif command == '*': return parsing(service, '*', ' '.join(args), jid) else: return parsing(service, login, text, jid)
def delAlias(self, login, alias): try: self.db.delAlias(login = login, alias = alias) return self.getText('MSG21') % alias except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG10') raise exc
def delRegexp(self, login, name): try: self.db.delRegexp(login=login, name=name) return self.getText('MSG23') % name except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG11') raise exc
def delUserList(self, login, list_name): try: self.db.delUserList(login, list_name) return self.getText('MSG17') % list_name except dmbErrorRepeat as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG8') raise exc
def delPost(self, login, post): try: self.db.delPost(login = login, post = post) return self.getText('MSG7') % self.numCoding(int(post), self.getUserParams(login)['num_type']) except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG1') raise exc
def addRegexp(self, login, name, regexp, command): try: self.db.addRegexp(login = login, name = name, regexp = regexp, command = command) return self.getText('MSG22') % name except dmbErrorRepeat as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG11') raise exc
def delFromUserList(self, login, list_name, user, tag): try: self.db.delFromUserList(login = login, list_name = list_name, user = user, tag = tag) return self.getText('MSG16') % list_name except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG7') raise exc
def delAlias(self, login, alias): try: self.db.delAlias(login=login, alias=alias) return self.getText('MSG21') % alias except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG10') raise exc
def delRegexp(self, login, name): try: self.db.delRegexp(login = login, name = name) return self.getText('MSG23') % name except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG11') raise exc
def show(self, count = 10, login = None, post = None, comment = None, tag = None, user = None, id_recommend = None, show_high_mesg = None, error_mesg = 1): try: if post: mess_type = self.getText('ERRMSG1') messages = self.db.show(post = post, comment = comment) elif user: mess_type = self.getText('ERRMSG2') messages = self.db.show(user = user) elif tag: mess_type = self.getText('ERRMSG3') messages = self.db.show(count = count, tag = tag, login = login) else: mess_type = self.getText('ERRMSG1') messages = self.db.show(count = count, login = login) if login: params = self.getUserParams(login) time_zone = params['time_zone'] else: params = self.getUserParams('anonymous') time_zone = 0 result = '' if id_recommend: recommend = self.db.getRecommend(id_recommend) if recommend: if recommend.has_key('message') and recommend['message']: message = '- %s' % recommend['message'] else: message = self.getText('PS1') result += '%s %s:\n' % (recommend['login'], message) for message in messages: if message[0] == const_post and not comment: message = message[1] tagStr = '' for t in message['tags']: if not tagStr.count('*%s ' % t): tagStr += '*%s ' % t if len(tagStr) > 0: tagStr += '\n' if not post and len(message['message']) > 200: message['message'] = '%s [...]' % message['message'][:200] result += '%s: %s\n%s%s\n#%s (%i %s, %s)\n\n' % (message['login'], '+' * message['count_recom'], tagStr, message['message'], self.numCoding(int(message['post']), params['num_type']), message['count_comments'], self.getText('PS2'), self.getStrTime(message['timestamp'], time_zone = time_zone)) elif message[0] == const_comment: message = message[1] mesgAdd = mesgHigh = '' if message['high_comment']: mesgAdd = ' %s /%s' % (self.getText('PS3'), self.numCoding(int(message['high_comment']), params['num_type'])) if show_high_mesg: mesgHigh = '>%s\n' % message['high_message'] result += '%s: %s\n%s%s\n#%s/%s (%s)%s\n\n' % (message['login'], '+' * message['count_recom'], mesgHigh, message['message'], self.numCoding(int(message['post']), params['num_type']) , self.numCoding(message['comment'], params['num_type']), self.getStrTime(message['timestamp'], time_zone = time_zone), mesgAdd) elif message[0] == const_user: message = message[1] result += '%s - %s\n\n' % (message['login'], message['jid']) return result except dmbErrorNotFound as exc: log.debug('%s, %s' % (self.getText(exc.getText(), 'en'), mess_type)) exc.message = mess_type raise exc
def delPost(self, login, post): try: self.db.delPost(login=login, post=post) return self.getText('MSG7') % self.numCoding( int(post), self.getUserParams(login)['num_type']) except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG1') raise exc
def delSubscribe(self, login, post = None, tag = None, user = None): try: self.db.unsubscribe(login = login, post = post, tag = tag, user = user) if user: self.queue_to_send.append({'jid': self.getJid(user), 'login': user, 'message': self.getText('MSG27', self.getUserParams(user)['locale']) % login}) return self.getText('MSG12') except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG5') raise exc
def regUser(self, jid, login, priority=50, protocol='xmpp', force=None): try: self.db.regUser(jid=jid, login=login, priority=priority, protocol=protocol, force=force) return self.getText('MSG3') % login except dmbErrorRepeat as exc: log.debug('%s, %s' % (self.getText(exc.getText(), 'en'), jid)) raise dmbErrorRepeat('MSG1')
def addRegexp(self, login, name, regexp, command): try: self.db.addRegexp(login=login, name=name, regexp=regexp, command=command) return self.getText('MSG22') % name except dmbErrorRepeat as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG11') raise exc
def delFromUserList(self, login, list_name, user, tag): try: self.db.delFromUserList(login=login, list_name=list_name, user=user, tag=tag) return self.getText('MSG16') % list_name except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG7') raise exc
def addToUserList(self, login, list_name, user = None, tag = None): try: self.db.addToUserList(login = login, list_name = list_name, user = user, tag = tag) if list_name == 'black': try: self.delSubscribe(login, user = user, tag = tag) except:pass return self.getText('MSG15') % list_name except (dmbErrorRepeat, dmbErrorNotFound) as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG7') raise exc
def addToPost(self, login, post, message = None, tags = []): try: result = self.db.addToPost(login = login, post = post, message = message, tags = tags) try: self.db.subscribe(login, post = result) except:pass self.addToQueueSend(user = login, post = result, tag = tags) return self.getText('MSG6') % self.numCoding(int(result), self.getUserParams(login)['num_type']) except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG1') raise exc
def addComment(self, login, message, post, comment = None): try: result = self.db.comment(login = login, message = message, post = post, comment = comment) try: self.db.subscribe(login, post = post) except:pass self.addToQueueSend(user = login, post = post, comment = result) num_type = self.getUserParams(login)['num_type'] return self.getText('MSG8') % (self.numCoding(int(post), num_type), self.numCoding(int(result), num_type)) except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG1') raise exc
def addRecommend(self, login, message = None, post = None, comment = None): try: result = self.db.recommend(login = login, message = message, post = post, comment = comment) if post: try: self.db.subscribe(login, post = post) except:pass self.addToQueueSend(user = login, post = post, comment = comment, id_recommend = result) return self.getText('MSG13') except (dmbErrorRepeat, dmbErrorNotFound) as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG6') raise exc
def addSubscribe(self, login, post = None, tag = None, user = None): try: if login == user: return self.getText('MSG10') self.db.subscribe(login = login, post = post, tag = tag, user = user) self.delFromUserList(login = login, list_name = 'black', user = user, tag = tag) if user: self.queue_to_send.append({'jid': self.getJid(user), 'login': user, 'message': self.getText('MSG26', self.getUserParams(user)['locale']) % login}) return self.getText('MSG11') except (dmbErrorRepeat, dmbErrorNotFound) as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG5') raise exc
def getUserList(self, login, list_name = None, to_print = None): try: lists = self.db.getUserList(login = login, list_name = list_name) if to_print: if list_name: result = '' for k, v in lists[list_name].iteritems(): result += '%s: %s\n' % (k, ', '.join(v)) return result else: return self.getText('MSG14') % ', '.join(lists.keys()) else: return lists except dmbError as exc: log.debug(self.getText(exc.getText(), 'en')) raise exc
def addToUserList(self, login, list_name, user=None, tag=None): try: self.db.addToUserList(login=login, list_name=list_name, user=user, tag=tag) if list_name == 'black': try: self.delSubscribe(login, user=user, tag=tag) except: pass return self.getText('MSG15') % list_name except (dmbErrorRepeat, dmbErrorNotFound) as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG7') raise exc
def getUserList(self, login, list_name=None, to_print=None): try: lists = self.db.getUserList(login=login, list_name=list_name) if to_print: if list_name: result = '' for k, v in lists[list_name].iteritems(): result += '%s: %s\n' % (k, ', '.join(v)) return result else: return self.getText('MSG14') % ', '.join(lists.keys()) else: return lists except dmbError as exc: log.debug(self.getText(exc.getText(), 'en')) raise exc
def delSubscribe(self, login, post=None, tag=None, user=None): try: self.db.unsubscribe(login=login, post=post, tag=tag, user=user) if user: self.queue_to_send.append({ 'jid': self.getJid(user), 'login': user, 'message': self.getText('MSG27', self.getUserParams(user)['locale']) % login }) return self.getText('MSG12') except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG5') raise exc
def addToPost(self, login, post, message=None, tags=[]): try: result = self.db.addToPost(login=login, post=post, message=message, tags=tags) try: self.db.subscribe(login, post=result) except: pass self.addToQueueSend(user=login, post=result, tag=tags) return self.getText('MSG6') % self.numCoding( int(result), self.getUserParams(login)['num_type']) except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG1') raise exc
def addComment(self, login, message, post, comment=None): try: result = self.db.comment(login=login, message=message, post=post, comment=comment) try: self.db.subscribe(login, post=post) except: pass self.addToQueueSend(user=login, post=post, comment=result) num_type = self.getUserParams(login)['num_type'] return self.getText('MSG8') % (self.numCoding( int(post), num_type), self.numCoding(int(result), num_type)) except dmbErrorNotFound as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG1') raise exc
def addRecommend(self, login, message=None, post=None, comment=None): try: result = self.db.recommend(login=login, message=message, post=post, comment=comment) if post: try: self.db.subscribe(login, post=post) except: pass self.addToQueueSend(user=login, post=post, comment=comment, id_recommend=result) return self.getText('MSG13') except (dmbErrorRepeat, dmbErrorNotFound) as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG6') raise exc
def addSubscribe(self, login, post=None, tag=None, user=None): try: if login == user: return self.getText('MSG10') self.db.subscribe(login=login, post=post, tag=tag, user=user) self.delFromUserList(login=login, list_name='black', user=user, tag=tag) if user: self.queue_to_send.append({ 'jid': self.getJid(user), 'login': user, 'message': self.getText('MSG26', self.getUserParams(user)['locale']) % login }) return self.getText('MSG11') except (dmbErrorRepeat, dmbErrorNotFound) as exc: log.debug(self.getText(exc.getText(), 'en')) exc.message = self.getText('ERRMSG5') raise exc
def parsing(service, login, text, jid = None): try: command, args = text.split(' ')[0].lower(), text.split(' ')[1:] if not (command in command_list): alias = service.getAlias(login = login) if alias.has_key(command): command, args = alias.get(command).split(' ')[0], alias.get(command).split(' ')[1:] + args else: regexp = service.getRegexp(login = login) go_exit = 1 for r in regexp: m = re.compile(r['regexp'], re.U).match(text) if m: result = r['command'] noname = '' for k, v in m.groupdict().iteritems(): if k!='noname' and v: result += ' --%s=%s' % (k, v) elif v: noname = ' %s' % v result += noname command, args = result.split(' ')[0], result.split(' ')[1:] go_exit = 0 break if go_exit: raise dmbErrorParsing log.debug('%s %s' % (command, args)) if login == 'anonymous' and not command in ('show', 'post', 'comment', 'register'): return service.getText('MSG70') if command == 'show': user = post = comment = tag = None count = 10 opts, args = getopt(args, 'u:p:t:', ['user='******'post=', 'tags=']) for o, a in opts: if o in ('-u', '--user'): user = a break elif o in ('-p', '--post'): post = a break elif o in ('-t', '--tags'): tag = a break for a in args: if a.isdigit(): count = int(a) break if post: post, comment = service.normilizeID(post) return service.show(login = login, count = count, post = post, comment = comment, tag = tag, user = user) elif command == 'post': post = message = None tags = [] opts, args = getopt(args, 'p:t:a', ['post=', 'tags=', 'anonymous']) for o, a in opts: if o in ('-p', '--post'): post = a elif o in ('-t', '--tags'): tags = a.split(',') elif o in ('-a', '--anonymous'): login = '******' if tags.count(''): tags.remove('') message = ' '.join(args) if post: return service.addToPost(login = login, post = int(post), message = message, tags = tags) else: return service.addPost(login = login, message = message, tags = tags) elif command == 'comment': post = comment = None opts, args = getopt(args, 'ap:', ['post=', 'anonymous']) for o, a in opts: if o in ('-p', '--post'): post = a elif o in ('-a', '--anonymous'): login = '******' if post: post, comment = service.normilizeID(post) message = ' '.join(args) return service.addComment(login = login, post = post, comment = comment, message = message) else: raise dmbErrorParsing elif command == 'delete': post = None opts, args = getopt(args, 'p:', ['post=']) for o, a in opts: if o in ('-p', '--post'): post = service.normilizeID(a)[0] if post: return service.delPost(login = login, post = post) else: raise dmbErrorParsing elif command == 'recommend': post = message = None opts, args = getopt(args, 'p:', ['post=']) for o, a in opts: if o in ('-p', '--post'): post = a message = ' '.join(args) if post: post, comment = service.normilizeID(post) if args: message = ' '.join(args) return service.addRecommend(login = login, message = message, post = post, comment = comment) else: raise dmbErrorParsing elif command == 'subscribes': return service.getSubscribes(login = login) elif command == 'subscribe': user = post = tag = None opts, args = getopt(args, 'u:p:t:', ['user='******'post=', 'tag=']) for o, a in opts: if o in ('-u', '--user'): user = a break elif o in ('-p', '--post'): post = a break elif o in ('-t', '--tag'): tag = a break if post: post = service.normilizeID(post)[0] return service.addSubscribe(login = login, post = post, tag = tag, user = user) elif command == 'unsubscribe': user = post = tag = None opts, args = getopt(args, 'u:p:t:', ['user='******'post=', 'tag=']) for o, a in opts: if o in ('-u', '--user'): user = a break elif o in ('-p', '--post'): post = a break elif o in ('-t', '--tag'): tag = a break if post: post = service.normilizeID(post)[0] return service.delSubscribe(login = login, post = post, tag = tag, user = user) elif command == 'register': priority = 50 opts, args = getopt(args, 'j:p:', ['jid=', 'priority=']) for o, a in opts: if o in ('-j', '--jid'): jid = a elif o in ('-p', '--priority'): priority = int(a) if login == 'anonymous' and len(args) > 0: login = args.split()[0] if not re.search(r'^\w+$', login): raise dmbError('MSG72') force = None else: force = 1 return service.regUser(jid = jid, login = login, priority = priority, force = force) elif command == 'unregister': jid_d = None opts, args = getopt(args, 'j:', ['jid=']) for o, a in opts: if o in ('-j', '--jid'): jid_d = a if jid_d != jid: return service.unRegUser(jid = jid_d, login = login) else: return service.getText('MSG71') elif command == 'list': add = 1 is_tag = 0 list_name = user = tag = None opts, args = getopt(args, 'adl:t', ['add', 'delete', 'list=', 'tag']) for o, a in opts: if o in ('-a', '--add'): add = 1 elif o in ('-d', '--delete'): add = 0 elif o in ('-t', '--tag'): is_tag = 1 elif o in ('-l', '--list'): if not list_name: list_name = a if len(args) > 0: if is_tag: tag = args.split()[0] else: user = args.split()[0] if list_name: if tag or user: if add: return service.addToUserList(login = login, list_name = list_name, user = user, tag = tag) else: return service.delFromUserList(login = login, list_name = list_name, user = user, tag = tag) elif not add: return service.delUserList(login = login, list_name = list_name) else: return service.getUserList(login = login, list_name = list_name, to_print = 1) else: return service.getUserList(login = login, to_print = 1) raise dmbErrorParsing elif command == 'get': if len(args) > 0: param = args.split()[0] else: param = None return service.getUserParam(login, param) elif command == 'set': if len(args) > 0: param = args.split()[0] if len(args) > 1: value = args[1] else: value = None return service.setUserParam(login, param, value) else: raise dmbErrorParsing elif command == 'alias': add = 1 name = command = None opts, args = getopt(args, 'adn:', ['add', 'delete', 'name=']) for o, a in opts: if o in ('-a', '--add'): add = 1 elif o in ('-d', '--delete'): add = 0 elif o in ('-n', '--name'): name = a command = ' '.join(args) if not name: return service.getAlias(login = login, to_print = 1) else: if add: return service.addAlias(login = login, alias = name, command = command) else: return service.delAlias(login = login, alias = name) raise dmbErrorParsing elif command == 'regexp': add = 1 name = regexp = command = None opts, args = getopt(args, 'adn:c:', ['add', 'delete', 'name=', 'command=']) for o, a in opts: if o in ('-a', '--add'): add = 1 elif o in ('-d', '--delete'): add = 0 elif o in ('-n', '--name'): name = a elif o in ('-c', '--command'): command = a regexp = ' '.join(args) if not name: return service.getRegexp(login = login, to_print = 1) else: if add: return service.addRegexp(login = login, name = name, regexp = regexp, command = command) else: return service.delRegexp(login = login, name = name) raise dmbErrorParsing else: raise dmbErrorParsing except GetoptError: log.warning('%d %s %s' % (sys.exc_traceback.tb_lineno, sys.exc_type, sys.exc_value)) raise dmbErrorCommand except TypeError: log.warning('%d %s %s' % (sys.exc_traceback.tb_lineno, sys.exc_type, sys.exc_value)) raise dmbErrorParsing
def show(self, count=10, login=None, post=None, comment=None, tag=None, user=None, id_recommend=None, show_high_mesg=None, error_mesg=1): try: if post: mess_type = self.getText('ERRMSG1') messages = self.db.show(post=post, comment=comment) elif user: mess_type = self.getText('ERRMSG2') messages = self.db.show(user=user) elif tag: mess_type = self.getText('ERRMSG3') messages = self.db.show(count=count, tag=tag, login=login) else: mess_type = self.getText('ERRMSG1') messages = self.db.show(count=count, login=login) if login: params = self.getUserParams(login) time_zone = params['time_zone'] else: params = self.getUserParams('anonymous') time_zone = 0 result = '' if id_recommend: recommend = self.db.getRecommend(id_recommend) if recommend: if recommend.has_key('message') and recommend['message']: message = '- %s' % recommend['message'] else: message = self.getText('PS1') result += '%s %s:\n' % (recommend['login'], message) for message in messages: if message[0] == const_post and not comment: message = message[1] tagStr = '' for t in message['tags']: if not tagStr.count('*%s ' % t): tagStr += '*%s ' % t if len(tagStr) > 0: tagStr += '\n' if not post and len(message['message']) > 200: message[ 'message'] = '%s [...]' % message['message'][:200] result += '%s: %s\n%s%s\n#%s (%i %s, %s)\n\n' % ( message['login'], '+' * message['count_recom'], tagStr, message['message'], self.numCoding(int(message['post']), params['num_type']), message['count_comments'], self.getText('PS2'), self.getStrTime(message['timestamp'], time_zone=time_zone)) elif message[0] == const_comment: message = message[1] mesgAdd = mesgHigh = '' if message['high_comment']: mesgAdd = ' %s /%s' % ( self.getText('PS3'), self.numCoding(int(message['high_comment']), params['num_type'])) if show_high_mesg: mesgHigh = '>%s\n' % message['high_message'] result += '%s: %s\n%s%s\n#%s/%s (%s)%s\n\n' % ( message['login'], '+' * message['count_recom'], mesgHigh, message['message'], self.numCoding(int(message['post']), params['num_type']), self.numCoding(message['comment'], params['num_type']), self.getStrTime(message['timestamp'], time_zone=time_zone), mesgAdd) elif message[0] == const_user: message = message[1] result += '%s - %s\n\n' % (message['login'], message['jid']) return result except dmbErrorNotFound as exc: log.debug('%s, %s' % (self.getText(exc.getText(), 'en'), mess_type)) exc.message = mess_type raise exc