ソースを参照

bart: fix buddy icon upload and clearing for QIP 2005 and AIM 7

- A BART upload of a new buddy icon replies with SNAC(0x01,0x21) so
  the client sets the feedbag item; an upload matching the current
  icon sends the updated user info to the user and their buddies.
- Deleting the BART feedbag item that holds the current buddy icon
  advertises the blank GIF to the user and their buddies.
- The blank GIF is a transparent 44x44, 90-byte GIF, served for both
  the clear icon hash and its own MD5 hash.
- Rename wire.GetClearIconHash to wire.ClearIconHash and add
  BARTID.IsBuddyIcon, which includes the big buddy icon type.
Mike 2 日 前
親
コミット
309338982b

+ 77 - 25
foodgroup/bart.go

@@ -12,13 +12,34 @@ import (
 	"github.com/mk6i/open-oscar-server/wire"
 )
 
-// blankGIF is a blank, transparent 50x50p GIF that takes the place of a
-// cleared buddy icon.
-var blankGIF = []byte{
-	0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x32, 0x00, 0x32, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
-	0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
-	0x32, 0x00, 0x32, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b,
-}
+var (
+	// blankGIF is a transparent GIF served for a cleared buddy icon. This GIF
+	// composition approximates the real one based on observed client behavior:
+	//   - QIP 2005 fails to decode a GIF whose image data ends before the last
+	//     pixel, so the image data covers every pixel.
+	//   - QIP 2005 doesn't erase behind a transparent icon that fills its 46x46
+	//     display, so the icon is 44x44.
+	//   - Pidgin discards a 90-byte icon and shows no icon, so this GIF is 90
+	//     bytes.
+	blankGIF = []byte{
+		0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x2c, 0x00, 0x2c, 0x00, 0x81, 0x00, 0x00, 0xff, 0xff, 0xff,
+		0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0xc0, 0xc0, 0xc0, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x00,
+		0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x2c, 0x00, 0x00, 0x02, 0x2b, 0x84, 0x8f, 0xa9,
+		0xcb, 0xed, 0x0f, 0xa3, 0x9c, 0xb4, 0xda, 0x8b, 0xb3, 0xde, 0xbc, 0xfb, 0x0f, 0x86, 0xe2, 0x48,
+		0x96, 0xe6, 0x89, 0xa6, 0xea, 0xca, 0xb6, 0xee, 0x0b, 0xc7, 0xf2, 0x4c, 0xd7, 0xf6, 0x8d, 0xe7,
+		0xfa, 0xce, 0xf7, 0xfe, 0x0f, 0x0c, 0xc6, 0x0a, 0x00, 0x3b,
+	}
+
+	blankGIFHash = md5.Sum(blankGIF)
+
+	blankGIFBARTItem = wire.BARTID{
+		Type: wire.BARTTypesBuddyIcon,
+		BARTInfo: wire.BARTInfo{
+			Flags: wire.BARTFlagsCustom,
+			Hash:  blankGIFHash[:],
+		},
+	}
+)
 
 func NewBARTService(
 	logger *slog.Logger,
@@ -57,25 +78,18 @@ func (s BARTService) UpsertItem(ctx context.Context, instance *state.SessionInst
 
 	s.logger.DebugContext(ctx, "successfully uploaded BART item", "hash", fmt.Sprintf("%x", hash))
 
-	bartID, hasIcon := instance.Session().BuddyIcon()
-	if hasIcon && bytes.Equal(hash, bartID.Hash) {
-		// unset unknown flag
-		bartID.Flags &^= wire.BARTFlagsUnknown
-		instance.Session().SetBuddyIcon(bartID)
-
-		sendUserInfoUpdateToAll(ctx, s.messageRelayer, instance)
+	bartID := wire.BARTID{
+		Type: inBody.Type,
+		BARTInfo: wire.BARTInfo{
+			Flags: wire.BARTFlagsCustom,
+			Hash:  hash,
+		},
+	}
 
-		if err := s.buddyUpdateBroadcaster.BroadcastBuddyArrived(ctx, instance.IdentScreenName(), instance.Session().TLVUserInfo()); err != nil {
+	if bartID.IsBuddyIcon() {
+		if err := s.updateBuddyIcon(ctx, instance, bartID); err != nil {
 			return wire.SNACMessage{}, err
 		}
-	} else {
-		bartID = wire.BARTID{
-			Type: inBody.Type,
-			BARTInfo: wire.BARTInfo{
-				Flags: wire.BARTFlagsCustom,
-				Hash:  hash,
-			},
-		}
 	}
 
 	return wire.SNACMessage{
@@ -91,12 +105,37 @@ func (s BARTService) UpsertItem(ctx context.Context, instance *state.SessionInst
 	}, nil
 }
 
+func (s BARTService) updateBuddyIcon(ctx context.Context, instance *state.SessionInstance, newID wire.BARTID) error {
+	curID, hasIcon := instance.Session().BuddyIcon()
+
+	if hasIcon && bytes.Equal(curID.Hash, newID.Hash) {
+		// client set icon metadata in prior FeedbagService call before upload
+		if err := setBuddyIconState(ctx, instance, newID, s.buddyUpdateBroadcaster, s.messageRelayer); err != nil {
+			return err
+		}
+	} else {
+		// client uploads buddy icon now and sets icon metadata in subsequent
+		// call to FeedbagService
+		s.messageRelayer.RelayToSelf(ctx, instance, wire.SNACMessage{
+			Frame: wire.SNACFrame{
+				FoodGroup: wire.OService,
+				SubGroup:  wire.OServiceBartReply,
+			},
+			Body: wire.SNAC_0x01_0x21_OServiceBARTReply{
+				BARTID: newID,
+			},
+		})
+	}
+
+	return nil
+}
+
 // RetrieveItem fetches a BART item from the data store. The item is selected
 // based on inBody.Hash. It's unclear what effect inBody.Flags is supposed to
 // have on the request.
 func (s BARTService) RetrieveItem(ctx context.Context, inFrame wire.SNACFrame, inBody wire.SNAC_0x10_0x04_BARTDownloadQuery) (wire.SNACMessage, error) {
 	var item []byte
-	if inBody.HasClearIconHash() {
+	if inBody.IsBuddyIcon() && (inBody.HasClearIconHash() || bytes.Equal(inBody.Hash, blankGIFHash[:])) {
 		item = blankGIF
 	} else {
 		var err error
@@ -127,7 +166,7 @@ func (s BARTService) RetrieveItemV2(ctx context.Context, inFrame wire.SNACFrame,
 	for _, id := range inBody.IDs {
 		var item []byte
 
-		if id.Type == wire.BARTTypesBuddyIcon && id.HasClearIconHash() {
+		if id.IsBuddyIcon() && (id.HasClearIconHash() || bytes.Equal(id.Hash, blankGIFHash[:])) {
 			item = blankGIF
 		} else {
 			var err error
@@ -159,3 +198,16 @@ func (s BARTService) RetrieveItemV2(ctx context.Context, inFrame wire.SNACFrame,
 
 	return result, nil
 }
+
+// setBuddyIconState sets bid as the session's buddy icon, then sends the
+// updated user info to the user's instances and to their buddies.
+func setBuddyIconState(ctx context.Context, instance *state.SessionInstance, bid wire.BARTID, buddyBroadcaster buddyBroadcaster, messageRelayer MessageRelayer) error {
+	instance.Session().SetBuddyIcon(bid)
+
+	sendUserInfoUpdateToAll(ctx, messageRelayer, instance)
+
+	if err := buddyBroadcaster.BroadcastBuddyArrived(ctx, instance.IdentScreenName(), instance.Session().TLVUserInfo()); err != nil {
+		return fmt.Errorf("BroadcastBuddyArrived: %w", err)
+	}
+	return nil
+}

+ 174 - 8
foodgroup/bart_test.go

@@ -3,6 +3,8 @@ package foodgroup
 import (
 	"bytes"
 	"context"
+	"image"
+	"image/gif"
 	"io"
 	"log/slog"
 	"testing"
@@ -360,7 +362,7 @@ func TestBARTService_UpsertItem(t *testing.T) {
 			wantErr:      io.EOF,
 		},
 		{
-			name: "insert new buddy icon unrelated to current session",
+			name: "upload of a new buddy icon confirms it with SNAC(0x01,0x21) without assigning it",
 			instance: newTestInstance("user_screen_name", sessOptBuddyIcon(wire.BARTID{
 				Type: wire.BARTTypesBuddyIcon,
 				BARTInfo: wire.BARTInfo{
@@ -387,7 +389,30 @@ func TestBARTService_UpsertItem(t *testing.T) {
 						},
 					},
 				},
-				messageRelayerParams: messageRelayerParams{},
+				messageRelayerParams: messageRelayerParams{
+					relayToSelfParams: relayToSelfParams{
+						{
+							screenName: state.NewIdentScreenName("user_screen_name"),
+							message: wire.SNACMessage{
+								Frame: wire.SNACFrame{
+									FoodGroup: wire.OService,
+									SubGroup:  wire.OServiceBartReply,
+								},
+								Body: func(val any) bool {
+									return assert.Equal(t, wire.SNAC_0x01_0x21_OServiceBARTReply{
+										BARTID: wire.BARTID{
+											Type: wire.BARTTypesBuddyIcon,
+											BARTInfo: wire.BARTInfo{
+												Flags: wire.BARTFlagsCustom,
+												Hash:  itemHash,
+											},
+										},
+									}, val)
+								},
+							},
+						},
+					},
+				},
 			},
 			expectOutput: wire.SNACMessage{
 				Frame: wire.SNACFrame{
@@ -407,7 +432,65 @@ func TestBARTService_UpsertItem(t *testing.T) {
 				},
 			},
 			instanceMatch: func(instance *state.SessionInstance) {
-				// assert session icon didn't change
+				have, hasIcon := instance.Session().BuddyIcon()
+				assert.True(t, hasIcon)
+				want := wire.BARTID{
+					Type: wire.BARTTypesBuddyIcon,
+					BARTInfo: wire.BARTInfo{
+						Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
+						Hash:  []byte("unrelated icon"),
+					},
+				}
+				assert.Equal(t, want, have)
+			},
+		},
+		{
+			name: "upload of a non-icon BART item doesn't change the buddy icon",
+			instance: newTestInstance("user_screen_name", sessOptBuddyIcon(wire.BARTID{
+				Type: wire.BARTTypesBuddyIcon,
+				BARTInfo: wire.BARTInfo{
+					Flags: wire.BARTFlagsCustom | wire.BARTFlagsKnown,
+					Hash:  []byte("unrelated icon"),
+				},
+			})),
+			inputSNAC: wire.SNACMessage{
+				Frame: wire.SNACFrame{
+					RequestID: 1234,
+				},
+				Body: wire.SNAC_0x10_0x02_BARTUploadQuery{
+					Type: wire.BARTTypesArriveSound,
+					Data: itemData,
+				},
+			},
+			mockParams: mockParams{
+				bartItemManagerParams: bartItemManagerParams{
+					bartItemManagerUpsertParams: bartItemManagerUpsertParams{
+						{
+							itemHash: itemHash,
+							payload:  itemData,
+							bartType: wire.BARTTypesArriveSound,
+						},
+					},
+				},
+			},
+			expectOutput: wire.SNACMessage{
+				Frame: wire.SNACFrame{
+					FoodGroup: wire.BART,
+					SubGroup:  wire.BARTUploadReply,
+					RequestID: 1234,
+				},
+				Body: wire.SNAC_0x10_0x03_BARTUploadReply{
+					Code: wire.BARTReplyCodesSuccess,
+					ID: wire.BARTID{
+						Type: wire.BARTTypesArriveSound,
+						BARTInfo: wire.BARTInfo{
+							Flags: wire.BARTFlagsCustom,
+							Hash:  itemHash,
+						},
+					},
+				},
+			},
+			instanceMatch: func(instance *state.SessionInstance) {
 				have, hasIcon := instance.Session().BuddyIcon()
 				assert.True(t, hasIcon)
 				want := wire.BARTID{
@@ -540,7 +623,7 @@ func TestBARTService_RetrieveItem(t *testing.T) {
 						Type: wire.BARTTypesBuddyIcon,
 						BARTInfo: wire.BARTInfo{
 							Flags: wire.BARTFlagsKnown,
-							Hash:  wire.GetClearIconHash(),
+							Hash:  wire.ClearIconHash(),
 						},
 					},
 				},
@@ -557,7 +640,7 @@ func TestBARTService_RetrieveItem(t *testing.T) {
 						Type: wire.BARTTypesBuddyIcon,
 						BARTInfo: wire.BARTInfo{
 							Flags: wire.BARTFlagsKnown,
-							Hash:  wire.GetClearIconHash(),
+							Hash:  wire.ClearIconHash(),
 						},
 					},
 					Data: blankGIF,
@@ -896,7 +979,7 @@ func TestBARTService_RetrieveItemV2(t *testing.T) {
 							Type: wire.BARTTypesBuddyIcon,
 							BARTInfo: wire.BARTInfo{
 								Flags: wire.BARTFlagsKnown,
-								Hash:  wire.GetClearIconHash(),
+								Hash:  wire.ClearIconHash(),
 							},
 						},
 					},
@@ -954,7 +1037,7 @@ func TestBARTService_RetrieveItemV2(t *testing.T) {
 								Type: wire.BARTTypesBuddyIcon,
 								BARTInfo: wire.BARTInfo{
 									Flags: wire.BARTFlagsKnown,
-									Hash:  wire.GetClearIconHash(),
+									Hash:  wire.ClearIconHash(),
 								},
 							},
 							Code: 0x00, // found
@@ -962,7 +1045,7 @@ func TestBARTService_RetrieveItemV2(t *testing.T) {
 								Type: wire.BARTTypesBuddyIcon,
 								BARTInfo: wire.BARTInfo{
 									Flags: wire.BARTFlagsKnown,
-									Hash:  wire.GetClearIconHash(),
+									Hash:  wire.ClearIconHash(),
 								},
 							},
 						},
@@ -991,3 +1074,86 @@ func TestBARTService_RetrieveItemV2(t *testing.T) {
 		})
 	}
 }
+
+func TestBARTService_RetrieveItem_BlankIcon(t *testing.T) {
+	storedItem := []byte("stored item")
+
+	tests := []struct {
+		name     string
+		bartID   wire.BARTID
+		wantData []byte
+	}{
+		{
+			name: "buddy icon with the clear icon hash gets the blank GIF",
+			bartID: wire.BARTID{
+				Type:     wire.BARTTypesBuddyIcon,
+				BARTInfo: wire.BARTInfo{Hash: wire.ClearIconHash()},
+			},
+			wantData: blankGIF,
+		},
+		{
+			name:     "buddy icon with the blank GIF hash gets the blank GIF",
+			bartID:   blankGIFBARTItem,
+			wantData: blankGIF,
+		},
+		{
+			name: "non-icon item with the clear icon hash comes from the BART store",
+			bartID: wire.BARTID{
+				Type:     wire.BARTTypesArriveSound,
+				BARTInfo: wire.BARTInfo{Hash: wire.ClearIconHash()},
+			},
+			wantData: storedItem,
+		},
+		{
+			name: "non-icon item with the blank GIF hash comes from the BART store",
+			bartID: wire.BARTID{
+				Type:     wire.BARTTypesArriveSound,
+				BARTInfo: wire.BARTInfo{Hash: blankGIFHash[:]},
+			},
+			wantData: storedItem,
+		},
+	}
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			bartItemManager := newMockBARTItemManager(t)
+			if !bytes.Equal(tc.wantData, blankGIF) {
+				bartItemManager.EXPECT().
+					BARTItem(matchContext(), tc.bartID.Hash).
+					Return(storedItem, nil).
+					Twice()
+			}
+			svc := NewBARTService(slog.Default(), bartItemManager, nil, nil, nil)
+
+			output, err := svc.RetrieveItem(context.Background(), wire.SNACFrame{RequestID: 1234}, wire.SNAC_0x10_0x04_BARTDownloadQuery{
+				ScreenName: "user",
+				BARTID:     tc.bartID,
+			})
+			assert.NoError(t, err)
+			assert.Equal(t, tc.wantData, output.Body.(wire.SNAC_0x10_0x05_BARTDownloadReply).Data)
+
+			outputs, err := svc.RetrieveItemV2(context.Background(), wire.SNACFrame{RequestID: 1234}, wire.SNAC_0x10_0x06_BARTDownload2Query{
+				ScreenName: "user",
+				IDs:        []wire.BARTID{tc.bartID},
+			})
+			assert.NoError(t, err)
+			assert.Len(t, outputs, 1)
+			assert.Equal(t, tc.wantData, outputs[0].Body.(wire.SNAC_0x10_0x07_BARTDownload2Reply).Data)
+		})
+	}
+}
+
+func TestBlankGIF(t *testing.T) {
+	assert.Len(t, blankGIF, 90)
+
+	img, err := gif.Decode(bytes.NewReader(blankGIF))
+	assert.NoError(t, err)
+
+	paletted, ok := img.(*image.Paletted)
+	assert.True(t, ok)
+	assert.Equal(t, image.Rect(0, 0, 44, 44), paletted.Bounds())
+	_, _, _, a := paletted.Palette[0].RGBA()
+	assert.Zero(t, a)
+	for _, px := range paletted.Pix {
+		assert.Zero(t, px)
+	}
+}

+ 34 - 14
foodgroup/feedbag.go

@@ -428,7 +428,7 @@ func (s *FeedbagService) setBARTItem(ctx context.Context, instance *state.Sessio
 
 	itemExists := false
 
-	if bytes.Equal(bartID.Hash, wire.GetClearIconHash()) {
+	if bytes.Equal(bartID.Hash, wire.ClearIconHash()) {
 		s.logger.DebugContext(ctx, "user is clearing icon",
 			"hash", fmt.Sprintf("%x", bartID.Hash))
 		itemExists = true
@@ -441,10 +441,9 @@ func (s *FeedbagService) setBARTItem(ctx context.Context, instance *state.Sessio
 	}
 
 	if itemExists {
-		if bartID.Type == wire.BARTTypesBuddyIconSmall || bartID.Type == wire.BARTTypesBuddyIcon {
-			instance.Session().SetBuddyIcon(bartID)
-			// tell buddies about the icon update
-			if err := s.buddyBroadcaster.BroadcastBuddyArrived(ctx, instance.IdentScreenName(), instance.Session().TLVUserInfo()); err != nil {
+		if bartID.IsBuddyIcon() {
+			// the icon is already in the BART store, so no upload is needed
+			if err := setBuddyIconState(ctx, instance, bartID, s.buddyBroadcaster, s.messageRelayer); err != nil {
 				return err
 			}
 		}
@@ -453,8 +452,9 @@ func (s *FeedbagService) setBARTItem(ctx context.Context, instance *state.Sessio
 	} else {
 		// icon doesn't exist, tell the client to upload buddy icon
 		bartID.Flags |= wire.BARTFlagsUnknown
-		if bartID.Type == wire.BARTTypesBuddyIconSmall || bartID.Type == wire.BARTTypesBuddyIcon {
+		if bartID.IsBuddyIcon() {
 			instance.Session().SetBuddyIcon(bartID)
+			sendUserInfoUpdateToAll(ctx, s.messageRelayer, instance)
 		}
 		s.logger.DebugContext(ctx, "icon doesn't exist in BART store, client must upload the icon file",
 			"hash", fmt.Sprintf("%x", bartID.Hash))
@@ -470,10 +470,6 @@ func (s *FeedbagService) setBARTItem(ctx context.Context, instance *state.Sessio
 		},
 	})
 
-	if bartID.Type == wire.BARTTypesBuddyIconSmall || bartID.Type == wire.BARTTypesBuddyIcon {
-		sendUserInfoUpdateToAll(ctx, s.messageRelayer, instance)
-	}
-
 	return nil
 }
 
@@ -482,15 +478,15 @@ func (s *FeedbagService) setBARTItem(ctx context.Context, instance *state.Sessio
 // Sends buddy arrival notifications to each unblocked buddy if current user is
 // visible. It returns wire.FeedbagStatus, which contains update confirmation.
 func (s *FeedbagService) DeleteItem(ctx context.Context, instance *state.SessionInstance, inFrame wire.SNACFrame, inBody wire.SNAC_0x13_0x0A_FeedbagDeleteItem) (*wire.SNACMessage, error) {
-	if err := s.feedbagManager.FeedbagDelete(ctx, instance.IdentScreenName(), inBody.Items); err != nil {
-		return nil, err
-	}
-
 	snacPayloadOut := wire.SNAC_0x13_0x0E_FeedbagStatus{}
 	for range inBody.Items {
 		snacPayloadOut.Results = append(snacPayloadOut.Results, 0x0000) // success by default
 	}
 
+	if err := s.feedbagManager.FeedbagDelete(ctx, instance.IdentScreenName(), inBody.Items); err != nil {
+		return nil, err
+	}
+
 	s.messageRelayer.RelayToSelf(ctx, instance, wire.SNACMessage{
 		Frame: wire.SNACFrame{
 			FoodGroup: wire.Feedbag,
@@ -528,6 +524,30 @@ func (s *FeedbagService) DeleteItem(ctx context.Context, instance *state.Session
 		}
 	}
 
+	// QIP 2005 and AIM 7 clear their buddy icon by deleting the BART item that
+	// holds it. Advertise the blank icon so their contacts see it cleared.
+	if curIcon, hasIcon := instance.Session().BuddyIcon(); hasIcon {
+		for _, item := range inBody.Items {
+			if item.ClassID != wire.FeedbagClassIdBart {
+				continue
+			}
+			b, ok := item.Bytes(wire.FeedbagAttributesBartInfo)
+			if !ok {
+				continue
+			}
+			info := wire.BARTInfo{}
+			if err := wire.UnmarshalBE(&info, bytes.NewBuffer(b)); err != nil {
+				continue
+			}
+			if bytes.Equal(info.Hash, curIcon.Hash) {
+				if err := setBuddyIconState(ctx, instance, blankGIFBARTItem, s.buddyBroadcaster, s.messageRelayer); err != nil {
+					return nil, err
+				}
+				break
+			}
+		}
+	}
+
 	return nil, nil
 }
 

+ 162 - 7
foodgroup/feedbag_test.go

@@ -1645,7 +1645,6 @@ func TestFeedbagService_UpsertItem(t *testing.T) {
 						},
 					},
 					adjacentUsersParams: adjacentUsersParams{},
-					feedbagParams:       feedbagParams{},
 				},
 				messageRelayerParams: messageRelayerParams{
 					relayToOtherInstancesParams: relayToOtherInstancesParams{
@@ -1770,7 +1769,7 @@ func TestFeedbagService_UpsertItem(t *testing.T) {
 								TLVList: wire.TLVList{
 									wire.NewTLVBE(wire.FeedbagAttributesBartInfo, wire.BARTInfo{
 										Flags: wire.BARTFlagsKnown,
-										Hash:  wire.GetClearIconHash(),
+										Hash:  wire.ClearIconHash(),
 									}),
 								},
 							},
@@ -1791,7 +1790,7 @@ func TestFeedbagService_UpsertItem(t *testing.T) {
 										TLVList: wire.TLVList{
 											wire.NewTLVBE(wire.FeedbagAttributesBartInfo, wire.BARTInfo{
 												Flags: wire.BARTFlagsKnown,
-												Hash:  wire.GetClearIconHash(),
+												Hash:  wire.ClearIconHash(),
 											}),
 										},
 									},
@@ -1825,7 +1824,7 @@ func TestFeedbagService_UpsertItem(t *testing.T) {
 											TLVLBlock: wire.TLVLBlock{
 												TLVList: wire.TLVList{
 													wire.NewTLVBE(wire.FeedbagAttributesBartInfo, wire.BARTInfo{
-														Hash: wire.GetClearIconHash(),
+														Hash: wire.ClearIconHash(),
 													}),
 												},
 											},
@@ -1852,7 +1851,7 @@ func TestFeedbagService_UpsertItem(t *testing.T) {
 									bartID, exists := snac.UserInfo[0].Bytes(wire.OServiceUserInfoBARTInfo)
 									return assert.True(t, exists) &&
 										assert.Equal(t, "me", snac.UserInfo[0].ScreenName) &&
-										assert.True(t, bytes.Contains(bartID, wire.GetClearIconHash()), "user info BART hash doesn't match")
+										assert.True(t, bytes.Contains(bartID, wire.ClearIconHash()), "user info BART hash doesn't match")
 								},
 							},
 						},
@@ -1868,7 +1867,7 @@ func TestFeedbagService_UpsertItem(t *testing.T) {
 										Type: wire.BARTTypesBuddyIcon,
 										BARTInfo: wire.BARTInfo{
 											Flags: wire.BARTFlagsKnown,
-											Hash:  wire.GetClearIconHash(),
+											Hash:  wire.ClearIconHash(),
 										},
 									},
 								},
@@ -1894,7 +1893,7 @@ func TestFeedbagService_UpsertItem(t *testing.T) {
 			instanceMatch: func(instance *state.SessionInstance) {
 				bartInfo, hasIcon := instance.Session().BuddyIcon()
 				assert.True(t, hasIcon)
-				assert.Equal(t, wire.GetClearIconHash(), bartInfo.Hash)
+				assert.Equal(t, wire.ClearIconHash(), bartInfo.Hash)
 			},
 		},
 		{
@@ -5356,3 +5355,159 @@ func TestFeedbagService_ForwardICQAuthEvents(t *testing.T) {
 		})
 	}
 }
+
+func TestFeedbagService_DeleteItem_BARTItem(t *testing.T) {
+	currentIcon := wire.BARTID{
+		Type: wire.BARTTypesBuddyIcon,
+		BARTInfo: wire.BARTInfo{
+			Flags: wire.BARTFlagsCustom,
+			Hash:  []byte("current icon...."),
+		},
+	}
+	bartItem := func(hash []byte) wire.FeedbagItem {
+		return wire.FeedbagItem{
+			Name:    fmt.Sprintf("%d", wire.BARTTypesBuddyIcon),
+			ItemID:  10,
+			ClassID: wire.FeedbagClassIdBart,
+			TLVLBlock: wire.TLVLBlock{
+				TLVList: wire.TLVList{
+					wire.NewTLVBE(wire.FeedbagAttributesBartInfo, wire.BARTInfo{
+						Flags: wire.BARTFlagsCustom,
+						Hash:  hash,
+					}),
+				},
+			},
+		}
+	}
+	buddyItem := wire.FeedbagItem{
+		Name:    "friend",
+		GroupID: 1,
+		ItemID:  20,
+		ClassID: wire.FeedbagClassIdBuddy,
+	}
+	hasBlankIcon := func(info wire.TLVUserInfo) bool {
+		b, ok := info.Bytes(wire.OServiceUserInfoBARTInfo)
+		return ok && bytes.Contains(b, blankGIFHash[:])
+	}
+	bartItemNoInfo := wire.FeedbagItem{
+		Name:    fmt.Sprintf("%d", wire.BARTTypesBuddyIcon),
+		ItemID:  10,
+		ClassID: wire.FeedbagClassIdBart,
+	}
+
+	tests := []struct {
+		name          string
+		instance      *state.SessionInstance
+		items         []wire.FeedbagItem
+		wantFilter    []state.IdentScreenName
+		wantBlankIcon bool
+		wantIcon      wire.BARTID
+		wantNoIcon    bool
+	}{
+		{
+			name:          "deleting the BART item that holds the current icon advertises the blank icon",
+			instance:      newTestInstance("me", sessOptBuddyIcon(currentIcon)),
+			items:         []wire.FeedbagItem{bartItem(currentIcon.Hash)},
+			wantBlankIcon: true,
+			wantIcon:      blankGIFBARTItem,
+		},
+		{
+			name:          "deleting the BART item that holds the current icon with a buddy advertises the blank icon",
+			instance:      newTestInstance("me", sessOptBuddyIcon(currentIcon)),
+			items:         []wire.FeedbagItem{buddyItem, bartItem(currentIcon.Hash)},
+			wantFilter:    []state.IdentScreenName{state.NewIdentScreenName("friend")},
+			wantBlankIcon: true,
+			wantIcon:      blankGIFBARTItem,
+		},
+		{
+			name:          "deleting two BART items that hold the current icon advertises the blank icon once",
+			instance:      newTestInstance("me", sessOptBuddyIcon(currentIcon)),
+			items:         []wire.FeedbagItem{bartItem(currentIcon.Hash), bartItem(currentIcon.Hash)},
+			wantBlankIcon: true,
+			wantIcon:      blankGIFBARTItem,
+		},
+		{
+			name:     "deleting a BART item that holds another icon keeps the current icon",
+			instance: newTestInstance("me", sessOptBuddyIcon(currentIcon)),
+			items:    []wire.FeedbagItem{bartItem([]byte("replaced icon..."))},
+			wantIcon: currentIcon,
+		},
+		{
+			name:     "deleting a BART item without BART info keeps the current icon",
+			instance: newTestInstance("me", sessOptBuddyIcon(currentIcon)),
+			items:    []wire.FeedbagItem{bartItemNoInfo},
+			wantIcon: currentIcon,
+		},
+		{
+			name:     "deleting a buddy keeps the current icon",
+			instance: newTestInstance("me", sessOptBuddyIcon(currentIcon)),
+			items:    []wire.FeedbagItem{buddyItem},
+			wantFilter: []state.IdentScreenName{
+				state.NewIdentScreenName("friend"),
+			},
+			wantIcon: currentIcon,
+		},
+		{
+			name:       "deleting a BART item without a session icon doesn't set one",
+			instance:   newTestInstance("me"),
+			items:      []wire.FeedbagItem{bartItem(currentIcon.Hash)},
+			wantNoIcon: true,
+		},
+	}
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			sn := tc.instance.IdentScreenName()
+
+			feedbagManager := newMockFeedbagManager(t)
+			feedbagManager.EXPECT().
+				FeedbagDelete(matchContext(), sn, tc.items).
+				Return(nil)
+
+			buddyBroadcaster := newMockbuddyBroadcaster(t)
+			buddyBroadcaster.EXPECT().
+				BroadcastVisibility(mock.Anything, matchSession(sn), tc.wantFilter, true).
+				Return(nil)
+
+			messageRelayer := newMockMessageRelayer(t)
+			messageRelayer.EXPECT().
+				RelayToSelf(mock.Anything, mock.Anything, mock.MatchedBy(func(msg wire.SNACMessage) bool {
+					return msg.Frame.FoodGroup == wire.Feedbag && msg.Frame.SubGroup == wire.FeedbagStatus
+				})).
+				Once()
+			messageRelayer.EXPECT().
+				RelayToOtherInstances(mock.Anything, mock.Anything, mock.Anything).
+				Once()
+
+			if tc.wantBlankIcon {
+				buddyBroadcaster.EXPECT().
+					BroadcastBuddyArrived(mock.Anything, sn, mock.MatchedBy(hasBlankIcon)).
+					Return(nil).
+					Once()
+				messageRelayer.EXPECT().
+					RelayToSelf(mock.Anything, mock.Anything, mock.MatchedBy(func(msg wire.SNACMessage) bool {
+						snac, ok := msg.Body.(wire.SNAC_0x01_0x0F_OServiceUserInfoUpdate)
+						return ok && hasBlankIcon(snac.UserInfo[0])
+					})).
+					Once()
+			}
+
+			svc := FeedbagService{
+				buddyBroadcaster: buddyBroadcaster,
+				feedbagManager:   feedbagManager,
+				messageRelayer:   messageRelayer,
+			}
+			output, err := svc.DeleteItem(context.Background(), tc.instance, wire.SNACFrame{RequestID: 1234},
+				wire.SNAC_0x13_0x0A_FeedbagDeleteItem{Items: tc.items})
+			assert.NoError(t, err)
+			assert.Nil(t, output)
+
+			have, hasIcon := tc.instance.Session().BuddyIcon()
+			if tc.wantNoIcon {
+				assert.False(t, hasIcon)
+				return
+			}
+			assert.True(t, hasIcon)
+			assert.Equal(t, tc.wantIcon, have)
+		})
+	}
+}

+ 1 - 1
server/http/mgmt_api_test.go

@@ -968,7 +968,7 @@ func TestUserBuddyIconHandler_GET(t *testing.T) {
 								Type: wire.BARTTypesBuddyIcon,
 								BARTInfo: wire.BARTInfo{
 									Flags: 0x00,
-									Hash:  wire.GetClearIconHash(),
+									Hash:  wire.ClearIconHash(),
 								},
 							},
 						},

+ 1 - 1
server/webapi/expressions_handler.go

@@ -126,7 +126,7 @@ func (h *ExpressionsHandler) serveIcon(w http.ResponseWriter, r *http.Request, s
 		if errors.Is(err, ErrNoBuddyIcon) {
 			// Serve the blank placeholder rather than 404 so a cleared icon still
 			// renders something and the client stops showing the previous one.
-			icon, err = h.IconSource.ImageForHash(ctx, screenName, wire.GetClearIconHash())
+			icon, err = h.IconSource.ImageForHash(ctx, screenName, wire.ClearIconHash())
 		}
 	}
 

+ 2 - 2
server/webapi/expressions_handler_test.go

@@ -409,7 +409,7 @@ func TestBuddyIconSource_URL(t *testing.T) {
 		{
 			name:    "a cleared icon is not published",
 			baseURL: "http://api.example.com",
-			id:      bartID(wire.GetClearIconHash()),
+			id:      bartID(wire.ClearIconHash()),
 			want:    "",
 		},
 		{
@@ -538,7 +538,7 @@ func TestBuddyIconSource_PublishedURL(t *testing.T) {
 		{
 			name:    "a cleared icon yields the placeholder URL",
 			baseURL: "http://api.example.com",
-			id:      bartID(wire.GetClearIconHash()),
+			id:      bartID(wire.ClearIconHash()),
 			want:    "http://api.example.com/expressions/get?t=mikekelly&type=buddyIcon",
 		},
 		{

+ 1 - 1
server/webapi/session.go

@@ -743,7 +743,7 @@ func bartIDs(info wire.TLVUserInfo) []wire.BARTID {
 // when it carries none.
 func buddyIconHash(info wire.TLVUserInfo) []byte {
 	for _, id := range bartIDs(info) {
-		if id.Type == wire.BARTTypesBuddyIcon || id.Type == wire.BARTTypesBuddyIconSmall {
+		if id.IsBuddyIcon() {
 			return id.Hash
 		}
 	}

+ 2 - 2
server/webapi/session_test.go

@@ -962,8 +962,8 @@ func TestSession_PublishesBuddyIconOnPresence(t *testing.T) {
 
 	t.Run("cleared icon yields a URL naming the sentinel hash", func(t *testing.T) {
 		sess := newSession()
-		arrived(sess, "Mike Kelly", wire.GetClearIconHash())
-		assert.Equal(t, "icon:"+hex.EncodeToString(wire.GetClearIconHash()), lastPresence(sess).BuddyIcon)
+		arrived(sess, "Mike Kelly", wire.ClearIconHash())
+		assert.Equal(t, "icon:"+hex.EncodeToString(wire.ClearIconHash()), lastPresence(sess).BuddyIcon)
 	})
 
 	t.Run("departed omits the icon so the client preserves it", func(t *testing.T) {

+ 11 - 4
wire/snacs.go

@@ -1637,9 +1637,10 @@ const (
 	BARTReplyCodesNotfound    uint8 = 0x07
 )
 
-// GetClearIconHash returns an opaque value set in BARTID hash that indicates
-// the user wants to clear their buddy icon.
-func GetClearIconHash() []byte {
+// ClearIconHash returns the BART ID for a blank, transparent icon requested by
+// some AIM clients that clears the current icon. This hash is not derived from
+// the icon payload.
+func ClearIconHash() []byte {
 	return []byte{0x02, 0x01, 0xd2, 0x04, 0x72}
 }
 
@@ -1652,7 +1653,7 @@ type BARTInfo struct {
 // HasClearIconHash reports whether the BART ID hash contains the
 // ClearIconHash sentinel value.
 func (h BARTInfo) HasClearIconHash() bool {
-	return bytes.Equal(h.Hash, GetClearIconHash())
+	return bytes.Equal(h.Hash, ClearIconHash())
 }
 
 // maxStatusMsgLen is the longest status message that fits in a BART item, whose
@@ -1702,6 +1703,12 @@ func (b *BARTID) StatusText() (string, error) {
 	return status.Status, nil
 }
 
+func (b *BARTID) IsBuddyIcon() bool {
+	return b.Type == BARTTypesBuddyIcon ||
+		b.Type == BARTTypesBuddyIconSmall ||
+		b.Type == BARTTypesBuddyIconBig
+}
+
 type BartQueryReplyID struct {
 	QueryID BARTID
 	Code    uint8

+ 1 - 1
wire/snacs_test.go

@@ -16,7 +16,7 @@ func TestBARTInfo_HasClearIconHash(t *testing.T) {
 	}{
 		{
 			bartInfo: BARTInfo{
-				Hash: GetClearIconHash(),
+				Hash: ClearIconHash(),
 			},
 			want: true,
 		},