@@ -131,7 +131,8 @@ var handlers = {
131131 ] ;
132132
133133 // Optional CAPs depending on settings
134- if ( handler . connection . options . password || handler . connection . options . sasl_mechanism === 'EXTERNAL' ) {
134+ const saslAuth = getSaslAuth ( handler ) ;
135+ if ( saslAuth || handler . connection . options . sasl_mechanism === 'EXTERNAL' ) {
135136 want . push ( 'sasl' ) ;
136137 }
137138 if ( handler . connection . options . enable_chghost ) {
@@ -256,9 +257,10 @@ var handlers = {
256257 return ;
257258 }
258259
259- const auth_str = handler . connection . options . nick + '\0' +
260- handler . connection . options . nick + '\0' +
261- handler . connection . options . password ;
260+ const saslAuth = getSaslAuth ( handler ) ;
261+ const auth_str = saslAuth . account + '\0' +
262+ saslAuth . account + '\0' +
263+ saslAuth . password ;
262264 const b = Buffer . from ( auth_str , 'utf8' ) ;
263265 let b64 = b . toString ( 'base64' ) ;
264266
@@ -354,6 +356,33 @@ var handlers = {
354356 }
355357} ;
356358
359+ /**
360+ * Only use the nick+password combo if an account has not been specifically given.
361+ * If an account:{account,password} has been given, use it for SASL auth.
362+ */
363+ function getSaslAuth ( handler ) {
364+ const options = handler . connection . options ;
365+ if ( options . account && options . account . account ) {
366+ // An account username has been given, use it for SASL auth
367+ return {
368+ account : options . account . account ,
369+ password : options . account . password || '' ,
370+ } ;
371+ } else if ( options . account ) {
372+ // An account object existed but without auth credentials
373+ return null ;
374+ } else if ( options . password ) {
375+ // No account credentials found but we have a server password. Also use it for SASL
376+ // for ease of use
377+ return {
378+ account : options . nick ,
379+ password : options . password ,
380+ } ;
381+ }
382+
383+ return null ;
384+ }
385+
357386module . exports = function AddCommandHandlers ( command_controller ) {
358387 _ . each ( handlers , function ( handler , handler_command ) {
359388 command_controller . addHandler ( handler_command , handler ) ;
0 commit comments