Skip to content

Commit

Permalink
res_phreaknet: Fix lookup and add exists to PHREAKNET.
Browse files Browse the repository at this point in the history
The lookup function was not properly parsing the number
to look up so only worked for the empty number. We now
also use a default Caller ID so this works from the CLI.

An exists option has also been added to allow easily
confirming number existence from the dialplan.
  • Loading branch information
InterLinked1 committed Dec 27, 2023
1 parent 4da9dde commit 1ab9fea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
3 changes: 3 additions & 0 deletions apps/app_verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,9 @@ static int outverify_exec(struct ast_channel *chan, const char *data)
}
}
ast_verb(3, "Updating CALLERID(name) from '%s' to '%s'\n", cnam, newcnam);
if (ast_channel_caller(chan)->id.name.str) {
ast_free(ast_channel_caller(chan)->id.name.str);
}
ast_channel_caller(chan)->id.name.str = ast_strdup(newcnam);
ast_trim_blanks(ast_channel_caller(chan)->id.name.str);
}
Expand Down
57 changes: 53 additions & 4 deletions res/res_phreaknet.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,14 @@
<parameter name="property" required="true">
<para>Must be one of the following:</para>
<enumlist>
<enum name="cdr">
<para>W/O. Enable PhreakNet CDR on the channel</para>
</enum>
<enum name="lookup">
<para>Perform a lookup for a PhreakNet number</para>
<para>R/O. Check if a PhreakNet number exists</para>
</enum>
<enum name="cdr">
<para>Enable PhreakNet CDR on the channel</para>
<enum name="lookup">
<para>R/O. Perform a lookup for a PhreakNet number</para>
</enum>
</enumlist>
</parameter>
Expand All @@ -196,6 +199,9 @@
<example title="Enable PhreakNet CDR on a channel">
same => n,Set(PHREAKNET(CDR)=${EXTEN})
</example>
<example title="Check whether 555-1212 is a number that exists">
same => n,Set(exists=${PHREAKNET(exists,5551212)})
</example>
</description>
<see-also>
<ref type="application">PhreakNetDial</ref>
Expand Down Expand Up @@ -1819,18 +1825,61 @@ static int lookup_number_token(struct ast_channel *chan, const char *number, con
return 0;
}

static int number_exists(const char *number)
{
struct ast_str *str;
char url[256];
int i;

snprintf(url, sizeof(url), "https://api.phreaknet.org/v1/exists?key=%s&number=%s", interlinked_api_key, number);
str = curl_get(url, NULL);
if (!str) {
return 0;
}
i = atoi(S_OR(ast_str_buffer(str), ""));
ast_free(str);
return i;
}

static int phreaknet_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
{
if (!chan) {
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
return -1;
}
if (!strcasecmp(data, "lookup")) {
if (ast_begins_with(data, "lookup")) {
char *number = strchr(data, ',');
if (number) {
*number++ = '\0';
}
if (!ast_channel_caller(chan)->id.number.str) {
const char *maindisa;
/* If there's no Caller ID (such as when querying from the CLI), this will fail.
* Use a generic Caller ID assigned to this node to get a valid response. */
ast_channel_lock(chan);
/* It's a global variable, so don't even bother providing a channel: */
maindisa = pbx_builtin_getvar_helper(NULL, "mainphreaknetdisa");
if (maindisa) {
ast_channel_caller(chan)->id.number.str = ast_strdup(maindisa);
if (ast_channel_caller(chan)->id.number.str) {
ast_channel_caller(chan)->id.number.valid = 1;
}
}
ast_channel_unlock(chan);
}
return lookup_number(chan, number, "", buf, len);
} else if (ast_begins_with(data, "exists")) {
const char *number = strchr(data, ',');
if (!number) {
ast_log(LOG_ERROR, "Missing number\n");
return -1;
}
number++;
if (ast_strlen_zero(number)) {
ast_log(LOG_ERROR, "Missing number\n");
return -1;
}
ast_copy_string(buf, number_exists(number) ? "1" : "0", len);
} else if (!strcasecmp(data, "CDR")) {
/* PhreakNet CDR already enabled on this channel? */
ast_copy_string(buf, cdr_channel_applicable(ast_channel_name(chan), 0) ? "1" : "0", len);
Expand Down

0 comments on commit 1ab9fea

Please sign in to comment.