package net.gotev.sipservice;

import org.pjsip.pjsua2.CallInfo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Obtains display name and remote uri from a CallInfo object.
 * @author gotev (Aleksandar Gotev)
 */
public class CallerInfo {
    private final Pattern displayNameAndRemoteUriPattern = Pattern.compile("^\"([^\"]+).*?sip:(.*?)>$");
    private final Pattern remoteUriPattern = Pattern.compile("^.*?sip:(.*?)>$");

    private static final String UNKNOWN = "Unknown";

    private String displayName;
    private String remoteUri;

    public CallerInfo(final CallInfo callInfo) {
        String temp = callInfo.getRemoteUri();

        if (temp == null || temp.isEmpty()) {
            displayName = remoteUri = UNKNOWN;
            return;
        }

        Matcher completeInfo = displayNameAndRemoteUriPattern.matcher(temp);
        if (completeInfo.matches()) {
            displayName = completeInfo.group(1);
            remoteUri = completeInfo.group(2);

        } else {
            Matcher remoteUriInfo = remoteUriPattern.matcher(temp);
            if (remoteUriInfo.matches()) {
                displayName = remoteUri = remoteUriInfo.group(1);
            } else {
                displayName = remoteUri = UNKNOWN;
            }
        }
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getRemoteUri() {
        return remoteUri;
    }
}

@@ -260,194 +260,8 @@
 fo %7B
-%0A    private final Pattern displayNameAndRemoteUriPattern = Pattern.compile(%22%5E%5C%22(%5B%5E%5C%22%5D+).*?sip:(.*?)%3E$%22);%0A    private final Pattern remoteUriPattern = Pattern.compile(%22%5E.*?sip:(.*?)%3E$%22);
 %0A%0A  
@@ -592,24 +592,119 @@
 %0A        %7D%0A%0A
+        Pattern displayNameAndRemoteUriPattern = Pattern.compile(%22%5E%5C%22(%5B%5E%5C%22%5D+).*?sip:(.*?)%3E$%22);%0A
         Matc
@@ -899,24 +899,24 @@
 .group(2);%0A%0A
-
         %7D el
@@ -912,32 +912,107 @@
        %7D else %7B%0A
+            Pattern remoteUriPattern = Pattern.compile(%22%5E.*?sip:(.*?)%3E$%22);%0A
             Matc

make local variables in CallerInfo