<commit_before>package main

import (
	"bytes"
	"encoding/xml"
	"errors"
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/shizeeg/xmpp"
)

type Session struct {
	conn              *xmpp.Conn
	config            *Config
	conferences       map[string]Conference
	pendingSubscribes map[string]string
	// timeouts maps from Cookies (from outstanding requests) to the
	// absolute time when that request should timeout.
	timeouts map[xmpp.Cookie]time.Time
}

func (s *Session) Say(stanza interface{}, msg string, private bool) (err error) {
	typ := "chat"
	var to string
	stnza, ok := stanza.(xmpp.Stanza)
	if !ok {
		msg = "error stanza type!"
	}
	switch st := stnza.Value.(type) {
	case *xmpp.ClientMessage:
		typ = st.Type
		to = st.From
		if !private && typ == "groupchat" {
			var nick string
			to, nick = SplitJID(st.From)
			msg = nick + ": " + msg
		}
		if private {
			typ = "chat"
			to = st.From
		}
	default:
		fmt.Printf("s.Say: unknown stanza: %#v\nmessage: %q\n", stanza, msg)
	}
	fmt.Printf("\nOn %#v\nSAY(to=%s, typ=%s, msg=%s)\n", stanza, to, typ, msg)
	return s.conn.SendMUC(to, typ, msg)
}

// readMessages reads stanza from channel and returns it
func (s *Session) readMessages(stanzaChan chan<- xmpp.Stanza) {
	defer close(stanzaChan)

	for {
		stanza, err := s.conn.Next()
		if err != nil {
			log.SetPrefix("s.readMessages() ")
			log.Println(err)
			return
		}
		stanzaChan <- stanza
	}
}

// processPresence handle incoming presences
// it also handles MUC (XEP-0045) "onJoin" presences.
func (s *Session) processPresence(stanza *xmpp.MUCPresence) {
	//	if out, err := xml.Marshal(stanza); err != nil {
	//		log.SetPrefix("!!!ERROR!!! ")
	//		log.Printf("PRESENCE: %#v\n", err)
	//	} else {
	//		log.SetPrefix("PRESENCE ")
	//		log.Printf("%#v\n%s\n-- \n", stanza, out)
	//	}
	confJID := xmpp.RemoveResourceFromJid(stanza.From)
	switch stanza.Type {
	case "unavailable":
		if conf, ok := s.conferences[confJID]; ok {
			occupant, err := conf.OccupantDel(stanza)
			if err != nil {
				log.Println(err)
			}
			s.conferences[confJID] = conf
			// We has left conference
			if stanza.IsCode("110") && occupant.Nick == conf.Parser.OwnNick {
				conf, err := s.ConfDel(stanza)
				if err != nil {
					fmt.Println(err)
				}
				fmt.Printf("We're %q has quit %q!\n", occupant.Nick, conf.JID)<commit_msg>fix: server don't send <status code="110"/> on kick/ban. Oops.<commit_after>package main

import (
	"bytes"
	"encoding/xml"
	"errors"
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/shizeeg/xmpp"
)

type Session struct {
	conn              *xmpp.Conn
	config            *Config
	conferences       map[string]Conference
	pendingSubscribes map[string]string
	// timeouts maps from Cookies (from outstanding requests) to the
	// absolute time when that request should timeout.
	timeouts map[xmpp.Cookie]time.Time
}

func (s *Session) Say(stanza interface{}, msg string, private bool) (err error) {
	typ := "chat"
	var to string
	stnza, ok := stanza.(xmpp.Stanza)
	if !ok {
		msg = "error stanza type!"
	}
	switch st := stnza.Value.(type) {
	case *xmpp.ClientMessage:
		typ = st.Type
		to = st.From
		if !private && typ == "groupchat" {
			var nick string
			to, nick = SplitJID(st.From)
			msg = nick + ": " + msg
		}
		if private {
			typ = "chat"
			to = st.From
		}
	default:
		fmt.Printf("s.Say: unknown stanza: %#v\nmessage: %q\n", stanza, msg)
	}
	fmt.Printf("\nOn %#v\nSAY(to=%s, typ=%s, msg=%s)\n", stanza, to, typ, msg)
	return s.conn.SendMUC(to, typ, msg)
}

// readMessages reads stanza from channel and returns it
func (s *Session) readMessages(stanzaChan chan<- xmpp.Stanza) {
	defer close(stanzaChan)

	for {
		stanza, err := s.conn.Next()
		if err != nil {
			log.SetPrefix("s.readMessages() ")
			log.Println(err)
			return
		}
		stanzaChan <- stanza
	}
}

// processPresence handle incoming presences
// it also handles MUC (XEP-0045) "onJoin" presences.
func (s *Session) processPresence(stanza *xmpp.MUCPresence) {
	//	if out, err := xml.Marshal(stanza); err != nil {
	//		log.SetPrefix("!!!ERROR!!! ")
	//		log.Printf("PRESENCE: %#v\n", err)
	//	} else {
	//		log.SetPrefix("PRESENCE ")
	//		log.Printf("%#v\n%s\n-- \n", stanza, out)
	//	}
	confJID := xmpp.RemoveResourceFromJid(stanza.From)
	switch stanza.Type {
	case "unavailable":
		if conf, ok := s.conferences[confJID]; ok {
			occupant, err := conf.OccupantDel(stanza)
			if err != nil {
				log.Println(err)
			}
			s.conferences[confJID] = conf
			// We has left conference
			if occupant.Nick == conf.Parser.OwnNick {
				conf, err := s.ConfDel(stanza)
				if err != nil {
					fmt.Println(err)
				}
				fmt.Printf("We're %q has quit %q!\n", occupant.Nick, conf.JID)
{'fraction_non_alphanumeric': 0.15919330615747693, 'fraction_numerical': 0.003003647285990131, 'mean_word_length': 3.1476868327402134, 'pattern_counts': {'":': 4, '<': 8, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2422668', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1802, 'n_words': 436}