def main(self): config, server, options = self.service.config, self.service.server, self.service.options setproctitle.setproctitle('kopano-msr control') def response(conn, msg): self.log.info('Response: %s', msg) conn.sendall((msg + '\r\n').encode()) s = kopano.server_socket(config['server_bind_name'], ssl_key=config['ssl_private_key_file'], ssl_cert=config['ssl_certificate_file'], log=self.log) while True: with log_exc(self.log): try: conn = None conn, _ = s.accept() fields_terms = [] for data in conn.makefile(): self.log.info('CMD: %s', data.strip()) data = data.split() if data[0] == 'ADD': user, target_user = data[1:] self.subscribe.put((user, target_user, True)) response(conn, 'OK:') break elif data[0] == 'REMOVE': user = data[1] self.subscribe.put((user, None, False)) response(conn, 'OK:') break elif data[0] == 'LIST': response( conn, 'OK: ' + ' '.join( user + ':' + target for user, target in USER_USER.items())) break else: response(conn, 'ERROR') break except Exception: response(conn, 'ERROR') raise finally: if conn: conn.close()
def main(self): config, plugin = self.service.config, self.service.plugin def response(conn, msg): self.log.info('Response: %s', msg) conn.sendall((msg + '\r\n').encode()) s = kopano.server_socket(config['server_bind_name'], ssl_key=config['ssl_private_key_file'], ssl_cert=config['ssl_certificate_file'], log=self.log) while True: with log_exc(self.log): conn, _ = s.accept() fields_terms = [] for data in conn.makefile(): data = data.strip() self.log.info('Command: %s', data) cmd, args = data.split()[0], data.split()[1:] if cmd == 'PROPS': response( conn, 'OK:' + ' '.join( map(str, config['index_exclude_properties']))) break if cmd == 'SYNCRUN': # wait for syncing to be up-to-date (only used in tests) self.syncrun.value = time.time() while self.syncrun.value: time.sleep(1) response(conn, 'OK:') break elif cmd == 'SCOPE': server_guid, store_guid, folder_ids = args[0], args[ 1], args[2:] response(conn, 'OK:') elif cmd == 'FIND': pos = data.find(':') fields = [int(x) for x in data[:pos].split()[1:]] orig = data[pos + 1:].lower() # Limit number of terms (32) so people do not # inadvertently DoS it if they paste prose. terms = plugin.extract_terms(orig)[:32] if fields and terms: fields_terms.append((fields, terms)) response(conn, 'OK:') elif cmd == 'SUGGEST': suggestion = u'' if config['suggestions'] and len(fields_terms) == 1: for fields, terms in fields_terms: suggestion = plugin.suggest( server_guid, store_guid, terms, orig, self.log) if suggestion == orig: suggestion = u'' response(conn, 'OK: ' + suggestion) elif cmd == 'QUERY': t0 = time.time() restrictions = [] if folder_ids: restrictions.append('(' + ' OR '.join( ['folderid:%s' % f for f in folder_ids]) + ')') for fields, terms in fields_terms: if fields: restrictions.append('(' + ' OR '.join( '(' + ' AND '.join('mapi%d:%s*' % (f, term) for term in terms) + ')' for f in fields) + ')') else: restrictions.append('(' + ' AND '.join( '%s*' % term for term in terms) + ')') query = ' AND '.join( restrictions ) # plugin doesn't have to use this relatively standard query format docids = plugin.search(server_guid, store_guid, folder_ids, fields_terms, query, config['limit_results'], self.log) response(conn, 'OK: ' + ' '.join(map(str, docids))) self.log.info('found %d results in %.2f seconds', len(docids), time.time() - t0) break elif cmd == 'REINDEX': self.reindex_queue.put(args[0]) response(conn, 'OK:') self.log.info("queued store %s for reindexing", args[0]) break else: self.log.error("unknown command: %s", cmd) break conn.close()
def main(self): config, server, options = self.service.config, self.service.server, self.service.options setproctitle.setproctitle('kopano-msr control') def response(conn, msg): self.log.info('Response: %s', msg) conn.sendall((msg+'\r\n').encode()) s = kopano.server_socket(config['server_bind_name'], ssl_key=config['ssl_private_key_file'], ssl_cert=config['ssl_certificate_file'], log=self.log) while True: with log_exc(self.log): try: conn = None conn, _ = s.accept() fields_terms = [] for data in conn.makefile(): self.log.info('CMD: %s', data.strip()) data = data.split() if data[0] == 'ADD': user, target_user, target_server = data[1:] if user in USER_INFO: self.log.error('user %s is already being relocated', user) # TODO return error response(conn, 'ERROR') break self.subscribe.put((user, target_user, target_server, True, None)) response(conn, 'OK:') break elif data[0] == 'REMOVE': user = data[1] if user not in USER_INFO: self.log.error('user %s is not being relocated', user) # TODO return error response(conn, 'ERROR') break self.subscribe.put((user, None, None, False, None)) response(conn, 'OK:') break elif data[0] == 'LIST': lines = [] for user, data in USER_INFO.items(): items = data['items'] init_done = data['init_done'] queue_empty = data['queue_empty'] servername = data['server'] line = 'user=%s server=%s items=%d init_done=%s queue_empty=%s' % (user, servername, items, init_done, queue_empty) lines.append(line) response(conn, 'OK:\n' + '\n'.join(lines)) break elif data[0] == 'DETAILS': response(conn, 'OK:\n' + '\n'.join(self.user_details(server, data[1]))) break else: response(conn, 'ERROR') break except Exception: response(conn, 'ERROR') raise finally: if conn: conn.close()