### Description
alias_db_lookup is returning `-1` if result is not found. The documentation says "The function returns TRUE if R-URI is alias and it was replaced by user's SIP uri." The implication is that it should return FALSE if that is not the case. Sample code:
``` $vn(init_ruri) = $ru; $vn(alias_found) = alias_db_lookup("DbAliases"); xlogl("L_NOTIFY", "$$vn(alias_found) is [$vn(alias_found)]\n"); if ( !$vn(alias_found) ) { xlogl("L_NOTIFY", "No alias found for [$vn(init_ruri)]\n"); return; }
xlogl("L_NOTIFY", "Found alias [$ru] for [$vn(init_ruri)]\n"); ```
The value for $vn(alias_found) is -1 if there is no alias, and thus the condition never occurs. "Found alias" line always occurs, even if there is no record in the DB.
I think this should probably be considered a documentation issue, but I'm not sure what the other possible values are for return and their meanings. Maybe there's a different value if an alias is found but not updated, etc?
### Troubleshooting
Example above
#### Reproduction
Example above.
#### Debugging Data
Not applicable
#### Log Messages
N/A
#### SIP Traffic
N/A
### Possible Solutions
Probably best to update documentation. The statement, "The function returns TRUE if R-URI is alias and it was replaced by user's SIP uri." implies that if that does not happen, then the return would be FALSE.
### Additional Information
`5.4.1`
* **Operating System**:
Debian 10
This is in line with expectations.
- <0 (negative value) - it is evaluated to false - >0 (positive value) - it is evaluated to true - =0 (zero) - it is evaluated as exit (stop execution of configuration file)
https://www.kamailio.org/wiki/tutorials/faq/main
Is my problem with the use of the negation operator? Or should I be calling the function in the condition instead of assigning to a variable and then evaluating the variable?
``` $vn(alias_found) = alias_db_lookup("DbAliases"); if ( !$vn(alias_found) ) { ## I would expect this to execute if no alias is found. return; } ## I would expect to reach here if the alias is found. ```
``` if (alias_db_lookup("DbAliases")) { # alias found / r-uri changed } else { # not found } ```
You could also use case or other methods as well... but in essence you're setting the $vn(alias_found) to the value... so there will be a value in most cases... positive or negative.
Thank you for the clarification. I think that's enough to close the ticket. It's not "as *I* expect," but it's easier to change my expectations to the existing behavior than to expect the world to revolve around me.
Closed #2517.
The rules for evaluating the return code of the function does not apply for (pseudo-)variables, otherwise it will not be possible to use variables with value 0 in logical expressions.
This function evaluation is not common along other scripting languages, but it is how it was added at the beginning of the project and it has been kept for backward compatibility. Other scripting languages have their own oddities , like Lua using 1-based index, python using whitespace-based incremented blocks, etc ... one has to learn this bits as it starts using a new scripting language...
I extended the FAQ to highlight the behaviour for pseudo-variables vs functions return code.