Justabot est un Bot écrit en python, il permet une présence sur les salons Jabber. https://www.devosi.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
12KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright Crestetto Kévin, (Mai 2012)
  3. # checkpoint [at] singularity (dot) fr
  4. # Ce logiciel est un programme informatique
  5. # servant à discuter et executer des commandes sur un salon jabber/XMPP.
  6. # Ce logiciel est régi par la licence CeCILL soumise au droit français et
  7. # respectant les principes de diffusion des logiciels libres. Vous pouvez
  8. # utiliser, modifier et/ou redistribuer ce programme sous les conditions
  9. # de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  10. # sur le site "http://www.cecill.info".
  11. # En contrepartie de l'accessibilité au code source et des droits de copie,
  12. # de modification et de redistribution accordés par cette licence, il n'est
  13. # offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  14. # seule une responsabilité restreinte pèse sur l'auteur du programme, le
  15. # titulaire des droits patrimoniaux et les concédants successifs.
  16. # A cet égard l'attention de l'utilisateur est attirée sur les risques
  17. # associés au chargement, à l'utilisation, à la modification et/ou au
  18. # développement et à la reproduction du logiciel par l'utilisateur étant
  19. # donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  20. # manipuler et qui le réserve donc à des développeurs et des professionnels
  21. # avertis possédant des connaissances informatiques approfondies. Les
  22. # utilisateurs sont donc invités à charger et tester l'adéquation du
  23. # logiciel à leurs besoins dans des conditions permettant d'assurer la
  24. # sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  25. # à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  26. # Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  27. # pris connaissance de la licence CeCILL, et que vous en avez accepté les
  28. # termes.
  29. import sys
  30. import os
  31. import time
  32. import _thread
  33. import commands
  34. from config.config import *
  35. from config.configpass import MUCPASS
  36. from config.configvar import MYDIRS
  37. import tools
  38. import admin
  39. import OBC
  40. import sleekxmpp as xmpp
  41. from sleekxmpp.xmlstream.handler import Callback
  42. from sleekxmpp.xmlstream.matcher import StanzaPath
  43. class inout:
  44. """ connect to server """
  45. def __init__(self):
  46. """ few init """
  47. self.go = False
  48. jid = None
  49. psw = None
  50. global RESOURCE
  51. if PASS:
  52. self.write('_')
  53. psw = PASS
  54. if not JID:
  55. return False
  56. if not RESOURCE:
  57. RESOURCE = 'me'
  58. self.write('_')
  59. jid = JID + '/' + RESOURCE
  60. self.cl = xmpp.ClientXMPP(jid, psw)
  61. self.cl.register_plugin('xep_0045')
  62. self.cl.register_plugin('xep_0030')
  63. self.cl.plugin["xep_0030"].add_feature("jabber:iq:version")
  64. self.cl.add_handler("<iq type='get' xmlns='jabber:client'>" +
  65. "<query xmlns='jabber:iq:version'/></iq>",
  66. self.sendos)
  67. self.write('_')
  68. self.cl.add_event_handler("session_start", self.start)
  69. self.cl.add_event_handler("disconnected", self.stop)
  70. self.cl.add_event_handler("stream_error", self.stop)
  71. self.cl.add_event_handler("socket_error", self.stop)
  72. self.cl.add_event_handler("session_end", self.stop)
  73. self.cl.add_event_handler("failed_auth", self.stop)
  74. def __call__(self):
  75. """ connect and launch the class Run """
  76. if (self.cl) and (not self.go):
  77. running = Run(self.cl)
  78. self.cl.add_event_handler("message", running.receivechat)
  79. self.cl.add_event_handler("subscribe", running.qpresence)
  80. self.cl.add_event_handler("subscribed", running.qpresence)
  81. self.cl.add_event_handler("unsubscribe", running.qpresence)
  82. self.cl.add_event_handler("unsubscribed", running.qpresence)
  83. self.cl.add_event_handler("groupchat_presence", running.presence)
  84. co = True
  85. while co:
  86. if not running.online in ('restart', 'first', 'online'):
  87. break
  88. if running.online == 'online':
  89. if co:
  90. running.online = "restart"
  91. self.go = False
  92. co = RECONNECT
  93. time.sleep(2)
  94. if not self.go:
  95. if self.connect() and self.proc():
  96. self.cl.get_roster(block=False)
  97. running.run()
  98. while self.go and running.online == 'online':
  99. try:
  100. time.sleep(2)
  101. except:
  102. co = False
  103. break
  104. self.go = False
  105. self.cl.disconnect(wait=True)
  106. print("disco")
  107. print('done')
  108. running.theend(None)
  109. def start(self, event):
  110. self.go = True
  111. def stop(self, event):
  112. self.go = False
  113. def proc(self):
  114. test = 0
  115. while not self.go:
  116. time.sleep(2)
  117. test += 1
  118. self.write('.')
  119. if test > 9:
  120. return False
  121. self.write('connected > ')
  122. return True
  123. def write(self, arg):
  124. """ raw print with flush """
  125. sys.stdout.write(arg)
  126. sys.stdout.flush()
  127. def connect(self):
  128. """ connect to server """
  129. addr=None
  130. if SERVER and PORT:
  131. addr=(SERVER, PORT)
  132. if not self.cl.connect(address=addr, reattempt=False):
  133. return False
  134. self.write('_')
  135. self.cl.process(block=False, wait=True)
  136. return True
  137. def sendos(self, el):
  138. OS="""
  139. <iq type="result" to='%s' id='%s'>
  140. <query xmlns="jabber:iq:version">
  141. <name>%s</name>
  142. <version>%s</version>
  143. <os>%s</os>
  144. </query>
  145. </iq>
  146. """ % (el.get('from'), el.get('id'), "justabot", my_version, "Unix-like")
  147. self.cl.send(OS)
  148. class Run(admin.CMDadmin):
  149. """ manage the connection """
  150. def __init__(self, cl):
  151. """ lot of init, launch some class """
  152. self.DIRS = MYDIRS
  153. self.cl = cl
  154. self.chan = []
  155. self.NICKNAME = NICKNAME
  156. self.noflood = OBC.onebychan(0.15)
  157. self.noflood.start()
  158. self.connected = []
  159. self.online = 'first'
  160. def disco(self):
  161. """ clean when disconnected """
  162. for chan in self.chan:
  163. self.unjoin(chan.name)
  164. self.cl.send_presence('disconnected')
  165. def passmuc(self, chan):
  166. """ return a presence with or without password """
  167. if chan in MUCPASS:
  168. passe = MUCPASS[chan]
  169. else:
  170. passe = None
  171. C = commands.Com(self, chan)
  172. self.chan.append(C)
  173. self.cl.plugin['xep_0045'].joinMUC(chan, self.NICKNAME, password=passe,
  174. wait=False)
  175. C.start()
  176. return True
  177. def run(self):
  178. """ join channel and switch all receive """
  179. self.cl.send_presence()
  180. for chan in CHANNELS:
  181. print(chan)
  182. self.passmuc(chan)
  183. if DOMAIN_FILTER:
  184. print('@')
  185. C = commands.Com(self, '@')
  186. self.chan.append(C)
  187. print('GO !')
  188. if self.online == 'first':
  189. commands.PLUGINS.it = self
  190. commands.PLUGINS.allplugload()
  191. self.online = 'online'
  192. def theend(self, event):
  193. self.disco()
  194. if self.online == 'online':
  195. self.online = 'offline'
  196. def qpresence(self, stanza):
  197. """ presence for admins """
  198. to = stanza.get_from()
  199. nick = to.node + '@' + to.domain
  200. typ = stanza.get_type()
  201. if typ in ('subscribe', 'subscribed'):
  202. if nick in ADMINS or to.domain in DOMAIN_FILTER or\
  203. '*' in DOMAIN_FILTER:
  204. p = stanza.make_accept_response()
  205. self.cl.stream.send(p)
  206. self.cl.presence(to)
  207. elif typ in ('unsubscribe', 'unsubscribed'):
  208. p = stanza.make_accept_response()
  209. self.cl.stream.send(p)
  210. def presence(self, pres):
  211. """ call when a presence message is receive """
  212. nick = pres['muc']['nick']
  213. typ = pres['type']
  214. serv = pres['muc']['room']
  215. jid = pres['muc']['from']
  216. aff = pres['muc']['affiliation']
  217. inchan = None
  218. for ch in self.chan:
  219. if serv == ch.name:
  220. inchan = ch
  221. me = False
  222. if nick == self.NICKNAME:
  223. if inchan != None:
  224. inchan.chMyPres(nick, pres, self.cl)
  225. me = True
  226. if typ == 'unavailable':
  227. if me:
  228. self.unjoin(jid)
  229. elif inchan != None:
  230. inchan.removeNick(nick)
  231. else:
  232. try:
  233. self.connected.remove(nick)
  234. except:
  235. pass
  236. elif not me:
  237. if inchan != None:
  238. inchan.setPres(pres, nick, aff)
  239. if not jid in self.connected:
  240. self.connected.append(jid)
  241. def sending(self, text, to='', typ='chat'):
  242. """ send a text to server """
  243. self.cl.send_message(mto=to, mbody=text, mtype=typ)
  244. def securesend(self, text, to='', typ='chat'):
  245. """ send a text to server with flood checking """
  246. if self.noflood.isnotflood() == False:
  247. return
  248. self.noflood.setflood()
  249. self.sending(text, to, typ)
  250. def receivechat(self, mess):
  251. """ redirect chat to self.receive """
  252. if mess['type'] == 'groupchat':
  253. self.receive(mess)
  254. return ''
  255. to = mess.get_from()
  256. jid = mess['from'].bare
  257. if mess['type'] == 'chat' and jid in ADMINS:
  258. if self.admin(mess, self.NICKNAME):
  259. return ''
  260. self.receive(mess)
  261. def receive(self, mess):
  262. """ get messages, check it, switch it, and can send answer """
  263. if mess:
  264. to = mess.get_from()
  265. typ = mess.get_type()
  266. ok = False
  267. if typ == 'groupchat':
  268. ttto = mess['mucroom']
  269. nick = mess['mucnick']
  270. for ch in self.chan:
  271. if ch.name == ttto:
  272. ok = True
  273. break
  274. else:
  275. nick = mess['from'].resource
  276. ok = True
  277. if not ok:
  278. return
  279. text = mess['body']
  280. if not text:
  281. return
  282. if nick == self.NICKNAME:
  283. return
  284. if typ == 'groupchat':
  285. reto = to.bare
  286. chan = reto
  287. elif typ == 'chat':
  288. reto = to
  289. chan = to.bare
  290. else:
  291. return
  292. onwait = ''
  293. for ch in self.chan:
  294. if ch.name == chan:
  295. onwait = ch(mess, text, nick, typ)
  296. break
  297. if typ == 'chat' and not onwait:
  298. if reto.domain in DOMAIN_FILTER or '*' in DOMAIN_FILTER:
  299. for ch in self.chan:
  300. if ch.name == '@':
  301. onwait = ch(mess, text, chan, 'chat')
  302. break
  303. if onwait and onwait != '':
  304. self.securesend(onwait, reto, typ)
  305. def unjoin(self, chan):
  306. """ clean when leave a chan """
  307. for ch in self.chan:
  308. if ch.name == chan:
  309. try:
  310. self.cl.plugin['xep_0045'].leaveMUC(chan, self.NICKNAME)
  311. except KeyError:
  312. pass
  313. ch.unjoin()
  314. self.chan.remove(ch)
  315. break
  316. def rejoin(self, c):
  317. """ try to rejoin if kicked """
  318. for a in [10, 20, 30, 40, 50]:
  319. try:
  320. self.unjoin(c)
  321. except:
  322. pass
  323. time.sleep(a)
  324. try:
  325. chan = commands.Com(self, c)
  326. chan.start()
  327. self.passmuc(chan.name)
  328. self.chan.append(chan)
  329. break
  330. except:
  331. pass