X-Git-Url: http://git.maemo.org/git/?p=python-purple;a=blobdiff_plain;f=nullclient.py;h=112ea10537b8b172b7b4e440f84eae6f72be2229;hp=ecda1f630afdb39aaf5cf72128d7051bfdcd432f;hb=d498550db3bfd489d8319e7342cc10b76cf24ac9;hpb=573def76e068006d507f09ea1239fcd6d6adfae7 diff --git a/nullclient.py b/nullclient.py index ecda1f6..112ea10 100644 --- a/nullclient.py +++ b/nullclient.py @@ -2,233 +2,99 @@ import purple import ecore import getpass import sys +from xml.dom import minidom -cbs = {} -acc_cbs = {} -blist_cbs = {} -conn_cbs = {} -conv_cbs = {} -notify_cbs = {} -request_cbs = {} -signal_cbs = {} - -def account_callback(name): - print "---- account callback example: %s" % name - -acc_cbs["notify_added"] = account_callback -acc_cbs["status_changed"] = account_callback -acc_cbs["request_add"] = account_callback -acc_cbs["request_authorize"] = account_callback -acc_cbs["close_account_request"] = account_callback - -cbs["account"] = acc_cbs - -def blist_callback(name): - print "---- blist callback example: %s" % name - -blist_cbs["new_list"] = blist_callback -blist_cbs["show"] = blist_callback -blist_cbs["destroy"] = blist_callback -blist_cbs["set_visible"] = blist_callback -blist_cbs["request_add_buddy"] = blist_callback -blist_cbs["request_add_chat"] = blist_callback -blist_cbs["request_add_group"] = blist_callback - -def new_node_cb(type, name=None, totalsize=None, currentsize=None, online=None): - if type == 0: - print "---- blist callback: new node (group): %s (%s/%s) (%s online)" % \ - (name, totalsize, currentsize, online) - elif type == 1: - print "---- blist callback: new node (contact): %s (%s/%s) (%s online)" % \ - (name, totalsize, currentsize, online) - elif type == 2: # totalsize = alias - print "---- blist callback: new node (buddy): %s (%s)" % (name, totalsize) - elif type == 3: - print "---- blist callback: new node (chat): %s" % alias - elif type == 4: - print "---- blist callback: new node (other type)" - else: - print "---- blist callback: new node (unknown type %s)" % type - -def update_cb(type, name=None, totalsize=None, currentsize=None, online=None): - if type == 0: - print "---- blist callback: update (group): %s (%s/%s) (%s online)" % \ - (name, totalsize, currentsize, online) - elif type == 1: - print "---- blist callback: update (contact): %s (%s/%s) (%s online)" % \ - (name, totalsize, currentsize, online) - elif type == 2: # totalsize = alias - print "---- blist callback: update (buddy): %s (%s)" % \ - (name, totalsize) - elif type == 3: - print "---- blist callback: update (chat): %s" % alias - elif type == 4: - print "---- blist callback: update (other type)" - else: - print "---- blist callback: update (unknown type %s)" % type - -def remove_cb(type, name=None, totalsize=None, currentsize=None, online=None): - if type == 0: - print "---- blist callback: remove (group): %s (%s/%s) (%s online)" % \ - (name, totalsize, currentsize, online) - elif type == 1: - print "---- blist callback: remove (contact): %s (%s/%s) (%s online)" % \ - (name, totalsize, currentsize, online) - elif type == 2: # totalsize = alias - print "---- blist callback: remove (buddy): %s (%s)" % \ - (name, totalsize) - elif type == 3: - print "---- blist callback: remove (chat): %s" % alias - elif type == 4: - print "---- blist callback: remove (other type)" - else: - print "---- blist callback: remove (unknown type %s)" % type - -blist_cbs["new_node"] = new_node_cb -blist_cbs["update"] = update_cb -blist_cbs["remove"] = remove_cb - -cbs["blist"] = blist_cbs - -def conn_callback(name): - print "---- connection callback example: %s" % name - -conn_cbs["notice"] = conn_callback -conn_cbs["network_connected"] = conn_callback -conn_cbs["network_disconnected"] = conn_callback - -def connect_progress_cb(text, step, step_count): - print "---- connection status: %s [%s/%s]" % (text, step, step_count) - -def connected_cb(): - print "---- connection status: Connected" - -def disconnected_cb(): - print "---- connection status: Disconnected" - -def report_disconnect_cb(text): - print "---- %s" % text - -def report_disconnect_reason_cb(reason, text): - print "---- %s (%s)" % (text, reason) - -conn_cbs["connect_progress"] = connect_progress_cb -conn_cbs["connected"] = connected_cb -conn_cbs["disconnected"] = disconnected_cb -conn_cbs["report_disconnect"] = report_disconnect_cb -conn_cbs["report_disconnect_reason"] = report_disconnect_reason_cb - -cbs["connection"] = conn_cbs - -def conv_callback(name): - print "---- conversation callback example: %s" % name - -conv_cbs["create_conversation"] = conv_callback -conv_cbs["destroy_conversation"] = conv_callback -conv_cbs["write_chat"] = conv_callback -conv_cbs["write_im"] = conv_callback -conv_cbs["write_conv"] = conv_callback -conv_cbs["chat_add_users"] = conv_callback -conv_cbs["chat_rename_user"] = conv_callback -conv_cbs["chat_remove_users"] = conv_callback -conv_cbs["chat_update_user"] = conv_callback -conv_cbs["present"] = conv_callback -conv_cbs["has_focus"] = conv_callback -conv_cbs["custom_smiley_add"] = conv_callback -conv_cbs["custom_smiley_write"] = conv_callback -conv_cbs["custom_smiley_close"] = conv_callback -conv_cbs["send_confirm"] = conv_callback - -cbs["conversation"] = conv_cbs - -def notify_callback(name): - print "---- notify callback example: %s" % name - -notify_cbs["notify_message"] = notify_callback -notify_cbs["notify_email"] = notify_callback -notify_cbs["notify_emails"] = notify_callback -notify_cbs["notify_formatted"] = notify_callback -notify_cbs["notify_searchresults"] = notify_callback -notify_cbs["notify_searchresults_new_rows"] = notify_callback -notify_cbs["notify_userinfo"] = notify_callback -notify_cbs["notify_uri"] = notify_callback -notify_cbs["close_notify"] = notify_callback - -cbs["notify"] = notify_cbs - -def request_callback(name): - print "---- request callback example: %s" % name - -request_cbs["request_input"] = request_callback -request_cbs["request_choice"] = request_callback -request_cbs["request_action"] = request_callback -request_cbs["request_fields"] = request_callback -request_cbs["request_file"] = request_callback -request_cbs["close_request"] = request_callback -request_cbs["request_folder"] = request_callback - -cbs["request"] = request_cbs - -def buddy_signed_off_cb(name): - print "---- sign off from buddy %s" % name - -def receiving_im_msg_cb(sender, name, message): - print "---- receiving IM message from %s: %s" % (name, message) - return False - -signal_cbs["buddy_signed_off"] = buddy_signed_off_cb -signal_cbs["receiving_im_msg"] = receiving_im_msg_cb - -class NullClient: + +class ClientModel: def __init__(self): - self.p = purple.Purple(debug_enabled=False) - self.account = None + self.purple = purple.Purple(debug_enabled=True) + self.purple.purple_init() + self.account = purple.Account() + + def add_account(self, acc): + ''' @param acc: {'username': "foo@gmail.com", 'protocol': "XMPP"} ''' + + new_acc = self.account.new(acc['username'], acc['protocol']) + + return new_acc + + def get_protocols(self): + return self.account.protocol.get_all() + + def get_protocol_options(self, protocol_id): + return self.account.protocol.get_options(protocol_id) + + def set_account_info(self, acc, info): + ''' @param info: {protocol: 'prpl-jabber', alias: '', password: ' ', server: 'new server', port: '' } || + {server: 'new server'} ''' - def execute(self): - global cbs - self.p.purple_init(cbs) + if info.has_key('protocol'): + self.account.set_protocol_id(acc, info['protocol']) + if info.has_key('alias'): + self.account.set_alias(acc, info['alias']) + if info.has_key('password'): + self.account.set_password(acc, info['password']) - def set_protocol(self, protocol): - for p in self.p.get_protocols(): - if p.get_name() == protocol: - print "-- NULLCLIENT --: Choosing %s as protocol" % protocol - self.protocol = p - print "-- NULLCLIENT --: Protocol successfully chosen: %s" % p.get_id() - return + self.account.protocol.set_options(acc, info) + + def get_account_info(self, acc): + info = {} + po = self.account.protocol.get_options(acc[1], acc[0]) + info['protocol'] = self.account.get_protocol_id(acc) + info['alias'] = self.account.get_alias(acc) + info['password'] = self.account.get_password(acc) + info['connect_server'] = po['connect_server'][1] + info['port'] = po['port'][1] + + return info + + def set_account_proxy(self, acc, info): + self.account.proxy.set_info(acc, info) + + def account_connect(self, acc): + self.account.set_enabled(acc, "carman-purple-python", True) + # self.account.connect(acc) + self.purple.connect() + + def account_disconnect(self, acc): + self.account.disconnect(acc) + + +class ClientCtrl: + def __init__(self): + self.clientmodel = ClientModel() + new_acc = {} + acc_info = {} + new_acc['username'] = self.getuser() + new_acc['protocol'] = 'prpl-jabber' - def new_account(self, username, protocol, password): - self.account = purple.Account(username, protocol.get_id()) - self.account.set_password(password) + acc = self.clientmodel.add_account(new_acc) - self.account.proxy.set_type(purple.ProxyInfoType().HTTP) - self.account.proxy.set_host("172.18.216.211") - self.account.proxy.set_port(8080) + acc_info = self.clientmodel.get_account_info(acc) + acc_info['password'] = self.getpassword() + acc_info['connect_server'] = 'talk.google.com' + acc_info['port'] = 443 + acc_info['old_ssl'] = True + self.clientmodel.set_account_info(acc, acc_info) - self.account.get_protocol_options() + acc_proxy = {} + acc_proxy['type'] = 'HTTP' + acc_proxy['host'] = '172.18.216.211' + acc_proxy['port'] = 8080 + self.clientmodel.set_account_proxy(acc, acc_proxy) - self.account.set_enabled("carman-purple-python", True) - def get_buddies(self): - buddies = self.account.get_buddies_online() - print buddies + self.clientmodel.account_connect(acc) -def getuser(): - sys.stdout.write("GTalk account: ") - username = sys.stdin.readline() - return username[:-1] + def getuser(self): + sys.stdout.write("GTalk account: ") + username = sys.stdin.readline() + return username[:-1] -def getpassword(): - return getpass.getpass() + def getpassword(self): + return getpass.getpass() if __name__ == '__main__': - client = NullClient() - client.execute() - client.set_protocol("XMPP") - username = getuser() - password = getpassword() - client.new_account(username, client.protocol, password) + ctrl = ClientCtrl() - client.p.connect() - client.p.attach_signals(signal_cbs) - ecore.timer_add(20, client.get_buddies) ecore.main_loop_begin()