1
+ #! /usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+ '''
4
+ Created on 25.11.2010
5
+
6
+ @author: moschlar
7
+ '''
8
+ from irclib import nm_to_n
9
+
10
+ RESPONSES_FILE = "responses.pkl"
11
+ CONFIG_FILE = "testbot.cfg"
12
+
13
+ class Responder (object ):
14
+ '''
15
+ classdocs
16
+ '''
17
+
18
+
19
+ def __init__ (self ):
20
+ '''
21
+ Constructor
22
+ '''
23
+ self .responses = {}
24
+ self .loadResponses ()
25
+ return
26
+
27
+ def saveResponses (self ):
28
+ """
29
+ Saves pickled responses to the file RESPONSES_FILE
30
+ """
31
+ import pickle
32
+ storage = file (RESPONSES_FILE , "wb" )
33
+ pickle .dump (self .responses ,storage ,protocol = pickle .HIGHEST_PROTOCOL )
34
+ storage .close ()
35
+ return
36
+
37
+ def loadResponses (self ):
38
+ """
39
+ Loads pickled responses from the file RESPONSES_FILE
40
+ """
41
+ import pickle
42
+ storage = file (RESPONSES_FILE , "rb" )
43
+ self .responses = pickle .load (storage )
44
+ storage .close ()
45
+ return
46
+
47
+ def respondTo (self ,c ,e ,line ):
48
+ for (keyword ,response ) in self .responses .items ():
49
+ if line .lower ().find (keyword .lower ()) != - 1 :
50
+ print ("Found %s" % keyword )
51
+ c .privmsg (e .target (), response )
52
+
53
+
54
+ def doCommand (self ,c ,e ,a ):
55
+ pass
56
+
57
+ def getAuth (self ,c ,e ,nick ):
58
+ """
59
+ Determines highest current user level of nick
60
+
61
+ Result: n: normal users, v: voiced users, o: op users
62
+ """
63
+ auth = "n"
64
+ for chname ,chobj in self .channels .items ():
65
+ if nm_to_n (nick ) in chobj .opers ():
66
+ auth = "o"
67
+ break
68
+ if nm_to_n (nick ) in chobj .voiced ():
69
+ auth = "v"
70
+ print ("%s has +%s" % (nm_to_n (nick ),auth ))
71
+ return auth
72
+
73
+ def response (self ,c ,e ,action ):
74
+ """
75
+ Handles response actions
76
+ """
77
+ cmd = action .split (None ,1 )[0 ]
78
+ params = action .split (None ,2 )[1 :]
79
+
80
+ authOp = self .getAuth (c , e , e .source ()) == "o"
81
+
82
+ if cmd == "list" :
83
+ c .privmsg (e .source (),self .responses .keys ())
84
+ elif cmd == "set" and len (params ) == 2 :
85
+ self .responses [params [0 ]] = params [1 ]
86
+ c .privmsg (e .source (),"Set %s to %s" % (params [0 ],params [1 ]))
87
+ self .saveResponses ()
88
+ elif cmd == "del" and len (params ) >= 1 :
89
+ # If we don't have the keyword or user is not auth, we must not try to do anything
90
+ if params [0 ] in self .responses .keys () and authOp :
91
+ c .privmsg (e .source (),"%s gel�scht" % params [0 ])
92
+ del (self .responses [params [0 ]])
93
+ self .saveResponses ()
94
+ else :
95
+ c .privmsg (e .source (), "Des geht nit!" )
96
+ elif cmd == "init" :
97
+ if authOp :
98
+ from ConfigParser import SafeConfigParser
99
+ config = SafeConfigParser ()
100
+ config .read (CONFIG_FILE )
101
+
102
+ responses = {}
103
+ for i in config .items ("responses" ):
104
+ responses [i [0 ]] = i [1 ]
105
+ c .privmsg (e .source (),"Now I'm back responding to: %s" % responses .keys ())
106
+ self .responses = responses
107
+ self .saveResponses ()
108
+ return
0 commit comments