Index: irc.c
===================================================================
--- irc.c	(revision f5d1b3185140e690a6503b9e68c7ea28655b405e)
+++ irc.c	(revision 6738a676c7a3895988de4bd9eacfe8fa0ef73cc3)
@@ -29,6 +29,9 @@
 #include "crypting.h"
 #include "ipc.h"
+#include <sys/types.h>
+#include <sys/wait.h>
 
 static gboolean irc_userping( gpointer _irc, int fd, b_input_condition cond );
+static void irc_welcome( irc_t *irc );
 
 GSList *irc_connection_list = NULL;
@@ -135,6 +138,5 @@
 
 	irc_connection_list = g_slist_append( irc_connection_list, irc );
-	
-	set_add( &irc->set, "away_devoice", "true",  set_eval_away_devoice, irc );
+
 	set_add( &irc->set, "auto_connect", "true", set_eval_bool, irc );
 	set_add( &irc->set, "auto_reconnect", "false", set_eval_bool, irc );
@@ -143,10 +145,15 @@
 	set_add( &irc->set, "buddy_sendbuffer_delay", "200", set_eval_int, irc );
 	set_add( &irc->set, "charset", "utf-8", set_eval_charset, irc );
+	set_add( &irc->set, "color_encrypted", "true", set_eval_bool, irc );
 	set_add( &irc->set, "debug", "false", set_eval_bool, irc );
 	set_add( &irc->set, "default_target", "root", NULL, irc );
 	set_add( &irc->set, "display_namechanges", "false", set_eval_bool, irc );
 	set_add( &irc->set, "handle_unknown", "root", NULL, irc );
+	set_add( &irc->set, "halfop_buddies", "encrypted", set_eval_halfop_buddies, irc );
 	set_add( &irc->set, "lcnicks", "true", set_eval_bool, irc );
-	set_add( &irc->set, "ops", "both", set_eval_ops, irc );
+	set_add( &irc->set, "op_buddies", "trusted", set_eval_op_buddies, irc );
+	set_add( &irc->set, "op_root", "true", set_eval_op_root, irc );
+	set_add( &irc->set, "op_user", "true", set_eval_op_user, irc );
+	set_add( &irc->set, "otr_policy", "opportunistic", set_eval_otr_policy, irc );
 	set_add( &irc->set, "password", NULL, passchange, irc );
 	set_add( &irc->set, "private", "true", set_eval_bool, irc );
@@ -158,6 +165,9 @@
 	set_add( &irc->set, "to_char", ": ", set_eval_to_char, irc );
 	set_add( &irc->set, "typing_notice", "false", set_eval_bool, irc );
+	set_add( &irc->set, "voice_buddies", "notaway",  set_eval_voice_buddies, irc );
 	
 	conf_loaddefaults( irc );
+
+	irc->otr = otr_new();
 	
 	/* Evaluator sets the iconv/oconv structures. */
@@ -312,4 +322,6 @@
 	
 	g_free( irc->last_target );
+	
+	otr_free(irc->otr);
 	
 	g_free( irc );
@@ -685,4 +697,38 @@
 } 
 
+const char *user_mode_prefix( irc_t *irc, user_t *u )
+{
+	static char op[] = "@";
+	static char halfop[] = "%";
+	static char voice[] = "+";
+	static char none[] = "";
+
+	int or = set_getbool(&irc->set, "op_root");
+	int ou = set_getbool(&irc->set, "op_user");
+	char *ob = set_getstr(&irc->set, "op_buddies");
+	char *hb = set_getstr(&irc->set, "halfop_buddies");
+	char *vb = set_getstr(&irc->set, "voice_buddies");
+
+	if( (!strcmp(u->nick, irc->mynick) && or) ||
+	    (!strcmp(u->nick, irc->nick) && ou) ||
+        (!u->away && !strcmp(ob, "notaway")) ||
+        (u->encrypted && !strcmp(ob, "encrypted")) ||
+        (u->encrypted>1 && !strcmp(ob, "trusted"))
+      )
+		return op;
+	else if( (!u->away && !strcmp(hb, "notaway")) ||
+             (u->encrypted && !strcmp(hb, "encrypted")) ||
+             (u->encrypted>1 && !strcmp(hb, "trusted"))
+	       )
+		return halfop;
+	else if( (!u->away && !strcmp(vb, "notaway")) ||
+             (u->encrypted && !strcmp(vb, "encrypted")) ||
+             (u->encrypted>1 && !strcmp(vb, "trusted"))
+	       )
+		return voice;
+	else
+		return none;
+}
+			
 void irc_names( irc_t *irc, char *channel )
 {
@@ -690,5 +736,4 @@
 	char namelist[385] = "";
 	struct groupchat *c = NULL;
-	char *ops = set_getstr( &irc->set, "ops" );
 	
 	/* RFCs say there is no error reply allowed on NAMES, so when the
@@ -705,10 +750,5 @@
 			}
 			
-			if( u->ic && !u->away && set_getbool( &irc->set, "away_devoice" ) )
-				strcat( namelist, "+" );
-			else if( ( strcmp( u->nick, irc->mynick ) == 0 && ( strcmp( ops, "root" ) == 0 || strcmp( ops, "both" ) == 0 ) ) ||
-			         ( strcmp( u->nick, irc->nick ) == 0 && ( strcmp( ops, "user" ) == 0 || strcmp( ops, "both" ) == 0 ) ) )
-				strcat( namelist, "@" );
-			
+			strcat( namelist, user_mode_prefix(irc, u) );
 			strcat( namelist, u->nick );
 			strcat( namelist, " " );
@@ -721,6 +761,6 @@
 		/* root and the user aren't in the channel userlist but should
 		   show up in /NAMES, so list them first: */
-		sprintf( namelist, "%s%s %s%s ", strcmp( ops, "root" ) == 0 || strcmp( ops, "both" ) ? "@" : "", irc->mynick,
-		                                 strcmp( ops, "user" ) == 0 || strcmp( ops, "both" ) ? "@" : "", irc->nick );
+		sprintf( namelist, "%s%s %s%s ", set_getbool(&irc->set, "op_root") ? "@" : "", irc->mynick,
+		                                 set_getbool(&irc->set, "op_user") ? "@" : "", irc->nick );
 		
 		for( l = c->in_room; l; l = l->next ) if( ( u = user_findhandle( c->ic, l->data ) ) )
@@ -732,4 +772,5 @@
 			}
 			
+			strcat( namelist, user_mode_prefix(irc, u) );
 			strcat( namelist, u->nick );
 			strcat( namelist, " " );
@@ -773,10 +814,9 @@
 	irc_reply( irc,   3, ":%s", IRCD_INFO );
 	irc_reply( irc,   4, "%s %s %s %s", irc->myhost, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES );
-	irc_reply( irc,   5, "PREFIX=(ov)@+ CHANTYPES=#& CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server", CMODES, MAX_NICK_LENGTH - 1 );
+	irc_reply( irc,   5, "PREFIX=(ohv)@%%+ CHANTYPES=#& CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server", CMODES, MAX_NICK_LENGTH - 1 );
 	irc_motd( irc );
 	irc->umode[0] = '\0';
 	irc_umode_set( irc, "+" UMODE, 1 );
-
-	u = user_add( irc, irc->mynick );
+u = user_add( irc, irc->mynick );
 	u->host = g_strdup( irc->myhost );
 	u->realname = g_strdup( ROOT_FN );
@@ -800,10 +840,5 @@
 	irc_spawn( irc, u );
 	
-	irc_usermsg( irc, "Welcome to the BitlBee gateway!\n\n"
-	                  "If you've never used BitlBee before, please do read the help "
-	                  "information using the \x02help\x02 command. Lots of FAQs are "
-	                  "answered there.\n"
-	                  "If you already have an account on this server, just use the "
-	                  "\x02identify\x02 command to identify yourself." );
+	irc_welcome( irc );
 	
 	if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON )
@@ -820,4 +855,33 @@
 		root_command( irc, send_cmd );
 		g_free( send_cmd[1] );
+	}
+}
+
+static void irc_welcome( irc_t *irc )
+{
+	FILE *f;
+	
+	f = fopen( global.conf->welcomefile, "r" );
+	if( !f )
+	{
+		irc_usermsg( irc, "Welcome to the BitlBee gateway!\n\n"
+	        	          "If you've never used BitlBee before, please do read the help "
+		                  "information using the \x02help\x02 command. Lots of FAQs are "
+		                  "answered there.\n"
+				  "OTR users please note: Private key files are owned by the user "
+				  "BitlBee is running as.\n"
+		                  "If you already have an account on this server, just use the "
+		                  "\x02identify\x02 command to identify yourself." );
+	}
+	else
+	{
+		char linebuf[380];
+		
+		while( fgets( linebuf, 380, f ) )
+		{
+			irc_usermsg( irc, linebuf );
+		}
+		
+		fclose( f );
 	}
 }
