Welcome to mirror list, hosted at ThFree Co, Russian Federation.

core.py « core - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18b8e04f282570d1441d1cddf72eda42ccc52104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
##	core/core.py
##
## Gajim Team:
## 	- Yann Le Boulanger <asterix@crans.org>
## 	- Vincent Hanquez <tab@tuxfamily.org>
## 	- David Ferlier <david@yazzy.org>
##
##	Copyright (C) 2003 Gajim Team
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

import socket
import sys
import time
import string
import logging

import plugins
import common.hub
import common.jabber
import common.optparser

log = logging.getLogger('core.core')
log.setLevel(logging.DEBUG)
CONFPATH = "~/.gajimrc"

class GajimCore:
	def __init__(self):
		self.connected = 0
		self.cfgParser = common.optparser.OptionsParser(CONFPATH)
		self.hub = common.hub.GajimHub()
		self.cfgParser.parseCfgFile()
	# END __init__

	def messageCB(self, con, msg):
		self.hub.sendPlugin('MSG', (msg.getFrom().getBasic(), \
			msg.getBody()))
	# END messageCB

	def presenceCB(self, con, prs):
		who = str(prs.getFrom())
		type = prs.getType()
		if type == None: type = 'available'
		log.debug("PresenceCB : %s" % type)
		if type == 'available':
			if prs.getShow():
				show = prs.getShow()
			else:
				show = 'online'
			self.hub.sendPlugin('NOTIFY', \
			(prs.getFrom().getBasic(), show, prs.getStatus()))
		elif type == 'unavailable':
			self.hub.sendPlugin('NOTIFY', \
				(prs.getFrom().getBasic(), 'offline', prs.getStatus()))
		elif type == 'subscribe':
			log.debug("subscribe request from %s" % who)
			if self.cfgParser.Core_alwaysauth == 1 or string.find(who, "@") <= 0:
				self.con.send(common.jabber.Presence(who, 'subscribed'))
			else:
				self.hub.sendPlugin('SUBSCRIBE', who)
		elif type == 'subscribed':
			jid = prs.getFrom()
			self.hub.sendPlugin('SUBSCRIBED', {'jid':jid.getBasic(), \
				'nom':jid.getNode()})
			self.con.updateRosterItem(jid=jid.getBasic(), name=jid.getNode())
			log.debug("we are now subscribed to %s" % who)
		elif type == 'unsubscribe':
			log.debug("unsubscribe request from %s" % who)
		elif type == 'unsubscribed':
			log.debug("we are now unsubscribed to %s" % who)
			
	# END presenceCB

	def disconnectedCB(self, con):
		log.debug("disconnectedCB")
	# END disconenctedCB

	def connect(self):
		self.con = common.jabber.Client(host = \
			self.cfgParser.Server_hostname, \
			debug = False, log = sys.stderr)
		try:
			self.con.connect()
		except IOError, e:
			log.debug("Couldn't connect to %s" % e)
			sys.exit(0)
		else:
			log.debug("Connected to server")

			self.con.setMessageHandler(self.messageCB)
			self.con.setPresenceHandler(self.presenceCB)
			self.con.setDisconnectHandler(self.disconnectedCB)
			if self.con.auth(self.cfgParser.Profile_name,
					self.cfgParser.Profile_password, 
					self.cfgParser.Profile_ressource):

				self.con.requestRoster()
				roster = self.con.getRoster().getRaw()
				if not roster :
					roster = {}
				self.hub.sendPlugin('ROSTER', roster)
				self.con.sendInitPresence()
				self.connected = 1
			else:
				sys.exit(1)
	# END connect

	def mainLoop(self):
		while 1:
			if not self.hub.queueIn.empty():
				ev = self.hub.queueIn.get()
				if ev[0] == 'QUIT':
					if self.connected == 1:
						self.con.disconnect()
					self.hub.sendPlugin('QUIT', ())
					return
				#('STATUS', status)
				elif ev[0] == 'STATUS':
					if (ev[1] != 'offline') and (self.connected == 0):
						self.connect()
					elif (ev[1] == 'offline') and (self.connected == 1):
						self.con.disconnect()
						self.connected = 0
					if ev[1] != 'offline':
						p = common.jabber.Presence()
						p.setShow(ev[1])
						self.con.send(p)
				#('MSG', (jid, msg))
				elif ev[0] == 'MSG':
					msg = common.jabber.Message(ev[1][0], ev[1][1])
					msg.setType('chat')
					self.con.send(msg)
					self.hub.sendPlugin('MSGSENT', ev[1])
				#('SUB', (jid, txt))
				elif ev[0] == 'SUB':
					log.debug('subscription request for %s' % ev[1][0])
					self.con.send(common.jabber.Presence(ev[1][0], 'subscribe'))
				#('REQ', jid)
				elif ev[0] == 'AUTH':
					self.con.send(common.jabber.Presence(ev[1], 'subscribed'))
				#('DENY', jid)
				elif ev[0] == 'DENY':
					self.con.send(common.jabber.Presence(ev[1], 'unsubscribed'))
				#('UNSUB', jid)
				elif ev[0] == 'UNSUB':
					delauth = self.cfgParser.Core_delauth
					if not delauth: delauth = 1
					delroster = self.cfgParser.Core_delroster
					if not delroster: delroster = 1
					if delauth:
						self.con.send(common.jabber.Presence(ev[1], 'unsubscribe'))
					if delroster:
						self.con.removeRosterItem(ev[1])
				#('UPDUSER', (jid, name, groups))
				elif ev[0] == 'UPDUSER':
					self.con.updateRosterItem(jid=ev[1][0], name=ev[1][1], groups=ev[1][2])
				elif ev[0] == 'REQ_AGENTS':
					agents = self.con.requestAgents()
					self.hub.sendPlugin('AGENTS', agents)
				elif ev[0] == 'REQ_AGENT_INFO':
					self.con.requestRegInfo(ev[1])
					agent_info = self.con.getRegInfo()
					self.hub.sendPlugin('AGENT_INFO', (ev[1], agent_info))
				elif ev[0] == 'REG_AGENT':
					self.con.sendRegInfo(ev[1])
				else:
					log.debug("Unknown Command")
			elif self.connected == 1:
				self.con.process(1)
			time.sleep(0.1)
	# END main
# END GajimCore

def start():
	gc = GajimCore()
	guiPl = gc.hub.newPlugin('gtkgui')
	gc.hub.register('gtkgui', 'ROSTER')
	gc.hub.register('gtkgui', 'NOTIFY')
	gc.hub.register('gtkgui', 'MSG')
	gc.hub.register('gtkgui', 'SUBSCRIBED')
	gc.hub.register('gtkgui', 'SUBSCRIBE')
	gc.hub.register('gtkgui', 'AGENTS')
	gc.hub.register('gtkgui', 'AGENT_INFO')
	guiPl.load()
	logPl = gc.hub.newPlugin('logger')
	gc.hub.register('logger', 'MSG')
	gc.hub.register('logger', 'MSGSENT')
	gc.hub.register('logger', 'NOTIFY')
	gc.hub.register('logger', 'QUIT')
	logPl.load()
	gc.mainLoop()