| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | package extension | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/xml" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 15:34:18 +01:00
										 |  |  | 	"dev.sum7.eu/genofire/yaja/database" | 
					
						
							|  |  |  | 	"dev.sum7.eu/genofire/yaja/messages" | 
					
						
							|  |  |  | 	"dev.sum7.eu/genofire/yaja/server/utils" | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 15:39:36 +01:00
										 |  |  | type IQDisco struct { | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | 	IQExtension | 
					
						
							|  |  |  | 	Database *database.State | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 15:39:36 +01:00
										 |  |  | func (ex *IQDisco) Spaces() []string { return []string{"http://jabber.org/protocol/disco#items"} } | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 19:32:11 +01:00
										 |  |  | func (ex *IQDisco) Get(msg *messages.IQClient, client *utils.Client) bool { | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | 	log := client.Log.WithField("extension", "disco-item").WithField("id", msg.ID) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// query encode
 | 
					
						
							|  |  |  | 	type query struct { | 
					
						
							|  |  |  | 		XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"` | 
					
						
							|  |  |  | 		Body    []byte   `xml:",innerxml"` | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	q := &query{} | 
					
						
							| 
									
										
										
										
											2017-12-17 17:50:51 +01:00
										 |  |  | 	if err := xml.Unmarshal(msg.Body, q); err != nil { | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// answer query
 | 
					
						
							|  |  |  | 	q.Body = []byte{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// build answer body
 | 
					
						
							|  |  |  | 	type item struct { | 
					
						
							|  |  |  | 		XMLName xml.Name `xml:"item"` | 
					
						
							|  |  |  | 		JID     string   `xml:"jid,attr"` | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-17 15:39:36 +01:00
										 |  |  | 	if acc := ex.Database.GetAccount(client.JID); acc != nil { | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | 		for jid, _ := range acc.Bookmarks { | 
					
						
							|  |  |  | 			itemByte, err := xml.Marshal(&item{ | 
					
						
							|  |  |  | 				JID: jid, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				log.Warn(err) | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			q.Body = append(q.Body, itemByte...) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// decode query
 | 
					
						
							|  |  |  | 	queryByte, err := xml.Marshal(q) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Warn(err) | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// reply
 | 
					
						
							| 
									
										
										
										
											2018-02-07 19:32:11 +01:00
										 |  |  | 	client.Messages <- &messages.IQClient{ | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | 		Type: messages.IQTypeResult, | 
					
						
							|  |  |  | 		To:   client.JID.String(), | 
					
						
							|  |  |  | 		From: client.JID.Domain, | 
					
						
							|  |  |  | 		ID:   msg.ID, | 
					
						
							|  |  |  | 		Body: queryByte, | 
					
						
							| 
									
										
										
										
											2017-12-17 15:39:36 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-17 13:31:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	log.Debug("send") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } |