Index: trunk/service.go
===================================================================
--- trunk/service.go	(revision 676)
+++ trunk/service.go	(revision 677)
@@ -40,5 +40,5 @@
 	usage    string
 	desc     string
-	handle   func(dc *downstreamConn, params []string) error
+	handle   func(ctx context.Context, dc *downstreamConn, params []string) error
 	children serviceCommandSet
 	admin    bool
@@ -114,5 +114,5 @@
 }
 
-func handleServicePRIVMSG(dc *downstreamConn, text string) {
+func handleServicePRIVMSG(ctx context.Context, dc *downstreamConn, text string) {
 	words, err := splitWords(text)
 	if err != nil {
@@ -145,5 +145,5 @@
 	}
 
-	if err := cmd.handle(dc, params); err != nil {
+	if err := cmd.handle(ctx, dc, params); err != nil {
 		sendServicePRIVMSG(dc, fmt.Sprintf("error: %v", err))
 	}
@@ -323,5 +323,5 @@
 }
 
-func handleServiceHelp(dc *downstreamConn, params []string) error {
+func handleServiceHelp(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) > 0 {
 		cmd, rest, err := serviceCommands.Get(params)
@@ -474,5 +474,5 @@
 }
 
-func handleServiceNetworkCreate(dc *downstreamConn, params []string) error {
+func handleServiceNetworkCreate(ctx context.Context, dc *downstreamConn, params []string) error {
 	fs := newNetworkFlagSet()
 	if err := fs.Parse(params); err != nil {
@@ -491,5 +491,5 @@
 	}
 
-	network, err := dc.user.createNetwork(context.TODO(), record)
+	network, err := dc.user.createNetwork(ctx, record)
 	if err != nil {
 		return fmt.Errorf("could not create network: %v", err)
@@ -500,5 +500,5 @@
 }
 
-func handleServiceNetworkStatus(dc *downstreamConn, params []string) error {
+func handleServiceNetworkStatus(ctx context.Context, dc *downstreamConn, params []string) error {
 	n := 0
 	dc.user.forEachNetwork(func(net *network) {
@@ -546,5 +546,5 @@
 }
 
-func handleServiceNetworkUpdate(dc *downstreamConn, params []string) error {
+func handleServiceNetworkUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) < 1 {
 		return fmt.Errorf("expected at least one argument")
@@ -566,5 +566,5 @@
 	}
 
-	network, err := dc.user.updateNetwork(context.TODO(), &record)
+	network, err := dc.user.updateNetwork(ctx, &record)
 	if err != nil {
 		return fmt.Errorf("could not update network: %v", err)
@@ -575,5 +575,5 @@
 }
 
-func handleServiceNetworkDelete(dc *downstreamConn, params []string) error {
+func handleServiceNetworkDelete(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) != 1 {
 		return fmt.Errorf("expected exactly one argument")
@@ -585,5 +585,5 @@
 	}
 
-	if err := dc.user.deleteNetwork(context.TODO(), net.ID); err != nil {
+	if err := dc.user.deleteNetwork(ctx, net.ID); err != nil {
 		return err
 	}
@@ -593,5 +593,5 @@
 }
 
-func handleServiceNetworkQuote(dc *downstreamConn, params []string) error {
+func handleServiceNetworkQuote(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) != 2 {
 		return fmt.Errorf("expected exactly two arguments")
@@ -627,5 +627,5 @@
 }
 
-func handleServiceCertFPGenerate(dc *downstreamConn, params []string) error {
+func handleServiceCertFPGenerate(ctx context.Context, dc *downstreamConn, params []string) error {
 	fs := newFlagSet()
 	keyType := fs.String("key-type", "rsa", "key type to generate (rsa, ecdsa, ed25519)")
@@ -658,5 +658,5 @@
 	net.SASL.Mechanism = "EXTERNAL"
 
-	if err := dc.srv.db.StoreNetwork(context.TODO(), dc.user.ID, &net.Network); err != nil {
+	if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &net.Network); err != nil {
 		return err
 	}
@@ -667,5 +667,5 @@
 }
 
-func handleServiceCertFPFingerprints(dc *downstreamConn, params []string) error {
+func handleServiceCertFPFingerprints(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) != 1 {
 		return fmt.Errorf("expected exactly one argument")
@@ -685,5 +685,5 @@
 }
 
-func handleServiceSASLSetPlain(dc *downstreamConn, params []string) error {
+func handleServiceSASLSetPlain(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) != 3 {
 		return fmt.Errorf("expected exactly 3 arguments")
@@ -699,5 +699,5 @@
 	net.SASL.Mechanism = "PLAIN"
 
-	if err := dc.srv.db.StoreNetwork(context.TODO(), dc.user.ID, &net.Network); err != nil {
+	if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &net.Network); err != nil {
 		return err
 	}
@@ -707,5 +707,5 @@
 }
 
-func handleServiceSASLReset(dc *downstreamConn, params []string) error {
+func handleServiceSASLReset(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) != 1 {
 		return fmt.Errorf("expected exactly one argument")
@@ -723,5 +723,5 @@
 	net.SASL.Mechanism = ""
 
-	if err := dc.srv.db.StoreNetwork(context.TODO(), dc.user.ID, &net.Network); err != nil {
+	if err := dc.srv.db.StoreNetwork(ctx, dc.user.ID, &net.Network); err != nil {
 		return err
 	}
@@ -731,5 +731,5 @@
 }
 
-func handleUserCreate(dc *downstreamConn, params []string) error {
+func handleUserCreate(ctx context.Context, dc *downstreamConn, params []string) error {
 	fs := newFlagSet()
 	username := fs.String("username", "", "")
@@ -774,5 +774,5 @@
 }
 
-func handleUserUpdate(dc *downstreamConn, params []string) error {
+func handleUserUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
 	var password, realname *string
 	var admin *bool
@@ -838,5 +838,5 @@
 		}
 
-		if err := dc.user.updateUser(context.TODO(), &record); err != nil {
+		if err := dc.user.updateUser(ctx, &record); err != nil {
 			return err
 		}
@@ -848,5 +848,5 @@
 }
 
-func handleUserDelete(dc *downstreamConn, params []string) error {
+func handleUserDelete(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) != 1 {
 		return fmt.Errorf("expected exactly one argument")
@@ -861,5 +861,5 @@
 	u.stop()
 
-	if err := dc.srv.db.DeleteUser(context.TODO(), u.ID); err != nil {
+	if err := dc.srv.db.DeleteUser(ctx, u.ID); err != nil {
 		return fmt.Errorf("failed to delete user: %v", err)
 	}
@@ -869,5 +869,5 @@
 }
 
-func handleServiceChannelStatus(dc *downstreamConn, params []string) error {
+func handleServiceChannelStatus(ctx context.Context, dc *downstreamConn, params []string) error {
 	var defaultNetworkName string
 	if dc.network != nil {
@@ -989,5 +989,5 @@
 }
 
-func handleServiceChannelUpdate(dc *downstreamConn, params []string) error {
+func handleServiceChannelUpdate(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) < 1 {
 		return fmt.Errorf("expected at least one argument")
@@ -1016,5 +1016,5 @@
 	uc.updateChannelAutoDetach(upstreamName)
 
-	if err := dc.srv.db.StoreChannel(context.TODO(), uc.network.ID, ch); err != nil {
+	if err := dc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
 		return fmt.Errorf("failed to update channel: %v", err)
 	}
@@ -1024,6 +1024,6 @@
 }
 
-func handleServiceServerStatus(dc *downstreamConn, params []string) error {
-	dbStats, err := dc.user.srv.db.Stats(context.TODO())
+func handleServiceServerStatus(ctx context.Context, dc *downstreamConn, params []string) error {
+	dbStats, err := dc.user.srv.db.Stats(ctx)
 	if err != nil {
 		return err
@@ -1034,5 +1034,5 @@
 }
 
-func handleServiceServerNotice(dc *downstreamConn, params []string) error {
+func handleServiceServerNotice(ctx context.Context, dc *downstreamConn, params []string) error {
 	if len(params) != 1 {
 		return fmt.Errorf("expected exactly one argument")
