From 4eeb96368b7b6df4d7a1621143076b60b8ffdc5f Mon Sep 17 00:00:00 2001
From: js <js-gajim@webkeks.org>
Date: Sat, 10 May 2008 21:29:47 +0000
Subject: [PATCH] Store otr_userstates at the right place and create it the
 right way. This fixes the crash when creating a new account, which made Gajim
 unusable on systems where it never ran before.

---
 config.h.in                       |  4 ++++
 src/chat_control.py               |  8 ++++----
 src/common/connection.py          | 20 ++++++++++++++++++++
 src/common/connection_handlers.py |  4 ++--
 src/gajim.py                      | 23 ++---------------------
 src/message_control.py            |  6 ++----
 src/otr_windows.py                | 12 ++++++------
 src/roster_window.py              |  4 ++--
 8 files changed, 42 insertions(+), 39 deletions(-)

diff --git a/config.h.in b/config.h.in
index ffb64246e..886160a42 100644
--- a/config.h.in
+++ b/config.h.in
@@ -63,6 +63,10 @@
 /* Define to 1 if you have the <unistd.h> header file. */
 #undef HAVE_UNISTD_H
 
+/* Define to the sub-directory in which libtool stores uninstalled libraries.
+   */
+#undef LT_OBJDIR
+
 /* Name of package */
 #undef PACKAGE
 
diff --git a/src/chat_control.py b/src/chat_control.py
index 828582eb7..870648a6e 100644
--- a/src/chat_control.py
+++ b/src/chat_control.py
@@ -1208,7 +1208,7 @@ class ChatControl(ChatControlBase):
 
 	def update_otr(self, print_status=False):
 		# retrieve the OTR context from the chat's contact data
-		ctx = gajim.otr_module.otrl_context_find(gajim.otr_userstates[self.account],
+		ctx = gajim.otr_module.otrl_context_find(gajim.connections[self.account].otr_userstates,
 			self.contact.get_full_jid().encode(),
 			gajim.get_jid_from_account(self.account).encode(), gajim.OTR_PROTO, 1,
 			(gajim.otr_add_appdata, self.account))[0]
@@ -1851,7 +1851,7 @@ class ChatControl(ChatControlBase):
 				self._on_smp_otr_menuitem_activate)
 			self.handlers[id] = smp_otr_menuitem
 
-			ctx = gajim.otr_module.otrl_context_find(gajim.otr_userstates[self.account],
+			ctx = gajim.otr_module.otrl_context_find(gajim.connections[self.account].otr_userstates,
 				self.contact.get_full_jid().encode(),
 				gajim.get_jid_from_account(self.account).encode(), gajim.OTR_PROTO, 1,
 				(gajim.otr_add_appdata, self.account))[0]
@@ -2349,7 +2349,7 @@ class ChatControl(ChatControlBase):
 		MessageControl.send_message(self, u"?OTR?", type="chat")
 	def _on_end_otr_menuitem_activate(self, widget):
 		fjid = self.contact.get_full_jid()
-		gajim.otr_module.otrl_message_disconnect(gajim.otr_userstates[self.account],
+		gajim.otr_module.otrl_message_disconnect(gajim.connections[self.account].otr_userstates,
 				(gajim.otr_ui_ops, {'account':self.account,'urgent':True}),
 				gajim.get_jid_from_account(self.account).encode(), gajim.OTR_PROTO,
 				fjid.encode())
@@ -2359,7 +2359,7 @@ class ChatControl(ChatControlBase):
 	def _on_otr_settings_menuitem_activate(self, widget):
 		gajim.otr_windows.ContactOtrWindow(self.contact, self.account, self)
 	def _on_smp_otr_menuitem_activate(self, widget):
-		ctx = gajim.otr_module.otrl_context_find(gajim.otr_userstates[self.account],
+		ctx = gajim.otr_module.otrl_context_find(gajim.connections[self.account].otr_userstates,
 			self.contact.get_full_jid().encode(),
 			gajim.get_jid_from_account(self.account).encode(), gajim.OTR_PROTO, 1,
 			(gajim.otr_add_appdata, self.account))[0]
diff --git a/src/common/connection.py b/src/common/connection.py
index 94c99494e..a9bd6c681 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -164,6 +164,26 @@ class Connection(ConnectionHandlers):
 		# server {'icq': ['icq.server.com', 'icq2.server.com'], }
 		self.vcard_supported = True
 		self.private_storage_supported = True
+
+		if gajim.otr_module:
+			self.otr_userstates = gajim.otr_module.otrl_userstate_create()
+
+			try:
+				gajim.otr_module.otrl_privkey_read(self.otr_userstates,
+					os.path.join(gajimpaths.root,
+					'%s.key' % self.name).encode())
+			except Exception, e:
+				if hasattr(e, 'os_errno') and e.os_errno == 2:
+					pass
+
+			try:
+				gajim.otr_module.otrl_privkey_read_fingerprints(
+					self.otr_userstates, os.path.join(
+					gajimpaths.root, '%s.fpr' % self.name
+					).encode(), (add_appdata, a))
+			except Exception, e:
+				if hasattr(e, 'os_errno') and e.os_errno == 2:
+					pass
 	# END __init__
 
 	def put_event(self, ev):
diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py
index a7191c6d1..dd4d9ce8a 100644
--- a/src/common/connection_handlers.py
+++ b/src/common/connection_handlers.py
@@ -1680,7 +1680,7 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
 
 			if gajim.otr_module and isinstance(msgtxt, unicode):
 				otr_msg_tuple = gajim.otr_module.otrl_message_receiving(
-					gajim.otr_userstates[self.name],
+					gajim.connections[self.name].otr_userstates,
 					(gajim.otr_ui_ops, {'account':self.name}),
 					gajim.get_jid_from_account(self.name).encode(),
 					gajim.OTR_PROTO, frm.encode(), msgtxt.encode(),
@@ -1699,7 +1699,7 @@ class ConnectionHandlers(ConnectionVcard, ConnectionBytestream, ConnectionDisco,
 					if ctrl:
 						ctrl.update_ui()
 
-				ctx = gajim.otr_module.otrl_context_find(gajim.otr_userstates[self.name], frm.encode(),
+				ctx = gajim.otr_module.otrl_context_find(gajim.connections[self.name].otr_userstates, frm.encode(),
 						gajim.get_jid_from_account(self.name).encode(), gajim.OTR_PROTO, 1,
 						(gajim.otr_add_appdata, self.name))[0]
 				tlvs = otr_msg_tuple[2]
diff --git a/src/gajim.py b/src/gajim.py
index 435c78957..66465e2b9 100755
--- a/src/gajim.py
+++ b/src/gajim.py
@@ -319,7 +319,7 @@ class OtrlMessageAppOps:
 		while gtk.events_pending():
 			gtk.main_iteration(block=False)
 
-		otr.otrl_privkey_generate(gajim.otr_userstates[opdata['account']],
+		otr.otrl_privkey_generate(gajim.connections[opdata['account']].otr_userstates,
 			os.path.join(gajimpaths.root, "%s.key"%opdata['account']).encode(),
 			accountname, gajim.OTR_PROTO)
 		permlabel.set_text("Generating a private key for %s...\ndone."%accountname)
@@ -371,7 +371,7 @@ class OtrlMessageAppOps:
 			otr.otrl_privkey_hash_to_human(fingerprint)), opdata['account'], username)
 
 	def write_fingerprints(self, opdata=""):
-		otr.otrl_privkey_write_fingerprints(gajim.otr_userstates[opdata['account']],
+		otr.otrl_privkey_write_fingerprints(gajim.connections[opdata['account']].otr_userstates,
 			os.path.join(gajimpaths.root, "%s.fpr"%opdata['account']).encode())
 
 	def gone_secure(self, opdata="", context=None):
@@ -3326,25 +3326,6 @@ class Interface:
 			gajim.status_before_autoaway[a] = ''
 			gajim.transport_avatar[a] = {}
 
-			if gajim.otr_module:
-				gajim.otr_userstates[a] = otr.otrl_userstate_create()
-				try:
-					otr.otrl_privkey_read(gajim.otr_userstates[a],
-						os.path.join(gajimpaths.root, "%s.key"%a).encode())
-				except Exception, e:
-					if hasattr(e,"os_errno") and e.os_errno == 2:
-						print "didn't find otr keyfile "+ \
-							(os.path.join(gajimpaths.root, "%s.key"%a).encode())
-						pass
-				try:
-					otr.otrl_privkey_read_fingerprints(gajim.otr_userstates[a],
-						os.path.join(gajimpaths.root, "%s.fpr"%a).encode(), (add_appdata, a))
-				except Exception, e:
-					if hasattr(e,"os_errno") and e.os_errno == 2:
-						print "didn't find otr fingerprint file "+ \
-							(os.path.join(gajimpaths.root, "%s.fpr"%a).encode())
-						pass
-
 		if gajim.config.get('remote_control'):
 			try:
 				import remote_control
diff --git a/src/message_control.py b/src/message_control.py
index 907ef0ba2..e527f778d 100644
--- a/src/message_control.py
+++ b/src/message_control.py
@@ -160,19 +160,17 @@ class MessageControl:
 						'original_message':original_message}, 'account':self.account}
 	
 				new_msg = gajim.otr_module.otrl_message_sending(
-					gajim.otr_userstates[self.account],
+					gajim.connections[self.account].otr_userstates,
 					(gajim.otr_ui_ops, d),
 					gajim.get_jid_from_account(self.account).encode(), gajim.OTR_PROTO,
 					self.contact.get_full_jid().encode(), message.encode(), None)
 
 				context = gajim.otr_module.otrl_context_find(
-						gajim.otr_userstates[self.account],
+						gajim.connections[self.account].otr_userstates,
 						self.contact.get_full_jid().encode(),
 						gajim.get_jid_from_account(self.account).encode(),
 						gajim.OTR_PROTO, 1)[0]
 
-				print repr(context.accountname), repr(context.username)
-
 				# we send all because inject_message can filter on HTML stuff then
 				gajim.otr_module.otrl_message_fragment_and_send(
 						(gajim.otr_ui_ops, d),
diff --git a/src/otr_windows.py b/src/otr_windows.py
index b0486dfc2..ac1878f92 100644
--- a/src/otr_windows.py
+++ b/src/otr_windows.py
@@ -49,7 +49,7 @@ class ContactOtrSMPWindow:
 				self.contact.get_full_jid())
 
 		self.ctx = gajim.otr_module.otrl_context_find(
-			gajim.otr_userstates[self.account],
+			gajim.connections[self.account].otr_userstates,
 			self.fjid.encode(), gajim.get_jid_from_account(self.account).encode(),
 			gajim.OTR_PROTO, 1, (gajim.otr_add_appdata, self.account))[0]
 
@@ -79,7 +79,7 @@ class ContactOtrSMPWindow:
 
 	def _abort(self, text=None):
 		self.smp_running = False
-		gajim.otr_module.otrl_message_abort_smp(gajim.otr_userstates[self.account],
+		gajim.otr_module.otrl_message_abort_smp(gajim.connections[self.account].otr_userstates,
 				(gajim.otr_ui_ops, {'account':self.account}), self.ctx)
 		if text:
 			gajim.otr_ui_ops.gajim_log(text, self.account, self.contact.get_full_jid())
@@ -152,10 +152,10 @@ class ContactOtrSMPWindow:
 			return
 		secret = self.gw("secret_entry").get_text()
 		if self.response:
-			gajim.otr_module.otrl_message_respond_smp(gajim.otr_userstates[self.account],
+			gajim.otr_module.otrl_message_respond_smp(gajim.connections[self.account].otr_userstates,
 					(gajim.otr_ui_ops, {'account':self.account}), self.ctx, secret)
 		else:
-			gajim.otr_module.otrl_message_initiate_smp(gajim.otr_userstates[self.account],
+			gajim.otr_module.otrl_message_initiate_smp(gajim.connections[self.account].otr_userstates,
 					(gajim.otr_ui_ops, {'account':self.account}), self.ctx, secret)
 			self.gw("progressbar").set_fraction(0.3)
 		self.smp_running = True
@@ -174,7 +174,7 @@ class ContactOtrWindow:
 		self.ctrl = ctrl
 
 		self.ctx = gajim.otr_module.otrl_context_find(
-			gajim.otr_userstates[self.account],
+			gajim.connections[self.account].otr_userstates,
 			self.contact.get_full_jid().encode(),
 			gajim.get_jid_from_account(self.account).encode(),
 			gajim.OTR_PROTO, 1, (gajim.otr_add_appdata, self.account))[0]
@@ -193,7 +193,7 @@ class ContactOtrWindow:
 		# always set the label containing our fingerprint
 		self.gw("our_fp_label").set_markup(our_fp_text%
 			gajim.otr_module.otrl_privkey_fingerprint(
-				gajim.otr_userstates[self.account],
+				gajim.connections[self.account].otr_userstates,
 				gajim.get_jid_from_account(self.account).encode(),
 				gajim.OTR_PROTO))
 
diff --git a/src/roster_window.py b/src/roster_window.py
index 2140e2e7c..1fee45872 100644
--- a/src/roster_window.py
+++ b/src/roster_window.py
@@ -1776,11 +1776,11 @@ class RosterWindow:
 				# disconnect from ENCRYPTED OTR contexts when going
 				# offline/invisible
 				if status == 'offline' or status == 'invisible':
-					ctx = gajim.otr_userstates[account].context_root
+					ctx = gajim.connections[account].otr_userstates.context_root
 					while ctx is not None:
 						if ctx.msgstate == gajim.otr_module.OTRL_MSGSTATE_ENCRYPTED:
 							disconnected = True
-							gajim.otr_module.otrl_message_disconnect(gajim.otr_userstates[account],
+							gajim.otr_module.otrl_message_disconnect(gajim.connections[account].otr_userstates,
 									(gajim.otr_ui_ops,
 									{'account':account,'urgent':True}), ctx.accountname,
 									ctx.protocol, ctx.username)