[ekg2-commit] ekg2: commands.c (HEAD) [peres]

CVS commit cvs w toxygen.net
Czw, 6 Wrz 2007, 15:32:46 CEST


Module name:	ekg2
Changes by:	peres	2007-09-06 15:32:44

Modified files:
	commands.c

Log message:
XEP-0071 rewritten and moved:
- syntax check is now being done before anything is put into socket, and
  failed one won't break stream anymore,
- more unified code (especially for non-syntax checked messages),
- fix segv if iconv fails.

Index: commands.c
===================================================================
RCS file: /home/cvs/ekg2/plugins/jabber/commands.c,v
diff -d -u -r1.197 -r1.198
--- commands.c	1 Sep 2007 10:28:44 -0000	1.197
+++ commands.c	6 Sep 2007 13:32:43 -0000	1.198
@@ -432,6 +432,7 @@
 	int chat		= !xstrcmp(name, ("chat"));
 	int subjectlen		= xstrlen(config_subject_prefix);
 	char *msg;
+	char *htmlmsg		= NULL;
 	char *subject		= NULL;
 	char *thread		= NULL;
 	const char *uid;
@@ -461,7 +462,7 @@
 		
 			/* and now we can set real thread */
 		thread = jabber_escape(params[2]);
-	} else if (!xstrcmp(name, "msg") && (session_int_get(session, "msg_gen_thread") > 0))
+	} else if (!xstrcmp(name, "msg") && (session_int_get(session, "msg_gen_thread")))
 		thread = jabber_thread_gen(j, uid); /* we don't return any chars to escape */
 
 		/* message subject, TheNewBetterWay^TM */
@@ -482,74 +483,80 @@
 	if ((c = newconference_find(session, target))) 
 		ismuc = 1;
 
-	if (j->send_watch) j->send_watch->transfer_limit = -1;
-
-	if (ismuc)
-		watch_write(j->send_watch, "<message type=\"groupchat\" to=\"%s\" id=\"%d\">", uid+payload, time(NULL));
-	else
-		watch_write(j->send_watch, "<message %sto=\"%s\" id=\"%d\">", 
-			chat ? "type=\"chat\" " : "",
-/*				j->istlen ? "type=\"normal\" " : "",  */
-			uid+payload, time(NULL));
-
-	if (subject) {
-		watch_write(j->send_watch, "<subject>%s</subject>", subject); 
-		xfree(subject); 
-	}
-	if (thread) {
-		watch_write(j->send_watch, "<thread>%s</thread>", thread);
-		xfree(thread);
-	}
-
-	if (!msg) goto nomsg;
-
 	if (!j->istlen) {
 		 /* Very, very simple XEP-0071 support + 'modified' jabber_encode() */
-		char *htmlmsg, *tmp;
+		char *s;
 
-		if (!config_use_unicode) { /* it is important to have utf8 here */
-			const char *s = ekg_convert_string_p(msg, jconv_out);
+		if (!config_use_unicode) {
+			s = ekg_convert_string_p(msg, jconv_out);
 			if (s)
 				msg = s;
 		}
-		if ((htmlmsg = utfstrchr(msg, 18))) { /* ^R */
+
+		if ((htmlmsg = strchr(msg, 18))) { /* ^R */
+			int omitsyntaxcheck;
+
 			*(htmlmsg++) = 0;
-			if (*htmlmsg != 18) { /* syntax checking */
+			if ((omitsyntaxcheck = (*htmlmsg == 18)))
+				htmlmsg++;
+			htmlmsg = saprintf("<html xmlns=\"http://jabber.org/protocol/xhtml-im\">"
+					"<body xmlns=\"http://www.w3.org/1999/xhtml\">"
+					"%s</body></html>", htmlmsg);
+
+			if (!omitsyntaxcheck) {
 				XML_Parser p = XML_ParserCreate("utf-8");
 				/* expat syntax-checking needs the code to be embedded in some parent element
 				 * so we create the whole block here, instead of giving %s to watch_write() */
-				char *fullmsg = saprintf("<html xmlns=\"http://jabber.org/protocol/xhtml-im\">"
-						"<body xmlns=\"http://www.w3.org/1999/xhtml\">"
-						"%s</body></html>", htmlmsg);
+				int r;
 
-				if (!XML_Parse(p, fullmsg, xstrlen(fullmsg), 1)) {
+				if (!(r = XML_Parse(p, htmlmsg, xstrlen(htmlmsg), 1))) {
 					enum XML_Error errc = XML_GetErrorCode(p);
 					const char *errs;
 
 					if (errc && (errs = XML_ErrorString(errc)))
-						print("jabber_msg_xmlsyntaxerr", errs);
-					else	print("jabber_msg_xmlsyntaxerr", "unknown");
+						print_window(target, session, 0, "jabber_msg_xmlsyntaxerr", errs);
+					else	print_window(target, session, 0, "jabber_msg_xmlsyntaxerr", "unknown");
 
-					XML_ParserFree(p);
-					xfree(fullmsg);
-					return -1;			/* XXX, return -1; here is invalid, we already sent data to server */
+					xfree(htmlmsg);
+					xfree(subject);
+					xfree(thread);
 				}
 				XML_ParserFree(p);
-				watch_write(j->send_watch, "%s", fullmsg); /* to avoid problems with %-formats */
-				xfree(fullmsg);
-			} else {
-				htmlmsg++;
-				watch_write(j->send_watch, "<html xmlns=\"http://jabber.org/protocol/xhtml-im\">"
-						"<body xmlns=\"http://www.w3.org/1999/xhtml\">"
-						"%s</body></html>", htmlmsg);
+				if (!r)
+					return -1;
 			}
 		}
-		tmp = xml_escape(msg);
-		if (!config_use_unicode)
-			xfree(msg);
-		msg = tmp;
+
+		{
+			char *tmp = xml_escape(msg);
+			if (!config_use_unicode)
+				xfree(s);
+			msg = tmp;
+		}
 	} else	msg = tlen_encode(msg);
 
+/* writing: */
+	if (j->send_watch) j->send_watch->transfer_limit = -1;
+
+	if (ismuc)
+		watch_write(j->send_watch, "<message type=\"groupchat\" to=\"%s\" id=\"%d\">", uid+payload, time(NULL));
+	else
+		watch_write(j->send_watch, "<message %sto=\"%s\" id=\"%d\">", 
+			chat ? "type=\"chat\" " : "",
+/*				j->istlen ? "type=\"normal\" " : "",  */
+			uid+payload, time(NULL));
+
+	if (subject) {
+		watch_write(j->send_watch, "<subject>%s</subject>", subject); 
+		xfree(subject); 
+	}
+	if (thread) {
+		watch_write(j->send_watch, "<thread>%s</thread>", thread);
+		xfree(thread);
+	}
+
+	if (!msg) goto nomsg;
+
 	if (session_int_get(session, "__gpg_enabled") == 1) {
 		char *e_msg = xstrdup(msg);
 
@@ -569,6 +576,11 @@
 	xfree(msg);
 
 nomsg:
+	if (htmlmsg) {
+		watch_write(j->send_watch, "%s", htmlmsg);
+		xfree(htmlmsg);
+	}
+
 	if (!j->istlen) 
 		watch_write(j->send_watch, "<x xmlns=\"jabber:x:event\">%s%s<displayed/><composing/></x>"
 				"<active xmlns=\"http://jabber.org/protocol/chatstates\"/>", 


Więcej informacji o liście dyskusyjnej ekg2-commit