pidgin/pidgin

simple: De-duplicate sipmsg_to_string.

2020-05-17, Elliott Sales de Andrade
54c050e07348
Parents f38b64939ecb
Children 48c621e76141
simple: De-duplicate sipmsg_to_string.

No need to try and do this manually in separate places.
--- a/libpurple/protocols/simple/simple.c Sun May 17 05:34:38 2020 -0400
+++ b/libpurple/protocols/simple/simple.c Sun May 17 22:03:11 2020 -0400
@@ -605,47 +605,33 @@
return len;
}
-static void sendout_sipmsg(struct simple_account_data *sip, struct sipmsg *msg) {
- GSList *tmp = msg->headers;
- GString *outstr = g_string_new("");
- g_string_append_printf(outstr, "%s %s SIP/2.0\r\n", msg->method, msg->target);
- while(tmp) {
- PurpleKeyValuePair *pair = tmp->data;
- g_string_append_printf(outstr, "%s: %s\r\n", pair->key,
- (gchar *)pair->value);
- tmp = g_slist_next(tmp);
- }
- g_string_append_printf(outstr, "\r\n%s", msg->body ? msg->body : "");
- sendout_pkt(sip->gc, outstr->str);
- g_string_free(outstr, TRUE);
-}
-
-static void send_sip_response(PurpleConnection *gc, struct sipmsg *msg, int code,
- const char *text, const char *body) {
- GSList *tmp = msg->headers;
- GString *outstr = g_string_new("");
+static void
+send_sip_response(PurpleConnection *gc, struct sipmsg *msg, gint code,
+ const gchar *text, const gchar *body)
+{
+ gchar *outstr;
/* When sending the acknowlegements and errors, the content length from the original
message is still here, but there is no body; we need to make sure we're sending the
correct content length */
sipmsg_remove_header(msg, "Content-Length");
+ g_clear_pointer(&msg->body, g_free);
if(body) {
gchar len[12];
- sprintf(len, "%" G_GSIZE_FORMAT , strlen(body));
+ msg->bodylen = strlen(body);
+ sprintf(len, "%d", msg->bodylen);
sipmsg_add_header(msg, "Content-Length", len);
- }
- else
+ msg->body = g_strdup(body);
+ } else {
sipmsg_add_header(msg, "Content-Length", "0");
- g_string_append_printf(outstr, "SIP/2.0 %d %s\r\n", code, text);
- while(tmp) {
- PurpleKeyValuePair *pair = tmp->data;
- g_string_append_printf(outstr, "%s: %s\r\n", pair->key,
- (gchar *)pair->value);
- tmp = g_slist_next(tmp);
+ msg->bodylen = 0;
}
- g_string_append_printf(outstr, "\r\n%s", body ? body : "");
- sendout_pkt(gc, outstr->str);
- g_string_free(outstr, TRUE);
+ msg->response = code;
+
+ outstr = sipmsg_to_string(msg, text);
+ sendout_pkt(gc, outstr);
+
+ g_free(outstr);
}
static void
@@ -1022,8 +1008,11 @@
/* TODO 408 */
} else {
if((currtime - trans->time > 2) && trans->retries == 0) {
+ gchar *outstr;
trans->retries++;
- sendout_sipmsg(sip, trans->msg);
+ outstr = sipmsg_to_string(trans->msg, NULL);
+ sendout_pkt(sip->gc, outstr);
+ g_free(outstr);
}
}
}
@@ -1590,7 +1579,7 @@
sipmsg_remove_header(trans->msg, "Proxy-Authorization");
sipmsg_add_header(trans->msg, "Proxy-Authorization", auth);
g_free(auth);
- resend = sipmsg_to_string(trans->msg);
+ resend = sipmsg_to_string(trans->msg, NULL);
/* resend request */
sendout_pkt(sip->gc, resend);
g_free(resend);
@@ -1634,7 +1623,7 @@
sipmsg_remove_header(trans->msg, "Authorization");
sipmsg_add_header(trans->msg, "Authorization", auth);
g_free(auth);
- resend = sipmsg_to_string(trans->msg);
+ resend = sipmsg_to_string(trans->msg, NULL);
/* resend request */
sendout_pkt(sip->gc, resend);
g_free(resend);
--- a/libpurple/protocols/simple/sipmsg.c Sun May 17 05:34:38 2020 -0400
+++ b/libpurple/protocols/simple/sipmsg.c Sun May 17 22:03:11 2020 -0400
@@ -150,17 +150,20 @@
}
}
-char *sipmsg_to_string(const struct sipmsg *msg) {
+gchar *
+sipmsg_to_string(const struct sipmsg *msg, const gchar *status_text)
+{
GSList *cur;
GString *outstr = g_string_new("");
PurpleKeyValuePair *elem;
- if(msg->response)
- g_string_append_printf(outstr, "SIP/2.0 %d Unknown\r\n",
- msg->response);
- else
+ if (msg->response) {
+ g_string_append_printf(outstr, "SIP/2.0 %d %s\r\n", msg->response,
+ status_text ? status_text : "Unknown");
+ } else {
g_string_append_printf(outstr, "%s %s SIP/2.0\r\n",
msg->method, msg->target);
+ }
cur = msg->headers;
while(cur) {
--- a/libpurple/protocols/simple/sipmsg.h Sun May 17 05:34:38 2020 -0400
+++ b/libpurple/protocols/simple/sipmsg.h Sun May 17 22:03:11 2020 -0400
@@ -39,6 +39,6 @@
const gchar *sipmsg_find_header(struct sipmsg *msg, const gchar *name);
void sipmsg_remove_header(struct sipmsg *msg, const gchar *name);
void sipmsg_print(const struct sipmsg *msg);
-char *sipmsg_to_string(const struct sipmsg *msg);
+gchar *sipmsg_to_string(const struct sipmsg *msg, const gchar *status_text);
#endif /* PURPLE_SIMPLE_SIPMSG_H */