Index: channels/chan_iax2.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_iax2.c,v
retrieving revision 1.245
diff -u -r1.245 chan_iax2.c
--- channels/chan_iax2.c	30 Jan 2005 21:32:26 -0000	1.245
+++ channels/chan_iax2.c	4 Feb 2005 22:23:56 -0000
@@ -207,7 +207,9 @@
 #define IAX_CODEC_USER_FIRST (1 << 14)  /* are we willing to let the other guy choose the codec? */
 #define IAX_CODEC_NOPREFS (1 << 15) /* Force old behaviour by turning off prefs */
 #define IAX_CODEC_NOCAP (1 << 16) /* only consider requested format and ignore capabilities*/
-
+#define IAX_RTCACHEFRIENDS (1 << 17) /* let realtime stay till your reload */
+#define IAX_RTNOUPDATE (1 << 18) /* Don't send a realtime update */
+#define IAX_RTAUTOCLEAR (1 << 19) /* erase me on expire */ 
 static struct iax2_peer *realtime_peer(const char *peername);
 
 struct iax2_user {
@@ -576,8 +578,15 @@
 static int send_command_immediate(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
 static int send_command_final(struct chan_iax2_pvt *, char, int, unsigned int, char *, int, int);
 static int send_command_transfer(struct chan_iax2_pvt *, char, int, unsigned int, char *, int);
+static struct iax2_user *build_user(const char *name, struct ast_variable *v, int temponly);
+static void destroy_user(struct iax2_user *user);
+static int expire_registry(void *data);
+static int iax2_write(struct ast_channel *c, struct ast_frame *f);
+static int iax2_do_register(struct iax2_registry *reg);
+static void prune_peers(void);
+static int iax2_poke_peer(struct iax2_peer *peer, int heldcall);
+static int iax2_provision(struct sockaddr_in *end, char *dest, const char *template, int force);
 
-static unsigned int calc_timestamp(struct chan_iax2_pvt *p, unsigned int ts, struct ast_frame *f);
 
 static int send_ping(void *data)
 {
@@ -2142,6 +2151,7 @@
 static struct iax2_user *build_user(const char *name, struct ast_variable *v, int temponly);
 
 static void destroy_user(struct iax2_user *user);
+static int expire_registry(void *data);
 
 static struct iax2_peer *realtime_peer(const char *peername)
 {
@@ -2153,10 +2163,9 @@
 	var = ast_load_realtime("iaxfriends", "name", peername, NULL);
 	if (var) {
 		/* Make sure it's not a user only... */
-		peer = build_peer(peername, var, 1);
+		peer = build_peer(peername, var, ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS) ? 0 : 1);
+
 		if (peer) {
-			/* Add some finishing touches, addresses, etc */
-			ast_set_flag(peer, IAX_TEMPONLY);	
 			tmp = var;
 			while(tmp) {
 				if (!strcasecmp(tmp->name, "type")) {
@@ -2180,6 +2189,20 @@
 				}
 				tmp = tmp->next;
 			}
+
+			/* Add some finishing touches, addresses, etc */
+		  	if(ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS)) {
+				ast_mutex_lock(&peerl.lock);
+				peer->next = peerl.peers;
+				peerl.peers = peer;
+				ast_mutex_unlock(&peerl.lock);
+				ast_set_flag(peer, IAX_RTCACHEFRIENDS);
+				if (!ast_test_flag(peer, IAX_RTAUTOCLEAR))
+					peer->expire = ast_sched_add(sched, (peer->expirey + 10) * 1000, expire_registry, (void *)peer);
+			} else {
+		    		ast_set_flag(peer, IAX_TEMPONLY);	
+			}
+
 			if (peer && dynamic) {
 				time(&nowtime);
 				if ((nowtime - regseconds) > IAX_DEFAULT_REG_EXPIRE) {
@@ -2202,10 +2225,18 @@
 	var = ast_load_realtime("iaxfriends", "name", username, NULL);
 	if (var) {
 		/* Make sure it's not a user only... */
-		user = build_user(username, var, 1);
+		user = build_user(username, var, ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS) ? 0 : 1);
 		if (user) {
 			/* Add some finishing touches, addresses, etc */
-			ast_set_flag(user, IAX_TEMPONLY);	
+		  	if(ast_test_flag((&globalflags), IAX_RTCACHEFRIENDS)) {
+				ast_mutex_lock(&userl.lock);
+				user->next = userl.users;
+				userl.users = user;
+				ast_mutex_unlock(&userl.lock);
+				ast_set_flag(user, IAX_RTCACHEFRIENDS);
+			} else {
+				ast_set_flag(user, IAX_TEMPONLY);	
+			}
 			tmp = var;
 			while(tmp) {
 				if (!strcasecmp(tmp->name, "type")) {
@@ -4835,6 +4866,7 @@
 		}
 	}
 }
+static void prune_peers(void);
 
 static int expire_registry(void *data)
 {
@@ -4850,6 +4882,12 @@
 	register_peer_exten(p, 0);
 	if (iax2_regfunk)
 		iax2_regfunk(p->name, 0);
+
+	if (!ast_test_flag(p, IAX_RTAUTOCLEAR)) {
+		ast_set_flag(p, IAX_DELME);
+		prune_peers();
+	}
+
 	return 0;
 }
 
@@ -4906,7 +4944,7 @@
 	memset(&ied, 0, sizeof(ied));
 	p = find_peer(name);
 	if (p) {
-		if (ast_test_flag(p, IAX_TEMPONLY))
+		if (!ast_test_flag((&globalflags), IAX_RTNOUPDATE) && (ast_test_flag(p, IAX_TEMPONLY) || ast_test_flag(p, IAX_RTCACHEFRIENDS)))
 			realtime_update_peer(name, sin);
 		if (inaddrcmp(&p->addr, sin)) {
 			if (iax2_regfunk)
@@ -7626,6 +7664,12 @@
 			delayreject = ast_true(v->value);
 		else if (!strcasecmp(v->name, "mailboxdetail"))
 			ast_set2_flag((&globalflags), ast_true(v->value), IAX_MESSAGEDETAIL);	
+		else if (!strcasecmp(v->name, "rtcachefriends"))
+			ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTCACHEFRIENDS);	
+		else if (!strcasecmp(v->name, "rtnoupdate"))
+			ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTNOUPDATE);	
+		else if (!strcasecmp(v->name, "rtautoclear"))
+			ast_set2_flag((&globalflags), ast_true(v->value), IAX_RTAUTOCLEAR);	
 		else if (!strcasecmp(v->name, "trunkfreq")) {
 			trunkfreq = atoi(v->value);
 			if (trunkfreq < 10)
Index: channels/chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.645
diff -u -r1.645 chan_sip.c
--- channels/chan_sip.c	1 Feb 2005 20:07:51 -0000	1.645
+++ channels/chan_sip.c	4 Feb 2005 22:23:56 -0000
@@ -138,6 +138,7 @@
 static char default_notifymime[AST_MAX_EXTENSION] = DEFAULT_NOTIFYMIME;
 
 static struct ast_flags global_flags = {0};		/* global SIP_ flags */
+static struct ast_flags global_flags_page2 = {0};		/* more global SIP_ flags */
 
 static int srvlookup = 0;		/* SRV Lookup on or off. Default is off, RFC behavior is on */
 
@@ -303,6 +304,10 @@
 #define SIP_OSPAUTH_YES		(1 << 26)
 #define SIP_OSPAUTH_EXCLUSIVE	(2 << 26)
 
+/* a new page of flags */
+#define SIP_PAGE2_RTCACHEFRIENDS 	(1 << 0)
+#define SIP_PAGE2_RTNOUPDATE 		(1 << 1)
+#define SIP_PAGE2_RTAUTOCLEAR 		(1 << 2)
 /* sip_pvt: PVT structures are used for each SIP conversation, ie. a call  */
 static struct sip_pvt {
 	ast_mutex_t lock;			/* Channel private lock */
@@ -469,6 +474,7 @@
 	int lastmsgssent;
 	time_t	lastmsgcheck;		/* Last time we checked for MWI */
 	unsigned int flags;		/* SIP_ flags */	
+	struct ast_flags flags_page2; /* SIP_PAGE2 flags */
 	int expire;			/* Registration expiration */
 	int expiry;
 	int capability;			/* Codec capability */
@@ -586,7 +592,7 @@
 static int update_user_counter(struct sip_pvt *fup, int event);
 static void prune_peers(void);
 static int sip_do_reload(void);
-
+static int expire_register(void *data);
 static int callevents = 0;
 
 /*--- sip_debug_test_addr: See if we pass debug IP filter */
@@ -1109,7 +1115,9 @@
 /*--- update_peer: Update peer data in database (if used) ---*/
 static void update_peer(struct sip_peer *p, int expiry)
 {
-	if (ast_test_flag(p, SIP_REALTIME))
+	if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_RTNOUPDATE) && 
+		(ast_test_flag(p, SIP_REALTIME) || 
+		 ast_test_flag(&(p->flags_page2), SIP_PAGE2_RTCACHEFRIENDS)))
 		realtime_update_peer(p->name, &p->addr, p->username, expiry);
 }
 
@@ -1144,9 +1152,18 @@
 		tmp = tmp->next;
 	}
 
-	peer = build_peer(peername, var, 1);
-	if (peer)
-		ast_set_flag(peer, SIP_REALTIME);
+	peer = build_peer(peername, var, ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS) ? 0 : 1);
+	if (peer) {
+		if(ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
+			ast_copy_flags((&peer->flags_page2),(&global_flags_page2), SIP_PAGE2_RTAUTOCLEAR|SIP_PAGE2_RTCACHEFRIENDS);
+			if(ast_test_flag((&global_flags_page2), SIP_PAGE2_RTAUTOCLEAR)) {
+				peer->expire = ast_sched_add(sched, (default_expiry) * 1000, expire_register, (void *)peer);
+			}
+			ASTOBJ_CONTAINER_LINK(&peerl,peer);
+		} else {
+			ast_set_flag(peer, SIP_REALTIME);
+		}
+	}
 	ast_variables_destroy(var);
 	return peer;
 }
@@ -1206,20 +1223,29 @@
 	tmp = var;
 	while (tmp) {
 		if (!strcasecmp(tmp->name, "type") &&
-		    !strcasecmp(tmp->value, "peer")) {
+			!strcasecmp(tmp->value, "peer")) {
 			ast_variables_destroy(var);
 			return NULL;
 		}
 		tmp = tmp->next;
 	}
+	
+
+
+	user = build_user(username, var, ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS) ? 1 : 1);
 
-	user = build_user(username, var, 1);
 	if (user) {
-		/* Move counter from s to r... */
-		suserobjs--;
-		ruserobjs++;
 		/* Add some finishing touches, addresses, etc */
-		ast_set_flag(user, SIP_REALTIME);	
+		if(ast_test_flag((&global_flags_page2), SIP_PAGE2_RTCACHEFRIENDS)) {
+			suserobjs++;
+
+            ASTOBJ_CONTAINER_LINK(&userl,user);
+        } else {
+			/* Move counter from s to r... */
+			suserobjs--;
+			ruserobjs++;
+            ast_set_flag(user, SIP_REALTIME);
+        }
 	}
 	ast_variables_destroy(var);
 	return user;
@@ -4450,7 +4476,7 @@
 	register_peer_exten(p, 0);
 	p->expire = -1;
 	ast_device_state_changed("SIP/%s", p->name);
-	if (ast_test_flag(p, SIP_SELFDESTRUCT)) {
+	if (ast_test_flag(p, SIP_SELFDESTRUCT) || ast_test_flag((&p->flags_page2), SIP_PAGE2_RTAUTOCLEAR)) {
 		ASTOBJ_MARK(p);	
 		prune_peers();
 	}
@@ -5855,7 +5881,7 @@
 	regex_t regexbuf;
 	int havepattern = 0;
 
-#define FORMAT  "%-25.25s  %-15.15s  %-15.15s  %-15.15s  %-5.5s%-5.5s\n"
+#define FORMAT  "%-25.25s  %-15.15s  %-15.15s  %-15.15s  %-5.5s%-10.10s\n"
 
 	if (argc > 4)
 		return RESULT_SHOWUSAGE;
@@ -9119,6 +9145,12 @@
 			strncpy(default_useragent, v->value, sizeof(default_useragent)-1);
 			ast_log(LOG_DEBUG, "Setting User Agent Name to %s\n",
 				default_useragent);
+		} else if (!strcasecmp(v->name, "rtcachefriends")) {
+			ast_set2_flag((&global_flags_page2), ast_true(v->value), SIP_PAGE2_RTCACHEFRIENDS);	
+		} else if (!strcasecmp(v->name, "rtnoupdate")) {
+			ast_set2_flag((&global_flags_page2), ast_true(v->value), SIP_PAGE2_RTNOUPDATE);	
+		} else if (!strcasecmp(v->name, "rtautoclear")) {
+			ast_set2_flag((&global_flags_page2), ast_true(v->value), SIP_PAGE2_RTAUTOCLEAR);
 		} else if (!strcasecmp(v->name, "usereqphone")) {
 			ast_set2_flag((&global_flags), ast_true(v->value), SIP_USEREQPHONE);	
 		} else if (!strcasecmp(v->name, "relaxdtmf")) {
Index: configs/sip.conf.sample
===================================================================
RCS file: /usr/cvsroot/asterisk/configs/sip.conf.sample,v
retrieving revision 1.55
diff -u -r1.55 sip.conf.sample
--- configs/sip.conf.sample	29 Jan 2005 23:19:01 -0000	1.55
+++ configs/sip.conf.sample	4 Feb 2005 22:23:56 -0000
@@ -80,6 +80,16 @@
 
 ;compactheaders = yes		; send compact sip headers.
 
+;rtcachefriends=yes		; Cache realtime friends by adding them to the internal list
+				; just like friends added from the config file only on a
+				; as-needed basis.
+;rtnoupdate=yes			; do not send the update request over realtime.
+;rtautoclear=yes		; Auto-Expire friends created on the fly on the same schedule
+				; as if it had just registered when the registration expires
+				; the friend will vanish from the configuration until requested
+				; again.
+
+
 ;
 ; If regcontext is specified, Asterisk will dynamically 
 ; create and destroy a NoOp priority 1 extension for a given
Index: configs/iax.conf.sample
===================================================================
RCS file: /usr/cvsroot/asterisk/configs/iax.conf.sample,v
retrieving revision 1.40
diff -u -r1.40 iax.conf.sample
--- configs/iax.conf.sample	20 Jan 2005 23:42:06 -0000	1.40
+++ configs/iax.conf.sample	4 Feb 2005 22:23:56 -0000
@@ -196,6 +196,15 @@
 ; Guest sections for unauthenticated connection attempts.  Just
 ; specify an empty secret, or provide no secret section.
 ;
+;rtcachefriends=yes		; Cache realtime friends by adding them to the internal list
+				; just like friends added from the config file only on a
+				; as-needed basis.
+;rtnoupdate=yes			; do not send the update request over realtime.
+;rtautoclear=yes		; Auto-Expire friends created on the fly on the same schedule
+				; as if it had just registered when the registration expires
+				; the friend will vanish from the configuration until requested
+				; again.
+
 [guest]
 type=user
 context=default
