Index: addons/chan_ooh323.c
diff -Nau addons/chan_ooh323.c.orig addons/chan_ooh323.c
--- addons/chan_ooh323.c.orig	2011-08-09 18:13:09.000000000 +0200
+++ addons/chan_ooh323.c	2011-09-14 14:29:50.740457577 +0200
@@ -24,6 +24,12 @@
 
 #include "chan_ooh323.h"
 #include <math.h>
+#if defined __SVR4 && defined __sun
+#include <sys/sockio.h>
+#ifndef IPTOS_MINCOST
+#define IPTOS_MINCOST 0x02
+#endif
+#endif
 
 #define FORMAT_STRING_SIZE	512
 
Index: addons/ooh323c/src/ooCmdChannel.c
diff -Nau addons/ooh323c/src/ooCmdChannel.c.orig addons/ooh323c/src/ooCmdChannel.c
--- addons/ooh323c/src/ooCmdChannel.c.orig	2011-08-04 21:37:16.000000000 +0200
+++ addons/ooh323c/src/ooCmdChannel.c	2011-09-14 14:29:50.740457577 +0200
@@ -25,6 +25,10 @@
 #include "ooCalls.h"
 #include "ooCmdChannel.h"
 
+#ifndef AF_LOCAL
+#define AF_LOCAL AF_UNIX
+#define PF_LOCAL PF_UNIX
+#endif
 
 /** Global endpoint structure */
 extern OOH323EndPoint gH323ep;
Index: addons/ooh323c/src/ooSocket.c
diff -Nau addons/ooh323c/src/ooSocket.c.orig addons/ooh323c/src/ooSocket.c
--- addons/ooh323c/src/ooSocket.c.orig	2011-05-04 22:50:18.000000000 +0200
+++ addons/ooh323c/src/ooSocket.c	2011-09-14 14:29:50.740457577 +0200
@@ -24,6 +24,9 @@
 
 #include "ooSocket.h"
 #include "ootrace.h"
+#if defined __SVR4 && defined __sun
+#include <sys/sockio.h>
+#endif
 #if defined(_WIN32_WCE)
 static int inited = 0;
 #define SEND_FLAGS     0
Index: addons/ooh323cDriver.c
diff -Nau addons/ooh323cDriver.c.orig addons/ooh323cDriver.c
--- addons/ooh323cDriver.c.orig	2011-02-18 01:07:20.000000000 +0100
+++ addons/ooh323cDriver.c	2011-09-14 14:29:50.740457577 +0200
@@ -27,6 +27,11 @@
 
 #define SEC_TO_HOLD_THREAD 24
 
+#ifndef AF_LOCAL
+#define AF_LOCAL AF_UNIX
+#define PF_LOCAL PF_UNIX
+#endif
+
 extern struct ast_module *myself;
 extern OOBOOL gH323Debug;
 extern OOH323EndPoint gH323ep;
Index: apps/app_backticks.c
diff -Nau apps/app_backticks.c.orig apps/app_backticks.c
--- apps/app_backticks.c.orig	1970-01-01 01:00:00.000000000 +0100
+++ apps/app_backticks.c	2011-09-14 14:29:50.740457577 +0200
@@ -0,0 +1,129 @@
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 1.52 $")
+
+#include <stdio.h> 
+#include <asterisk/file.h>
+#include <asterisk/logger.h>
+#include <asterisk/channel.h>
+#include <asterisk/pbx.h>
+#include <asterisk/module.h>
+#include <asterisk/lock.h>
+#include <asterisk/app.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+static char *app      = "BackTicks";
+static char *synopsis = "Execute a shell command and save the result as a variable.";
+static char *desc     = "  Backticks(<VARNAME>|<command>)\n\n"
+                        "Be sure to include a full path to the command!\n";
+
+static char *do_backticks(char *command, char *buf, size_t len) 
+{
+    int fds[2], pid = 0;
+    char *ret = NULL;
+
+    memset(buf, 0, len);
+    if (pipe(fds)) {    
+        ast_log(LOG_WARNING, "Pipe/Exec failed\n");
+    } else {
+        pid = fork();
+        if (pid < 0) {
+            ast_log(LOG_WARNING, "Fork failed\n");
+            close(fds[0]);
+            close(fds[1]);
+        } else if (pid) {
+            /* parent */
+            close(fds[1]);
+            read(fds[0], buf, len);
+            close(fds[0]);
+            ret = buf;
+        } else {
+            /* child */
+            char *argv[255] = {0};
+            int argc = 0;
+            char *p;
+            char *mycmd = ast_strdupa(command);
+            close(fds[0]);
+            dup2(fds[1], STDOUT_FILENO);
+            argv[argc++] = mycmd;
+            do {
+                if ((p = strchr(mycmd, ' '))) {
+                    *p = '\0';
+                    mycmd = ++p;
+                    argv[argc++] = mycmd;
+                }
+            } while (p != NULL);
+            close(fds[1]);          
+            execv(argv[0], argv); 
+            ast_log(LOG_ERROR, "exec of %s failed\n", argv[0]);
+            exit(0);
+        }
+    }
+    return ret;
+}
+
+static int backticks_exec(struct ast_channel *chan, void *data)
+{
+    int res = 0;
+    const char *usage = "Usage: Backticks(<VARNAME>|<command>)";
+    char buf[1024], *argv[2], *mydata;
+    int argc = 0;
+    
+    if (!data) {
+        ast_log(LOG_WARNING, "%s\n", usage);
+        return -1;
+    }
+    ast_autoservice_start(chan);
+    if (!(mydata = ast_strdupa(data))) {
+        ast_log(LOG_ERROR, "Memory Error!\n");
+        res = -1;
+    } else {
+        if((argc = ast_app_separate_args(mydata, '|', argv, sizeof(argv) / sizeof(argv[0]))) < 2) {
+            ast_log(LOG_WARNING, "%s\n", usage);
+            res = -1;
+        }
+        if (do_backticks(argv[1], buf, sizeof(buf)))
+            pbx_builtin_setvar_helper(chan, argv[0], buf);
+        else {
+            ast_log(LOG_WARNING, "No Data!\n");
+            res = -1;
+        }
+    }
+    ast_autoservice_stop(chan);
+    return res;
+}
+
+static int function_backticks(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+    if (!do_backticks(data, buf, len)) {
+        ast_log(LOG_WARNING, "No Data!\n");
+        return -1;
+    }
+    return 0;
+}
+
+static struct ast_custom_function backticks_function = {
+    .name     = "BACKTICKS", 
+    .desc     = "Executes a shell command and evaluates to the result.", 
+    .syntax   = "BACKTICKS(<command>)", 
+    .synopsis = "Executes a shell command.", 
+    .read     = function_backticks
+};
+
+static int unload_module(void)
+{
+    ast_custom_function_unregister(&backticks_function);
+    return ast_unregister_application(app);
+}
+
+static int load_module(void)
+{
+    ast_custom_function_register(&backticks_function);
+    return ast_register_application(app, backticks_exec, synopsis, desc);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "BACKTICKS() dialplan function");
+
Index: apps/app_meetme.c
diff -Nau apps/app_meetme.c.orig apps/app_meetme.c
--- apps/app_meetme.c.orig	2011-07-19 17:43:32.000000000 +0200
+++ apps/app_meetme.c	2011-09-14 14:29:50.747960016 +0200
@@ -607,6 +607,7 @@
 
 /*! Do not write any audio to this channel until the state is up. */
 #define CONFFLAG_NO_AUDIO_UNTIL_UP  (1ULL << 31)
+#define CONFFLAG_USERNAME (1 << 32)
 /*! If set play an intro announcement at start of conference */
 #define CONFFLAG_INTROMSG           (1ULL << 32)
 
@@ -618,6 +619,7 @@
 	OPT_ARG_MOH_CLASS = 4,
 	OPT_ARG_INTROMSG = 5,
 	OPT_ARG_ARRAY_SIZE = 6,
+	OPT_ARG_USERNAME = 7,
 };
 
 AST_APP_OPTIONS(meetme_opts, BEGIN_OPTIONS
@@ -651,6 +653,7 @@
 	AST_APP_OPTION('1', CONFFLAG_NOONLYPERSON ),
  	AST_APP_OPTION_ARG('S', CONFFLAG_DURATION_STOP, OPT_ARG_DURATION_STOP),
 	AST_APP_OPTION_ARG('L', CONFFLAG_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
+	AST_APP_OPTION_ARG('n', CONFFLAG_USERNAME, OPT_ARG_USERNAME),
 END_OPTIONS );
 
 static const char * const app = "MeetMe";
@@ -2446,6 +2449,12 @@
 		ast_test_flag64(confflags, CONFFLAG_INTROUSERNOREVIEW))) {
 		char destdir[PATH_MAX];
 
+ 	    if (!ast_test_flag64(confflags, CONFFLAG_USERNAME)
+ 		&& !ast_strlen_zero(optargs[OPT_ARG_USERNAME]) 
+ 		&& ast_fileexists(optargs[OPT_ARG_USERNAME], NULL, NULL))
+ 		snprintf(destdir, sizeof(destdir), "%s", optargs[OPT_ARG_USERNAME]);
+ 	    else {
+
 		snprintf(destdir, sizeof(destdir), "%s/meetme", ast_config_AST_SPOOL_DIR);
 
 		if (ast_mkdir(destdir, 0777) != 0) {
@@ -2462,6 +2471,7 @@
 			res = ast_record_review(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, NULL);
 		if (res == -1)
 			goto outrun;
+	    }
 	}
 
 	ast_mutex_lock(&conf->playlock);
Index: apps/app_voicemail.c
diff -Nau apps/app_voicemail.c.orig apps/app_voicemail.c
--- apps/app_voicemail.c.orig	2011-07-26 16:04:55.000000000 +0200
+++ apps/app_voicemail.c	2011-09-14 14:29:50.747960016 +0200
@@ -373,6 +373,7 @@
 static char imapport[8];
 static char imapflags[128];
 static char imapfolder[64];
+static int  imapsubfold = 0;
 static char imapparentfolder[64] = "\0";
 static char greetingfolder[64];
 static char authuser[32];
@@ -2517,7 +2518,7 @@
 	}
 
 	/* Build up server information */
-	ast_build_string(&t, &left, "{%s:%s/imap", imapserver, imapport);
+	ast_build_string(&t, &left, "{%s:%s", imapserver, imapport);
 
 	/* Add authentication user if present */
 	if (!ast_strlen_zero(authuser))
@@ -6174,6 +6175,7 @@
 	/* simple. huh? */
 	char sequence[10];
 	char mailbox[256];
+	char folder[256];
 	int res;
 
 	/* get the real IMAP message number for this message */
@@ -6189,10 +6191,24 @@
 		mail_setflag(vms->mailstream, sequence, "\\Unseen");
 		mail_clearflag(vms->mailstream, sequence, "\\Seen");
 	}
-	if (!strcasecmp(mbox(vmu, NEW_FOLDER), vms->curbox) && (box == NEW_FOLDER || box == OLD_FOLDER)) {
-		ast_mutex_unlock(&vms->lock);
+
+	if ((!strcasecmp(mbox(vmu, NEW_FOLDER), vms->curbox) || \
+		!strcasecmp(mbox(vmu, OLD_FOLDER), vms->curbox)) && \
+		(box == NEW_FOLDER || box == OLD_FOLDER)) {  /* Don't copy data, */
+		ast_mutex_unlock(&vms->lock);                /* just change Seen flag */
 		return 0;
+	} else if (box != NEW_FOLDER && box != OLD_FOLDER) { /* Do copy data */
+		if (imapsubfold == 1)                /* using INBOX or subfolder */
+			snprintf(folder, sizeof(folder), "%s%c%s", imapfolder, delimiter, mbox(vmu, box));
+		else
+			strncpy(folder, mbox(vmu, box), sizeof(folder));
+		int res = !mail_copy(vms->mailstream,sequence,folder);
+		ast_mutex_unlock(&vms->lock);
+		return res;
+	} else {  /* Copy data to INBOX delegating new/old status to Seen flag */
+		int res = !mail_copy(vms->mailstream,sequence,imapfolder);
 	}
+
 	/* Create the folder if it don't exist */
 	imap_mailbox_name(mailbox, sizeof(mailbox), vms, box, 1); /* Get the full mailbox name */
 	ast_debug(5, "Checking if folder exists: %s\n", mailbox);
@@ -10467,6 +10483,10 @@
 #ifndef IMAP_STORAGE
 				} else if (!cmd) {
 					vms.deleted[vms.curmsg] = 1;
+#else
+				} else if (!cmd && (folder_int(vms.curbox) > 1  || box > 1)) {
+					vms.deleted[vms.curmsg] = 1;  /* Enforce deletion after */
+					deleted = 1;                  /* successful copy op */
 #endif
 				} else {
 					vms.deleted[vms.curmsg] = 0;
@@ -11963,6 +11983,15 @@
 		} else {
 			ast_copy_string(imapfolder, "INBOX", sizeof(imapfolder));
 		}
+		/* IMAP saved (sub)folder location policy */
+		if ((val = ast_variable_retrieve(cfg, "general", "imapsubfold"))) {
+			if (ast_false(val))
+				imapsubfold = 0;
+			else
+				imapsubfold = 1;
+		} else {
+			imapsubfold = 0;
+		}
 		if ((val = ast_variable_retrieve(cfg, "general", "imapparentfolder"))) {
 			ast_copy_string(imapparentfolder, val, sizeof(imapparentfolder));
 		}
Index: cdr/cdr_radius.c
diff -Nau cdr/cdr_radius.c.orig cdr/cdr_radius.c
--- cdr/cdr_radius.c.orig	2011-07-14 22:13:06.000000000 +0200
+++ cdr/cdr_radius.c	2011-09-14 14:29:50.777958246 +0200
@@ -106,10 +106,18 @@
 	if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
 		return -1;
 
+ 	/* RADIUS standard identifier patch */
+	if (!rc_avpair_add(rh, tosend, PW_CALLING_STATION_ID, &cdr->src, strlen(cdr->src), 0))
+		return -1;
+
  	/* Destination */
 	if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
 		return -1;
 
+ 	/* RADIUS standard identifier patch */
+	if (!rc_avpair_add(rh, tosend, PW_CALLED_STATION_ID, &cdr->dst, strlen(cdr->dst), 0))
+		return -1;
+
  	/* Destination context */
 	if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
 		return -1;
@@ -164,6 +172,10 @@
 	if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
 		return -1;
 
+ 	/* RADIUS standard identifier patch */
+	if (!rc_avpair_add(rh, tosend, PW_ACCT_SESSION_TIME, &cdr->billsec, 0, 0))
+		return -1;
+
 	/* Disposition */
 	tmp = ast_cdr_disp2str(cdr->disposition);
 	if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
@@ -187,10 +199,14 @@
 	}
 
 	/* Setting Acct-Session-Id & User-Name attributes for proper generation
-	   of Acct-Unique-Session-Id on server side */
-	/* Channel */
-	if (!rc_avpair_add(rh, tosend, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
-		return -1;
+	   of Acct-Unique-Session-Id on server side Channel */
+	{
+		char szChanuser[PATH_MAX] = {0};
+		strncpy(szChanuser, &cdr->channel, PATH_MAX-1);
+		*(strrchr(szChanuser, '-')) = 0;
+		if (!rc_avpair_add(rh, tosend, PW_USER_NAME, szChanuser, strlen(cdr->channel), 0))
+			return -1;
+	}
 
 	/* Unique ID */
 	if (!rc_avpair_add(rh, tosend, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
Index: chan_capi-1.1.5.20110914/chan_capi20.h
diff -Nau chan_capi-1.1.5.20110914/chan_capi20.h.orig chan_capi-1.1.5.20110914/chan_capi20.h
--- chan_capi-1.1.5.20110914/chan_capi20.h.orig	2011-01-07 02:29:32.000000000 +0100
+++ chan_capi-1.1.5.20110914/chan_capi20.h	2011-05-23 17:35:28.348531751 +0200
@@ -4,10 +4,13 @@
  * first. Else the checks below will fail.
  */
 
+#include <stddef.h>
 #include <capi20.h>
 
 #undef CAPI_OS_HINT
 
+#ifndef USE_OWN_LIBCAPI
+
 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || \
      defined(__NetBSD__)  || defined(__APPLE__))
 
@@ -29,6 +32,8 @@
 #include <capiutils.h>
 #endif /* BSD */
 
+#endif
+
 #ifndef HEADER_CID
 #define HEADER_CID(x) ((x)->adr.adrNCCI)
 #endif
Index: chan_capi-1.1.5.20110914/chan_capi_utils.c
diff -Nau chan_capi-1.1.5.20110914/chan_capi_utils.c.orig chan_capi-1.1.5.20110914/chan_capi_utils.c
--- chan_capi-1.1.5.20110914/chan_capi_utils.c.orig	2011-01-07 02:29:32.000000000 +0100
+++ chan_capi-1.1.5.20110914/chan_capi_utils.c	2011-05-23 17:35:28.348531751 +0200
@@ -1158,6 +1158,9 @@
 {
 	MESSAGE_EXCHANGE_ERROR error;
 	int waitcount = 50;
+#ifndef CAPI_MANUFACTURER_LEN
+#define CAPI_MANUFACTURER_LEN 64
+#endif
 	unsigned char manbuf[CAPI_MANUFACTURER_LEN];
 	_cmsg CMSG;
 
Index: chan_capi-1.1.5.20110914/libcapi20/capi20.c
diff -Nau chan_capi-1.1.5.20110914/libcapi20/capi20.c.orig chan_capi-1.1.5.20110914/libcapi20/capi20.c
--- chan_capi-1.1.5.20110914/libcapi20/capi20.c.orig	2011-01-07 02:29:31.000000000 +0100
+++ chan_capi-1.1.5.20110914/libcapi20/capi20.c	2011-05-23 17:35:28.348531751 +0200
@@ -19,8 +19,10 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <assert.h>
+#ifdef __linux__
 #define _LINUX_LIST_H
 #include <linux/capi.h>
+#endif
  
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -48,17 +50,23 @@
 
 #define SEND_BUFSIZ		(128+2048)
 
+#if 0
 static char capidevname[] = "/dev/capi20";
 static char capidevnamenew[] = "/dev/isdn/capi20";
+#endif
 
 static int                  capi_fd = -1;
+#if 0
 static capi_ioctl_struct    ioctl_data;
+#endif
 
 static int remote_capi;
+#if 0
 static char *globalconfigfilename = "/etc/capi20.conf";
 static char *userconfigfilename = ".capi20rc";
 static unsigned short int port;
 static char hostname[1024];
+#endif
 static int tracelevel;
 static char *tracefile;
 
@@ -77,17 +85,21 @@
 #define RCAPI_AUTH_USER_REQ                     CAPICMD(0xff, 0x00)
 #define RCAPI_AUTH_USER_CONF                    CAPICMD(0xff, 0x01)
 
+#if 0
 static char *skip_whitespace(char *s)
 {
 	while (*s && isspace(*s)) s++;
 		return s;
 }
+#endif
 
+#if 0
 static char *skip_nonwhitespace(char *s)
 {
 	while (*s && !isspace(*s)) s++;
 		return s;
 } 
+#endif
 
 static unsigned char get_byte(unsigned char **p)
 {
@@ -95,10 +107,12 @@
 	return((unsigned char)*(*p - 1));
 }
  
+#if 0
 static unsigned short get_word(unsigned char **p)
 {
 	return(get_byte(p) | (get_byte(p) << 8));
 }
+#endif
 
 static unsigned short get_netword(unsigned char **p)
 {
@@ -144,6 +158,7 @@
  * read config file
  */
 
+#if 0
 static int read_config(void)
 {
 	FILE *fp = NULL;
@@ -197,11 +212,13 @@
 	fclose(fp);
 	return(1);
 }
+#endif
 
 /*
  *	socket function
  */
 
+#if 0
 static int open_socket(void)
 {
 	int fd;
@@ -225,6 +242,7 @@
 	close(fd);
 	return(-1);
 }
+#endif
 
 static int socket_read(int fd, unsigned char *buf, int l)
 {
@@ -328,6 +346,8 @@
 	if (likely(capi_fd >= 0))
 		return CapiNoError;
 
+#if 0
+
 	/*----- open managment link -----*/
 	if (read_config() && (remote_capi)) {
 		capi_fd = open_socket();
@@ -347,6 +367,8 @@
 	if (ioctl(capi_fd, CAPI_INSTALLED, 0) == 0)
 		return CapiNoError;
 
+#endif
+
 	return CapiRegNotInstalled;
 }
 
@@ -421,6 +443,7 @@
 	unsigned char *bufferstart;
 };
 
+#if 0
 static struct applinfo *alloc_buffers(
 	unsigned MaxB3Connection,
 	unsigned MaxB3Blks,
@@ -459,6 +482,7 @@
 	ap->lastfree->next = 0;
 	return ap;
 }
+#endif
 
 static void free_buffers(struct applinfo *ap)
 {
@@ -576,14 +600,17 @@
 	unsigned MaxSizeB3,
 	unsigned *ApplID)
 {
+#if 0
 	int applid = 0;
 	char buf[PATH_MAX];
 	int i, fd = -1;
 
     *ApplID = 0;
+#endif
 
     if (capi20_isinstalled() != CapiNoError)
        return CapiRegNotInstalled;
+#if 0
 	if ((!remote_capi) || ((remote_capi) && ((fd = open_socket()) < 0))) {
 	    if ((fd = open(capidevname, O_RDWR|O_NONBLOCK, 0666)) < 0 && 
 		     (errno == ENOENT)) {
@@ -621,6 +648,8 @@
 			close(fd);
 			return(errcode);
 		}
+    }
+#if 0
 	} else if ((applid = ioctl(fd, CAPI_REGISTER, &ioctl_data)) < 0) {
 		if (errno == EIO) {
 			if (ioctl(fd, CAPI_GET_ERRCODE, &ioctl_data) < 0) {
@@ -666,6 +695,7 @@
 		    applid = alloc_applid(fd);
 		} // end old driver compatibility
 	}
+#endif
 	if (remember_applid(applid, fd) < 0) {
 		close(fd);
 		return CapiRegOSResourceErr;
@@ -676,6 +706,7 @@
 		return CapiRegOSResourceErr;
 	}
 	*ApplID = applid;
+#endif
 	return CapiNoError;
 }
 
@@ -784,11 +815,15 @@
 	                ret = CapiIllAppNr;
 	                break;
 	            case EIO:
+#if 0
 	                if (ioctl(fd, CAPI_GET_ERRCODE, &ioctl_data) < 0) {
+#endif
 	                    ret = CapiMsgOSResourceErr;
+#if 0
 	                } else {
 						ret = (unsigned)ioctl_data.errcode;
 					}
+#endif
 	                break;
 	          default:
 	                ret = CapiMsgOSResourceErr;
@@ -842,7 +877,7 @@
 				rcvbuf[15] = (data >> 24) & 0xff;
 			} else {
 				u_int64_t data;
-				ulong radr = (ulong)rcvbuf;
+				unsigned long radr = (unsigned long)rcvbuf;
 				if (CAPIMSG_LEN(rcvbuf) < 30) {
 					/*
 					 * grr, 64bit arch, but no data64 included,
@@ -899,6 +934,9 @@
 {
 	if (capi20_isinstalled() != CapiNoError)
 		return 0;
+#ifndef CAPI_MANUFACTURER_LEN
+#define CAPI_MANUFACTURER_LEN 64
+#endif
 
 	if (remote_capi) {
 		unsigned char buf[100];
@@ -911,15 +949,19 @@
 		return Buf;
 	}
 
+#if 0
     ioctl_data.contr = Ctrl;
 
 	if (ioctl(capi_fd, CAPI_GET_MANUFACTURER, &ioctl_data) < 0)
+#endif
 		return 0;
 
+#if 0
 	memcpy(Buf, ioctl_data.manufacturer, CAPI_MANUFACTURER_LEN);
 	Buf[CAPI_MANUFACTURER_LEN-1] = 0;
 
 	return Buf;
+#endif
 }
 
 unsigned char *
@@ -934,16 +976,20 @@
 		set_rcapicmd_header(&p, 14, RCAPI_GET_VERSION_REQ, Ctrl);
 		if(!(remote_command(capi_fd, buf, 14, RCAPI_GET_VERSION_CONF)))
 			return 0;
-		memcpy(Buf, buf + 1, sizeof(capi_version));
+		memcpy(Buf, buf + 1, 128 /* sizeof(capi_version) */);
 		return Buf;
 	}
 
+#if 0
 	ioctl_data.contr = Ctrl;
 	if (ioctl(capi_fd, CAPI_GET_VERSION, &ioctl_data) < 0) {
+#endif
 		return 0;
+#if 0
 	}
 	memcpy(Buf, &ioctl_data.version, sizeof(capi_version));
 	return Buf;
+#endif
 }
 
 unsigned char * 
@@ -952,6 +998,10 @@
 	if (capi20_isinstalled() != CapiNoError)
 		return 0;
 
+#ifndef CAPI_SERIAL_LEN
+#define CAPI_SERIAL_LEN 8
+#endif
+
 	if (remote_capi) {
 		unsigned char buf[100];
 		unsigned char *p = buf;
@@ -963,15 +1013,19 @@
 		return Buf;
 	}
 
+#if 0
 	ioctl_data.contr = Ctrl;
 
 	if (ioctl(capi_fd, CAPI_GET_SERIAL, &ioctl_data) < 0)
+#endif
 		return 0;
 
+#if 0
 	memcpy(Buf, &ioctl_data.serial, CAPI_SERIAL_LEN);
 	Buf[CAPI_SERIAL_LEN-1] = 0;
 
 	return Buf;
+#endif
 }
 
 unsigned
@@ -1018,6 +1072,7 @@
 			sizeof(ioctl_data.profile.ncontroller));
 	}
 	return CapiNoError;
+#endif
 }
 /*
  * functions added to the CAPI2.0 spec
Index: chan_capi-1.1.5.20110914/libcapi20/convert.c
diff -Nau chan_capi-1.1.5.20110914/libcapi20/convert.c.orig chan_capi-1.1.5.20110914/libcapi20/convert.c
--- chan_capi-1.1.5.20110914/libcapi20/convert.c.orig	2011-01-07 02:29:31.000000000 +0100
+++ chan_capi-1.1.5.20110914/libcapi20/convert.c	2011-05-23 17:35:28.348531751 +0200
@@ -11,7 +11,14 @@
 #include <stddef.h>
 #include <time.h>
 #include <ctype.h>
+#ifdef __FreeBSD__
+#include <sys/endian.h>
+#define bswap_16 bswap16
+#define bswap_32 bswap32
+#define bswap_64 bswap64
+#else
 #include <byteswap.h>
+#endif
 
 #include "capi20.h"
 
Index: chan_capi-1.1.5.20110914/Makefile
diff -Nau chan_capi-1.1.5.20110914/Makefile.orig chan_capi-1.1.5.20110914/Makefile
--- chan_capi-1.1.5.20110914/Makefile.orig	2011-01-07 02:29:32.000000000 +0100
+++ chan_capi-1.1.5.20110914/Makefile	2011-05-23 17:35:28.348531751 +0200
@@ -114,6 +114,9 @@
 CFLAGS+=-O2
 CFLAGS+=$(shell if $(CC) -march=$(PROC) -S -o /dev/null -xc /dev/null >/dev/null 2>&1; then echo "-march=$(PROC)"; fi)
 CFLAGS+=$(shell if uname -m | grep -q "ppc\|arm\|s390"; then echo "-fsigned-char"; fi)
+ifeq (${USE_OWN_LIBCAPI},yes)
+CFLAGS+=-DUSE_OWN_LIBCAPI
+endif
 ifeq (${DIVA_STREAMING},1)
 CFLAGS += -DDIVA_STREAMING=1
 endif
Index: channels/chan_sip.c
diff -Nau channels/chan_sip.c.orig channels/chan_sip.c
--- channels/chan_sip.c.orig	2011-08-10 00:12:59.000000000 +0200
+++ channels/chan_sip.c	2011-09-14 14:29:50.757959000 +0200
@@ -11650,7 +11650,16 @@
  	} else {
  		if (sipmethod == SIP_NOTIFY && !ast_strlen_zero(p->theirtag)) {
  			/* If this is a NOTIFY, use the From: tag in the subscribe (RFC 3265) */
-			snprintf(to, sizeof(to), "<%s%s>;tag=%s", (strncasecmp(p->uri, "sip:", 4) ? "sip:" : ""), p->uri, p->theirtag);
+			if (strncasecmp(p->uri, "sip:", strlen("sip:")))
+				if (strncasecmp(p->uri, "sips:", strlen("sips:")))
+					if (p->socket.type == SIP_TRANSPORT_TLS)
+						snprintf(to, sizeof(to), "<%s%s>;tag=%s", "sips:", p->uri, p->theirtag);
+					else
+						snprintf(to, sizeof(to), "<%s%s>;tag=%s", "sips:", p->uri, p->theirtag);
+				else /* if (strncasecmp(p->uri, "sips:"... */
+					snprintf(to, sizeof(to), "<%s%s>;tag=%s", "", p->uri, p->theirtag);
+			else /* if (strncasecmp(p->uri, "sip:"... */
+				snprintf(to, sizeof(to), "<%s%s>;tag=%s", "", p->uri, p->theirtag);
  		} else if (p->options && p->options->vxml_url) {
  			/* If there is a VXML URL append it to the SIP URL */
  			snprintf(to, sizeof(to), "<%s>;%s", p->uri, p->options->vxml_url);
Index: channels/console_video.h
diff -Nau channels/console_video.h.orig channels/console_video.h
--- channels/console_video.h.orig	2008-06-30 17:45:15.000000000 +0200
+++ channels/console_video.h	2011-09-14 14:29:50.767970949 +0200
@@ -28,10 +28,7 @@
 		"console {device}"
 #else
 
-#include <ffmpeg/avcodec.h>
-#ifndef OLD_FFMPEG
-#include <ffmpeg/swscale.h>     /* requires a recent ffmpeg */
-#endif
+#include <libavcoded/avcodec.h>
 
 #define CONSOLE_VIDEO_CMDS			\
 	"console {videodevice|videocodec"	\
Index: configure
diff -Nau configure.orig configure
--- configure.orig	2011-08-25 21:08:04.000000000 +0200
+++ configure	2011-09-14 14:29:50.767970949 +0200
@@ -4244,11 +4244,6 @@
 esac
 
 case "${host_os}" in
-     freebsd*)
-     ac_default_prefix=/usr/local
-     CPPFLAGS=-I/usr/local/include
-     LDFLAGS=-L/usr/local/lib
-     ;;
      openbsd*)
      ac_default_prefix=/usr/local
      if test ${prefix} = '/usr/local' || test ${prefix} = 'NONE'; then
@@ -29617,8 +29612,8 @@
 		if test -f "${IMAP_TK_DIR}/c-client/LDFLAGS"; then
       		imap_ldflags=`cat ${IMAP_TK_DIR}/c-client/LDFLAGS`
 		fi
-		imap_libs="${IMAP_TK_DIR}/c-client/c-client.a"
-	  	imap_include="-I${IMAP_TK_DIR}/c-client"
+		imap_libs="-limap -lssl -lcrypto -lcrypt"
+	  	imap_include="-DUSE_SYSTEM_IMAP -I${IMAP_TK_DIR}/include/imap"
       	CPPFLAGS="${CPPFLAGS} ${imap_include}"
 	  	LIBS="${LIBS} ${imap_libs} "`echo ${imap_ldflags}`
 	  	cat >conftest.$ac_ext <<_ACEOF
@@ -47493,7 +47488,7 @@
       	 pbxlibdir="-L${SQLITE_DIR}"
       fi
    fi
-   pbxfuncname="sqlite_exec"
+   pbxfuncname="sqlite3_exec"
    if test "x${pbxfuncname}" = "x" ; then   # empty lib, assume only headers
       AST_SQLITE_FOUND=yes
    else
@@ -49185,16 +49180,16 @@
     if test "x${PBX_GMIME}" != "x1" -a "${USE_GMIME}" != "no"; then
 	PBX_GMIME=0
 	if test -n "$ac_tool_prefix"; then
-  # Extract the first word of "${ac_tool_prefix}gmime-config", so it can be a program name with args.
-set dummy ${ac_tool_prefix}gmime-config; ac_word=$2
+  # Extract the first word of "${ac_tool_prefix}pkg-config", so it can be a program name with args.
+set dummy ${ac_tool_prefix}pkg-config; ac_word=$2
 { echo "$as_me:$LINENO: checking for $ac_word" >&5
 echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6; }
-if test "${ac_cv_path_CONFIG_GMIME+set}" = set; then
+if test "${ac_cv_path_PKGCONFIG+set}" = set; then
   echo $ECHO_N "(cached) $ECHO_C" >&6
 else
-  case $CONFIG_GMIME in
+  case $PKGCONFIG in
   [\\/]* | ?:[\\/]*)
-  ac_cv_path_CONFIG_GMIME="$CONFIG_GMIME" # Let the user override the test with a path.
+  ac_cv_path_PKGCONFIG="$PKGCONFIG" # Let the user override the test with a path.
   ;;
   *)
   as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
@@ -49205,7 +49200,7 @@
   test -z "$as_dir" && as_dir=.
   for ac_exec_ext in '' $ac_executable_extensions; do
   if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
-    ac_cv_path_CONFIG_GMIME="$as_dir/$ac_word$ac_exec_ext"
+    ac_cv_path_PKGCONFIG="$as_dir/$ac_word$ac_exec_ext"
     echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
   fi
@@ -49216,10 +49211,10 @@
   ;;
 esac
 fi
-CONFIG_GMIME=$ac_cv_path_CONFIG_GMIME
-if test -n "$CONFIG_GMIME"; then
-  { echo "$as_me:$LINENO: result: $CONFIG_GMIME" >&5
-echo "${ECHO_T}$CONFIG_GMIME" >&6; }
+PKGCONFIG=$ac_cv_path_PKGCONFIG
+if test -n "$PKGCONFIG"; then
+  { echo "$as_me:$LINENO: result: $PKGCONFIG" >&5
+echo "${ECHO_T}$PKGCONFIG" >&6; }
 else
   { echo "$as_me:$LINENO: result: no" >&5
 echo "${ECHO_T}no" >&6; }
@@ -49227,18 +49222,18 @@
 
 
 fi
-if test -z "$ac_cv_path_CONFIG_GMIME"; then
-  ac_pt_CONFIG_GMIME=$CONFIG_GMIME
-  # Extract the first word of "gmime-config", so it can be a program name with args.
-set dummy gmime-config; ac_word=$2
+if test -z "$ac_cv_path_PKGCONFIG"; then
+  ac_pt_PKGCONFIG=$PKGCONFIG
+  # Extract the first word of "pkg-config", so it can be a program name with args.
+set dummy pkg-config; ac_word=$2
 { echo "$as_me:$LINENO: checking for $ac_word" >&5
 echo $ECHO_N "checking for $ac_word... $ECHO_C" >&6; }
-if test "${ac_cv_path_ac_pt_CONFIG_GMIME+set}" = set; then
+if test "${ac_cv_path_ac_pt_PKGCONFIG+set}" = set; then
   echo $ECHO_N "(cached) $ECHO_C" >&6
 else
-  case $ac_pt_CONFIG_GMIME in
+  case $ac_pt_PKGCONFIG in
   [\\/]* | ?:[\\/]*)
-  ac_cv_path_ac_pt_CONFIG_GMIME="$ac_pt_CONFIG_GMIME" # Let the user override the test with a path.
+  ac_cv_path_ac_pt_PKGCONFIG="$ac_pt_PKGCONFIG" # Let the user override the test with a path.
   ;;
   *)
   as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
@@ -49249,7 +49244,7 @@
   test -z "$as_dir" && as_dir=.
   for ac_exec_ext in '' $ac_executable_extensions; do
   if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
-    ac_cv_path_ac_pt_CONFIG_GMIME="$as_dir/$ac_word$ac_exec_ext"
+    ac_cv_path_ac_pt_PKGCONFIG="$as_dir/$ac_word$ac_exec_ext"
     echo "$as_me:$LINENO: found $as_dir/$ac_word$ac_exec_ext" >&5
     break 2
   fi
@@ -49260,17 +49255,17 @@
   ;;
 esac
 fi
-ac_pt_CONFIG_GMIME=$ac_cv_path_ac_pt_CONFIG_GMIME
-if test -n "$ac_pt_CONFIG_GMIME"; then
-  { echo "$as_me:$LINENO: result: $ac_pt_CONFIG_GMIME" >&5
-echo "${ECHO_T}$ac_pt_CONFIG_GMIME" >&6; }
+ac_pt_PKGCONFIG=$ac_cv_path_ac_pt_PKGCONFIG
+if test -n "$ac_pt_PKGCONFIG"; then
+  { echo "$as_me:$LINENO: result: $ac_pt_PKGCONFIG" >&5
+echo "${ECHO_T}$ac_pt_PKGCONFIG" >&6; }
 else
   { echo "$as_me:$LINENO: result: no" >&5
 echo "${ECHO_T}no" >&6; }
 fi
 
-  if test "x$ac_pt_CONFIG_GMIME" = x; then
-    CONFIG_GMIME="No"
+  if test "x$ac_pt_PKGCONFIG" = x; then
+    PKGCONFIG="No"
   else
     case $cross_compiling:$ac_tool_warned in
 yes:)
@@ -49282,22 +49277,17 @@
 configuration is useful to you, please write to autoconf@gnu.org." >&2;}
 ac_tool_warned=yes ;;
 esac
-    CONFIG_GMIME=$ac_pt_CONFIG_GMIME
+    PKGCONFIG=$ac_pt_PKGCONFIG
   fi
 else
-  CONFIG_GMIME="$ac_cv_path_CONFIG_GMIME"
+  PKGCONFIG="$ac_cv_path_PKGCONFIG"
 fi
 
-	if test ! "x${CONFIG_GMIME}" = xNo; then
-	    if test x"" = x ; then A=--cflags ; else A="" ; fi
-	    GMIME_INCLUDE=$(${CONFIG_GMIME} $A)
-	    if test x"" = x ; then A=--libs ; else A="" ; fi
-	    GMIME_LIB=$(${CONFIG_GMIME} $A)
+	if test ! "x${PKGCONFIG}" = xNo; then
+	    GMIME_INCLUDE=$(${PKGCONFIG} gmime-2.4 --cflags 2>/dev/null)
+	    GMIME_LIB=$(${PKGCONFIG} gmime-2.4 --libs)
 	    if test x"#include <gmime/gmime.h>" != x ; then
 		saved_cppflags="${CPPFLAGS}"
-		if test "x${GMIME_DIR}" != "x"; then
-		    GMIME_INCLUDE="-I${GMIME_DIR}/include"
-		fi
 		CPPFLAGS="${CPPFLAGS} ${GMIME_INCLUDE}"
 
 		saved_libs="${LIBS}"
Index: formats/format_pcm.c
diff -Nau formats/format_pcm.c.orig formats/format_pcm.c
--- formats/format_pcm.c.orig	2011-07-14 22:13:06.000000000 +0200
+++ formats/format_pcm.c	2011-09-14 14:29:50.767970949 +0200
@@ -354,6 +354,7 @@
 		ast_log(LOG_WARNING, "Unable to write header\n");
 		return -1;
 	}
+    fflush(f);  /* issues.asterisk.org bug 0016610 */
 	return 0;
 }
 
Index: formats/format_wav.c
diff -Nau formats/format_wav.c.orig formats/format_wav.c
--- formats/format_wav.c.orig	2011-07-29 19:18:56.000000000 +0200
+++ formats/format_wav.c	2011-09-14 14:29:50.767970949 +0200
@@ -308,6 +308,7 @@
 		ast_log(LOG_WARNING, "Unable to write header\n");
 		return -1;
 	}
+    fflush(f);  /* issues.asterisk.org bug 0016610 */
 	return 0;
 }
 
Index: formats/format_wav_gsm.c
diff -Nau formats/format_wav_gsm.c.orig formats/format_wav_gsm.c
--- formats/format_wav_gsm.c.orig	2011-07-14 22:13:06.000000000 +0200
+++ formats/format_wav_gsm.c	2011-09-14 14:29:50.767970949 +0200
@@ -366,6 +366,7 @@
 		ast_log(LOG_WARNING, "Unable to write header\n");
 		return -1;
 	}
+    fflush(f);  /* issues.asterisk.org bug 0016610 */
 	return 0;
 }
 
Index: main/db1-ast/hash/hash.h
diff -Nau main/db1-ast/hash/hash.h.orig main/db1-ast/hash/hash.h
--- main/db1-ast/hash/hash.h.orig	2006-08-21 04:11:39.000000000 +0200
+++ main/db1-ast/hash/hash.h	2011-09-14 14:29:50.767970949 +0200
@@ -36,6 +36,8 @@
  *	@(#)hash.h	8.3 (Berkeley) 5/31/94
  */
 
+#include <stdint.h>
+
 /* Operations */
 typedef enum {
 	HASH_GET, HASH_PUT, HASH_PUTNEW, HASH_DELETE, HASH_FIRST, HASH_NEXT
Index: main/db1-ast/hash/ndbm.c
diff -Nau main/db1-ast/hash/ndbm.c.orig main/db1-ast/hash/ndbm.c
--- main/db1-ast/hash/ndbm.c.orig	2006-08-21 04:11:39.000000000 +0200
+++ main/db1-ast/hash/ndbm.c	2011-09-14 14:29:50.767970949 +0200
@@ -49,7 +49,8 @@
 #include <string.h>
 #include <stdlib.h>
 
-#include <ndbm.h>
+#include "../include/ndbm.h"
+#include "../include/db.h"
 #include "hash.h"
 
 /*
Index: main/features.c
diff -Nau main/features.c.orig main/features.c
--- main/features.c.orig	2011-08-10 00:12:59.000000000 +0200
+++ main/features.c	2011-09-14 14:29:50.777958246 +0200
@@ -1922,6 +1922,10 @@
 			snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
 		}
 
+		for(x = 0; x < strlen(touch_filename); x++) {
+			if (args[x] == '/')
+				args[x] = '-';
+		}
 		for(x = 0; x < strlen(args); x++) {
 			if (args[x] == '/')
 				args[x] = '-';
@@ -2038,6 +2042,10 @@
 			snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
 		}
 
+		for( x = 0; x < strlen(touch_filename); x++) {
+			if (args[x] == '/')
+				args[x] = '-';
+		}
 		for( x = 0; x < strlen(args); x++) {
 			if (args[x] == '/')
 				args[x] = '-';
Index: main/file.c
diff -Nau main/file.c.orig main/file.c
--- main/file.c.orig	2011-07-05 15:23:57.000000000 +0200
+++ main/file.c	2011-09-14 14:29:50.777958246 +0200
@@ -256,7 +256,7 @@
 	char *fn = NULL;
 
 	if (!strcmp(ext, "wav49"))
-		ext = "WAV";
+		ext = "wav";
 
 	if (filename[0] == '/') {
 		if (asprintf(&fn, "%s.%s", filename, ext) < 0) {
Index: main/Makefile
diff -Nau main/Makefile.orig main/Makefile
--- main/Makefile.orig	2011-08-03 17:14:36.000000000 +0200
+++ main/Makefile	2011-09-14 14:29:50.777958246 +0200
@@ -69,10 +69,7 @@
 endif
 
 ifeq ($(OSARCH),FreeBSD)
-  # -V is understood by BSD Make, not by GNU make.
-  BSDVERSION=$(shell make -V OSVERSION -f /usr/share/mk/bsd.port.subdir.mk)
-  AST_LIBS+=$(shell if test $(BSDVERSION) -lt 502102 ; then echo "-lc_r"; else echo "-pthread"; fi)
-  AST_LIBS+=-lcrypto
+  AST_LIBS+=-lpthread -lcrypto
 endif
 
 ifneq ($(findstring $(OSARCH), mingw32 cygwin ),)
Index: main/tcptls.c
diff -Nau main/tcptls.c.orig main/tcptls.c
--- main/tcptls.c.orig	2011-05-23 18:18:33.000000000 +0200
+++ main/tcptls.c	2011-09-14 14:29:50.777958246 +0200
@@ -357,6 +357,7 @@
 	if (!ast_strlen_zero(cfg->cafile) || !ast_strlen_zero(cfg->capath)) {
 		if (SSL_CTX_load_verify_locations(cfg->ssl_ctx, S_OR(cfg->cafile, NULL), S_OR(cfg->capath,NULL)) == 0)
 			ast_verb(0, "SSL CA file(%s)/path(%s) error\n", cfg->cafile, cfg->capath);
+        SSL_CTX_set_client_CA_list(cfg->ssl_ctx, S_OR(cfg->cafile, NULL));
 	}
 
 	ast_verb(0, "SSL certificate ok\n");
Index: main/udptl.c
diff -Nau main/udptl.c.orig main/udptl.c
--- main/udptl.c.orig	2011-05-03 21:55:49.000000000 +0200
+++ main/udptl.c	2011-09-14 14:29:50.777958246 +0200
@@ -98,6 +98,18 @@
 
 #define UDPTL_BUF_MASK              15
 
+/*! Copied from chan_oss.c, corrects link errors:
+udptl.o: In function `calculate_local_max_datagram':
+main/udptl.c:740: undefined reference to `MIN'
+udptl.o: In function `calculate_far_max_ifp':
+main/udptl.c:770: undefined reference to `MAX' */
+#ifndef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
 typedef struct {
 	int buf_len;
 	uint8_t buf[LOCAL_FAX_MAX_DATAGRAM];
Index: Makefile
diff -Nau Makefile.orig Makefile
--- Makefile.orig	2011-08-25 21:08:04.000000000 +0200
+++ Makefile	2011-09-14 14:29:50.777958246 +0200
@@ -230,15 +230,6 @@
   _ASTCFLAGS+=-fsigned-char
 endif
 
-ifeq ($(OSARCH),FreeBSD)
-  ifeq ($(PROC),i386)
-    _ASTCFLAGS+=-march=i686
-  endif
-  # -V is understood by BSD Make, not by GNU make.
-  BSDVERSION=$(shell make -V OSVERSION -f /usr/share/mk/bsd.port.subdir.mk)
-  _ASTCFLAGS+=$(shell if test $(BSDVERSION) -lt 500016 ; then echo "-D_THREAD_SAFE"; fi)
-endif
-
 ifeq ($(OSARCH),NetBSD)
   _ASTCFLAGS+=-pthread -I/usr/pkg/include
 endif
