1package gomatrix 2 3import ( 4 "html" 5 "regexp" 6) 7 8// Event represents a single Matrix event. 9type Event struct { 10 StateKey *string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events. 11 Sender string `json:"sender"` // The user ID of the sender of the event 12 Type string `json:"type"` // The event type 13 Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server 14 ID string `json:"event_id"` // The unique ID of this event 15 RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence) 16 Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event 17 Unsigned map[string]interface{} `json:"unsigned"` // The unsigned portions of the event, such as age and prev_content 18 Content map[string]interface{} `json:"content"` // The JSON content of the event. 19 PrevContent map[string]interface{} `json:"prev_content,omitempty"` // The JSON prev_content of the event. 20} 21 22// Body returns the value of the "body" key in the event content if it is 23// present and is a string. 24func (event *Event) Body() (body string, ok bool) { 25 value, exists := event.Content["body"] 26 if !exists { 27 return 28 } 29 body, ok = value.(string) 30 return 31} 32 33// MessageType returns the value of the "msgtype" key in the event content if 34// it is present and is a string. 35func (event *Event) MessageType() (msgtype string, ok bool) { 36 value, exists := event.Content["msgtype"] 37 if !exists { 38 return 39 } 40 msgtype, ok = value.(string) 41 return 42} 43 44// TextMessage is the contents of a Matrix formated message event. 45type TextMessage struct { 46 MsgType string `json:"msgtype"` 47 Body string `json:"body"` 48 FormattedBody string `json:"formatted_body"` 49 Format string `json:"format"` 50} 51 52// ThumbnailInfo contains info about an thumbnail image - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-image 53type ThumbnailInfo struct { 54 Height uint `json:"h,omitempty"` 55 Width uint `json:"w,omitempty"` 56 Mimetype string `json:"mimetype,omitempty"` 57 Size uint `json:"size,omitempty"` 58} 59 60// ImageInfo contains info about an image - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-image 61type ImageInfo struct { 62 Height uint `json:"h,omitempty"` 63 Width uint `json:"w,omitempty"` 64 Mimetype string `json:"mimetype,omitempty"` 65 Size uint `json:"size,omitempty"` 66 ThumbnailInfo ThumbnailInfo `json:"thumbnail_info,omitempty"` 67 ThumbnailURL string `json:"thumbnail_url,omitempty"` 68} 69 70// VideoInfo contains info about a video - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-video 71type VideoInfo struct { 72 Mimetype string `json:"mimetype,omitempty"` 73 ThumbnailInfo ThumbnailInfo `json:"thumbnail_info"` 74 ThumbnailURL string `json:"thumbnail_url,omitempty"` 75 Height uint `json:"h,omitempty"` 76 Width uint `json:"w,omitempty"` 77 Duration uint `json:"duration,omitempty"` 78 Size uint `json:"size,omitempty"` 79} 80 81// VideoMessage is an m.video - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-video 82type VideoMessage struct { 83 MsgType string `json:"msgtype"` 84 Body string `json:"body"` 85 URL string `json:"url"` 86 Info VideoInfo `json:"info"` 87} 88 89// ImageMessage is an m.image event 90type ImageMessage struct { 91 MsgType string `json:"msgtype"` 92 Body string `json:"body"` 93 URL string `json:"url"` 94 Info ImageInfo `json:"info"` 95} 96 97// An HTMLMessage is the contents of a Matrix HTML formated message event. 98type HTMLMessage struct { 99 Body string `json:"body"` 100 MsgType string `json:"msgtype"` 101 Format string `json:"format"` 102 FormattedBody string `json:"formatted_body"` 103} 104 105// FileInfo contains info about an file - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-file 106type FileInfo struct { 107 Mimetype string `json:"mimetype,omitempty"` 108 Size uint `json:"size,omitempty"` //filesize in bytes 109} 110 111// FileMessage is an m.file event - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-file 112type FileMessage struct { 113 MsgType string `json:"msgtype"` 114 Body string `json:"body"` 115 URL string `json:"url"` 116 Filename string `json:"filename"` 117 Info FileInfo `json:"info,omitempty"` 118 ThumbnailURL string `json:"thumbnail_url,omitempty"` 119 ThumbnailInfo ImageInfo `json:"thumbnail_info,omitempty"` 120} 121 122// LocationMessage is an m.location event - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-location 123type LocationMessage struct { 124 MsgType string `json:"msgtype"` 125 Body string `json:"body"` 126 GeoURI string `json:"geo_uri"` 127 ThumbnailURL string `json:"thumbnail_url,omitempty"` 128 ThumbnailInfo ImageInfo `json:"thumbnail_info,omitempty"` 129} 130 131// AudioInfo contains info about an file - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-audio 132type AudioInfo struct { 133 Mimetype string `json:"mimetype,omitempty"` 134 Size uint `json:"size,omitempty"` //filesize in bytes 135 Duration uint `json:"duration,omitempty"` //audio duration in ms 136} 137 138// AudioMessage is an m.audio event - http://matrix.org/docs/spec/client_server/r0.2.0.html#m-audio 139type AudioMessage struct { 140 MsgType string `json:"msgtype"` 141 Body string `json:"body"` 142 URL string `json:"url"` 143 Info AudioInfo `json:"info,omitempty"` 144} 145 146var htmlRegex = regexp.MustCompile("<[^<]+?>") 147 148// GetHTMLMessage returns an HTMLMessage with the body set to a stripped version of the provided HTML, in addition 149// to the provided HTML. 150func GetHTMLMessage(msgtype, htmlText string) HTMLMessage { 151 return HTMLMessage{ 152 Body: html.UnescapeString(htmlRegex.ReplaceAllLiteralString(htmlText, "")), 153 MsgType: msgtype, 154 Format: "org.matrix.custom.html", 155 FormattedBody: htmlText, 156 } 157} 158