docs: def encode(tags):
      '''Encodes a dictionary of tags to fit into an IRC-message.
      See IRC Message Tags: http://ircv3.net/specs/core/message-tags-3.2.html
  
      >>> from collections import OrderedDict
      >>> encode({'key': 'value'})
      'key=value'
  
      >>> d = {'aaa': 'bbb', 'ccc': None, 'example.com/ddd': 'eee'}
      >>> d_ordered = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
      >>> encode(d_ordered)
      'aaa=bbb;ccc;example.com/ddd=eee'
  
      >>> d = {'key': 'value;with special\\\\characters', 'key2': 'with=equals'}
      >>> d_ordered = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
      >>> print(encode(d_ordered))
      key=value\\:with\\sspecial\\\characters;key2=with=equals
  
      >>> print(encode({'key': r'\\something'}))
      key=\\\\something
  
      '''
      tagstrings = []
      for key, value in tags.items():
          if not _valid_key.match(key):
              raise ValueError("dictionary key is invalid as tag key: " + key)
          # if no value, just append the key
          if value:
              tagstrings.append(key + "=" + _escape(value))
          else:
              tagstrings.append(key)
      return ";".join(tagstrings)