Skip to content

Commit

Permalink
[mms-lib] Ensure that phone number in M-Read-Rec.ind has a type suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed Feb 20, 2015
1 parent 3e31053 commit f6da896
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
26 changes: 4 additions & 22 deletions mms-lib/src/mms_task_encode.c
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand Down Expand Up @@ -533,25 +534,6 @@ mms_task_encode_prepare_attachments(
}
}

static
char*
mms_task_encode_prepare_address(
const char* s)
{
if (s && s[0]) {
char* str = g_strstrip(g_strdup(s));
const char* type = g_strrstr(str, MMS_ADDRESS_TYPE_SUFFIX);
if (type) {
return str;
} else {
char* adr = g_strconcat(str, MMS_ADDRESS_TYPE_SUFFIX_PHONE, NULL);
g_free(str);
return adr;
}
}
return NULL;
}

static
char*
mms_task_encode_prepare_address_list(
Expand All @@ -563,7 +545,7 @@ mms_task_encode_prepare_address_list(
char** part = g_strsplit(s, ",", 0);
GString* buf = g_string_sized_new(strlen(s));
for (i=0; part[i]; i++) {
char* addr = mms_task_encode_prepare_address(part[i]);
char* addr = mms_address_normalize(part[i]);
if (addr) {
if (buf->len > 0) g_string_append_c(buf, ',');
g_string_append(buf, addr);
Expand All @@ -573,7 +555,7 @@ mms_task_encode_prepare_address_list(
g_strfreev(part);
return g_string_free(buf, FALSE);
} else {
return mms_task_encode_prepare_address(s);
return mms_address_normalize(s);
}
}
return NULL;
Expand Down
6 changes: 4 additions & 2 deletions mms-lib/src/mms_task_read.c
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand All @@ -16,6 +17,7 @@
#include "mms_task_http.h"
#include "mms_file_util.h"
#include "mms_codec.h"
#include "mms_util.h"
#include "mms_log.h"
#include "mms_error.h"

Expand All @@ -41,7 +43,7 @@ mms_task_read_encode(
pdu->ri.rr_status = (status == MMS_READ_STATUS_DELETED) ?
MMS_MESSAGE_READ_STATUS_DELETED : MMS_MESSAGE_READ_STATUS_READ;
pdu->ri.msgid = g_strdup(message_id);
pdu->ri.to = g_strdup(to);
pdu->ri.to = mms_address_normalize(to);
time(&pdu->ri.date);
if (mms_message_encode(pdu, fd)) {
result = file;
Expand Down
39 changes: 38 additions & 1 deletion mms-lib/src/mms_util.c
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand Down Expand Up @@ -52,6 +53,42 @@ mms_split_address_list(
return list;
}

/**
* Removes leading and trailing whitespaces from the address and appends
* the address type if necessary (defaults to phone number). In any case
* the caller must deallocate the returned string. Returns NULL if the
* input string is either NULL or empty.
*/
char*
mms_address_normalize(
const char* address)
{
if (address) {
gssize len;

/* Skip leading whitespaces */
while (*address && g_ascii_isspace(*address)) address++;

/* Calculate the length without traling whitespaces */
len = strlen(address);
while (len > 0 && g_ascii_isspace(address[len-1])) len--;
if (len > 0) {

/* Append the address type if necessary */
char* out;
if (g_strrstr_len(address, len, MMS_ADDRESS_TYPE_SUFFIX)) {
out = g_strndup(address, len);
} else {
out = g_new(char, len+sizeof(MMS_ADDRESS_TYPE_SUFFIX_PHONE));
strncpy(out, address, len);
strcpy(out+len, MMS_ADDRESS_TYPE_SUFFIX_PHONE);
}
return out;
}
}
return NULL;
}

/**
* Allocates and decodes WAP push PDU. Returns NULL if decoding fails.
*/
Expand Down
7 changes: 6 additions & 1 deletion mms-lib/src/mms_util.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
* Copyright (C) 2013-2015 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand All @@ -25,6 +26,10 @@ char**
mms_split_address_list(
const char* addres_list);

char*
mms_address_normalize(
const char* address);

MMSPdu*
mms_decode_bytes(
GBytes* bytes);
Expand Down

0 comments on commit f6da896

Please sign in to comment.