Seems like the list server rejected my last email because of the
attachment, here goes again with the patch inline.

I've noticed there were no way for Kannel to do service routing based
on the account field. This is specially handy when you use an HTTP
module to receive messages and the smsc gets encoded on the account
field, so you get a single smsc with different accounts depending on
the carrier.

I'm attaching a patch that adds two new sms-service parameters:
accepted-account and accepted-account-regex. Their functionality it's
exactly as their smsc counterparts (accepted-smsc and
accepted-smsc-regex) only that they do their logic on the account
field.

The patch can also be downloaded from:

http://www.magicom-bcn.net/kannel/account-routing.patch

Hope it helps,

--
Alejandro Guerrieri
Magicom
http://www.magicom-bcn.net/
LinkedIn: http://www.linkedin.com/in/aguerrieri

-----BEGIN-----

Index: gw/smsbox.c
===================================================================
RCS file: /home/cvs/gateway/gw/smsbox.c,v
retrieving revision 1.267
diff -u -r1.267 smsbox.c
--- gw/smsbox.c 12 Jul 2006 18:44:55 -0000      1.267
+++ gw/smsbox.c 22 Jul 2006 02:27:17 -0000
@@ -1743,7 +1743,7 @@

        } else {
            trans = urltrans_find(translations, msg->sms.msgdata,
-                             msg->sms.smsc_id, msg->sms.sender, 
msg->sms.receiver);
+                             msg->sms.smsc_id, msg->sms.sender,
msg->sms.receiver, msg->sms.account);
            if (trans == NULL) {
                warning(0, "No translation found for <%s> from <%s> to <%s>",
                    octstr_get_cstr(msg->sms.msgdata),
Index: gw/urltrans.c
===================================================================
RCS file: /home/cvs/gateway/gw/urltrans.c,v
retrieving revision 1.101
diff -u -r1.101 urltrans.c
--- gw/urltrans.c       24 Apr 2006 21:32:01 -0000      1.101
+++ gw/urltrans.c       22 Jul 2006 02:27:24 -0000
@@ -102,6 +102,8 @@
    Octstr *footer;     /* string to be appended to each SMS */
    List *accepted_smsc; /* smsc id's allowed to use this service. If not set,
                            all messages can use this service */
+    List *accepted_account; /* account id's allowed to use this
service. If not set,
+                           all messages can use this service */

    Octstr *name;       /* Translation name */
    Octstr *username;   /* send sms username */
@@ -130,6 +132,7 @@

    regex_t *keyword_regex;       /* the compiled regular expression
for the keyword*/
    regex_t *accepted_smsc_regex;
+    regex_t *accepted_account_regex;
    regex_t *allowed_prefix_regex;
    regex_t *denied_prefix_regex;
    regex_t *allowed_receiver_prefix_regex;
@@ -159,12 +162,12 @@
static void destroy_onetrans(void *ot);
static URLTranslation *find_translation(URLTranslationList *trans,
                                        List *words, Octstr *smsc,
-                                       Octstr *sender, Octstr *receiver, int 
*reject);
+                                       Octstr *sender, Octstr *receiver, int 
*reject, Octstr *account);
static URLTranslation *find_default_translation(URLTranslationList *trans,
                                                Octstr *smsc, Octstr *sender, 
Octstr *receiver,
-                                               int *reject);
+                                               int *reject, Octstr *account);
static URLTranslation *find_black_list_translation(URLTranslationList *trans,
-                                               Octstr *smsc);
+                                               Octstr *smsc, Octstr *account);


/***********************************************************************
@@ -273,7 +276,7 @@


URLTranslation *urltrans_find(URLTranslationList *trans, Octstr *text,
-                             Octstr *smsc, Octstr *sender, Octstr *receiver)
+                             Octstr *smsc, Octstr *sender, Octstr *receiver, 
Octstr *account)
{
    List *words;
    URLTranslation *t = NULL;
@@ -282,16 +285,16 @@
    /* do not panic if text == NULL */
    if (text != NULL) {
        words = octstr_split_words(text);
-        t = find_translation(trans, words, smsc, sender, receiver, &reject);
+        t = find_translation(trans, words, smsc, sender, receiver,
&reject, account);
        gwlist_destroy(words, octstr_destroy_item);
    }

    if (reject)
-       t = find_black_list_translation(trans, smsc);
+       t = find_black_list_translation(trans, smsc, account);
    if (t == NULL) {
-       t = find_default_translation(trans, smsc, sender, receiver, &reject);
+       t = find_default_translation(trans, smsc, sender, receiver, &reject, 
account);
        if (reject)
-           t = find_black_list_translation(trans, smsc);
+           t = find_black_list_translation(trans, smsc, account);
    }
    return t;
}
@@ -874,10 +877,11 @@
{
    URLTranslation *ot;
    Octstr *aliases, *url, *post_url, *post_xml, *text, *file, *exec;
-    Octstr *accepted_smsc, *forced_smsc, *default_smsc;
+    Octstr *accepted_smsc, *accepted_account, *forced_smsc, *default_smsc;
    Octstr *grpname, *sendsms_user, *sms_service;
    int is_sms_service;
    Octstr *accepted_smsc_regex;
+    Octstr *accepted_account_regex;
    Octstr *allowed_prefix_regex;
    Octstr *denied_prefix_regex;
    Octstr *allowed_receiver_prefix_regex;
@@ -921,6 +925,7 @@
    ot->password = NULL;
    ot->omit_empty = 0;
    ot->accepted_smsc = NULL;
+    ot->accepted_account = NULL;
    ot->forced_smsc = NULL;
    ot->default_smsc = NULL;
    ot->allow_ip = NULL;
@@ -933,6 +938,7 @@
    ot->black_list = NULL;
    ot->keyword_regex = NULL;
    ot->accepted_smsc_regex = NULL;
+    ot->accepted_account_regex = NULL;
    ot->allowed_prefix_regex = NULL;
    ot->denied_prefix_regex = NULL;
    ot->allowed_receiver_prefix_regex = NULL;
@@ -1019,12 +1025,23 @@
            ot->accepted_smsc = octstr_split(accepted_smsc, octstr_imm(";"));
            octstr_destroy(accepted_smsc);
        }
+       accepted_account = cfg_get(grp, octstr_imm("accepted-account"));
+       if (accepted_account != NULL) {
+           ot->accepted_account = octstr_split(accepted_account, 
octstr_imm(";"));
+           octstr_destroy(accepted_account);
+       }
        accepted_smsc_regex = cfg_get(grp, octstr_imm("accepted-smsc-regex"));
        if (accepted_smsc_regex != NULL) {
            if ( (ot->accepted_smsc_regex =
gw_regex_comp(accepted_smsc_regex, REG_EXTENDED)) == NULL)
            panic(0, "Could not compile pattern '%s'",
octstr_get_cstr(accepted_smsc_regex));
            octstr_destroy(accepted_smsc_regex);
        }
+        accepted_account_regex = cfg_get(grp,
octstr_imm("accepted-account-regex"));
+        if (accepted_account_regex != NULL) {
+            if ( (ot->accepted_account_regex =
gw_regex_comp(accepted_account_regex, REG_EXTENDED)) == NULL)
+            panic(0, "Could not compile pattern '%s'",
octstr_get_cstr(accepted_account_regex));
+            octstr_destroy(accepted_account_regex);
+        }

        cfg_get_bool(&ot->assume_plain_text, grp,
                     octstr_imm("assume-plain-text"));
@@ -1187,6 +1204,7 @@
        octstr_destroy(ot->header);
        octstr_destroy(ot->footer);
        gwlist_destroy(ot->accepted_smsc, octstr_destroy_item);
+       gwlist_destroy(ot->accepted_account, octstr_destroy_item);
        octstr_destroy(ot->name);
        octstr_destroy(ot->username);
        octstr_destroy(ot->password);
@@ -1202,6 +1220,7 @@
        numhash_destroy(ot->black_list);
        if (ot->keyword_regex != NULL) gw_regex_destroy(ot->keyword_regex);
        if (ot->accepted_smsc_regex != NULL)
gw_regex_destroy(ot->accepted_smsc_regex);
+        if (ot->accepted_account_regex != NULL)
gw_regex_destroy(ot->accepted_account_regex);
        if (ot->allowed_prefix_regex != NULL)
gw_regex_destroy(ot->allowed_prefix_regex);
        if (ot->denied_prefix_regex != NULL)
gw_regex_destroy(ot->denied_prefix_regex);
        if (ot->allowed_receiver_prefix_regex != NULL)
gw_regex_destroy(ot->allowed_receiver_prefix_regex);
@@ -1244,7 +1263,7 @@
 * reject will be set to 1 is a number is rejected due to white/black-lists.
 */
static int check_allowed_translation(URLTranslation *t,
-                  Octstr *smsc, Octstr *sender, Octstr *receiver, int *reject)
+                  Octstr *smsc, Octstr *sender, Octstr *receiver, int
*reject, Octstr *account)
{
    const int IS_ALLOWED = 0;
    const int NOT_ALLOWED = -1;
@@ -1258,6 +1277,15 @@
    if (smsc && t->accepted_smsc_regex && gw_regex_match_pre(
t->accepted_smsc_regex, smsc) == 0)
        return NOT_ALLOWED;

+    /* if account_id set and accepted_account exist, accept
+     * translation only if smsc id is in accept string
+     */
+    if (account && t->accepted_account &&
!gwlist_search(t->accepted_account, account, octstr_item_match))
+        return NOT_ALLOWED;
+
+    if (account && t->accepted_account_regex && gw_regex_match_pre(
t->accepted_account_regex, account) == 0)
+        return NOT_ALLOWED;
+
    /* Have allowed for sender */
    if (t->allowed_prefix && !t->denied_prefix &&
does_prefix_match(t->allowed_prefix, sender) != 1)
        return NOT_ALLOWED;
@@ -1364,7 +1392,7 @@
 * Find the appropriate translation
 */
static URLTranslation *find_translation(URLTranslationList *trans,
-                    List *words, Octstr *smsc, Octstr *sender, Octstr
*receiver, int *reject)
+                    List *words, Octstr *smsc, Octstr *sender, Octstr
*receiver, int *reject, Octstr *account)
{
    Octstr *keyword;
    int i, n;
@@ -1389,7 +1417,7 @@
    for (i = 0; i < gwlist_len(list); ++i) {
        t = gwlist_get(list, i);

-        if (check_allowed_translation(t, smsc, sender, receiver, reject) == 0
+        if (check_allowed_translation(t, smsc, sender, receiver,
reject, account) == 0
            && check_num_args(t, words) == 0)
            break;

@@ -1408,7 +1436,7 @@

static URLTranslation *find_default_translation(URLTranslationList *trans,
                                                Octstr *smsc, Octstr *sender, 
Octstr *receiver,
-                                               int *reject)
+                                               int *reject, Octstr *account)
{
    URLTranslation *t;
    int i;
@@ -1421,7 +1449,7 @@
    for (i = 0; i < gwlist_len(list); ++i) {
        t = gwlist_get(list, i);

-    if (check_allowed_translation(t, smsc, sender, receiver, reject) == 0)
+    if (check_allowed_translation(t, smsc, sender, receiver, reject,
account) == 0)
        break;

            t = NULL;
@@ -1434,7 +1462,7 @@
}

static URLTranslation *find_black_list_translation(URLTranslationList *trans,
-                                               Octstr *smsc)
+                                               Octstr *smsc, Octstr *account)
{
    URLTranslation *t;
    int i;
@@ -1450,6 +1478,12 @@
                continue;
            }
        }
+       if (account && t->accepted_account) {
+           if (!gwlist_search(t->accepted_account, account, 
octstr_item_match)) {
+               t = NULL;
+               continue;
+           }
+       }
        break;
    }
    return t;
Index: gw/urltrans.h
===================================================================
RCS file: /home/cvs/gateway/gw/urltrans.h,v
retrieving revision 1.32
diff -u -r1.32 urltrans.h
--- gw/urltrans.h       11 Feb 2005 15:35:48 -0000      1.32
+++ gw/urltrans.h       22 Jul 2006 02:27:24 -0000
@@ -166,9 +166,12 @@
 *
 * If 'smsc' is set, only accept translation with no 'accepted-smsc' set or
 * with matching smsc in that list.
+ *
+ * If 'account' is set, only accept translation with no
'accepted-account' set or
+ * with matching account in that list.
 */
URLTranslation *urltrans_find(URLTranslationList *trans, Octstr *text,
-                             Octstr *smsc, Octstr *sender, Octstr *receiver);
+                             Octstr *smsc, Octstr *sender, Octstr *receiver,
Octstr *account);

/*
 * Find the translation that corresponds to a given name
Index: gwlib/cfg.def
===================================================================
RCS file: /home/cvs/gateway/gwlib/cfg.def,v
retrieving revision 1.119
diff -u -r1.119 cfg.def
--- gwlib/cfg.def       12 Jul 2006 18:44:55 -0000      1.119
+++ gwlib/cfg.def       22 Jul 2006 02:27:28 -0000
@@ -386,6 +386,8 @@
    OCTSTR(exec)
    OCTSTR(accepted-smsc)
    OCTSTR(accepted-smsc-regex)
+    OCTSTR(accepted-account)
+    OCTSTR(accepted-account-regex)
    OCTSTR(forced-smsc)
    OCTSTR(default-smsc)
    OCTSTR(faked-sender)
Index: test/test_urltrans.c
===================================================================
RCS file: /home/cvs/gateway/test/test_urltrans.c,v
retrieving revision 1.10
diff -u -r1.10 test_urltrans.c
--- test/test_urltrans.c        11 Feb 2005 15:35:49 -0000      1.10
+++ test/test_urltrans.c        22 Jul 2006 02:27:30 -0000
@@ -121,7 +121,7 @@
        while (repeats-- > 0) {
                for (i = optind + 1; i < argc; ++i) {
                        url = octstr_create(argv[i]);
-                       t = urltrans_find(list, url, NULL, NULL, NULL);
+                       t = urltrans_find(list, url, NULL, NULL, NULL, NULL);
                        info(0, "type = %d", urltrans_type(t));
                        octstr_destroy(url);
                }


----END----

Reply via email to